Class: Decidim::Initiatives::VoteForm
- Inherits:
-
Form
- Object
- AttributeObject::Form
- Form
- Decidim::Initiatives::VoteForm
- Includes:
- TranslatableAttributes
- Defined in:
- decidim-initiatives/app/forms/decidim/initiatives/vote_form.rb
Overview
A form object used to collect the data for a new initiative.
Constant Summary
Constants included from AttributeObject::TypeMap
AttributeObject::TypeMap::Boolean, AttributeObject::TypeMap::Decimal
Instance Attribute Summary
Attributes inherited from AttributeObject::Form
Instance Method Summary collapse
-
#authorized_scope_candidates ⇒ Object
Public: Builds a list of Decidim::Scopes where the user could have a valid authorization.
-
#authorized_scopes ⇒ Object
Public: Builds the list of scopes where the user is authorized to vote in.
- #encrypted_metadata ⇒ Object
-
#hash_id ⇒ Object
Public: The hash to uniquely identify an initiative vote.
- #metadata ⇒ Object
-
#user_authorized_scope ⇒ Object
Public: Finds the scope the user has an authorization for, this way the user can vote on that scope and its parents.
Methods included from TranslatableAttributes
Methods inherited from AttributeObject::Form
ensure_hash, from_model, from_params, hash_from, infer_model_name, #map_model, mimic, mimicked_model_name, model_name, #persisted?, #to_key, #to_model, #to_param, #valid?, #with_context
Methods included from AttributeObject::Model
#[], #[]=, #attributes, #attributes_with_values, #initialize, #to_h
Instance Method Details
#authorized_scope_candidates ⇒ Object
Public: Builds a list of Decidim::Scopes where the user could have a valid authorization.
If the initiative is set with a global scope (meaning the scope is nil), all the scopes in the organization are valid.
Returns an array of Decidim::Scopes.
100 101 102 103 104 105 106 107 108 |
# File 'decidim-initiatives/app/forms/decidim/initiatives/vote_form.rb', line 100 def = [initiative.scope] += if initiative.scope.present? initiative.scope.descendants else initiative.organization.scopes end .uniq end |
#authorized_scopes ⇒ Object
Public: Builds the list of scopes where the user is authorized to vote in. This is used when the initiative allows also voting on child scopes, not only the main scope.
Instead of just listing the children of the main scope, we just want to select the ones that have been added to the InitiativeType with its voting settings.
62 63 64 65 66 67 68 |
# File 'decidim-initiatives/app/forms/decidim/initiatives/vote_form.rb', line 62 def initiative.votable_initiative_type_scopes.select do |initiative_type_scope| initiative_type_scope.global_scope? || initiative_type_scope.scope == || initiative_type_scope.scope.ancestor_of?() end.flat_map(&:scope) end |
#encrypted_metadata ⇒ Object
34 35 36 37 38 |
# File 'decidim-initiatives/app/forms/decidim/initiatives/vote_form.rb', line 34 def return unless required_personal_data? @encrypted_metadata ||= encryptor.encrypt() end |
#hash_id ⇒ Object
Public: The hash to uniquely identify an initiative vote. It uses the initiative scope as a default.
Returns a String.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'decidim-initiatives/app/forms/decidim/initiatives/vote_form.rb', line 44 def hash_id return unless initiative && (document_number || signer) @hash_id ||= Digest::MD5.hexdigest( [ initiative.id, document_number || signer.id, Rails.application.secret_key_base ].compact.join("-") ) end |
#metadata ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'decidim-initiatives/app/forms/decidim/initiatives/vote_form.rb', line 110 def { name_and_surname:, document_number:, date_of_birth:, postal_code: } end |
#user_authorized_scope ⇒ Object
Public: Finds the scope the user has an authorization for, this way the user can vote on that scope and its parents.
This is can be used to allow users that are authorized with a children scope to sign an initiative with a parent scope.
As an example: A city (global scope) has many districts (scopes with parent nil), and each district has different neighbourhoods (with its parent as a district). If we setup the authorization handler to match a neighbourhood, the same authorization can be used to participate in district, neighbourhoods or city initiatives.
Returns a Decidim::Scope.
83 84 85 86 87 88 89 90 91 |
# File 'decidim-initiatives/app/forms/decidim/initiatives/vote_form.rb', line 83 def return scope if handler_name.blank? return unless return if ..blank? @user_authorized_scope ||= .find do |scope| scope&.id == ..symbolize_keys[:scope_id] end end |