Class: Decidim::Debates::Debate
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Debates::Debate
- Includes:
- Authorable, Comments::Commentable, Decidim::DataPortability, Endorsable, Followable, HasCategory, HasComponent, HasReference, Loggable, NewsletterParticipant, Randomable, Reportable, Resourceable, ScopableResource, Searchable, 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
- .export_serializer ⇒ Object
- .log_presenter_class_for(_log) ⇒ Object
- .newsletter_participant_ids(component) ⇒ Object
Instance Method Summary collapse
-
#accepts_new_comments? ⇒ Boolean
Public: Overrides the ‘accepts_new_comments?` Commentable concern method.
-
#ama? ⇒ Boolean
Public: Calculates whether the current debate is an AMA-styled one or not.
-
#closeable_by?(user) ⇒ Boolean
Checks whether the user can edit the debate.
-
#closed? ⇒ Boolean
Checks whether the debate is closed or not.
-
#commentable? ⇒ Boolean
Public: Overrides the ‘commentable?` Commentable concern method.
-
#commentable_type ⇒ Object
Public: Identifies the commentable type in the API.
-
#comments_have_alignment? ⇒ Boolean
Public: Overrides the ‘comments_have_alignment?` Commentable concern method.
-
#comments_have_votes? ⇒ Boolean
Public: Overrides the ‘comments_have_votes?` Commentable concern method.
-
#editable_by?(user) ⇒ Boolean
Checks whether the user can edit the debate.
-
#open? ⇒ Boolean
Public: Checks if the debate is open or not.
-
#open_ama? ⇒ Boolean
Public: Checks whether the debate is an AMA-styled one and is open.
-
#reported_attributes ⇒ Object
Public: Overrides the ‘reported_attributes` Reportable concern method.
-
#reported_content_url ⇒ Object
Public: Overrides the ‘reported_content_url` Reportable concern method.
-
#reported_searchable_content_extras ⇒ Object
Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method.
-
#update_comments_count ⇒ Object
Public: Updates the comments counter cache.
-
#user_allowed_to_comment?(user) ⇒ Boolean
Public: Whether the object can have new comments or not.
-
#users_to_notify_on_comment_created ⇒ Object
Public: Override Commentable concern method ‘users_to_notify_on_comment_created`.
Class Method Details
.export_serializer ⇒ Object
134 135 136 |
# File 'app/models/decidim/debates/debate.rb', line 134 def self.export_serializer Decidim::Debates::DataPortabilityDebateSerializer end |
.log_presenter_class_for(_log) ⇒ Object
56 57 58 |
# File 'app/models/decidim/debates/debate.rb', line 56 def self.log_presenter_class_for(_log) Decidim::Debates::AdminLog::DebatePresenter end |
.newsletter_participant_ids(component) ⇒ Object
143 144 145 146 147 148 149 150 151 |
# File 'app/models/decidim/debates/debate.rb', line 143 def self.(component) = Decidim::Debates::Debate.where(component: 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: component)) ( + commentators_ids).flatten.compact.uniq end |
Instance Method Details
#accepts_new_comments? ⇒ Boolean
Public: Overrides the ‘accepts_new_comments?` Commentable concern method.
105 106 107 108 109 110 |
# File 'app/models/decidim/debates/debate.rb', line 105 def accepts_new_comments? return false unless open? return false if closed? commentable? && !comments_blocked? 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.
81 82 83 |
# File 'app/models/decidim/debates/debate.rb', line 81 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
169 170 171 |
# File 'app/models/decidim/debates/debate.rb', line 169 def closeable_by?(user) (user) end |
#closed? ⇒ Boolean
Checks whether the debate is closed or not.
162 163 164 |
# File 'app/models/decidim/debates/debate.rb', line 162 def closed? closed_at.present? && conclusions.present? end |
#commentable? ⇒ Boolean
Public: Overrides the ‘commentable?` Commentable concern method.
100 101 102 |
# File 'app/models/decidim/debates/debate.rb', line 100 def commentable? component.settings.comments_enabled? end |
#commentable_type ⇒ Object
Public: Identifies the commentable type in the API.
123 124 125 |
# File 'app/models/decidim/debates/debate.rb', line 123 def commentable_type self.class.name end |
#comments_have_alignment? ⇒ Boolean
Public: Overrides the ‘comments_have_alignment?` Commentable concern method.
113 114 115 |
# File 'app/models/decidim/debates/debate.rb', line 113 def comments_have_alignment? true end |
#comments_have_votes? ⇒ Boolean
Public: Overrides the ‘comments_have_votes?` Commentable concern method.
118 119 120 |
# File 'app/models/decidim/debates/debate.rb', line 118 def comments_have_votes? true end |
#editable_by?(user) ⇒ Boolean
Checks whether the user can edit the debate.
user - the user to check for authorship
156 157 158 |
# File 'app/models/decidim/debates/debate.rb', line 156 def editable_by?(user) !closed? && (user) end |
#open? ⇒ Boolean
Public: Checks if the debate is open or not.
Returns a boolean.
95 96 97 |
# File 'app/models/decidim/debates/debate.rb', line 95 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.
88 89 90 |
# File 'app/models/decidim/debates/debate.rb', line 88 def open_ama? ama? && Time.current.between?(start_time, end_time) end |
#reported_attributes ⇒ Object
Public: Overrides the ‘reported_attributes` Reportable concern method.
66 67 68 |
# File 'app/models/decidim/debates/debate.rb', line 66 def reported_attributes [:title, :description] end |
#reported_content_url ⇒ Object
Public: Overrides the ‘reported_content_url` Reportable concern method.
61 62 63 |
# File 'app/models/decidim/debates/debate.rb', line 61 def reported_content_url ResourceLocatorPresenter.new(self).url end |
#reported_searchable_content_extras ⇒ Object
Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method.
71 72 73 |
# File 'app/models/decidim/debates/debate.rb', line 71 def reported_searchable_content_extras [.name] end |
#update_comments_count ⇒ Object
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
178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'app/models/decidim/debates/debate.rb', line 178 def update_comments_count comments_count = comments.not_hidden.count last_comment = comments.not_hidden.order("created_at DESC").first update_columns( last_comment_at: last_comment&.created_at, last_comment_by_id: last_comment&., last_comment_by_type: last_comment&., comments_count: comments_count, updated_at: Time.current ) end |
#user_allowed_to_comment?(user) ⇒ Boolean
Public: Whether the object can have new comments or not.
139 140 141 |
# File 'app/models/decidim/debates/debate.rb', line 139 def user_allowed_to_comment?(user) can_participate_in_space?(user) end |
#users_to_notify_on_comment_created ⇒ Object
Public: Override Commentable concern method ‘users_to_notify_on_comment_created`
128 129 130 131 132 |
# File 'app/models/decidim/debates/debate.rb', line 128 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 |