Module: Staypuft::Concerns::LookupKeyExtensions

Extended by:
ActiveSupport::Concern
Defined in:
app/models/staypuft/concerns/lookup_key_extensions.rb

Constant Summary collapse

LIMPET_FORMAT =
'<%%={key_id:%s};'
LIMPET_FORMAT_REGEXP =
/#{LIMPET_FORMAT % '(\d+)'}/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.evaluate_value(hostgroup, value) ⇒ Object



41
42
43
44
45
46
# File 'app/models/staypuft/concerns/lookup_key_extensions.rb', line 41

def self.evaluate_value(hostgroup, value)
  host = Host::Managed.new(hostgroup: hostgroup, name: 'renderer')
  SafeRender.new(:variables => { :host => host }).parse(value)
rescue => e
  "ERROR: #{e.message} (#{e.class})"
end

.has_erb?(value) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/staypuft/concerns/lookup_key_extensions.rb', line 33

def self.has_erb?(value)
  value =~ /<%.*%>/
end

.monkey_path_safe_renderObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/staypuft/concerns/lookup_key_extensions.rb', line 56

def self.monkey_path_safe_render
  ::SafeRender.class_eval do
    def parse_string(string)
      raise ::ForemanException.new(N_('SafeRender#parse_string was passed a %s instead of a string') % string.class) unless string.is_a? String

      lookup_key_id = string[Staypuft::Concerns::LookupKeyExtensions::LIMPET_FORMAT_REGEXP, 1]
      if lookup_key_id
        lookup_key = LookupKey.find(lookup_key_id)
        type       = lookup_key.key_type
      end

      value = if Setting[:safemode_render]
                box = Safemode::Box.new self, @allowed_methods
                box.eval(ERB.new(string, nil, '-').src, @allowed_vars)
              else
                @allowed_vars.each { |k, v| instance_variable_set "@#{k}", v }
                ERB.new(string, nil, '-').result(binding)
              end

      if type
        lookup_key.cast_validate_value_after_erb value, type
      else
        value
      end
    rescue => exc
      # TODO fix in foreman
      Rails.logger.error "There was error rendering string: #{string.inspect}\n#{exc} (#{exc.class})\n#{exc.backtrace.join("\n")}"
      raise exc
    end
  end
end

Instance Method Details

#cast_validate_value_after_erb(value, type) ⇒ Object



27
28
29
30
31
# File 'app/models/staypuft/concerns/lookup_key_extensions.rb', line 27

def cast_validate_value_after_erb(value, type)
  method = "cast_value_#{type}".to_sym
  return value unless self.respond_to? method, true
  self.send(method, value) rescue raise TypeError
end

#cast_validate_value_with_erb(value) ⇒ Object



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

def cast_validate_value_with_erb(value)
  if has_erb? value
    if value =~ LIMPET_FORMAT_REGEXP
      value.gsub(/#{LIMPET_FORMAT_REGEXP}/, LIMPET_FORMAT % id)
    else
      value.gsub(/<%=/, LIMPET_FORMAT % id)
    end
  else
    cast_validate_value_without_erb value
  end
end

#has_erb?(value) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/staypuft/concerns/lookup_key_extensions.rb', line 37

def has_erb?(value)
  Staypuft::Concerns::LookupKeyExtensions.has_erb? value
end

#value_before_type_cast_with_limpet(value) ⇒ Object



48
49
50
51
52
53
54
# File 'app/models/staypuft/concerns/lookup_key_extensions.rb', line 48

def value_before_type_cast_with_limpet(value)
  if has_erb? value
    value.gsub(LIMPET_FORMAT_REGEXP, '<%=')
  else
    value_before_type_cast_without_limpet(value)
  end
end