Class: Decidim::Consultations::Question

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



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

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)


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

def allow_resource_permissions?
  true
end


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

def banner_image_url
  banner_image.present? ? banner_image.url : consultation.banner_image.url
end

#can_be_unvoted_by?(user) ⇒ Boolean

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

Returns a Boolean.

Returns:

  • (Boolean)


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

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)


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

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)


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

def comments_have_alignment?
  true
end

#comments_have_votes?Boolean

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

Returns:

  • (Boolean)


97
98
99
# File 'app/models/decidim/consultations/question.rb', line 97

def comments_have_votes?
  true
end

#hashtagObject



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

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

#module_nameObject

Overrides module name from participable concern



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

def module_name
  "Decidim::Consultations"
end

#most_voted_responseObject



69
70
71
# File 'app/models/decidim/consultations/question.rb', line 69

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

#mounted_admin_engineObject



158
159
160
# File 'app/models/decidim/consultations/question.rb', line 158

def mounted_admin_engine
  "decidim_admin_consultations"
end

#mounted_engineObject



154
155
156
# File 'app/models/decidim/consultations/question.rb', line 154

def mounted_engine
  "decidim_consultations"
end

#multiple?Boolean

Multiple answers allowed?

Returns:

  • (Boolean)


84
85
86
87
88
89
# File 'app/models/decidim/consultations/question.rb', line 84

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)


65
66
67
# File 'app/models/decidim/consultations/question.rb', line 65

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

#resource_descriptionObject



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

def resource_description
  subtitle
end

#scopes_enabledObject



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

def scopes_enabled
  false
end

#scopes_enabled?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'app/models/decidim/consultations/question.rb', line 137

def scopes_enabled?
  false
end

#sorted_resultsObject

Sorted results for the given question.



60
61
62
# File 'app/models/decidim/consultations/question.rb', line 60

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

#to_paramObject



145
146
147
# File 'app/models/decidim/consultations/question.rb', line 145

def to_param
  slug
end

#total_participantsObject

Total number of users voting



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

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



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

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.

Returns:

  • (Boolean)


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

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