Project

General

Profile

Download (6.26 KB) Statistics
| Branch: | Tag: | Revision:
611f5bff Amos Benari
class Taxonomy < ActiveRecord::Base
acfbc458 Marek Hulan
include Authorizable

1fa008a4 Joseph Magen
include NestedAncestryCommon
611f5bff Amos Benari
307c352e Joseph Mitchell Magen
serialize :ignore_types, Array
611f5bff Amos Benari
3034e8e2 Ori Rabin
validates_lengths_from_database
611f5bff Amos Benari
belongs_to :user
d5bfe587 Ohad Levy
before_destroy EnsureNotUsedBy.new(:hosts)
a1af1096 Joseph Magen
after_create :assign_taxonomy_to_user
611f5bff Amos Benari
has_many :taxable_taxonomies, :dependent => :destroy
has_many :users, :through => :taxable_taxonomies, :source => :taxable, :source_type => 'User'
has_many :smart_proxies, :through => :taxable_taxonomies, :source => :taxable, :source_type => 'SmartProxy'
has_many :compute_resources, :through => :taxable_taxonomies, :source => :taxable, :source_type => 'ComputeResource'
has_many :media, :through => :taxable_taxonomies, :source => :taxable, :source_type => 'Medium'
510d53cd Marek Hulan
has_many :provisioning_templates, :through => :taxable_taxonomies, :source => :taxable, :source_type => 'ProvisioningTemplate'
has_many :ptables, :through => :taxable_taxonomies, :source => :taxable, :source_type => 'Ptable'
611f5bff Amos Benari
has_many :domains, :through => :taxable_taxonomies, :source => :taxable, :source_type => 'Domain'
77f70152 Stephen Benjamin
has_many :realms, :through => :taxable_taxonomies, :source => :taxable, :source_type => 'Realm'
611f5bff Amos Benari
has_many :hostgroups, :through => :taxable_taxonomies, :source => :taxable, :source_type => 'Hostgroup'
has_many :environments, :through => :taxable_taxonomies, :source => :taxable, :source_type => 'Environment'
has_many :subnets, :through => :taxable_taxonomies, :source => :taxable, :source_type => 'Subnet'

4d72215c Ohad Levy
validate :check_for_orphans, :unless => Proc.new {|t| t.new_record?}
0a17d4da Tom Caspy
validates :name, :presence => true, :uniqueness => {:scope => [:ancestry, :type], :case_sensitive => false}
validates :title, :presence => true, :uniqueness => {:scope => :type}

307c352e Joseph Mitchell Magen
before_validation :sanitize_ignored_types
4a28771d Stephen Benjamin
after_create :assign_default_templates
2fa95807 Joseph Mitchell Magen
581d3b6b Ori Rabin
scoped_search :on => :description, :complete_enabled => :false, :only_explicit => true

e611a26c Joseph Magen
delegate :import_missing_ids, :inherited_ids, :used_and_selected_or_inherited_ids, :selected_or_inherited_ids,
:non_inherited_ids, :used_or_inherited_ids, :used_ids, :to => :tax_host
1fa008a4 Joseph Magen
bb3572ff Daniel Lobato
default_scope -> { order(:title) }
611f5bff Amos Benari
26ee3b1e Tomer Brisker
scope :completer_scope, lambda{|opts|
if opts[:controller] == 'organizations'
Organization.completer_scope opts
elsif opts[:controller] == 'locations'
Location.completer_scope opts
end
}

611f5bff Amos Benari
def self.locations_enabled
410505f4 Marek Hulan
enabled?(:location)
611f5bff Amos Benari
end

def self.organizations_enabled
410505f4 Marek Hulan
enabled?(:organization)
611f5bff Amos Benari
end

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

5f029ed6 Daniel Lobato
def self.as_taxonomy(org, location)
611f5bff Amos Benari
Organization.as_org org do
Location.as_location location do
yield if block_given?
end
end
end

410505f4 Marek Hulan
def self.enabled?(taxonomy)
case taxonomy
when :organization
SETTINGS[:organizations_enabled]
when :location
SETTINGS[:locations_enabled]
else
raise ArgumentError, "unknown taxonomy #{taxonomy}"
end
end

3279c309 Stephen Benjamin
def self.enabled_taxonomies
%w(locations organizations).select { |taxonomy| SETTINGS["#{taxonomy}_enabled".to_sym] }
end

1f1367d6 Amos Benari
def self.ignore?(taxable_type)
7deaced2 Daniel Lobato
Array.wrap(self.current).each do |current|
1f1367d6 Amos Benari
return true if current.ignore?(taxable_type)
7deaced2 Daniel Lobato
end
1f1367d6 Amos Benari
false
end

