Sha256: 9fedac4a598059c27694378b2fbd50896884c4fdfb3826450a6a59801dd386e9

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

class InputTemplateRenderer
  class UndefinedInput < ::Foreman::Exception
  end

  include UnattendedHelper

  attr_accessor :template, :host, :invocation, :error_message

  # 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
  def initialize(template, host = nil, invocation = nil)
    @host = host
    @invocation = invocation
    @template = template
  end

  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

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

  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
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
foreman_remote_execution-0.0.6 app/models/input_template_renderer.rb
foreman_remote_execution-0.0.5 app/models/input_template_renderer.rb
foreman_remote_execution-0.0.4 app/models/input_template_renderer.rb
foreman_remote_execution-0.0.3 app/models/input_template_renderer.rb
foreman_remote_execution-0.0.2 app/models/input_template_renderer.rb
foreman_remote_execution-0.0.1 app/models/input_template_renderer.rb