Class: Decidim::Scope

Inherits:
ApplicationRecord show all
Includes:
FilterableResource, 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



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

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.



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

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)


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

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

#descendantsObject



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

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



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

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