-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy patherb_renderer.rb
61 lines (51 loc) · 1.5 KB
/
erb_renderer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module SmartAnswer
class ErbRenderer
module QuestionOptionsHelper
def options(options = nil)
if options
@options = options
else
@options || {}
end
end
end
def initialize(template_directory:, template_name:, locals: {}, helpers: [])
@template_directory = template_directory
@template_name = template_name
@locals = locals
@view = ActionView::Base.new([@template_directory, FlowRegistry.instance.load_path])
helpers.each { |helper| @view.extend(helper) }
@view.extend(QuestionOptionsHelper)
end
def single_line_of_content_for(key)
content_for(key, html: false).chomp.html_safe
end
def content_for(key, html: true)
content = rendered_view.content_for(key) || ''
content = strip_leading_spaces(content.to_str)
html ? GovspeakPresenter.new(content).html : normalize_blank_lines(content).html_safe
end
def option_text(key)
rendered_view
@view.options.fetch(key).html_safe
end
def erb_template_path
@template_directory.join(erb_template_name)
end
private
def erb_template_name
"#{@template_name}.govspeak.erb"
end
def rendered_view
@rendered_view ||= @view.tap do |view|
view.render(template: erb_template_name, locals: @locals)
end
end
def strip_leading_spaces(string)
string.gsub(/^ +/, '')
end
def normalize_blank_lines(string)
string.gsub(/(\n$){2,}/m, "\n")
end
end
end