Class: Decidim::Elections::CensusManifest
- Inherits:
-
Object
- Object
- Decidim::Elections::CensusManifest
- Includes:
- ActiveModel::Model, AttributeObject::Model
- Defined in:
- lib/decidim/elections/census_manifest.rb
Instance Method Summary collapse
- #auth_form? ⇒ Boolean
-
#census_ready_validator(&block) ⇒ Object
a callback that will be called by the method “census_ready?”.
-
#count(election) ⇒ Object
returns the number of users in the census.
- #form_instance(data = {}, context = {}) ⇒ Object
- #label ⇒ Object
-
#ready?(election) ⇒ Boolean
validates the census using the Proc defined by census_ready_validator Receives the election object.
-
#user_iterator(&block) ⇒ Object
a callback that will be called by the method “users”.
-
#user_query(&block) ⇒ Object
Instead of individually defining “user_validator”, “census_ready_validator” and “user_iterator”, if the user list can be obtained by a SQL query, a user_query can be specified and the rest of the methods will be defined automatically.
-
#user_validator(&block) ⇒ Object
a callback that will be called by the method “valid_user?”.
- #users(election, offset = 0, limit = 5) ⇒ Object
-
#valid_user?(election, data, context = {}) ⇒ Boolean
validates the user using the Proc defined by user_validator Receives the same parameters as voter_uid.
-
#voter_uid(election, data, context = {}) ⇒ Object
Generates a unique voter identifier based on the provided data Receives the election object and user data (will depend on the census type)) returns nil if the identifier cannot be generated.
-
#voter_uid_generator(&block) ⇒ Object
Registers a block that will be used to generate a unique voter identifier The block will receive the data of the voter and should return a unique identifier If the block is not set, a default identifier will be generated based on the data.
Instance Method Details
#auth_form? ⇒ Boolean
38 39 40 |
# File 'lib/decidim/elections/census_manifest.rb', line 38 def auth_form? voter_form.present? end |
#census_ready_validator(&block) ⇒ Object
a callback that will be called by the method “census_ready?”
66 67 68 |
# File 'lib/decidim/elections/census_manifest.rb', line 66 def census_ready_validator(&block) @on_census_validation = block end |
#count(election) ⇒ Object
returns the number of users in the census
86 87 88 89 90 91 92 93 |
# File 'lib/decidim/elections/census_manifest.rb', line 86 def count(election) if @on_census_count @on_census_count.call(election) elsif @user_query # If a user query is defined, we assume that the count is the number of users in the query @user_query.call(election).count end end |
#form_instance(data = {}, context = {}) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/decidim/elections/census_manifest.rb', line 100 def form_instance(data = {}, context = {}) return if voter_form.blank? form_class = voter_form.constantize form_class.from_params(data).with_context(context) end |
#label ⇒ Object
42 43 44 |
# File 'lib/decidim/elections/census_manifest.rb', line 42 def label I18n.t("decidim.elections.censuses.#{name}.label", default: name.to_s.humanize) end |
#ready?(election) ⇒ Boolean
validates the census using the Proc defined by census_ready_validator Receives the election object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/decidim/elections/census_manifest.rb', line 72 def ready?(election) return false if election.census_manifest_changed? # needs to be persisted to be valid if @on_census_validation @on_census_validation.call(election) elsif @user_query # If a user query is defined, we assume that the census is ready if there are users in the query @user_query.call(election).exists? else false end end |
#user_iterator(&block) ⇒ Object
a callback that will be called by the method “users”
96 97 98 |
# File 'lib/decidim/elections/census_manifest.rb', line 96 def user_iterator(&block) @on_user_iteration = block end |
#user_query(&block) ⇒ Object
Instead of individually defining “user_validator”, “census_ready_validator” and “user_iterator”, if the user list can be obtained by a SQL query, a user_query can be specified and the rest of the methods will be defined automatically. If used, the called block will receive the election object and should return an ActiveRecord::Relation
49 50 51 |
# File 'lib/decidim/elections/census_manifest.rb', line 49 def user_query(&block) @user_query = block end |
#user_validator(&block) ⇒ Object
a callback that will be called by the method “valid_user?”
61 62 63 |
# File 'lib/decidim/elections/census_manifest.rb', line 61 def user_validator(&block) @on_user_validation = block end |
#users(election, offset = 0, limit = 5) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/decidim/elections/census_manifest.rb', line 107 def users(election, offset = 0, limit = 5) if @on_user_iteration @on_user_iteration.call(election, offset, limit) elsif @user_query # If a user query is defined, we assume that the users are the users in the query @user_query.call(election).offset(offset).limit(limit) else Decidim::User.none end end |
#valid_user?(election, data, context = {}) ⇒ Boolean
validates the user using the Proc defined by user_validator Receives the same parameters as voter_uid
135 136 137 138 139 140 141 |
# File 'lib/decidim/elections/census_manifest.rb', line 135 def valid_user?(election, data, context = {}) if @on_user_validation @on_user_validation.call(election, data) else voter_uid(election, data, context).present? end end |
#voter_uid(election, data, context = {}) ⇒ Object
Generates a unique voter identifier based on the provided data Receives the election object and user data (will depend on the census type)) returns nil if the identifier cannot be generated
121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/decidim/elections/census_manifest.rb', line 121 def voter_uid(election, data, context = {}) return @on_voter_uid_generation.call(data) if @on_voter_uid_generation if (form = form_instance(data, context.merge(election:))) return unless form.valid? return form.voter_uid if form.respond_to?(:voter_uid) elsif @user_query && context.is_a?(Decidim::User) @user_query.call(election).find_by(id: context.id)&.to_global_id end end |
#voter_uid_generator(&block) ⇒ Object
Registers a block that will be used to generate a unique voter identifier The block will receive the data of the voter and should return a unique identifier If the block is not set, a default identifier will be generated based on the data
56 57 58 |
# File 'lib/decidim/elections/census_manifest.rb', line 56 def voter_uid_generator(&block) @on_voter_uid_generation = block end |