Module: Decidim::Scopable

Extended by:
ActiveSupport::Concern
Defined in:
lib/decidim/scopable.rb

Overview

A concern with the components needed when you want a model to have a scope.

The including model needs to implement the following interface:

@abstract An instance method that returns the id of the scope
@method decidim_scope_id
  @return [Integer]

@abstract An instance method that states whether scopes are enabled or not
@method scopes_enabled
  @return [Boolean]

@abstract An method that gives an associated organization
@method organization
  @return [Decidim::Organization]

Instance Method Summary collapse

Instance Method Details

#has_subscopes?Boolean

Whether the resource has subscopes or not.

Returns a boolean.

Returns:

  • (Boolean)


65
66
67
# File 'lib/decidim/scopable.rb', line 65

def has_subscopes?
  scopes_enabled? && subscopes.any?
end

#out_of_scope?(subscope) ⇒ Boolean

Whether the passed subscope is out of the resource’s scope.

Returns a boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/decidim/scopable.rb', line 72

def out_of_scope?(subscope)
  scope && !scope.ancestor_of?(subscope)
end

#previous_scopeObject

If any, gets the previous scope of the object.

Returns a Decidim::Scope



80
81
82
83
84
# File 'lib/decidim/scopable.rb', line 80

def previous_scope
  return if versions.count <= 1

  Decidim::Scope.find_by(id: versions.last.reify.decidim_scope_id)
end

#scopes_enabled?Boolean

Whether the resource has scopes enabled or not.

Returns a boolean.

Returns:

  • (Boolean)


49
50
51
# File 'lib/decidim/scopable.rb', line 49

def scopes_enabled?
  scopes_enabled
end

#subscopesObject

Gets the children scopes of the object’s scope.

If it is global, returns the organization’s top scopes.

Returns an ActiveRecord::Relation.



58
59
60
# File 'lib/decidim/scopable.rb', line 58

def subscopes
  scope ? scope.children : organization.top_scopes
end