Module: LookupKeysHelper

Included in:
ComputeResourcesHelper, PuppetclassesHelper
Defined in:
app/helpers/lookup_keys_helper.rb

Instance Method Summary collapse

Instance Method Details


12
13
14
15
16
# File 'app/helpers/lookup_keys_helper.rb', line 12

def add_child_link(name, association, opts = {})
  opts[:class] = [opts[:class], "add_nested_fields btn btn-primary"].compact.join(" ")
  opts[:"data-association"] = association
  link_to_function(name.to_s, "add_child_node(this);", opts)
end

#can_edit_params?Boolean

Returns:

  • (Boolean)

50
51
52
# File 'app/helpers/lookup_keys_helper.rb', line 50

def can_edit_params?
  authorized_via_my_scope("host_editing", "edit_params")
end

7
8
9
10
# File 'app/helpers/lookup_keys_helper.rb', line 7

def delete_child_link(name, f, opts = {})
  opts[:class] = [opts[:class], "remove_nested_fields"].compact.join(" ")
  link_to_function(name, "delete_child_node(this);", opts)
end

#diagnostic_popover(lookup_key, matcher, inherited_value, warnings) ⇒ Object


87
88
89
90
91
92
93
94
# File 'app/helpers/lookup_keys_helper.rb', line 87

def diagnostic_popover(lookup_key, matcher, inherited_value, warnings)
  description = lookup_key_description(lookup_key, matcher, inherited_value)
  popover('', description.prepend(warnings[:text]),
    :data => { :placement => 'top' },
    :title => _("Original value info"),
    :icon => "info-circle",
    :kind => "fa")
end

#hidden_lookup_value_fields(lookup_key, lookup_value, disabled) ⇒ Object


158
159
160
161
162
163
164
165
166
167
# File 'app/helpers/lookup_keys_helper.rb', line 158

def hidden_lookup_value_fields(lookup_key, lookup_value, disabled)
  return unless can_edit_params?
  value_prefix = lookup_value_name_prefix(lookup_key.id)
  hidden_field(value_prefix, :lookup_key_id, :value => lookup_key.id,
               :disabled => disabled, :class => 'send_to_remove') +
    hidden_field(value_prefix, :id, :value => lookup_value.id,
                 :disabled => disabled, :class => 'send_to_remove') +
    hidden_field(value_prefix, :_destroy, :value => false,
                 :disabled => disabled, :class => 'send_to_remove destroy')
end

#hidden_toggle(hidden, hide_icon = 'eye-slash', unhide_icon = 'eye', strikethrough = false) ⇒ Object


129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/lookup_keys_helper.rb', line 129

def hidden_toggle(hidden, hide_icon = 'eye-slash', unhide_icon = 'eye', strikethrough = false)
  return unless can_edit_params?
  if strikethrough && !hidden
    link_to_function(icon_text(hide_icon, '', :kind => 'fa'), "", :class => "btn btn-default btn-md btn-hide", :disabled => "disabled", :rel => "twipsy", :title => _("This value is not hidden"))
  else
    link_to_function(icon_text(unhide_icon, '', :kind => 'fa'), "input_group_hidden(this)",
      :title => _("Unhide this value"),
      :class => "btn btn-default btn-md btn-hide #{'hide' unless hidden}") +
        link_to_function(icon_text(hide_icon, "", :class => ('btn-strike' if strikethrough).to_s, :kind => 'fa'), "input_group_hidden(this)",
          :title => _("Hide this value"),
          :class => "btn btn-default btn-md btn-hide #{'hide' if hidden}")
  end
end

#lookup_key_description(lookup_key, matcher, inherited_value) ⇒ Object


96
97
98
99
100
101
102
103
# File 'app/helpers/lookup_keys_helper.rb', line 96

