Revision acfbc458
Added by Marek Hulán about 10 years ago
app/models/concerns/taxonomix.rb | ||
---|---|---|
after_initialize :set_current_taxonomy
|
||
|
||
scoped_search :in => :locations, :on => :name, :rename => :location, :complete_value => true
|
||
scoped_search :in => :locations, :on => :id, :rename => :location_id, :complete_value => true
|
||
scoped_search :in => :organizations, :on => :name, :rename => :organization, :complete_value => true
|
||
scoped_search :in => :organizations, :on => :id, :rename => :organization_id, :complete_value => true
|
||
end
|
||
|
||
module ClassMethods
|
||
|
||
attr_accessor :which_ancestry_method, :which_location, :which_organization
|
||
|
||
# default inner_method includes children (subtree_ids)
|
||
def with_taxonomy_scope(loc = Location.current, org = Organization.current, inner_method = :subtree_ids)
|
||
self.which_ancestry_method = inner_method
|
||
self.which_location = loc
|
||
self.which_organization = org
|
||
scope = block_given? ? yield : where('1=1')
|
||
scope = scope.where(:id => taxable_ids) if taxable_ids
|
||
scope.readonly(false)
|
||
end
|
||
|
||
# default inner_method includes parents (path_ids)
|
||
def with_taxonomy_scope_override(loc = nil, org = nil, inner_method = :path_ids)
|
||
# need to .unscoped or default_scope {with_taxonomy_scope} overrides inner_method
|
||
unscoped.with_taxonomy_scope(loc, org, inner_method)
|
||
end
|
||
|
||
def used_taxonomy_ids
|
||
used_location_ids + used_organization_ids
|
||
end
|
||
|
||
def used_location_ids
|
||
enforce_default
|
||
return [] unless which_location && SETTINGS[:locations_enabled]
|
||
(which_location.send(which_ancestry_method) + which_location.ancestor_ids).uniq
|
||
end
|
||
|
||
def used_organization_ids
|
||
enforce_default
|
||
return [] unless which_organization && SETTINGS[:organizations_enabled]
|
||
(which_organization.send(which_ancestry_method) + which_organization.ancestor_ids).uniq
|
||
end
|
||
|
||
# default scope is not called if we just use #scoped therefore we have to enforce quering
|
||
# to get correct default values
|
||
def enforce_default
|
||
if which_ancestry_method.nil?
|
||
self.scoped.limit(0).all
|
||
end
|
||
end
|
||
|
||
def taxable_ids(loc = which_location, org = which_organization, inner_method = which_ancestry_method)
|
||
if SETTINGS[:locations_enabled] && loc
|
||
inner_ids_loc = if Location.ignore?(self.to_s)
|
||
self.pluck(:id)
|
||
... | ... | |
inner_ids ||= inner_ids_org if inner_ids_org
|
||
# In the case of users we want the taxonomy scope to get both the users of the taxonomy and admins.
|
||
inner_ids << admin_ids if inner_ids && self == User
|
||
scope = scope.where(:id => inner_ids) if inner_ids
|
||
scope.readonly(false)
|
||
end
|
||
|
||
# default inner_method includes parents (path_ids)
|
||
def with_taxonomy_scope_override(loc = nil, org = nil, inner_method = :path_ids)
|
||
with_taxonomy_scope(loc, org, inner_method)
|
||
inner_ids
|
||
end
|
||
|
||
def inner_select(taxonomy, inner_method)
|
||
def inner_select(taxonomy, inner_method = which_ancestry_method)
|
||
# always include ancestor_ids in inner select
|
||
taxonomy_ids = (taxonomy.send(inner_method) + taxonomy.ancestor_ids).uniq
|
||
TaxableTaxonomy.where(:taxable_type => self.name, :taxonomy_id => taxonomy_ids).pluck(:taxable_id).compact.uniq
|
Also available in: Unified diff
fixes #812 - new permissions model, user group role and nest support, role filters for better granularity
Contributions from: