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
  @template_input_values = input_values
  @preview = preview
  @templates_stack = templates_stack + [template]
end

Instance Attribute Details

#current_userObject

Returns the value of attribute current_user.



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

def current_user
  @current_user
end

#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

#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

#template_input_valuesObject

Returns the value of attribute template_input_values.



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

def template_input_values
  @template_input_values
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

#previewObject



56
57
58
59
60
61
62
# File 'app/models/input_template_renderer.rb', line 56

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

def render
  @template_input_values ||= values_from_invocation
  @template.validate_unique_inputs!
  source = Foreman::Renderer.get_source(
    template: template,
    host: host
  )
  @scope = Foreman::Renderer.get_scope(
    source: source,
    host: host,
    klass: renderer_scope,
    template_input_values: @template_input_values,
    variables: {
      host: host,
      template: template,
      preview: @preview,
      invocation: invocation,
      input_values: @template_input_values,
      templates_stack: templates_stack,
      input_template_instance: self,
      current_user: User.current.try(:login),
    }
  )
  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