Class: Decidim::AuthorizationHandler
- Defined in:
- app/services/decidim/authorization_handler.rb
Overview
This is the base class for authorization handlers, all implementations should inherit from it. Each AuthorizationHandler is a form that will be used to check if the authorization is valid or not. When it is valid a new authorization will be created for the user.
Feel free to use validations to assert fields against a remote API, local database, or whatever.
It also sets two default attributes, ‘user` and `handler_name`.
Class Method Summary collapse
- .active_handler?(name) ⇒ Boolean
-
.handler_for(name, params = {}) ⇒ Object
Finds a handler class from a String.
-
.handler_name ⇒ Object
A serialized version of the handler’s name.
Instance Method Summary collapse
-
#form_attributes ⇒ Object
THe attributes of the handler that should be exposed as form input when rendering the handler in a form.
-
#handler_name ⇒ Object
Same as the class method but accessible from the instance.
-
#metadata ⇒ Object
Any data that the developer would like to inject to the ‘metadata` field of an authorization when it’s created.
-
#to_partial_path ⇒ Object
The String partial path so Rails can render the handler as a form.
-
#unique_id ⇒ Object
A unique ID to be implemented by the authorization handler that ensures no duplicates are created.
Class Method Details
.active_handler?(name) ⇒ Boolean
91 92 93 |
# File 'app/services/decidim/authorization_handler.rb', line 91 def self.active_handler?(name) name && Decidim..include?(name.classify) end |
.handler_for(name, params = {}) ⇒ Object
Finds a handler class from a String. This is necessary when processing the form data. It will only look for valid handlers that have also been configured in ‘Decidim.authorization_handlers`.
name - The String name of the class to find, usually in the same shape as the one returned by ‘handler_name`. params - An optional Hash with params to initialize the handler.
Returns an AuthorizationHandler descendant. Returns nil when no handlers could be found.
85 86 87 88 89 |
# File 'app/services/decidim/authorization_handler.rb', line 85 def self.handler_for(name, params = {}) return unless active_handler?(name) name.classify.constantize.from_params(params || {}) end |
.handler_name ⇒ Object
A serialized version of the handler’s name.
Returns a String.
64 65 66 |
# File 'app/services/decidim/authorization_handler.rb', line 64 def self.handler_name name.underscore end |
Instance Method Details
#form_attributes ⇒ Object
THe attributes of the handler that should be exposed as form input when rendering the handler in a form.
Returns an Array of Strings.
33 34 35 |
# File 'app/services/decidim/authorization_handler.rb', line 33 def form_attributes attributes.except(:id, :user).keys end |
#handler_name ⇒ Object
Same as the class method but accessible from the instance.
Returns a String.
71 72 73 |
# File 'app/services/decidim/authorization_handler.rb', line 71 def handler_name self.class.handler_name end |
#metadata ⇒ Object
Any data that the developer would like to inject to the ‘metadata` field of an authorization when it’s created. Can be useful if some of the params the user sent with the authorization form want to be persisted for future use.
Returns a Hash.
57 58 59 |
# File 'app/services/decidim/authorization_handler.rb', line 57 def {} end |
#to_partial_path ⇒ Object
The String partial path so Rails can render the handler as a form. This is useful if you want to have a custom view to render the form instead of the default view.
Example:
A handler named Decidim::CensusHandler would look for its partial in:
decidim/census/form
Returns a String.
47 48 49 |
# File 'app/services/decidim/authorization_handler.rb', line 47 def to_partial_path handler_name.sub!(/_handler$/, "") + "/form" end |
#unique_id ⇒ Object
A unique ID to be implemented by the authorization handler that ensures no duplicates are created. This uniqueness check will be skipped if unique_id returns nil.
25 26 27 |
# File 'app/services/decidim/authorization_handler.rb', line 25 def unique_id nil end |