Module: Katello::ContentOptionsHelper

Included in:
HostsAndHostgroupsHelper
Defined in:
app/helpers/katello/content_options_helper.rb

Instance Method Summary collapse

Instance Method Details

#blank_or_inherit_cve(f) ⇒ Object

f is Rails convention for form objects



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/helpers/katello/content_options_helper.rb', line 161

def blank_or_inherit_cve(f) # f is Rails convention for form objects
  return true unless f.object.respond_to?(:parent_id) && f.object.parent_id
  parent_cve = f.object.parent&.content_facet&.content_view_environment
  inherited_value = parent_cve.try(:id) || ''

  if parent_cve
    cve_name = if parent_cve.default_environment?
                 parent_cve.environment.name
               else
                 "#{parent_cve.environment.name} / #{parent_cve.content_view.name}"
               end
    %(<option data-id="#{inherited_value}" value="">#{_('Inherit parent (%s)') % cve_name}</option>)
  else
    %(<option value="">#{_('Inherit parent')}</option>)
  end
end

#blank_or_inherit_with_id(f, attr) ⇒ Object

rubocop:disable Naming/MethodParameterName



155
156
157
158
159
# File 'app/helpers/katello/content_options_helper.rb', line 155

def blank_or_inherit_with_id(f, attr) # f is Rails convention for form objects
  return true unless f.object.respond_to?(:parent_id) && f.object.parent_id
  inherited_value = f.object.send(attr).try(:id) || ''
  %(<option data-id="#{inherited_value}" value="">#{blank_or_inherit_f(f, attr)}</option>)
end

#build_cve_option_tags(cves, current_cve) ⇒ Object



104
105
106
107
108
109
110
111
# File 'app/helpers/katello/content_options_helper.rb', line 104

def build_cve_option_tags(cves, current_cve)
  option_tags = cves.map do |cve|
    selected = current_cve&.id == cve.id ? 'selected' : ''
    label = cve.default_environment? ? cve.environment.name : "#{cve.environment.name} / #{cve.content_view.name}"
    %(<option value="#{cve.id}" #{selected}>#{h(label)}</option>)
  end
  option_tags.join
end

#build_cve_options_for_orgs(orgs, current_cve, content_source) ⇒ Object

rubocop:enable Rails/OutputSafety



71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/helpers/katello/content_options_helper.rb', line 71

def build_cve_options_for_orgs(orgs, current_cve, content_source)
  orgs.map do |org|
    cves = fetch_cves_for_org(org, current_cve, content_source)
    cve_options = build_cve_option_tags(cves, current_cve)

    if orgs.count > 1
      %(<optgroup label="#{org.name}">#{cve_options}</optgroup>)
    else
      cve_options
    end
  end
end

#content_options(host, selected_id, object_type, options = {}) ⇒ Object

rubocop:disable Rails/OutputSafety Generic method to provide a list of options in the UI



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/katello/content_options_helper.rb', line 5

def content_options(host, selected_id, object_type, options = {})
  include_blank = options.fetch(:include_blank, nil)
  include_blank = '<option></option>' if include_blank == true #check for true specifically
  orgs = relevant_organizations(host)
  all_options = []
  orgs.each do |org|
    content_object_options = ""
    accessible_content_objects = case object_type
                                 when :lifecycle_environment
                                   accessible_lifecycle_environments(org, host)
                                 when :content_source
                                   accessible_content_proxies(host)
                                 end
    accessible_content_objects.each do |content_object|
      selected = selected_id == content_object.id ? 'selected' : ''
      content_object_options << %(<option value="#{content_object.id}" class="kt-env" #{selected}>#{h(content_object.name)}</option>)
    end

    if orgs.count > 1
      all_options << %(<optgroup label="#{org.name}">#{content_object_options}</optgroup>)
    else
      all_options << content_object_options
    end
  end

  all_options = all_options.join
  all_options.insert(0, include_blank) if include_blank
  all_options.html_safe # User content is safely escaped with h()
end

#content_source_options(host, options = {}) ⇒ Object



45
46
47
48
49
50
51
52
# File 'app/helpers/katello/content_options_helper.rb', line 45

def content_source_options(host, options = {})
  content_options(
    host,
    fetch_content_source(host, options).try(:id),
    :content_source,
    options
  )
end

#content_view_environment_options(hostgroup, options = {}) ⇒ Object

rubocop:disable Rails/OutputSafety Generate <option> tags for Content View Environment dropdown (hostgroups only)



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/helpers/katello/content_options_helper.rb', line 56

def content_view_environment_options(hostgroup, options = {})
  include_blank = options.fetch(:include_blank, nil)
  include_blank = '<option></option>' if include_blank == true

  orgs = relevant_organizations(hostgroup)
  current_cve = fetch_content_view_environment(hostgroup, options)
  content_source = fetch_content_source(hostgroup, options)

  all_options = build_cve_options_for_orgs(orgs, current_cve, content_source)
  all_options = all_options.join
  all_options.insert(0, include_blank) if include_blank
  all_options.html_safe # User content is safely escaped with h()
end

#content_views_for_host(host, options) ⇒ Object

rubocop:disable Rails/OutputSafety



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/helpers/katello/content_options_helper.rb', line 114

def content_views_for_host(host, options)
  include_blank = options.fetch(:include_blank, nil)
  if include_blank == true #check for true specifically
    include_blank = '<option></option>'
  end
  lifecycle_environment = fetch_lifecycle_environment(host, options)
  content_view = fetch_content_view(host, options)

  views = []
  if lifecycle_environment
    views = Katello::ContentView.in_environment(lifecycle_environment).ignore_generated.readable.order(:name)
    views |= [content_view] if content_view.present? && content_view.in_environment?(lifecycle_environment)
  elsif content_view
    views = [content_view]
  end
  view_options = views.map do |view|
    selected = content_view.try(:id) == view.id ? 'selected' : ''
    %(<option #{selected} value="#{view.id}">#{h(view.name)}</option>)
  end
  view_options = view_options.join
  view_options.insert(0, include_blank) if include_blank
  view_options.html_safe # User content is safely escaped with h()
end

#fetch_cves_for_org(org, current_cve, content_source) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/katello/content_options_helper.rb', line 84

def fetch_cves_for_org(org, current_cve, content_source)
  cves = Katello::ContentViewEnvironment.joins(:content_view, :environment)
           .where("#{Katello::ContentView.table_name}.organization_id" => org.id)
           .order("#{Katello::KTEnvironment.table_name}.name", "#{Katello::ContentView.table_name}.name")
           .to_a

  cves = filter_cves_by_content_source(cves, content_source, org) if content_source.present?
  cves |= [current_cve] if current_cve.present? && current_cve.content_view.organization_id == org.id
  cves
end

#filter_cves_by_content_source(cves, content_source, org) ⇒ Object



95
96
97
98
99
100
101
102
# File 'app/helpers/katello/content_options_helper.rb', line 95

def filter_cves_by_content_source(cves, content_source, org)
  return cves if content_source.pulp_primary?

  available_env_ids = content_source.lifecycle_environments.where(organization_id: org.id).pluck(:id)
  return cves unless available_env_ids.any?

  cves.select { |cve| available_env_ids.include?(cve.environment_id) }
end

#lifecycle_environment_options(host, options = {}) ⇒ Object

rubocop:enable Rails/OutputSafety



36
37
38
39
40
41
42
43
# File 'app/helpers/katello/content_options_helper.rb', line 36

def lifecycle_environment_options(host, options = {})
  content_options(
    host,
    fetch_lifecycle_environment(host, options).try(:id),
    :lifecycle_environment,
    options
  )
end

#view_to_options(view_options, selected_val, include_blank = false) ⇒ Object

rubocop:disable Rails/OutputSafety



140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/helpers/katello/content_options_helper.rb', line 140

def view_to_options(view_options, selected_val, include_blank = false)
  if include_blank == true #check for true specifically
    include_blank = '<option></option>'
  end
  views = view_options.map do |view|
    selected = selected_val == view.id ? 'selected' : ''
    %(<option #{selected} value="#{view.id}">#{h(view.name)}</option>)
  end
  views = views.join
  views.insert(0, include_blank) if include_blank
  views.html_safe # User content is safely escaped with h()
end