Class: TaxHost

Inherits:
Object
  • Object
show all
Defined in:
app/models/tax_host.rb

Constant Summary collapse

FOREIGN_KEYS =
[:location_id, :organization_id, :hostgroup_id,
:environment_id, :domain_id, :medium_id,
:subnet_id, :compute_resource_id]
HASH_KEYS =
[:location_ids, :organization_ids, :hostgroup_ids,
:environment_ids, :domain_ids, :medium_ids,
:subnet_ids, :compute_resource_ids,
:smart_proxy_ids, :user_ids, :config_template_ids]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(taxonomy, hosts = nil) ⇒ TaxHost


12
13
14
15
# File 'app/models/tax_host.rb', line 12

def initialize(taxonomy, hosts=nil)
  @taxonomy = taxonomy
  @hosts    = hosts.nil? ? @taxonomy.hosts : Host.where(:id => Array.wrap(hosts).map(&:id))
end

Instance Attribute Details

#hostsObject (readonly)

Returns the value of attribute hosts


17
18
19
# File 'app/models/tax_host.rb', line 17

def hosts
  @hosts
end

#taxonomyObject (readonly)

Returns the value of attribute taxonomy


17
18
19
# File 'app/models/tax_host.rb', line 17

def taxonomy
  @taxonomy
end

Instance Method Details

#check_for_orphansObject


113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/tax_host.rb', line 113

def check_for_orphans
  found_orphan = false
  error_msg = "The following must be selected since they belong to hosts:\n\n"
  need_to_be_selected_ids.each do |key, array_values|
    taxable_type = hash_key_to_class(key)
    unless array_values.empty?
      found_orphan = true
      taxonomy.errors.add(taxable_type.tableize, _("You cannot remove %s that are used by hosts.") % taxable_type.tableize.humanize.downcase)
    end
  end
  !found_orphan
end

#import_missing_idsObject


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/tax_host.rb', line 80

def import_missing_ids
  missing_ids.each do |row|
    # no object for table locations_organizations, so use method *_ids = [array id's] to create relationship
    if %w[Location Organization].include?(row[:taxable_type])
      if (tax = Taxonomy.find_by_id(row[:taxonomy_id]))
        tax.send("#{opposite_taxonomy_type}_ids=".to_sym, [row[:taxable_id]] + tax.send("#{opposite_taxonomy_type}_ids"))
      end
    else
      TaxableTaxonomy.create!(row)
    end
  end
  clear!
end

#mismatchesObject


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/models/tax_host.rb', line 94

def mismatches
  return @mismatches if @mismatches
  mismatches = Array.new
  need_to_be_selected_ids.each do |key, values|
    taxable_type = hash_key_to_class(key)
    values.each do |v|
      #TODO: use IN (ids) instead of find per id
      taxable_record = taxable_type.constantize.find(v)
      mismatches << { :taxonomy_id   => taxonomy.id,
                      :taxonomy_name => taxonomy.name,
                      :taxonomy_type => taxonomy.class.to_s,
                      :taxable_value => taxable_record.name,
                      :taxable_type  => taxable_type
      }
    end
  end
  @mismatches = mismatches.uniq.compact
end

#missing_idsObject


68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/tax_host.rb', line 68

def missing_ids
  return @missing_ids if @missing_ids
  missing_ids = Array.new
  need_to_be_selected_ids.each do |key, values|
    taxable_type = hash_key_to_class(key)
    values.each do |v|
      missing_ids << { :taxonomy_id => taxonomy.id, :taxable_type => taxable_type, :taxable_id => v }
    end
  end
  @missing_ids = missing_ids
end

#need_to_be_selected_idsObject


57
58
59
60
61
62
63
64
65
66
# File 'app/models/tax_host.rb', line 57

def need_to_be_selected_ids
  @need_to_be_selected_ids ||= Hash[hash_keys.map do |col|
    if taxonomy.ignore?(hash_key_to_class(col))
      [col, [] ] # empty array since nothing needs to be selected
    else
      [col, used_ids[col] - selected_ids[col]] # - operator find NON-common elements of arrays
    end
  end]

end

#selected_idsObject


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/tax_host.rb', line 30

def selected_ids
  return @selected_ids if @selected_ids
  ids         = HashWithIndifferentAccess.new
  ids.default = []
  #types NOT ignored - get ids that are selected
  taxonomy.taxable_taxonomies.without(taxonomy.ignore_types).group_by { |d| d[:taxable_type] }.map do |k, v|
    ids["#{k.tableize.singularize}_ids"] = v.map { |i| i[:taxable_id] }
  end
  #types that ARE ignored - get ALL ids for object
  Array(taxonomy.ignore_types).each do |taxonomy_type|
    ids["#{taxonomy_type.tableize.singularize}_ids"] = taxonomy_type.constantize.pluck(:id)
  end

  ids["#{opposite_taxonomy_type}_ids"] = taxonomy.send("#{opposite_taxonomy_type}_ids")
  @selected_ids                        = ids
end

#used_and_selected_idsObject


47
48
49
50
51
52
53
54
55
# File 'app/models/tax_host.rb', line 47

def used_and_selected_ids
  @used_and_selected_ids ||= Hash[hash_keys.map do |col|
    if taxonomy.ignore?(hash_key_to_class(col))
      [col, used_ids[col] ] # used_ids only if ignore selected
    else
      [col, used_ids[col] & selected_ids[col]] # & operator to intersect COMMON elements of arrays
    end
  end]
end

#used_idsObject

returns a hash of HASH_KEYS used ids by hosts in a given taxonomy


20
21
22
23
24
25
26
27
28
# File 'app/models/tax_host.rb', line 20

def used_ids
  return @used_ids if @used_ids
  ids         = HashWithIndifferentAccess.new
  ids.default = []
  hash_keys.each do |col|
    ids[col] = self.send(col)
  end
  @used_ids = ids
end