Module: ForemanPuppet::HostsAndHostgroupsHelper

Defined in:
app/helpers/foreman_puppet/hosts_and_hostgroups_helper.rb

Instance Method Summary collapse

Instance Method Details

#environment_inherited_by_default?(host) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
# File 'app/helpers/foreman_puppet/hosts_and_hostgroups_helper.rb', line 3

def environment_inherited_by_default?(host)
  return false unless host.hostgroup && host.hostgroup_id_was.nil?
  return false if params[:action] == 'clone'
  return true unless params[:host]
  !params[:host].dig(:puppet_attributes, :environment_id)
end

#host_puppet_environment_field(form, select_options = {}, html_options = {}) ⇒ Object

rubocop:enable Rails/HelperInstanceVariable



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/foreman_puppet/hosts_and_hostgroups_helper.rb', line 22

def host_puppet_environment_field(form, select_options = {}, html_options = {})
  form.fields_for(:puppet, form.object.puppet || form.object.build_puppet, include_id: false) do |fields|
    select_options = {
      include_blank: true,
      disable_button: _(::HostsAndHostgroupsHelper::INHERIT_TEXT),
      disable_button_enabled: environment_inherited_by_default?(form.object),
      user_set: user_set_environment?,
    }.deep_merge(select_options)

    html_options = {
      data: {
        host: {
          id: form.object.id,
        },
      },
    }.deep_merge(html_options)

    puppet_environment_field(
      fields,
      accessible_resource(fields.object, 'ForemanPuppet::Environment', association: :environment),
      select_options,
      html_options
    )
  end
end

#hostgroup_puppet_environment_field(form, select_options = {}, html_options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/helpers/foreman_puppet/hosts_and_hostgroups_helper.rb', line 48

def hostgroup_puppet_environment_field(form, select_options = {}, html_options = {})
  form.fields_for(:puppet, form.object.puppet || form.object.build_puppet, include_id: false) do |fields|
    select_options = {
      include_blank: blank_or_inherit_f(fields, :environment),
    }.deep_merge(select_options)

    puppet_environment_field(
      fields,
      accessible_resource(fields.object, 'ForemanPuppet::Environment', association: :environment),
      select_options,
      html_options
    )
  end
end

#puppet_environment_field(form, environments_choice, select_options = {}, html_options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/foreman_puppet/hosts_and_hostgroups_helper.rb', line 63

def puppet_environment_field(form, environments_choice, select_options = {}, html_options = {})
  html_options = {
    onchange: 'tfm.puppetEnc.hostForm.updatePuppetclasses(this)',
    help_inline: :indicator,
  }.deep_merge(html_options)

  select_f(
    form,
    :environment_id,
    environments_choice,
    :id,
    :to_label,
    select_options,
    html_options
  )
end

#puppetclasses_with_parameters_for(obj) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'app/helpers/foreman_puppet/hosts_and_hostgroups_helper.rb', line 80

def puppetclasses_with_parameters_for(obj)
  classes = obj.all_puppetclasses
  ids = classes.reorder(nil).pluck(:id)
  class_vars = ForemanPuppet::PuppetclassLookupKey.reorder(nil)
                                                  .joins(:environment_classes)
                                                  .where(EnvironmentClass.arel_table[:puppetclass_id].in(ids))
                                                  .distinct
                                                  .pluck('environment_classes.puppetclass_id')
  classes.where(id: class_vars)
end

#user_set_environment?Boolean

rubocop:disable Rails/HelperInstanceVariable

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
# File 'app/helpers/foreman_puppet/hosts_and_hostgroups_helper.rb', line 11

def user_set_environment?
  # if the host has no hostgroup
  return true unless @host&.hostgroup
  # when editing a host, the values are specified explicitly
  return true if params[:action] == 'edit'
  return true if params[:action] == 'clone'
  # check if the user set the field explicitly despite setting a hostgroup.
  params[:host] && params[:host][:hostgroup_id] && params[:host].dig(:puppet_attributes, :environment_id)
end