def lookup_key_description(lookup_key, matcher, inherited_value)
  _("<b>Description:</b> %{desc}<br/>
   <b>Type:</b> %{type}<br/>
   <b>Matcher:</b> %{matcher}<br/>
   <b>Inherited value:</b> %{inherited_value}") %
  { :desc => html_escape(lookup_key.description), :type => lookup_key.key_type,
    :matcher => html_escape(matcher), :inherited_value => html_escape(inherited_value) }
end

#lookup_key_warnings(required, has_value) ⇒ Object


105
106
107
108
109
110
111
112
113
114
115
# File 'app/helpers/lookup_keys_helper.rb', line 105

def lookup_key_warnings(required, has_value)
  return { :text => '', :icon => 'info' } if has_value

  if required
    { :text => _("Required parameter without value.<br/><b>Please override!</b><br/>"),
      :icon => "error-circle-o" }
  else
    { :text => _("Optional parameter without value.<br/><i>Still managed by Foreman, the value will be empty.</i><br/>"),
      :icon => "warning-triangle-o" }
  end
end

#lookup_key_with_diagnostic(obj, lookup_key, lookup_value) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/lookup_keys_helper.rb', line 54

def lookup_key_with_diagnostic(obj, lookup_key, lookup_value)
  value, matcher = value_matcher(obj, lookup_key)
  inherited_value = LookupKey.format_value_before_type_cast(value, lookup_key.key_type)
  effective_value = lookup_value.lookup_key_id.nil? ? inherited_value.to_s : lookup_value.value_before_type_cast.to_s
  warnings = lookup_key_warnings(lookup_key.required, effective_value.present?)
  popover_value = lookup_key.hidden_value? ? lookup_key.hidden_value : inherited_value

  parameter_value_content(
    "#{parameters_receiver}_lookup_values_attributes_#{lookup_key.id}_value",
    effective_value,
    :popover => diagnostic_popover(lookup_key, matcher, popover_value, warnings),
    :name => "#{lookup_value_name_prefix(lookup_key.id)}[value]",
    :disabled => !lookup_key.overridden?(obj) || lookup_value.omit || !can_edit_params?,
    :inherited_value => inherited_value,
    :lookup_key => lookup_key,
    :hidden_value? => lookup_key.hidden_value?,
    :lookup_key_type => lookup_key.key_type)
end

#lookup_keys_breadcrumbsObject


183
184
185
186
187
# File 'app/helpers/lookup_keys_helper.rb', line 183

def lookup_keys_breadcrumbs
  breadcrumbs(resource_url: "/api/v2/smart_class_parameters",
              name_field: "parameter",
              switcher_item_url: "/puppetclass_lookup_keys/:id-:name/edit")
end

#lookup_value(host_or_hostgroup, lookup_key) ⇒ Object


143
144
145
# File 'app/helpers/lookup_keys_helper.rb', line 143

def lookup_value(host_or_hostgroup, lookup_key)
  lookup_key.overridden_value(host_or_hostgroup) || LookupValue.new
end

#lookup_value_name_prefix(lookup_key_id) ⇒ Object

Input tags used to override lookup keys need a 'name' HTML attribute to tell Rails which lookup_value they belong to. This method returns the name attribute for any combination of lookup_key and host/hostgroup. Other objects that may receive parameters too will need to override this method in their respective helpers.


174
175
176
# File 'app/helpers/lookup_keys_helper.rb', line 174

def lookup_value_name_prefix(lookup_key_id)
  "#{parameters_receiver}[lookup_values_attributes][#{lookup_key_id}]"
end

#omit_check_box(lookup_key, lookup_value, disabled) ⇒ Object


147
148
149
150
151
152
153
154
155
156
# File 'app/helpers/lookup_keys_helper.rb', line 147

def omit_check_box(lookup_key, lookup_value, disabled)
  return unless lookup_key.type == "PuppetclassLookupKey"
  check_box(lookup_value_name_prefix(lookup_key.id), :omit,
    :value    => lookup_value.id,
    :disabled => disabled || !can_edit_params?,
    :onchange => "toggleOmitValue(this, 'value')",
    :hidden   => disabled,
    :title    => _('Omit from classification output'),
    :checked  => lookup_value.omit)
end

#overridable_lookup_keys(klass, obj) ⇒ Object


46
47
48
# File 'app/helpers/lookup_keys_helper.rb', line 46

def overridable_lookup_keys(klass, obj)
  klass.class_params.override.where(:environment_classes => {:environment_id => obj.environment})
end

#override_toggle(overridden) ⇒ Object


117
118
119
120
121
122
123
124
125
126
127
# File 'app/helpers/lookup_keys_helper.rb', line 117

def override_toggle(overridden)
  return unless can_edit_params?
  link_to_function(icon_text('pencil-square-o', '', :kind => 'fa'), "override_class_param(this)",
    :title => _("Override this value"),
    :'data-tag' => 'override',
    :class => "btn btn-default btn-md btn-override #{'hide' if overridden}") +
    link_to_function(icon_text('times', '', :kind => 'fa'), "override_class_param(this)",
      :title => _("Remove this override"),
     :'data-tag' => 'remove',
     :class => "btn btn-default btn-md btn-override #{'hide' unless overridden}")
end

#parameters_receiverObject


178
179
180
181
# File 'app/helpers/lookup_keys_helper.rb', line 178

def parameters_receiver
  return 'host' if params.has_key?(:host) || params[:controller] == 'hosts'
  'hostgroup'
end

2
3
4
5
# File 'app/helpers/lookup_keys_helper.rb', line 2

def remove_child_link(name, f, opts = {})
  opts[:class] = [opts[:class], "remove_nested_fields"].compact.join(" ")
  f.hidden_field(opts[:method] || :_destroy) + link_to_function(name, "remove_child_node(this);", opts)
end

#show_puppet_class(f) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/lookup_keys_helper.rb', line 18

def show_puppet_class(f)
  # In case of a new smart-var inside a puppetclass (REST nesting only), or a class parameter:
  # Show the parent puppetclass as a context, but permit no change.
  unless @puppetclass
    if params["puppetclass_id"]
      select_f f, :puppetclass_id, [Puppetclass.find(params["puppetclass_id"])], :id, :to_label, {}, {:label => _("Puppet class"), :disabled => true}
    elsif f.object.puppet? && f.object.param_class
      text_f(f, :puppetclass_id, :label => _('Puppet Class'), :value => f.object.param_class, :disabled => true)
    else # new smart-var with no particular context
      # Give a select for choosing the parent puppetclass
      puppetclasses = accessible_resource(f.object, :puppetclass)
      select_f(f, :puppetclass_id, puppetclasses, :id, :to_label, { :include_blank => true }, {:label => _("Puppet class")})
    end
    # nested smart-vars form in a tab of puppetclass/_form: no edition allowed, and the puppetclass is already visible as a context
  end
end

#validator_type_selector(f) ⇒ Object


35
36
37
38
39
40
41
42
43
44
# File 'app/helpers/lookup_keys_helper.rb', line 35

def validator_type_selector(f)
  selectable_f f, :validator_type, options_for_select(LookupKey::VALIDATOR_TYPES.map { |e| [_(e), e] }, f.object.validator_type), {:include_blank => _("None")},
    { :disabled => (f.object.puppet? && !f.object.override), :size => "col-md-8", :class => "without_select2",
      :onchange => 'validatorTypeSelected(this)',
      :label_help => _("<dl>" +
        "<dt>List</dt> <dd>A list of the allowed values, specified in the Validator rule field.</dd>" +
        "<dt>Regexp</dt> <dd>Validates the input with the regular expression in the Validator rule field.</dd>" +
        "</dl>").html_safe,
      :label_help_options => { :title => _("Validation types") } }
end

#value_matcher(obj, lookup_key) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/helpers/lookup_keys_helper.rb', line 73

def value_matcher(obj, lookup_key)
  if parameters_receiver == "host"
    value = value_hash_cache(obj)[lookup_key.id]
    value_for_key = value.try(:[], lookup_key.key)
    if value_for_key.present?
      [value_for_key[:value], "#{value_for_key[:element]} (#{value_for_key[:element_name]})"]
    else
      [lookup_key.default_value, _("Default value")]
    end
  else # hostgroup
    obj.inherited_lookup_value(lookup_key)
  end
end