Class: Decidim::Consultations::Question

Inherits:
ApplicationRecord show all
Includes:
Decidim::Comments::Commentable, Followable, HasAttachmentCollections, HasAttachments, HasResourcePermission, HasUploadValidations, Loggable, Participable, ParticipatorySpaceResourceable, Publicable, Randomable, ScopableParticipatorySpace, Traceable, TranslatableResource
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

Class Method Details

.participatory_space_manifestObject



192
193
194
# File 'app/models/decidim/consultations/question.rb', line 192

def self.participatory_space_manifest
  Decidim.find_participatory_space_manifest(Decidim::Consultation.name.demodulize.underscore.pluralize)
end

Instance Method Details

#allow_resource_permissions?Boolean

Public: Overrides the ‘allow_resource_permissions?` Resourceable concern method.

Returns:

  • (Boolean)


201
202
203
# File 'app/models/decidim/consultations/question.rb', line 201

def allow_resource_permissions?
  true
end

#attachment_contextObject



213
214
215
# File 'app/models/decidim/consultations/question.rb', line 213

def attachment_context
  :admin
end


135
136
137
# File 'app/models/decidim/consultations/question.rb', line 135

def banner_image_url
  banner_image.attached? ? attached_uploader(:banner_image).path : consultation.attached_uploader(:banner_image).path
end

#can_be_unvoted_by?(user) ⇒ Boolean

Public: Checks whether the given user can unvote the question or note.

Returns a Boolean.

Returns:

  • (Boolean)


149
150
151
152
153
154
# File 'app/models/decidim/consultations/question.rb', line 149

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.

Returns:

  • (Boolean)


159
160
161
162
163
164
165
# File 'app/models/decidim/consultations/question.rb', line 159

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.

Returns:

  • (Boolean)


122
123
124
# File 'app/models/decidim/consultations/question.rb', line 122

def comments_have_alignment?
  true
end

#comments_have_votes?Boolean

Public: Overrides the ‘comments_have_votes?` Commentable concern method.

Returns:

  • (Boolean)


127
128
129
# File 'app/models/decidim/consultations/question.rb', line 127

def comments_have_votes?
  true
end

#componentObject



209
210
211
# File 'app/models/decidim/consultations/question.rb', line 209

def component
  nil
end

#grouped?Boolean

Returns:

  • (Boolean)


115
116
117
118
119
# File 'app/models/decidim/consultations/question.rb', line 115

def grouped?
  return false unless multiple?

  response_groups_count.positive?
end

#grouped_responsesObject

matrix of responses by group (sorted by configuration)



111
112
113
# File 'app/models/decidim/consultations/question.rb', line 111

def grouped_responses
  @grouped_responses ||= sorted_responses.group_by(&:response_group)
end

#hashtagObject



131
132
133
# File 'app/models/decidim/consultations/question.rb', line 131

def hashtag
  attributes["hashtag"].to_s.delete("#")
end

#module_nameObject

Overrides module name from participable concern



180
181
182
# File 'app/models/decidim/consultations/question.rb', line 180

def module_name
  "Decidim::Consultations"
end

#most_voted_responseObject



83
84
85
# File 'app/models/decidim/consultations/question.rb', line 83

def most_voted_response
  @most_voted_response ||= responses.order(votes_count: :desc).first
end

#mounted_admin_engineObject



188
189
190
# File 'app/models/decidim/consultations/question.rb', line 188

def mounted_admin_engine
  "decidim_admin_consultations"
end

#mounted_engineObject



184
185
186
# File 'app/models/decidim/consultations/question.rb', line 184

def mounted_engine
  "decidim_consultations"
end

#multiple?Boolean

Multiple answers allowed?

Returns:

  • (Boolean)


98
99
100
101
102
103
# File 'app/models/decidim/consultations/question.rb', line 98

def multiple?
  return false if external_voting
  return false if max_votes.blank?

  max_votes > 1
end

#publishable_results?Boolean

if results can be shown to admins

Returns:

  • (Boolean)


79
80
81
# File 'app/models/decidim/consultations/question.rb', line 79

def publishable_results?
  consultation.finished? && sorted_results.any?
end

#resource_descriptionObject



196
197
198
# File 'app/models/decidim/consultations/question.rb', line 196

def resource_description
  subtitle
end

#scopes_enabledObject



171
172
173
# File 'app/models/decidim/consultations/question.rb', line 171

def scopes_enabled
  false
end

#scopes_enabled?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'app/models/decidim/consultations/question.rb', line 167

def scopes_enabled?
  false
end

#sorted_responsesObject

Sorted responses by date so admins have a way to predict it



106
107
108
# File 'app/models/decidim/consultations/question.rb', line 106

def sorted_responses
  @sorted_responses ||= responses.sort_by(&:created_at)
end

#sorted_resultsObject

Sorted results for the given question.



74
75
76
# File 'app/models/decidim/consultations/question.rb', line 74

def sorted_results
  responses.order(votes_count: :desc)
end

#to_paramObject



175
176
177
# File 'app/models/decidim/consultations/question.rb', line 175

def to_param
  slug
end

#total_participantsObject

Total number of users voting



93
94
95
# File 'app/models/decidim/consultations/question.rb', line 93

def total_participants
  @total_participants ||= votes.select(:decidim_author_id).distinct.count
end

#total_votesObject

Total number of votes, on multiple votes questions does not match users voting



88
89
90
# File 'app/models/decidim/consultations/question.rb', line 88

def total_votes
  @total_votes ||= responses.sum(&:votes_count)
end

#user_allowed_to_comment?(user) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
# File 'app/models/decidim/consultations/question.rb', line 205

def user_allowed_to_comment?(user)
  ActionAuthorizer.new(user, "comment", self, nil).authorize.ok?
end

#voted_by?(user) ⇒ Boolean

Public: Check if the user has voted the question.

Returns Boolean.

Returns:

  • (Boolean)


142
143
144
# File 'app/models/decidim/consultations/question.rb', line 142

def voted_by?(user)
  votes.where(author: user).any?
end