Class: InputTemplateRenderer

Inherits:
Object
  • Object
show all
Includes:
UnattendedHelper
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
  @invocation = invocation
  @template = template
  @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

Instance Method Details

#find_by_name(collection, name) ⇒ Object



99
100
101
# File 'app/models/input_template_renderer.rb', line 99

def find_by_name(collection, name)
  collection.detect { |i| i.name == name.to_s }
end

#foreign_input_set_values(target_template, overrides = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/input_template_renderer.rb', line 83

def foreign_input_set_values(target_template, overrides = {})
  input_set = @template.foreign_input_sets.find_by(:target_template_id => target_template)
  return overrides if input_set.nil?

  inputs_to_generate = input_set.inputs.map(&:name) - overrides.keys.map(&:to_s)
  included_renderer = self.class.new(input_set.target_template, host, invocation, nil, @preview, @templates_stack)
  input_values = inputs_to_generate.inject(HashWithIndifferentAccess.new) do |hash, input_name|
    hash.merge(input_name => included_renderer.input(input_name))
  end
  input_values.merge(overrides)
end

#input(name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'app/models/input_template_renderer.rb', line 43

def input(name)
  return @input_values[name.to_s] if @input_values
  input = find_by_name(@template.template_inputs_with_foreign, name)
  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

#loggerObject



95
96
97
# File 'app/models/input_template_renderer.rb', line 95

def logger
  Rails.logger
end

#previewObject



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

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

#preview?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'app/models/input_template_renderer.rb', line 79

def preview?
  !!@preview
end

#renderObject



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

def render
  @template.validate_unique_inputs!
  render_safe(@template.template, ::Foreman::Renderer::ALLOWED_HELPERS + [ :input, :render_template, :preview?, :render_error ], :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

#render_error(message) ⇒ Object

Raises:



54
55
56
# File 'app/models/input_template_renderer.rb', line 54

def render_error(message)
  raise RenderError.new(message)
end

#render_template(template_name, input_values = {}, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/input_template_renderer.rb', line 58

def render_template(template_name, input_values = {}, options = {})
  options.assert_valid_keys(:with_foreign_input_set)
  with_foreign_input_set = options.fetch(:with_foreign_input_set, true)
  template = JobTemplate.authorized(:view_job_templates).find_by(:name => template_name)
  unless template
    self.error_message = _('included template \'%s\' not found') % template_name
    raise error_message
  end
  if with_foreign_input_set
    input_values = foreign_input_set_values(template, input_values)
  end
  included_renderer = self.class.new(template, host, invocation, input_values.with_indifferent_access, @preview, @templates_stack)
  out = included_renderer.render
  if included_renderer.error_message
    self.error_message = included_renderer.error_message
    raise error_message
  else
    out
  end
end