Class: Decidim::Debates::Debate

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

Methods included from TranslatableAttributes

#default_locale?

Methods included from Searchable

searchable_resources, searchable_resources_by_type, searchable_resources_of_type_comment, searchable_resources_of_type_component, searchable_resources_of_type_participant, searchable_resources_of_type_participatory_space

Methods included from Followable

#followers

Class Method Details

.export_serializerObject



154
155
156
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 154

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

.log_presenter_class_for(_log) ⇒ Object



68
69
70
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 68

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

.newsletter_participant_ids(component) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 158

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



215
216
217
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 215

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

.ransackable_associations(_auth_object = nil) ⇒ Object



223
224
225
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 223

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

.ransackable_attributes(_auth_object = nil) ⇒ Object



219
220
221
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 219

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

.ransackable_scopes(_auth_object = nil) ⇒ Object



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

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.

Returns:

  • (Boolean)


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

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

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

#allow_resource_permissions?Boolean

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

Returns:

  • (Boolean)


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

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.

Returns:

  • (Boolean)


101
102
103
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 101

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

Returns:

  • (Boolean)


184
185
186
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 184

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

#closed?Boolean

Checks whether the debate is closed or not.

Returns:

  • (Boolean)


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

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

#commentable_typeObject

Public: Identifies the commentable type in the API.



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

def commentable_type
  self.class.name
end

#comments_end_timeObject



76
77
78
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 76

def comments_end_time
  end_time
end

#comments_have_alignment?Boolean

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

Returns:

  • (Boolean)


128
129
130
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 128

def comments_have_alignment?
  true
end

#comments_have_votes?Boolean

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

Returns:

  • (Boolean)


133
134
135
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 133

def comments_have_votes?
  true
end

#comments_start_timeObject



72
73
74
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 72

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

Returns:

  • (Boolean)


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

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

#open?Boolean

Public: Checks if the debate is open or not.

Returns a boolean.

Returns:

  • (Boolean)


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

def open?
  (ama? && open_ama?) || !ama?
end

#open_ama?Boolean

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

Returns a boolean.

Returns:

  • (Boolean)


108
109
110
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 108

def open_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.



64
65
66
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 64

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

#reported_attributesObject

Public: Overrides the ‘reported_attributes` Reportable concern method.



86
87
88
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 86

def reported_attributes
  [:title, :description]
end

#reported_content_urlObject

Public: Overrides the ‘reported_content_url` Reportable concern method.



81
82
83
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 81

def reported_content_url
  ResourceLocatorPresenter.new(self).url
end

#reported_searchable_content_extrasObject

Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method.



91
92
93
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 91

def reported_searchable_content_extras
  [author_name]
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



193
194
195
196
197
198
199
200
201
202
203
204
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 193

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_user_group_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`



143
144
145
146
147
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 143

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