Class: InputTemplateRenderer

Inherits:
Object
  • Object
show all
Includes:
UnattendedHelper
Defined in:
app/models/input_template_renderer.rb

Defined Under Namespace

Classes: UndefinedInput

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, host = nil, invocation = nil) ⇒ InputTemplateRenderer

takes template object that should be rendered host and template invocation arguments are optional so we can render values based on parameters, facts or user inputs



12
13
14
15
16
# File 'app/models/input_template_renderer.rb', line 12

def initialize(template, host = nil, invocation = nil)
  @host = host
  @invocation = invocation
  @template = template
end

Instance Attribute Details

#error_messageObject

Returns the value of attribute error_message.



7
8
9
# File 'app/models/input_template_renderer.rb', line 7

def error_message
  @error_message
end

#hostObject

Returns the value of attribute host.



7
8
9
# File 'app/models/input_template_renderer.rb', line 7

def host
  @host
end

#invocationObject

Returns the value of attribute invocation.



7
8
9
# File 'app/models/input_template_renderer.rb', line 7

def invocation
  @invocation
end

#templateObject

Returns the value of attribute template.



7
8
9
# File 'app/models/input_template_renderer.rb', line 7

def template
  @template
end

Instance Method Details

#input(name) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'app/models/input_template_renderer.rb', line 33

def input(name)
  input = @template.template_inputs.where(:name => name.to_s).first || @template.template_inputs.detect { |i| i.name == name.to_s }
  if input
    @preview ? input.preview(self) : input.value(self)
  else
    self.error_message = _('input macro with name \'%s\' used, but no input with such name defined for this template') % name
    raise UndefinedInput, "Rendering failed, no input with name #{name} for input macro found"
  end
end

#previewObject



26
27
28
29
30
31
# File 'app/models/input_template_renderer.rb', line 26

def preview
  @preview = true
  output = render
  @preview = false
  output
end

#renderObject



18
19
20
21
22
23
24
# File 'app/models/input_template_renderer.rb', line 18

def render
  render_safe(@template.template, [ :input ], :host => @host)
rescue => e
  self.error_message ||= _('error during rendering: %s') % e.message
  Rails.logger.debug e.to_s + "\n" + e.backtrace.join("\n")
  return false
end