Class: Decidim::Initiative
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- Decidim::Initiative
- Includes:
- ActiveModel::Dirty, Authorable, Comments::Commentable, Followable, HasAttachmentCollections, HasAttachments, HasReference, HasResourcePermission, Decidim::Initiatives::HasArea, Decidim::Initiatives::InitiativeSlug, Loggable, Participable, Publicable, Randomable, Resourceable, ScopableParticipatorySpace, Searchable, Traceable, TranslatableResource
- Defined in:
- app/models/decidim/initiative.rb
Overview
The data store for a Initiative in the Decidim::Initiatives component.
Class Method Summary collapse
Instance Method Summary collapse
- #accepts_offline_votes? ⇒ Boolean
- #accepts_online_unvotes? ⇒ Boolean
- #accepts_online_votes? ⇒ Boolean
-
#allow_resource_permissions? ⇒ Boolean
Public: Overrides the `allow_resource_permissions?` Resourceable concern method.
-
#answered? ⇒ Boolean
Public: Checks if the organization has given an answer for the initiative.
-
#author_name ⇒ Object
Public: Returns the author name.
- #author_users ⇒ Object
-
#banner_image ⇒ Object
Public: Overrides participatory space's banner image with the banner image defined for the initiative type.
-
#closed? ⇒ Boolean
Public: Checks if an initiative is closed.
-
#comments_have_alignment? ⇒ Boolean
Public: Overrides the `comments_have_alignment?` Commentable concern method.
-
#comments_have_votes? ⇒ Boolean
Public: Overrides the `comments_have_votes?` Commentable concern method.
- #component ⇒ Object
-
#created_by_individual? ⇒ Boolean
Public: Check if an initiative has been created by an individual person.
- #enough_committee_members? ⇒ Boolean
-
#has_authorship?(user) ⇒ Boolean
Public: Checks if user is the author or is part of the promotal committee of the initiative.
-
#has_signature_interval_defined? ⇒ Boolean
Public: Returns wether the signature interval is already defined or not.
-
#hashtag ⇒ Object
Public: Returns the hashtag for the initiative.
- #minimum_committee_members ⇒ Object
- #offline_votes_count ⇒ Object
- #offline_votes_count_for(scope) ⇒ Object
-
#online_votes_count ⇒ Object
Public: Calculates all the votes across all the scopes.
- #online_votes_count_for(scope) ⇒ Object
-
#open? ⇒ Boolean
Public: check if an initiative is open.
-
#percentage ⇒ Object
Public: Returns the percentage of required supports reached.
-
#publish! ⇒ Object
Public: Publishes this initiative.
-
#scopes_enabled ⇒ Object
Public: Overrides scopes enabled attribute value.
-
#scopes_enabled? ⇒ Boolean
Public: Overrides scopes enabled flag available in other models like participatory space or assemblies.
- #set_offline_votes_total ⇒ Object
-
#slug ⇒ Object
Public: Overrides slug attribute from participatory processes.
-
#supports_count ⇒ Object
Public: Calculates the number of total current supports.
-
#supports_count_for(scope) ⇒ Object
Public: Calculates the number of current supports for a scope.
-
#supports_goal_reached? ⇒ Boolean
Public: Whether the supports required objective has been reached.
-
#supports_goal_reached_for?(scope) ⇒ Boolean
Public: Whether the supports required objective has been reached for a scope.
-
#supports_required_for(scope) ⇒ Object
Public: Calculates the number of supports required to accept the initiative for a scope.
- #to_param ⇒ Object
-
#unpublish! ⇒ Object
Public: Unpublishes this initiative.
- #update_online_votes_counters ⇒ Object
- #user_allowed_to_comment?(user) ⇒ Boolean
-
#user_role_config_for(_user, _role_name) ⇒ Object
Public: Returns an empty object.
-
#validate_sms_code_on_votes? ⇒ Boolean
Public: Checks if the type the initiative belongs to enables SMS code verification step.
-
#votable_initiative_type_scopes ⇒ Object
Public: Finds all the InitiativeTypeScopes that are eligible to be voted by a user.
-
#voted_by?(user) ⇒ Boolean
Public: Check if the user has voted the question.
- #votes_enabled? ⇒ Boolean
Methods included from Decidim::Initiatives::InitiativeSlug
Class Method Details
.log_presenter_class_for(_log) ⇒ Object
123 124 125 |
# File 'app/models/decidim/initiative.rb', line 123 def self.log_presenter_class_for(_log) Decidim::Initiatives::AdminLog::InitiativePresenter end |
Instance Method Details
#accepts_offline_votes? ⇒ Boolean
376 377 378 |
# File 'app/models/decidim/initiative.rb', line 376 def accepts_offline_votes? published? && (offline_signature_type? || any_signature_type?) end |
#accepts_online_unvotes? ⇒ Boolean
384 385 386 |
# File 'app/models/decidim/initiative.rb', line 384 def accepts_online_unvotes? accepts_online_votes? && type.undo_online_signatures_enabled? end |
#accepts_online_votes? ⇒ Boolean
380 381 382 |
# File 'app/models/decidim/initiative.rb', line 380 def accepts_online_votes? votes_enabled? && (online_signature_type? || any_signature_type?) end |
#allow_resource_permissions? ⇒ Boolean
Public: Overrides the `allow_resource_permissions?` Resourceable concern method.
417 418 419 |
# File 'app/models/decidim/initiative.rb', line 417 def true end |
#answered? ⇒ Boolean
Public: Checks if the organization has given an answer for the initiative.
Returns a Boolean.
190 191 192 |
# File 'app/models/decidim/initiative.rb', line 190 def answered? answered_at.present? end |
#author_name ⇒ Object
Public: Returns the author name. If it has been created by an organization it will return the organization's name. Otherwise it will return author's name.
Returns a string
170 171 172 |
# File 'app/models/decidim/initiative.rb', line 170 def user_group&.name || .name end |
#author_users ⇒ Object
372 373 374 |
# File 'app/models/decidim/initiative.rb', line 372 def [].concat(committee_members..map(&:user)) end |
#banner_image ⇒ Object
Public: Overrides participatory space's banner image with the banner image defined for the initiative type.
Returns Decidim::BannerImageUploader
134 135 136 |
# File 'app/models/decidim/initiative.rb', line 134 def type.attached_uploader(:banner_image) end |
#closed? ⇒ Boolean
Public: Checks if an initiative is closed. An initiative is closed when at least one of the following conditions is true:
-
It has been discarded.
-
It has been rejected.
-
It has been accepted.
-
Signature collection period has finished.
Returns a Boolean
162 163 164 |
# File 'app/models/decidim/initiative.rb', line 162 def closed? discarded? || rejected? || accepted? || !votes_enabled? end |
#comments_have_alignment? ⇒ Boolean
Public: Overrides the `comments_have_alignment?` Commentable concern method.
353 354 355 |
# File 'app/models/decidim/initiative.rb', line 353 def comments_have_alignment? true end |
#comments_have_votes? ⇒ Boolean
Public: Overrides the `comments_have_votes?` Commentable concern method.
358 359 360 |
# File 'app/models/decidim/initiative.rb', line 358 def comments_have_votes? true end |
#component ⇒ Object
396 397 398 |
# File 'app/models/decidim/initiative.rb', line 396 def component nil end |
#created_by_individual? ⇒ Boolean
Public: Check if an initiative has been created by an individual person. If it's false, then it has been created by an authorized organization.
Returns a Boolean
142 143 144 |
# File 'app/models/decidim/initiative.rb', line 142 def created_by_individual? decidim_user_group_id.nil? end |
#enough_committee_members? ⇒ Boolean
392 393 394 |
# File 'app/models/decidim/initiative.rb', line 392 def enough_committee_members? committee_members.approved.count >= minimum_committee_members end |
#has_authorship?(user) ⇒ Boolean
Public: Checks if user is the author or is part of the promotal committee of the initiative.
Returns a Boolean.
366 367 368 369 370 |
# File 'app/models/decidim/initiative.rb', line 366 def (user) return true if .id == user.id committee_members.approved.where(decidim_users_id: user.id).any? end |
#has_signature_interval_defined? ⇒ Boolean
Public: Returns wether the signature interval is already defined or not.
232 233 234 |
# File 'app/models/decidim/initiative.rb', line 232 def has_signature_interval_defined? signature_end_date.present? && signature_start_date.present? end |
#hashtag ⇒ Object
Public: Returns the hashtag for the initiative.
237 238 239 |
# File 'app/models/decidim/initiative.rb', line 237 def hashtag attributes["hashtag"].to_s.delete("#") end |
#minimum_committee_members ⇒ Object
388 389 390 |
# File 'app/models/decidim/initiative.rb', line 388 def minimum_committee_members type.minimum_committee_members || Decidim::Initiatives.minimum_committee_members end |
#offline_votes_count ⇒ Object
288 289 290 291 292 |
# File 'app/models/decidim/initiative.rb', line 288 def offline_votes_count return 0 if online_signature_type? offline_votes["total"].to_i end |
#offline_votes_count_for(scope) ⇒ Object
302 303 304 305 306 307 308 |
# File 'app/models/decidim/initiative.rb', line 302 def offline_votes_count_for(scope) return 0 if online_signature_type? scope_key = (scope&.id || "global").to_s (offline_votes || {}).fetch(scope_key, 0).to_i end |
#online_votes_count ⇒ Object
Public: Calculates all the votes across all the scopes.
Returns an Integer.
282 283 284 285 286 |
# File 'app/models/decidim/initiative.rb', line 282 def online_votes_count return 0 if offline_signature_type? online_votes["total"].to_i end |
#online_votes_count_for(scope) ⇒ Object
294 295 296 297 298 299 300 |
# File 'app/models/decidim/initiative.rb', line 294 def online_votes_count_for(scope) return 0 if offline_signature_type? scope_key = (scope&.id || "global").to_s (online_votes || {}).fetch(scope_key, 0).to_i end |
#open? ⇒ Boolean
Public: check if an initiative is open
Returns a Boolean
149 150 151 |
# File 'app/models/decidim/initiative.rb', line 149 def open? !closed? end |
#percentage ⇒ Object
Public: Returns the percentage of required supports reached
263 264 265 266 267 |
# File 'app/models/decidim/initiative.rb', line 263 def percentage return 100 if supports_goal_reached? supports_count * 100 / supports_required end |
#publish! ⇒ Object
Public: Publishes this initiative
Returns true if the record was properly saved, false otherwise.
211 212 213 214 215 216 217 218 219 220 |
# File 'app/models/decidim/initiative.rb', line 211 def publish! return false if published? update( published_at: Time.current, state: "published", signature_start_date: Date.current, signature_end_date: signature_end_date || Date.current + Decidim::Initiatives.default_signature_time_period_length ) end |
#scopes_enabled ⇒ Object
Public: Overrides scopes enabled attribute value. For initiatives it won't be directly managed by the user and it will be enabled by default.
204 205 206 |
# File 'app/models/decidim/initiative.rb', line 204 def scopes_enabled true end |
#scopes_enabled? ⇒ Boolean
Public: Overrides scopes enabled flag available in other models like participatory space or assemblies. For initiatives it won't be directly managed by the user and it will be enabled by default.
197 198 199 |
# File 'app/models/decidim/initiative.rb', line 197 def scopes_enabled? true end |
#set_offline_votes_total ⇒ Object
322 323 324 325 326 |
# File 'app/models/decidim/initiative.rb', line 322 def set_offline_votes_total return if offline_votes.blank? || scope.nil? offline_votes["total"] = offline_votes[scope.id.to_s] end |
#slug ⇒ Object
Public: Overrides slug attribute from participatory processes.
343 344 345 |
# File 'app/models/decidim/initiative.rb', line 343 def slug slug_from_id(id) end |
#supports_count ⇒ Object
Public: Calculates the number of total current supports.
Returns an Integer.
244 245 246 |
# File 'app/models/decidim/initiative.rb', line 244 def supports_count online_votes_count + offline_votes_count end |
#supports_count_for(scope) ⇒ Object
Public: Calculates the number of current supports for a scope.
Returns an Integer.
251 252 253 |
# File 'app/models/decidim/initiative.rb', line 251 def supports_count_for(scope) online_votes_count_for(scope) + offline_votes_count_for(scope) end |
#supports_goal_reached? ⇒ Boolean
Public: Whether the supports required objective has been reached
270 271 272 |
# File 'app/models/decidim/initiative.rb', line 270 def supports_goal_reached? initiative_type_scopes.map(&:scope).all? { |scope| supports_goal_reached_for?(scope) } end |
#supports_goal_reached_for?(scope) ⇒ Boolean
Public: Whether the supports required objective has been reached for a scope
275 276 277 |
# File 'app/models/decidim/initiative.rb', line 275 def supports_goal_reached_for?(scope) supports_count_for(scope) >= supports_required_for(scope) end |
#supports_required_for(scope) ⇒ Object
Public: Calculates the number of supports required to accept the initiative for a scope.
Returns an Integer.
258 259 260 |
# File 'app/models/decidim/initiative.rb', line 258 def supports_required_for(scope) initiative_type_scopes.find_by(decidim_scopes_id: scope&.id).supports_required end |
#to_param ⇒ Object
347 348 349 |
# File 'app/models/decidim/initiative.rb', line 347 def to_param slug end |
#unpublish! ⇒ Object
Public: Unpublishes this initiative
Returns true if the record was properly saved, false otherwise.
225 226 227 228 229 |
# File 'app/models/decidim/initiative.rb', line 225 def unpublish! return false unless published? update(published_at: nil, state: "discarded") end |
#update_online_votes_counters ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 |
# File 'app/models/decidim/initiative.rb', line 310 def update_online_votes_counters online_votes = votes.group(:scope).count.each_with_object({}) do |(scope, count), counters| counters[scope&.id || "global"] = count counters["total"] ||= 0 counters["total"] += count end # rubocop:disable Rails/SkipsModelValidations update_column("online_votes", online_votes) # rubocop:enable Rails/SkipsModelValidations end |
#user_allowed_to_comment?(user) ⇒ Boolean
421 422 423 |
# File 'app/models/decidim/initiative.rb', line 421 def user_allowed_to_comment?(user) ActionAuthorizer.new(user, "comment", self, nil)..ok? end |
#user_role_config_for(_user, _role_name) ⇒ Object
Public: Returns an empty object. This method should be implemented by `ParticipatorySpaceResourceable`, but for some reason this model does not implement this interface.
412 413 414 |
# File 'app/models/decidim/initiative.rb', line 412 def user_role_config_for(_user, _role_name) Decidim::ParticipatorySpaceRoleConfig::Base.new(:empty_role_name) end |
#validate_sms_code_on_votes? ⇒ Boolean
Public: Checks if the type the initiative belongs to enables SMS code verification step. Tis configuration is ignored if the organization doesn't have the sms authorization available
Returns a Boolean
405 406 407 |
# File 'app/models/decidim/initiative.rb', line 405 def validate_sms_code_on_votes? organization..include?("sms") && type.validate_sms_code_on_votes? end |
#votable_initiative_type_scopes ⇒ Object
Public: Finds all the InitiativeTypeScopes that are eligible to be voted by a user. Usually this is only the `scoped_type` but voting on children of the scoped type is enabled we have to filter all the available scopes in the initiative type to select the ones that are a descendant of the initiative type.
Returns an Array of Decidim::InitiativesScopeType.
334 335 336 337 338 339 340 |
# File 'app/models/decidim/initiative.rb', line 334 def votable_initiative_type_scopes return Array(scoped_type) unless type.child_scope_threshold_enabled? initiative_type_scopes.select do |initiative_type_scope| initiative_type_scope.scope.present? && (scoped_type.global_scope? || scoped_type.scope.ancestor_of?(initiative_type_scope.scope)) end.prepend(scoped_type).uniq end |
#voted_by?(user) ⇒ Boolean
Public: Check if the user has voted the question.
Returns Boolean.
183 184 185 |
# File 'app/models/decidim/initiative.rb', line 183 def voted_by?(user) votes.where(author: user).any? end |
#votes_enabled? ⇒ Boolean
174 175 176 177 178 |
# File 'app/models/decidim/initiative.rb', line 174 def votes_enabled? published? && signature_start_date <= Date.current && signature_end_date >= Date.current end |