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



54
55
56
# File 'app/models/decidim/scope.rb', line 54

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.



48
49
50
51
52
# File 'app/models/decidim/scope.rb', line 48

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)


62
63
64
# File 'app/models/decidim/scope.rb', line 62

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

#descendantsObject



58
59
60
# File 'app/models/decidim/scope.rb', line 58

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



71
72
73
74
75
# File 'app/models/decidim/scope.rb', line 71

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