Class: Decidim::Scope

Inherits:
ApplicationRecord show all
Includes:
Loggable, Traceable, TranslatableResource
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

Instance Method Summary collapse

Class Method Details

.log_presenter_class_for(_log) ⇒ Object



52
53
54
# File 'app/models/decidim/scope.rb', line 52

def self.log_presenter_class_for(_log)
  Decidim::AdminLog::ScopePresenter
end

.top_level(organization_id = nil) ⇒ Object

Scope to return only the top level scopes.

Returns an ActiveRecord::Relation.



46
47
48
49
50
# File 'app/models/decidim/scope.rb', line 46

def self.top_level(organization_id = nil)
  query = where parent_id: nil
  query = query.where(decidim_organization_id: organization_id) if organization_id
  query
end

Instance Method Details

#ancestor_of?(scope) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/decidim/scope.rb', line 60

def ancestor_of?(scope)
  scope && scope.part_of.member?(id)
end

#descendantsObject



56
57
58
# File 'app/models/decidim/scope.rb', line 56

def descendants
  @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



69
70
71
72
73
# File 'app/models/decidim/scope.rb', line 69

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