Class: Decidim::Consultations::Question
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Consultations::Question
- Includes:
- Decidim::Comments::Commentable, Followable, HasAttachmentCollections, HasAttachments, Participable, Publicable, Scopable
- Defined in:
- app/models/decidim/consultations/question.rb
Overview
The data store for Consultation questions in the Decidim::Consultations component.
Class Method Summary collapse
Instance Method Summary collapse
- #banner_image_url ⇒ Object
-
#can_be_unvoted_by?(user) ⇒ Boolean
Public: Checks whether the given user can unvote the question or note.
-
#can_be_voted_by?(user) ⇒ Boolean
Public: Checks whether the given user can vote the question or note.
-
#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.
- #hashtag ⇒ Object
-
#module_name ⇒ Object
Overrides module name from participable concern.
- #most_voted_response ⇒ Object
- #mounted_admin_engine ⇒ Object
- #mounted_engine ⇒ Object
- #scopes_enabled ⇒ Object
- #scopes_enabled? ⇒ Boolean
-
#sorted_results ⇒ Object
Sorted results for the given question.
- #to_param ⇒ Object
- #total_votes ⇒ Object
-
#voted_by?(user) ⇒ Boolean
Public: Check if the user has voted the question.
Class Method Details
.order_randomly(seed) ⇒ Object
140 141 142 143 144 145 |
# File 'app/models/decidim/consultations/question.rb', line 140 def self.order_randomly(seed) transaction do connection.execute("SELECT setseed(#{connection.quote(seed)})") select('"decidim_consultations_questions".*, RANDOM()').order(Arel.sql("RANDOM()")).load end end |
.participatory_space_manifest ⇒ Object
136 137 138 |
# File 'app/models/decidim/consultations/question.rb', line 136 def self.participatory_space_manifest Decidim.find_participatory_space_manifest(Decidim::Consultation.name.demodulize.underscore.pluralize) end |
Instance Method Details
#banner_image_url ⇒ Object
79 80 81 |
# File 'app/models/decidim/consultations/question.rb', line 79 def .present? ? .url : consultation..url end |
#can_be_unvoted_by?(user) ⇒ Boolean
Public: Checks whether the given user can unvote the question or note.
Returns a Boolean.
93 94 95 96 97 98 |
# File 'app/models/decidim/consultations/question.rb', line 93 def can_be_unvoted_by?(user) consultation.active? && consultation.published? && published? && voted_by?(user) end |
#can_be_voted_by?(user) ⇒ Boolean
Public: Checks whether the given user can vote the question or note.
Returns a Boolean.
103 104 105 106 107 108 109 |
# File 'app/models/decidim/consultations/question.rb', line 103 def can_be_voted_by?(user) organization.id == user.organization.id && consultation.active? && consultation.published? && published? && !voted_by?(user) end |
#comments_have_alignment? ⇒ Boolean
Public: Overrides the ‘comments_have_alignment?` Commentable concern method.
66 67 68 |
# File 'app/models/decidim/consultations/question.rb', line 66 def comments_have_alignment? true end |
#comments_have_votes? ⇒ Boolean
Public: Overrides the ‘comments_have_votes?` Commentable concern method.
71 72 73 |
# File 'app/models/decidim/consultations/question.rb', line 71 def comments_have_votes? true end |
#hashtag ⇒ Object
75 76 77 |
# File 'app/models/decidim/consultations/question.rb', line 75 def hashtag attributes["hashtag"].to_s.delete("#") end |
#module_name ⇒ Object
Overrides module name from participable concern
124 125 126 |
# File 'app/models/decidim/consultations/question.rb', line 124 def module_name "Decidim::Consultations" end |
#most_voted_response ⇒ Object
57 58 59 |
# File 'app/models/decidim/consultations/question.rb', line 57 def most_voted_response @most_voted_response ||= responses.order(votes_count: :desc).first end |
#mounted_admin_engine ⇒ Object
132 133 134 |
# File 'app/models/decidim/consultations/question.rb', line 132 def mounted_admin_engine "decidim_admin_consultations" end |
#mounted_engine ⇒ Object
128 129 130 |
# File 'app/models/decidim/consultations/question.rb', line 128 def mounted_engine "decidim_consultations" end |
#scopes_enabled ⇒ Object
115 116 117 |
# File 'app/models/decidim/consultations/question.rb', line 115 def scopes_enabled false end |
#scopes_enabled? ⇒ Boolean
111 112 113 |
# File 'app/models/decidim/consultations/question.rb', line 111 def scopes_enabled? false end |
#sorted_results ⇒ Object
Sorted results for the given question.
53 54 55 |
# File 'app/models/decidim/consultations/question.rb', line 53 def sorted_results responses.order(votes_count: :desc) end |
#to_param ⇒ Object
119 120 121 |
# File 'app/models/decidim/consultations/question.rb', line 119 def to_param slug end |
#total_votes ⇒ Object
61 62 63 |
# File 'app/models/decidim/consultations/question.rb', line 61 def total_votes @total_votes ||= responses.sum(&:votes_count) end |
#voted_by?(user) ⇒ Boolean
Public: Check if the user has voted the question.
Returns Boolean.
86 87 88 |
# File 'app/models/decidim/consultations/question.rb', line 86 def voted_by?(user) votes.where(author: user).any? end |