abe910f2 Marek Hulan
# 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 (in what organizations
# is he assigned to)
#
# if user is not assigned to any organization then empty array is returned which means
# that we should use all organizations
#
# if user is admin we we return the original value since it does not need any additional scoping
def self.expand(value)
if value.blank? && User.current.present? && !User.current.admin?
value = self.send("my_#{self.to_s.underscore.pluralize}").all
end
value
end

307c352e Joseph Mitchell Magen
def ignore?(taxable_type)
if ignore_types.empty?
false
else
ignore_types.include?(taxable_type.classify)
end
end

2fa95807 Joseph Mitchell Magen
def self.all_import_missing_ids
671b45e9 Joseph Mitchell Magen
all.each do |taxonomy|
2fa95807 Joseph Mitchell Magen
taxonomy.import_missing_ids
end
end

def self.all_mismatcheds
671b45e9 Joseph Mitchell Magen
includes(:hosts).map { |taxonomy| taxonomy.mismatches }
2fa95807 Joseph Mitchell Magen
end

feacea35 Amos Benari
def dup
afa59582 Ohad Levy
new = super
new.name = ""
new.users = users
2312cccf Daniel Lobato
new.environments = environments
afa59582 Ohad Levy
new.smart_proxies = smart_proxies
new.subnets = subnets
new.compute_resources = compute_resources
510d53cd Marek Hulan
new.provisioning_templates = provisioning_templates
afa59582 Ohad Levy
new.media = media
new.domains = domains
77f70152 Stephen Benjamin
new.realms = realms
afa59582 Ohad Levy
new.media = media
new.hostgroups = hostgroups
new
end
2fa95807 Joseph Mitchell Magen
feacea35 Amos Benari
# overwrite *_ids since need to check if ignored? - don't overwrite location_ids and organizations_ids since these aren't ignored
e1a1fa81 Joseph Mitchell Magen
(TaxHost::HASH_KEYS - [:location_ids, :organizations_ids]).each do |key|
# def domain_ids
feacea35 Amos Benari
# if ignore?("Domain")
# Domain.pluck(:id)
# else
e1a1fa81 Joseph Mitchell Magen
# self.taxable_taxonomies.where(:taxable_type => "Domain").pluck(:taxable_id)
# end
define_method(key) do
klass = hash_key_to_class(key)
if ignore?(klass)
e07f9a12 Dominic Cleal
return User.unscoped.except_admin.except_hidden.map(&:id) if klass == "User"
feacea35 Amos Benari
return klass.constantize.pluck(:id)
e1a1fa81 Joseph Mitchell Magen
else
taxable_taxonomies.where(:taxable_type => klass).pluck(:taxable_id)
end
end
end

70acceb5 Greg Sutcliffe
def expire_topbar_cache(sweeper)
8fabe661 Tomer Brisker
(users+User.only_admin).each { |u| u.expire_topbar_cache(sweeper) }
70acceb5 Greg Sutcliffe
end

307c352e Joseph Mitchell Magen
private

e611a26c Joseph Magen
delegate :need_to_be_selected_ids, :selected_ids, :used_and_selected_ids, :mismatches, :missing_ids, :check_for_orphans,
f2c78d4a Joseph Magen
:to => :tax_host
2fa95807 Joseph Mitchell Magen
4a28771d Stephen Benjamin
def assign_default_templates
510d53cd Marek Hulan
Template.where(:default => true).each do |template|
self.send("#{template.class.to_s.underscore.pluralize}") << template
end
4a28771d Stephen Benjamin
end

307c352e Joseph Mitchell Magen
def sanitize_ignored_types
self.ignore_types ||= []
1fa008a4 Joseph Magen
self.ignore_types = self.ignore_types.compact.uniq - ["0"]
307c352e Joseph Mitchell Magen
end
afa59582 Ohad Levy
2fa95807 Joseph Mitchell Magen
def tax_host
@tax_host ||= TaxHost.new(self)
end

e1a1fa81 Joseph Mitchell Magen
def hash_key_to_class(key)
f2c78d4a Joseph Magen
key.to_s.gsub(/_ids?\Z/, '').classify
e1a1fa81 Joseph Mitchell Magen
end

a1af1096 Joseph Magen
def assign_taxonomy_to_user
868faa8f Daniel Lobato
return if User.current.nil? || User.current.admin
a1af1096 Joseph Magen
TaxableTaxonomy.create(:taxonomy_id => self.id, :taxable_id => User.current.id, :taxable_type => 'User')
end
611f5bff Amos Benari
end