Class: Taxonomy

Inherits:
ApplicationRecord show all
Includes:
Authorizable, NestedAncestryCommon, TopbarCacheExpiry
Defined in:
app/models/taxonomy.rb

Direct Known Subclasses

Location, Organization

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TopbarCacheExpiry

#expire_topbar_cache_within_controller

Methods included from NestedAncestryCommon

#get_title, #nested, #parent_name, #title, #to_param

Methods included from Authorizable

#authorized?, #check_permissions_after_save

Methods included from PermissionName

#permission_name

Methods inherited from ApplicationRecord

graphql_type, #logger, logger

Methods included from AuditAssociations::AssociationsDefinitions

#audit_associations, #audited, #configure_dirty_associations, #normalize_associations

Class Method Details

.all_import_missing_idsObject


122
123
124
125
126
# File 'app/models/taxonomy.rb', line 122

def self.all_import_missing_ids
  all.find_each do |taxonomy|
    taxonomy.import_missing_ids
  end
end

.all_mismatchedsObject


128
129
130
# File 'app/models/taxonomy.rb', line 128

def self.all_mismatcheds
  includes(:hosts).map { |taxonomy| taxonomy.mismatches }
end

.as_taxonomy(org, location) ⇒ Object


80
81
82
83
84
85
86
# File 'app/models/taxonomy.rb', line 80

def self.as_taxonomy(org, location)
  Organization.as_org org do
    Location.as_location location do
      yield if block_given?
    end
  end
end

.expand(value) ⇒ Object

if taxonomy e.g. organization was not set by current context (e.g. Any organization) then we have to compute what this context mean for current user (what organizations they are assigned to)

if user is not assigned to any organization then empty relation is returned.

if user is admin we we return the original value (even if nil).


111
112
113
114
115
116
# File 'app/models/taxonomy.rb', line 111

def self.expand(value)
  if value.blank? && User.current.present? && !User.current.admin?
    value = send("my_#{to_s.underscore.pluralize}")
  end
  value
end

.ignore?(taxable_type) ⇒ Boolean

Returns:

  • (Boolean)

92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/taxonomy.rb', line 92

def self.ignore?(taxable_type)
  current_taxonomies = if current.nil? && User.current.present?
                         # "Any context" - all available taxonomies"
                         User.current.public_send("my_#{to_s.underscore.pluralize}")
                       else
                         [current]
                       end
  current_taxonomies.compact.any? do |current|
    current.ignore?(taxable_type)
  end
end

.inherited(child) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/taxonomy.rb', line 38

def self.inherited(child)
  child.instance_eval do
    scoped_search :on => :description, :complete_enabled => :false, :only_explicit => true
    scoped_search :on => :id, :validator => ScopedSearch::Validators::INTEGER

    apipie :class, desc: "A class representing #{model_name.human} object" do
      sections only: %w[all additional]
      name_exl, title_exl = class_scope.model_name.human == 'Location' ? ['Europe', 'Europe/Prague'] : ['Red Hat', 'Red Hat/Engineering']
      prop_group :basic_model_props, ApplicationRecord, meta: { example: name_exl }
      property :title, String, desc: "Title of the #{class_scope}. Comparing to the Name, Title contains also names of all parent #{class_scope}s, e.g. #{title_exl}"
      property :description, String, desc: "Description of the #{class_scope}"
      property :created_at, String, desc: "The time when the #{class_scope} was created"
      property :updated_at, String, desc: "The last time when the #{class_scope} was updated"
    end
    jail_class = Class.new(::Safemode::Jail) do
      allow :id, :name, :title, :created_at, :updated_at, :description
    end
    child.const_set('Jail', jail_class)
  end
  child.send(:include, NestedAncestryCommon::Search)
  super
end

.no_taxonomy_scopeObject


74
75
76
77
78
# File 'app/models/taxonomy.rb', line 74

def self.no_taxonomy_scope
  as_taxonomy nil, nil do
    yield if block_given?
  end
end

.typesObject


88
89
90
# File 'app/models/taxonomy.rb', line 88

def self.types
  [Organization, Location]
end

Instance Method Details

#destroy_taxable_taxonomiesObject

note - this method used by before_destroy callbacks in extension files from plugins audits for 'destroy' action on resources lead to taxable_taxonomies records. This will check if any taxable_taxonomies records present and apply destroy_all so that it nullifies all associated audit records


222
223
224
# File 'app/models/taxonomy.rb', line 222

def destroy_taxable_taxonomies
  TaxableTaxonomy.where(taxonomy_id: id).destroy_all
end

#dupObject


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/taxonomy.rb', line 132

def dup
  new = super
  new.name = ""
  new.users             = users
  new.environments      = environments
  new.smart_proxies     = smart_proxies
  new.subnets           = subnets
  new.compute_resources = compute_resources
  new.provisioning_templates = provisioning_templates
  new.ptables = ptables
  new.report_templates = report_templates
  new.media             = media
  new.domains           = domains
  new.realms            = realms
  new.media             = media
  new.hostgroups        = hostgroups
  new.auth_sources      = auth_sources
  new
end

#expire_topbar_cacheObject


171
172
173
# File 'app/models/taxonomy.rb', line 171

def expire_topbar_cache
  (users + User.only_admin).each { |u| u.expire_topbar_cache }
end

#ignore?(taxable_type) ⇒ Boolean

Returns:

  • (Boolean)

118
119
120
# File 'app/models/taxonomy.rb', line 118

def ignore?(taxable_type)
  ignore_types.include?(taxable_type.classify)
end

#notification_recipients_idsObject


214
215
216
# File 'app/models/taxonomy.rb', line 214

def notification_recipients_ids
  subtree.flat_map(&:users).map(&:id).uniq
end

#parameters(include_source = false) ⇒ Object

returns self and parent parameters as a hash


187
188
189
190
191
192
193
# File 'app/models/taxonomy.rb', line 187

def parameters(include_source = false)
  hash = parent_params(include_source)
  send("#{type.downcase}_parameters".to_sym).authorized(:view_params).each do |p|
    hash[p.name] = include_source ? p.hash_for_include_source(sti_name, el.title) : p.value
  end
  hash
end

#params_objectsObject


210
211
212
# File 'app/models/taxonomy.rb', line 210

def params_objects
  (send("#{type.downcase}_parameters".to_sym).authorized(:view_params) + taxonomy_inherited_params_objects.to_a.reverse!).uniq { |param| param.name }
end

#parent_params(include_source = false) ⇒ Object


175
176
177
178
179
180
181
182
183
184
# File 'app/models/taxonomy.rb', line 175

def parent_params(include_source = false)
  hash = {}
  elements = parents_with_params
  elements.each do |el|
    el.send("#{type.downcase}_parameters".to_sym).authorized(:view_params).each do |p|
      hash[p.name] = include_source ? p.hash_for_include_source(sti_name, el.title) : p.value
    end
  end
  hash
end

#parents_with_paramsObject


195
196
197
# File 'app/models/taxonomy.rb', line 195

def parents_with_params
  self.class.sort_by_ancestry(self.class.includes("#{type.downcase}_parameters".to_sym).find(ancestor_ids))
end

#taxonomy_inherited_params_objectsObject


199
200
201
202
203
204
205
206
207
208
# File 'app/models/taxonomy.rb', line 199

def taxonomy_inherited_params_objects
  # need to pull out the locations to ensure they are sorted first,
  # otherwise we might be overwriting the hash in the wrong order.
  parents = parents_with_params
  parents_parameters = []
  parents.each do |parent|
    parents_parameters << parent.send("#{parent.type.downcase}_parameters".to_sym)
  end
  parents_parameters
end