Class: Decidim::Debates::Debate

Inherits:
ApplicationRecord show all
Includes:
Authorable, Comments::CommentableWithComponent, Comments::HasAvailabilityAttributes, Decidim::DownloadYourData, FilterableResource, Followable, HasAttachments, HasCategory, HasComponent, HasReference, Likeable, Loggable, NewsletterParticipant, Randomable, Reportable, Resourceable, ScopableResource, Searchable, SoftDeletable, Taxonomizable, Traceable, TranslatableAttributes, TranslatableResource
Defined in:
app/models/decidim/debates/debate.rb

Overview

The data store for a Debate in the Decidim::Debates component. It stores a title, description and any other useful information to render a custom debate.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.export_serializerObject



182
183
184
# File 'app/models/decidim/debates/debate.rb', line 182

def self.export_serializer
  Decidim::Debates::DownloadYourDataDebateSerializer
end

.log_presenter_class_for(_log) ⇒ Object



70
71
72
# File 'app/models/decidim/debates/debate.rb', line 70

def self.log_presenter_class_for(_log)
  Decidim::Debates::AdminLog::DebatePresenter
end

.newsletter_participant_ids(component) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'app/models/decidim/debates/debate.rb', line 186

def self.newsletter_participant_ids(component)
  authors_ids = Decidim::Debates::Debate.where(component:)
                                        .where(decidim_author_type: Decidim::UserBaseEntity.name)
                                        .where.not(author: nil)
                                        .group(:decidim_author_id)
                                        .pluck(:decidim_author_id).flatten.compact
  commentators_ids = Decidim::Comments::Comment.user_commentators_ids_in(Decidim::Debates::Debate.where(component:))
  (authors_ids + commentators_ids).flatten.compact.uniq
end

.ransack(params = {}, options = {}) ⇒ Object



243
244
245
# File 'app/models/decidim/debates/debate.rb', line 243

def self.ransack(params = {}, options = {})
  DebateSearch.new(self, params, options)
end

.ransackable_associations(_auth_object = nil) ⇒ Object



251
252
253
# File 'app/models/decidim/debates/debate.rb', line 251

def self.ransackable_associations(_auth_object = nil)
  %w(taxonomies)
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



247
248
249
# File 'app/models/decidim/debates/debate.rb', line 247

def self.ransackable_attributes(_auth_object = nil)
  %w(search_text title description)
end

.ransackable_scopes(_auth_object = nil) ⇒ Object



239
240
241
# File 'app/models/decidim/debates/debate.rb', line 239

def self.ransackable_scopes(_auth_object = nil)
  [:with_any_state, :with_any_origin, :with_any_taxonomies]
end

Instance Method Details

#accepts_new_comments?Boolean

