Class: Decidim::PermissionsRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/permissions_registry.rb

Overview

Takes care of holding and accessing permissions classes for each artifact.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePermissionsRegistry

Returns a new instance of PermissionsRegistry.



8
9
10
# File 'lib/decidim/permissions_registry.rb', line 8

def initialize
  @registry = {}
end

Class Method Details

.chain_for(artifact) ⇒ Object

Syntactic sugar for the ‘chain_for` instance method.



13
14
15
# File 'lib/decidim/permissions_registry.rb', line 13

def self.chain_for(artifact)
  ::Decidim.permissions_registry.chain_for(artifact)
end

Instance Method Details

#artifact_to_key(artifact) ⇒ Object

Registry accepts the class or the class name of the artifact, but the registry only indexes by the name. Artifact name normalization is done here.



35
36
37
# File 'lib/decidim/permissions_registry.rb', line 35

def artifact_to_key(artifact)
  artifact.respond_to?(:name) ? artifact.name : artifact
end

#chain_for(artifact) ⇒ Object

Returns the registered array of permissions for the given ‘artifact`.

artifact is expected to be the class or module that declares ‘NeedsPermission.permission_class_chain`.



20
21
22
# File 'lib/decidim/permissions_registry.rb', line 20

def chain_for(artifact)
  @registry[artifact_to_key(artifact)]
end

#register_permissions(artifact, *permission_classes) ⇒ Object

Registers the of ‘Permissions` for the given `artifact`.

artifact is expected to be the class or module that declares ‘NeedsPermission.permission_class_chain`. permission_classes are subclasses of `DefaultPermissions`.



28
29
30
# File 'lib/decidim/permissions_registry.rb', line 28

def register_permissions(artifact, *permission_classes)
  @registry[artifact_to_key(artifact)] = permission_classes.dup
end