Class: Decidim::Scope
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Scope
- Defined in:
- app/models/decidim/scope.rb
Overview
Scopes are used in some entities through Decidim to help users know which is the scope of a participatory process. (i.e. does it affect the whole city or just a district?)
Class Method Summary collapse
- .log_presenter_class_for(_log) ⇒ Object
-
.top_level ⇒ Object
Scope to return only the top level scopes.
Instance Method Summary collapse
- #ancestor_of?(scope) ⇒ Boolean
- #descendants ⇒ Object
-
#part_of_scopes(root = nil) ⇒ Object
Gets the scopes from the part_of list in descending order (first the top level scope, last itself).
Class Method Details
.log_presenter_class_for(_log) ⇒ Object
49 50 51 |
# File 'app/models/decidim/scope.rb', line 49 def self.log_presenter_class_for(_log) Decidim::AdminLog::ScopePresenter end |
.top_level ⇒ Object
Scope to return only the top level scopes.
Returns an ActiveRecord::Relation.
45 46 47 |
# File 'app/models/decidim/scope.rb', line 45 def self.top_level where parent_id: nil end |
Instance Method Details
#ancestor_of?(scope) ⇒ Boolean
57 58 59 |
# File 'app/models/decidim/scope.rb', line 57 def ancestor_of?(scope) scope && scope.part_of.member?(id) end |
#descendants ⇒ Object
53 54 55 |
# File 'app/models/decidim/scope.rb', line 53 def descendants organization.scopes.where("? = ANY(decidim_scopes.part_of)", id) end |
#part_of_scopes(root = nil) ⇒ Object
Gets the scopes from the part_of list in descending order (first the top level scope, last itself)
root - The root scope to start retrieval. If present, ignores top level scopes until reaching the root scope.
Returns an array of Scope objects
66 67 68 69 70 |
# File 'app/models/decidim/scope.rb', line 66 def part_of_scopes(root = nil) scope_ids = part_of scope_ids.select! { |id| id == root.id || !root.part_of.member?(id) } if root organization.scopes.where(id: scope_ids).sort { |s1, s2| part_of.index(s2.id) <=> part_of.index(s1.id) } end |