Public: Overrides the ‘accepts_new_comments?` CommentableWithComponent concern method.



138
139
140
141
142
143
# File 'app/models/decidim/debates/debate.rb', line 138

def accepts_new_comments?
  return false unless ongoing?
  return false if closed?

  commentable? && !comments_blocked? && comments_allowed?
end

#allow_resource_permissions?Boolean

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



178
179
180
# File 'app/models/decidim/debates/debate.rb', line 178

def allow_resource_permissions?
  true
end

#ama?Boolean

Public: Calculates whether the current debate is an AMA-styled one or not. AMA-styled debates are those that have a start and end time set, and comments are only open during that timelapse. AMA stands for Ask Me Anything, a type of debate inspired by Reddit.

Returns a Boolean.



98
99
100
# File 'app/models/decidim/debates/debate.rb', line 98

def ama?
  start_time.present? && end_time.present?
end

#closeable_by?(user) ⇒ Boolean

Checks whether the user can edit the debate.

user - the user to check for authorship



212
213
214
# File 'app/models/decidim/debates/debate.rb', line 212

def closeable_by?(user)
  authored_by?(user)
end

#closed?Boolean

Checks whether the debate is closed or not.



205
206
207
# File 'app/models/decidim/debates/debate.rb', line 205

def closed?
  closed_at.present? && conclusions.present?
end

#commentable_typeObject

Public: Identifies the commentable type in the API.



156
157
158
# File 'app/models/decidim/debates/debate.rb', line 156

def commentable_type
  self.class.name
end

#comments_end_timeObject



78
79
80
# File 'app/models/decidim/debates/debate.rb', line 78

def comments_end_time
  end_time
end

#comments_have_alignment?Boolean

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



146
147
148
# File 'app/models/decidim/debates/debate.rb', line 146

def comments_have_alignment?
  true
end

#comments_have_votes?Boolean

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



151
152
153
# File 'app/models/decidim/debates/debate.rb', line 151

def comments_have_votes?
  true
end

#comments_start_timeObject



74
75
76
# File 'app/models/decidim/debates/debate.rb', line 74

def comments_start_time
  start_time
end

#editable_by?(user) ⇒ Boolean

Checks whether the user can edit the debate.

user - the user to check for authorship



199
200
201
# File 'app/models/decidim/debates/debate.rb', line 199

def editable_by?(user)
  !closed? && authored_by?(user)
end

#finished?Boolean

Note that a debate can be finished even if it is not closed (meaning it has no conclusions).



121
122
123
# File 'app/models/decidim/debates/debate.rb', line 121

def finished?
  end_time.present? && end_time < Time.current
end

#not_started?Boolean



116
117
118
# File 'app/models/decidim/debates/debate.rb', line 116

def not_started?
  start_time.present? && start_time > Time.current
end

#ongoing?Boolean

Public: Checks if the debate is ongoing or not.

Returns a boolean.



112
113
114
# File 'app/models/decidim/debates/debate.rb', line 112

def ongoing?
  (ama? && ongoing_ama?) || !ama?
end

#ongoing_ama?Boolean

Public: Checks whether the debate is an AMA-styled one and is ongoing.

Returns a boolean.



105
106
107
# File 'app/models/decidim/debates/debate.rb', line 105

def ongoing_ama?
  ama? && Time.current.between?(start_time, end_time)
end

#presenterObject

Returns the presenter for this debate, to be used in the views. Required by ResourceRenderer.



66
67
68
# File 'app/models/decidim/debates/debate.rb', line 66

def presenter
  Decidim::Debates::DebatePresenter.new(self)
end

#reported_attributesObject

Public: Overrides the ‘reported_attributes` Reportable concern method.



83
84
85
# File 'app/models/decidim/debates/debate.rb', line 83

def reported_attributes
  [:title, :description]
end

#reported_searchable_content_extrasObject

Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method.



88
89
90
# File 'app/models/decidim/debates/debate.rb', line 88

def reported_searchable_content_extras
  [author_name]
end

#single_column_layout?Boolean

Public: Checks whether the comments are displayed in a single-column layout.



161
162
163
# File 'app/models/decidim/debates/debate.rb', line 161

def single_column_layout?
  comments_layout == "single_column"
end

#stateObject



125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/decidim/debates/debate.rb', line 125

def state
  if closed?
    :closed
  elsif ongoing?
    :ongoing
  elsif not_started?
    :not_started
  else
    :finished
  end
end

#two_columns_layout?Boolean

Public: Checks whether the comments are displayed in a two-column layout.



166
167
168
# File 'app/models/decidim/debates/debate.rb', line 166

def two_columns_layout?
  comments_layout == "two_columns"
end

#update_comments_countObject

Public: Updates the comments counter cache. We have to do it these way in order to properly calculate the counter with hidden comments.

rubocop:disable Rails/SkipsModelValidations



221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/models/decidim/debates/debate.rb', line 221

def update_comments_count
  comments_count = comments.not_hidden.not_deleted.count
  last_comment = comments.not_hidden.not_deleted.order("created_at DESC").first

  update_columns(
    last_comment_at: last_comment&.created_at,
    last_comment_by_id: last_comment&.decidim_author_id,
    last_comment_by_type: last_comment&.decidim_author_type,
    comments_count:,
    updated_at: Time.current
  )
end

#users_to_notify_on_comment_createdObject

Public: Override Commentable concern method ‘users_to_notify_on_comment_created`



171
172
173
174
175
# File 'app/models/decidim/debates/debate.rb', line 171

def users_to_notify_on_comment_created
  return Decidim::User.where(id: followers).or(Decidim::User.where(id: component.participatory_space.admins)).distinct if official?

  followers
end