Class: Decidim::Proposals::Proposal
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Proposals::Proposal
- Includes:
- Amendable, Coauthorable, DataPortability, Endorsable, Fingerprintable, Followable, HasAttachments, HasCategory, HasComponent, HasReference, Loggable, NewsletterParticipant, CommentableProposal, ParticipatoryTextSection, Valuatable, Randomable, Reportable, Resourceable, ScopableResource, Searchable, Traceable, TranslatableAttributes, TranslatableResource
- Defined in:
- app/models/decidim/proposals/proposal.rb
Overview
The data store for a Proposal in the Decidim::Proposals component.
Constant Summary collapse
- POSSIBLE_STATES =
%w(not_answered evaluating accepted rejected withdrawn).freeze
Constants included from ParticipatoryTextSection
Decidim::Proposals::ParticipatoryTextSection::LEVELS
Class Method Summary collapse
- .data_portability_images(user) ⇒ Object
- .export_serializer ⇒ Object
- .log_presenter_class_for(_log) ⇒ Object
- .newsletter_participant_ids(component) ⇒ Object
- .ransackable_scopes(_auth = nil) ⇒ Object
- .retrieve_proposals_for(component) ⇒ Object
- .sort_by_translated_title_asc ⇒ Object
- .sort_by_translated_title_desc ⇒ Object
-
.sort_by_valuation_assignments_count_nulls_last_query ⇒ Object
Defines the base query so that ransack can actually sort by this value.
-
.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.
-
#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.
-
#can_accumulate_supports_beyond_threshold ⇒ Object
Public: Can accumulate more votres than maximum for this proposal.
-
#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.
-
#voted_by?(user) ⇒ Boolean
Public: Check if the user has voted the proposal.
-
#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.
Class Method Details
.data_portability_images(user) ⇒ Object
382 383 384 |
# File 'app/models/decidim/proposals/proposal.rb', line 382 def self.data_portability_images(user) user_collection(user).map { |p| p..collect(&:file) } end |
.export_serializer ⇒ Object
378 379 380 |
# File 'app/models/decidim/proposals/proposal.rb', line 378 def self.export_serializer Decidim::Proposals::ProposalSerializer end |
.log_presenter_class_for(_log) ⇒ Object
101 102 103 |
# File 'app/models/decidim/proposals/proposal.rb', line 101 def self.log_presenter_class_for(_log) Decidim::Proposals::AdminLog::ProposalPresenter end |
.newsletter_participant_ids(component) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/models/decidim/proposals/proposal.rb', line 124 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 |
.ransackable_scopes(_auth = nil) ⇒ Object
330 331 332 |
# File 'app/models/decidim/proposals/proposal.rb', line 330 def self.ransackable_scopes(_auth = nil) [:valuator_role_ids_has] end |
.retrieve_proposals_for(component) ⇒ Object
115 116 117 118 119 120 121 122 |
# File 'app/models/decidim/proposals/proposal.rb', line 115 def self.retrieve_proposals_for(component) Decidim::Proposals::Proposal.where(component: component).joins(:coauthorships) .includes(:votes, :endorsements) .where(decidim_coauthorships: { decidim_author_type: "Decidim::UserBaseEntity" }) .not_hidden .published .except_withdrawn end |
.sort_by_translated_title_asc ⇒ Object
347 348 349 350 |
# File 'app/models/decidim/proposals/proposal.rb', line 347 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
352 353 354 355 |
# File 'app/models/decidim/proposals/proposal.rb', line 352 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 |
.sort_by_valuation_assignments_count_nulls_last_query ⇒ Object
Defines the base query so that ransack can actually sort by this value
306 307 308 309 310 311 312 313 314 315 |
# File 'app/models/decidim/proposals/proposal.rb', line 306 def self.sort_by_valuation_assignments_count_nulls_last_query <<-SQL.squish ( SELECT COUNT(decidim_proposals_valuation_assignments.id) FROM decidim_proposals_valuation_assignments WHERE decidim_proposals_valuation_assignments.decidim_proposal_id = decidim_proposals_proposals.id GROUP BY decidim_proposals_valuation_assignments.decidim_proposal_id ) SQL end |
.user_collection(author) ⇒ Object
Returns a collection scoped by an author. Overrides this method in DataPortability to support Coauthorable.
107 108 109 110 111 112 113 |
# File 'app/models/decidim/proposals/proposal.rb', line 107 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
318 319 320 321 322 323 324 325 326 327 328 |
# File 'app/models/decidim/proposals/proposal.rb', line 318 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: value) end |
.with_valuation_assigned_to(user, space) ⇒ Object
82 83 84 85 86 87 |
# File 'app/models/decidim/proposals/proposal.rb', line 82 def self.with_valuation_assigned_to(user, space) valuator_roles = space.user_roles(:valuator).where(user: 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.
209 210 211 |
# File 'app/models/decidim/proposals/proposal.rb', line 209 def accepted? state == "accepted" end |
#allow_resource_permissions? ⇒ Boolean
Public: Overrides the ‘allow_resource_permissions?` Resourceable concern method.
387 388 389 |
# File 'app/models/decidim/proposals/proposal.rb', line 387 def component.settings. end |
#answered? ⇒ Boolean
Public: Checks if the organization has given an answer for the proposal.
Returns Boolean.
195 196 197 |
# File 'app/models/decidim/proposals/proposal.rb', line 195 def answered? answered_at.present? end |
#can_accumulate_supports_beyond_threshold ⇒ Object
Public: Can accumulate more votres than maximum for this proposal.
Returns true if can accumulate, false otherwise
280 281 282 |
# File 'app/models/decidim/proposals/proposal.rb', line 280 def can_accumulate_supports_beyond_threshold component.settings.can_accumulate_supports_beyond_threshold end |
#draft? ⇒ Boolean
Public: Whether the proposal is a draft or not.
301 302 303 |
# File 'app/models/decidim/proposals/proposal.rb', line 301 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
287 288 289 290 291 |
# File 'app/models/decidim/proposals/proposal.rb', line 287 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.
223 224 225 |
# File 'app/models/decidim/proposals/proposal.rb', line 223 def evaluating? state == "evaluating" end |
#internal_state ⇒ Object
Public: Returns the internal state of the proposal.
Returns Boolean.
179 180 181 182 183 |
# File 'app/models/decidim/proposals/proposal.rb', line 179 def internal_state return amendment.state if emendation? self[:state] 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.
261 262 263 264 265 266 |
# File 'app/models/decidim/proposals/proposal.rb', line 261 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.
271 272 273 274 275 |
# File 'app/models/decidim/proposals/proposal.rb', line 271 def maximum_votes_reached? return false unless maximum_votes votes.count >= maximum_votes end |
#official? ⇒ Boolean
Public: Whether the proposal is official or not.
249 250 251 |
# File 'app/models/decidim/proposals/proposal.rb', line 249 def official? .first.is_a?(Decidim::Organization) end |
#official_meeting? ⇒ Boolean
Public: Whether the proposal is created in a meeting or not.
254 255 256 |
# File 'app/models/decidim/proposals/proposal.rb', line 254 def official_meeting? .first.class.name == "Decidim::Meetings::Meeting" end |
#presenter ⇒ Object
Returns the presenter for this author, to be used in the views. Required by ResourceRenderer.
234 235 236 |
# File 'app/models/decidim/proposals/proposal.rb', line 234 def presenter Decidim::Proposals::ProposalPresenter.new(self) end |
#process_amendment_state_change! ⇒ Object
400 401 402 403 404 405 406 407 408 409 |
# File 'app/models/decidim/proposals/proposal.rb', line 400 def process_amendment_state_change! return unless %w(accepted rejected evaluating withdrawn).member?(amendment.state) PaperTrail.request(enabled: false) do update!( state: amendment.state, state_published_at: Time.current ) end end |
#published? ⇒ Boolean
Public: Checks if the proposal has been published or not.
Returns Boolean.
159 160 161 |
# File 'app/models/decidim/proposals/proposal.rb', line 159 def published? published_at.present? end |
#published_state? ⇒ Boolean
Public: Checks if the organization has published the state for the proposal.
Returns Boolean.
188 189 190 |
# File 'app/models/decidim/proposals/proposal.rb', line 188 def published_state? emendation? || state_published_at.present? end |
#rejected? ⇒ Boolean
Public: Checks if the organization has rejected a proposal.
Returns Boolean.
216 217 218 |
# File 'app/models/decidim/proposals/proposal.rb', line 216 def rejected? state == "rejected" end |
#reported_attributes ⇒ Object
Public: Overrides the ‘reported_attributes` Reportable concern method.
239 240 241 |
# File 'app/models/decidim/proposals/proposal.rb', line 239 def reported_attributes [:title, :body] end |
#reported_content_url ⇒ Object
Public: Overrides the ‘reported_content_url` Reportable concern method.
228 229 230 |
# File 'app/models/decidim/proposals/proposal.rb', line 228 def reported_content_url ResourceLocatorPresenter.new(self).url end |
#reported_searchable_content_extras ⇒ Object
Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method.
244 245 246 |
# File 'app/models/decidim/proposals/proposal.rb', line 244 def reported_searchable_content_extras [.map(&:name).join("\n")] end |
#state ⇒ Object
Public: Returns the published state of the proposal.
Returns Boolean.
166 167 168 169 170 171 |
# File 'app/models/decidim/proposals/proposal.rb', line 166 def state return amendment.state if emendation? return nil unless published_state? || withdrawn? super end |
#update_votes_count ⇒ Object
Public: Updates the vote count of this proposal.
Returns nothing. rubocop:disable Rails/SkipsModelValidations
144 145 146 |
# File 'app/models/decidim/proposals/proposal.rb', line 144 def update_votes_count update_columns(proposal_votes_count: votes.count) end |
#voted_by?(user) ⇒ Boolean
Public: Check if the user has voted the proposal.
Returns Boolean.
152 153 154 |
# File 'app/models/decidim/proposals/proposal.rb', line 152 def voted_by?(user) ProposalVote.where(proposal: self, author: user).any? end |
#withdrawable_by?(user) ⇒ Boolean
Checks whether the user can withdraw the given proposal.
user - the user to check for withdrawability.
296 297 298 |
# File 'app/models/decidim/proposals/proposal.rb', line 296 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.
202 203 204 |
# File 'app/models/decidim/proposals/proposal.rb', line 202 def withdrawn? internal_state == "withdrawn" end |
#within_edit_time_limit? ⇒ Boolean
Checks whether the proposal is inside the time window to be editable or not once published.
392 393 394 395 396 397 398 |
# File 'app/models/decidim/proposals/proposal.rb', line 392 def within_edit_time_limit? return true if draft? return true if component.settings.proposal_edit_time == "infinite" limit = updated_at + component.settings.proposal_edit_before_minutes.minutes Time.current < limit end |