Class: InputTemplateRenderer

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

Defined Under Namespace

Classes: RenderError, UndefinedInput

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, host = nil, invocation = nil, input_values = nil, preview = false, templates_stack = []) ⇒ 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

Raises:

  • (Foreman::Exception)


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

def initialize(template, host = nil, invocation = nil, input_values = nil, preview = false, templates_stack = [])
  raise Foreman::Exception, N_('Recursive rendering of templates detected') if templates_stack.include?(template)

  @host = host
  @template = template
  @invocation = invocation
  @input_values = input_values
  @preview = preview
  @templates_stack = templates_stack + [template]
end

Instance Attribute Details

#error_messageObject

Returns the value of attribute error_message.



10
11
12
# File 'app/models/input_template_renderer.rb', line 10

def error_message
  @error_message
end

#hostObject

Returns the value of attribute host.



10
11
12
# File 'app/models/input_template_renderer.rb', line 10

def host
  @host
end

#input_valuesObject

Returns the value of attribute input_values.



10
11
12
# File 'app/models/input_template_renderer.rb', line 10

def input_values
  @input_values
end

#invocationObject

Returns the value of attribute invocation.



10
11
12
# File 'app/models/input_template_renderer.rb', line 10

def invocation
  @invocation
end

#templateObject

Returns the value of attribute template.



10
11
12
# File 'app/models/input_template_renderer.rb', line 10

def template
  @template
end

#templates_stackObject

Returns the value of attribute templates_stack.



10
11
12
# File 'app/models/input_template_renderer.rb', line 10

def templates_stack
  @templates_stack
end

Instance Method Details

#input(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'app/models/input_template_renderer.rb', line 52

def input(name)
  return @input_values[name.to_s] if @input_values
  input = find_by_name(template.template_inputs_with_foreign, name) # rubocop:disable Rails/DynamicFindBy
  if input
    @preview ? input.preview(self) : input.value(self)
  else
    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



63
64
65
66
67
68
69
# File 'app/models/input_template_renderer.rb', line 63

def preview
  old_preview = @preview
  @preview = true
  render
ensure
  @preview = old_preview
end

#renderObject



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
# File 'app/models/input_template_renderer.rb', line 26

def render
  @template.validate_unique_inputs!
  source = Foreman::Renderer.get_source(
    template: template,
    host: host
  )
  scope = Foreman::Renderer.get_scope(
    host: host,
    klass: renderer_scope,
    variables: {
      host: host,
      template: template,
      preview: @preview,
      invocation: invocation,
      input_values: input_values,
      templates_stack: templates_stack,
      input_template_instance: self
    }
  )
  Foreman::Renderer.render(source, scope)
rescue => e
  self.error_message ||= _('error during rendering: %s') % e.message
  Foreman::Logging.exception('Error during rendering of a job template', e)
  false
end