Class: Decidim::Proposals::Proposal
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Proposals::Proposal
- Includes:
- Amendable, Coauthorable, DownloadYourData, Endorsable, FilterableResource, Fingerprintable, Followable, HasAttachments, HasCategory, HasComponent, HasReference, Loggable, NewsletterParticipant, CommentableProposal, ParticipatoryTextSection, Valuatable, Randomable, Reportable, Resourceable, ScopableResource, Searchable, SoftDeletable, Taxonomizable, Traceable, TranslatableAttributes, TranslatableResource
- Defined in:
- decidim-proposals/app/models/decidim/proposals/proposal.rb
Overview
The data store for a Proposal in the Decidim::Proposals component.
Constant Summary
Constants included from ParticipatoryTextSection
Decidim::Proposals::ParticipatoryTextSection::LEVELS
Class Method Summary collapse
- .download_your_data_images(user) ⇒ Object
- .export_serializer ⇒ Object
- .log_presenter_class_for(_log) ⇒ Object
- .newsletter_participant_ids(component) ⇒ Object
- .ransack(params = {}, options = {}) ⇒ Object
- .ransackable_associations(_auth_object = nil) ⇒ Object
- .ransackable_attributes(_auth_object = nil) ⇒ Object
- .ransackable_scopes(_auth_object = nil) ⇒ Object
- .retrieve_proposals_for(component) ⇒ Object
- .sort_by_translated_title_asc ⇒ Object
- .sort_by_translated_title_desc ⇒ Object
-
.user_collection(author) ⇒ Object
Returns a collection scoped by an author.
-
.valuator_role_ids_has(value) ⇒ Object
method to filter by assigned valuator role ID.
- .with_valuation_assigned_to(user, space) ⇒ Object
Instance Method Summary collapse
-
#accepted? ⇒ Boolean
Public: Checks if the organization has accepted a proposal.
- #actions_for_comment(comment, current_user) ⇒ Object
-
#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 proposal.
- #assign_state(token) ⇒ Object
-
#can_accumulate_votes_beyond_threshold ⇒ Object
Public: Can accumulate more votes than maximum for this proposal.
- #coauthor_invitations_for(user) ⇒ Object
-
#draft? ⇒ Boolean
Public: Whether the proposal is a draft or not.
-
#editable_by?(user) ⇒ Boolean
Checks whether the user can edit the given proposal.
-
#evaluating? ⇒ Boolean
Public: Checks if the organization has marked the proposal as evaluating it.
-
#internal_state ⇒ Object
Public: Returns the internal state of the proposal.
-
#maximum_votes ⇒ Object
Public: The maximum amount of votes allowed for this proposal.
-
#maximum_votes_reached? ⇒ Boolean
Public: The maximum amount of votes allowed for this proposal.
-
#official? ⇒ Boolean
Public: Whether the proposal is official or not.
-
#official_meeting? ⇒ Boolean
Public: Whether the proposal is created in a meeting or not.
-
#presenter ⇒ Object
Returns the presenter for this author, to be used in the views.
- #process_amendment_state_change! ⇒ Object
-
#published? ⇒ Boolean
Public: Checks if the proposal has been published or not.
-
#published_state? ⇒ Boolean
Public: Checks if the organization has published the state for the proposal.
-
#rejected? ⇒ Boolean
Public: Checks if the organization has rejected a proposal.
-
#reported_attributes ⇒ Object
Public: Overrides the ‘reported_attributes` Reportable concern method.
-
#reported_content_url ⇒ Object
Public: Overrides the ‘reported_content_url` Reportable concern method.
-
#reported_searchable_content_extras ⇒ Object
Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method.
-
#state ⇒ Object
Public: Returns the published state of the proposal.
-
#update_votes_count ⇒ Object
Public: Updates the vote count of this proposal.
- #user_has_actions?(user) ⇒ Boolean
-
#voted_by?(user) ⇒ Boolean
Public: Check if the user has voted the proposal.
- #withdraw! ⇒ Object
-
#withdrawable_by?(user) ⇒ Boolean
Checks whether the user can withdraw the given proposal.
-
#withdrawn? ⇒ Boolean
Public: Checks if the author has withdrawn the proposal.
-
#within_edit_time_limit? ⇒ Boolean
Checks whether the proposal is inside the time window to be editable or not once published.
Methods included from TranslatableAttributes
Methods included from Amendable
#add_author, #amendable?, #amendable_fields, #amendable_form, #amendment, #emendation?, #linked_promoted_resource, #notifiable_identities, #visible_amendments_for, #visible_emendations_for
Methods included from Fingerprintable
Methods included from Searchable
searchable_resources, searchable_resources_by_type, searchable_resources_of_type_comment, searchable_resources_of_type_component, searchable_resources_of_type_participant, searchable_resources_of_type_participatory_space
Methods included from Followable
Methods included from HasAttachments
Class Method Details
.download_your_data_images(user) ⇒ Object
471 472 473 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 471 def self.download_your_data_images(user) user_collection(user).map { |p| p..collect(&:file) } end |
.export_serializer ⇒ Object
467 468 469 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 467 def self.export_serializer Decidim::Proposals::ProposalSerializer end |
.log_presenter_class_for(_log) ⇒ Object
180 181 182 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 180 def self.log_presenter_class_for(_log) Decidim::Proposals::AdminLog::ProposalPresenter end |
.newsletter_participant_ids(component) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 203 def self.(component) proposals = retrieve_proposals_for(component).uniq = proposals.map { |p| p.notifiable_identities.pluck(:id) }.flatten.compact.uniq participants_has_voted_ids = Decidim::Proposals::ProposalVote.joins(:proposal).where(proposal: proposals).joins(:author).map(&:decidim_author_id).flatten.compact.uniq endorsements_participants_ids = Decidim::Endorsement.where(resource: proposals) .where(decidim_author_type: "Decidim::UserBaseEntity") .pluck(:decidim_author_id).to_a.compact.uniq commentators_ids = Decidim::Comments::Comment.user_commentators_ids_in(proposals) (endorsements_participants_ids + participants_has_voted_ids + + commentators_ids).flatten.compact.uniq end |
.ransack(params = {}, options = {}) ⇒ Object
390 391 392 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 390 def self.ransack(params = {}, = {}) ProposalSearch.new(self, params, ) end |
.ransackable_associations(_auth_object = nil) ⇒ Object
419 420 421 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 419 def self.ransackable_associations(_auth_object = nil) %w(taxonomies proposal_state) end |
.ransackable_attributes(_auth_object = nil) ⇒ Object
415 416 417 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 415 def self.ransackable_attributes(_auth_object = nil) %w(id_string search_text title body is_emendation comments_count proposal_votes_count published_at proposal_notes_count) end |
.ransackable_scopes(_auth_object = nil) ⇒ Object
407 408 409 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 407 def self.ransackable_scopes(_auth_object = nil) [:with_any_origin, :with_any_state, :state_eq, :voted_by, :coauthored_by, :related_to, :with_any_taxonomies, :valuator_role_ids_has] end |
.retrieve_proposals_for(component) ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 194 def self.retrieve_proposals_for(component) Decidim::Proposals::Proposal.where(component:).joins(:coauthorships) .includes(:votes, :endorsements) .where(decidim_coauthorships: { decidim_author_type: "Decidim::UserBaseEntity" }) .not_hidden .published .not_withdrawn end |
.sort_by_translated_title_asc ⇒ Object
436 437 438 439 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 436 def self.sort_by_translated_title_asc field = Arel::Nodes::InfixOperation.new("->>", arel_table[:title], Arel::Nodes.build_quoted(I18n.locale)) order(Arel::Nodes::InfixOperation.new("", field, Arel.sql("ASC"))) end |
.sort_by_translated_title_desc ⇒ Object
441 442 443 444 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 441 def self.sort_by_translated_title_desc field = Arel::Nodes::InfixOperation.new("->>", arel_table[:title], Arel::Nodes.build_quoted(I18n.locale)) order(Arel::Nodes::InfixOperation.new("", field, Arel.sql("DESC"))) end |
.user_collection(author) ⇒ Object
Returns a collection scoped by an author. Overrides this method in DownloadYourData to support Coauthorable.
186 187 188 189 190 191 192 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 186 def self.user_collection() return unless .is_a?(Decidim::User) joins(:coauthorships) .where(decidim_coauthorships: { coauthorable_type: name }) .where("decidim_coauthorships.decidim_author_id = ? AND decidim_coauthorships.decidim_author_type = ? ", .id, .class.base_class.name) end |
.valuator_role_ids_has(value) ⇒ Object
method to filter by assigned valuator role ID
395 396 397 398 399 400 401 402 403 404 405 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 395 def self.valuator_role_ids_has(value) query = <<-SQL.squish :value = any( (SELECT decidim_proposals_valuation_assignments.valuator_role_id FROM decidim_proposals_valuation_assignments WHERE decidim_proposals_valuation_assignments.decidim_proposal_id = decidim_proposals_proposals.id ) ) SQL where(query, value:) end |
.with_valuation_assigned_to(user, space) ⇒ Object
161 162 163 164 165 166 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 161 def self.with_valuation_assigned_to(user, space) valuator_roles = space.user_roles(:valuator).where(user:) includes(:valuation_assignments) .where(decidim_proposals_valuation_assignments: { valuator_role_id: valuator_roles }) end |
Instance Method Details
#accepted? ⇒ Boolean
Public: Checks if the organization has accepted a proposal.
Returns Boolean.
288 289 290 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 288 def accepted? state == "accepted" end |
#actions_for_comment(comment, current_user) ⇒ Object
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 518 def actions_for_comment(comment, current_user) return if comment.commentable != self return unless .include?(current_user) return unless user_has_actions?(comment.) if (comment.).any? [ { label: I18n.t("decidim.proposals.actions.cancel_coauthor_invitation"), url: EngineRouter.main_proxy(component).(proposal_id: id, id: comment..id), icon: "user-forbid-line", method: :delete, data: { confirm: I18n.t("decidim.proposals.actions.cancel_coauthor_invitation_confirm") } } ] else [ { label: I18n.t("decidim.proposals.actions.mark_as_coauthor"), url: EngineRouter.main_proxy(component).(proposal_id: id, id: comment..id), icon: "user-add-line", method: :post, data: { confirm: I18n.t("decidim.proposals.actions.mark_as_coauthor_confirm") } } ] end end |
#allow_resource_permissions? ⇒ Boolean
Public: Overrides the ‘allow_resource_permissions?` Resourceable concern method.
476 477 478 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 476 def component.settings. end |
#answered? ⇒ Boolean
Public: Checks if the organization has given an answer for the proposal.
Returns Boolean.
274 275 276 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 274 def answered? answered_at.present? end |
#assign_state(token) ⇒ Object
34 35 36 37 38 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 34 def assign_state(token) proposal_state = Decidim::Proposals::ProposalState.where(component:, token:).first self.proposal_state = proposal_state end |
#can_accumulate_votes_beyond_threshold ⇒ Object
Public: Can accumulate more votes than maximum for this proposal.
Returns true if can accumulate, false otherwise
360 361 362 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 360 def can_accumulate_votes_beyond_threshold component.settings.can_accumulate_votes_beyond_threshold end |
#coauthor_invitations_for(user) ⇒ Object
546 547 548 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 546 def (user) Decidim::Notification.where(event_class: "Decidim::Proposals::CoauthorInvitedEvent", resource: self, user:) end |
#draft? ⇒ Boolean
Public: Whether the proposal is a draft or not.
386 387 388 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 386 def draft? published_at.nil? end |
#editable_by?(user) ⇒ Boolean
Checks whether the user can edit the given proposal.
user - the user to check for authorship
367 368 369 370 371 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 367 def editable_by?(user) return true if draft? && created_by?(user) !published_state? && within_edit_time_limit? && !copied_from_other_component? && created_by?(user) end |
#evaluating? ⇒ Boolean
Public: Checks if the organization has marked the proposal as evaluating it.
Returns Boolean.
302 303 304 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 302 def evaluating? state == "evaluating" end |
#internal_state ⇒ Object
Public: Returns the internal state of the proposal.
Returns Boolean.
258 259 260 261 262 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 258 def internal_state return amendment.state if emendation? proposal_state&.token || "not_answered" end |
#maximum_votes ⇒ Object
Public: The maximum amount of votes allowed for this proposal.
Returns an Integer with the maximum amount of votes, nil otherwise.
341 342 343 344 345 346 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 341 def maximum_votes maximum_votes = component.settings.threshold_per_proposal return nil if maximum_votes.zero? maximum_votes end |
#maximum_votes_reached? ⇒ Boolean
Public: The maximum amount of votes allowed for this proposal. 0 means infinite.
Returns true if reached, false otherwise.
351 352 353 354 355 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 351 def maximum_votes_reached? return false unless maximum_votes votes.count >= maximum_votes end |
#official? ⇒ Boolean
Public: Whether the proposal is official or not.
329 330 331 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 329 def official? .first.is_a?(Decidim::Organization) end |
#official_meeting? ⇒ Boolean
Public: Whether the proposal is created in a meeting or not.
334 335 336 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 334 def official_meeting? .first.instance_of?(Decidim::Meetings::Meeting) end |
#presenter ⇒ Object
Returns the presenter for this author, to be used in the views. Required by ResourceRenderer.
313 314 315 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 313 def presenter Decidim::Proposals::ProposalPresenter.new(self) end |
#process_amendment_state_change! ⇒ Object
499 500 501 502 503 504 505 506 507 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 499 def process_amendment_state_change! return withdraw! if amendment.withdrawn? return unless %w(accepted rejected evaluating).member?(amendment.state) PaperTrail.request(enabled: false) do assign_state(amendment.state) update!(state_published_at: Time.current) end end |
#published? ⇒ Boolean
Public: Checks if the proposal has been published or not.
Returns Boolean.
238 239 240 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 238 def published? published_at.present? end |
#published_state? ⇒ Boolean
Public: Checks if the organization has published the state for the proposal.
Returns Boolean.
267 268 269 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 267 def published_state? emendation? || state_published_at.present? end |
#rejected? ⇒ Boolean
Public: Checks if the organization has rejected a proposal.
Returns Boolean.
295 296 297 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 295 def rejected? state == "rejected" end |
#reported_attributes ⇒ Object
Public: Overrides the ‘reported_attributes` Reportable concern method.
318 319 320 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 318 def reported_attributes [:title, :body] end |
#reported_content_url ⇒ Object
Public: Overrides the ‘reported_content_url` Reportable concern method.
307 308 309 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 307 def reported_content_url ResourceLocatorPresenter.new(self).url end |
#reported_searchable_content_extras ⇒ Object
Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method. Returns authors name or title in case it is a meeting
324 325 326 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 324 def reported_searchable_content_extras [.map { |p| p.respond_to?(:name) ? p.name : p.title }.join("\n")] end |
#state ⇒ Object
Public: Returns the published state of the proposal.
Returns Boolean.
245 246 247 248 249 250 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 245 def state return amendment.state if emendation? return nil unless published_state? || withdrawn? proposal_state&.token || "not_answered" end |
#update_votes_count ⇒ Object
Public: Updates the vote count of this proposal.
Returns nothing. rubocop:disable Rails/SkipsModelValidations
223 224 225 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 223 def update_votes_count update_columns(proposal_votes_count: votes.count) end |
#user_has_actions?(user) ⇒ Boolean
509 510 511 512 513 514 515 516 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 509 def user_has_actions?(user) return false if .include?(user) return false if user&.blocked? return false if user&.deleted? return false unless user&.confirmed? true end |
#voted_by?(user) ⇒ Boolean
Public: Check if the user has voted the proposal.
Returns Boolean.
231 232 233 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 231 def voted_by?(user) ProposalVote.where(proposal: self, author: user).any? end |
#withdraw! ⇒ Object
380 381 382 383 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 380 def withdraw! self.withdrawn_at = Time.zone.now save end |
#withdrawable_by?(user) ⇒ Boolean
Checks whether the user can withdraw the given proposal.
user - the user to check for withdrawability.
376 377 378 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 376 def withdrawable_by?(user) user && !withdrawn? && (user) && !copied_from_other_component? end |
#withdrawn? ⇒ Boolean
Public: Checks if the author has withdrawn the proposal.
Returns Boolean.
281 282 283 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 281 def withdrawn? withdrawn_at.present? end |
#within_edit_time_limit? ⇒ Boolean
Checks whether the proposal is inside the time window to be editable or not once published.
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'decidim-proposals/app/models/decidim/proposals/proposal.rb', line 481 def within_edit_time_limit? return true if draft? return true if component.settings.proposal_edit_time == "infinite" time_value, time_unit = component.settings.edit_time limit_time = case time_unit when "minutes" updated_at + time_value.minutes when "hours" updated_at + time_value.hours else updated_at + time_value.days end Time.current < limit_time end |