Class: Decidim::Consultations::Question

Inherits:
ApplicationRecord show all
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

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_manifestObject



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



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

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)


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.

Returns:

  • (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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


71
72
73
# File 'app/models/decidim/consultations/question.rb', line 71

def comments_have_votes?
  true
end

#hashtagObject



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

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

#module_nameObject

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_responseObject



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_engineObject



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

def mounted_admin_engine
  "decidim_admin_consultations"
end

#mounted_engineObject



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

def mounted_engine
  "decidim_consultations"
end

#scopes_enabledObject



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

def scopes_enabled
  false
end

#scopes_enabled?Boolean

Returns:

  • (Boolean)


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

def scopes_enabled?
  false
end

#sorted_resultsObject

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_paramObject



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

def to_param
  slug
end

#total_votesObject



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.

Returns:

  • (Boolean)


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

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