Class: Decidim::Debates::Debate
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Debates::Debate
- Includes:
- Authorable, Comments::CommentableWithComponent, Comments::HasAvailabilityAttributes, Decidim::DownloadYourData, Endorsable, FilterableResource, Followable, HasAttachments, HasCategory, HasComponent, HasReference, Loggable, NewsletterParticipant, Randomable, Reportable, Resourceable, ScopableResource, Searchable, SoftDeletable, Taxonomizable, Traceable, TranslatableAttributes, TranslatableResource
- Defined in:
- decidim-debates/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
- .ransack(params = {}, options = {}) ⇒ Object
- .ransackable_associations(_auth_object = nil) ⇒ Object
- .ransackable_attributes(_auth_object = nil) ⇒ Object
- .ransackable_scopes(_auth_object = nil) ⇒ Object
Instance Method Summary collapse
-
#accepts_new_comments? ⇒ Boolean
Public: Overrides the ‘accepts_new_comments?` CommentableWithComponent concern method.
-
#allow_resource_permissions? ⇒ Boolean
Public: Overrides the ‘allow_resource_permissions?` Resourceable 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_type ⇒ Object
Public: Identifies the commentable type in the API.
- #comments_end_time ⇒ Object
-
#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.
- #comments_start_time ⇒ Object
-
#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.
-
#presenter ⇒ Object
Returns the presenter for this debate, to be used in the views.
-
#reported_attributes ⇒ Object
Public: Overrides the ‘reported_attributes` Reportable concern method.
-
#reported_searchable_content_extras ⇒ Object
Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method.
-
#single_column_layout? ⇒ Boolean
Public: Checks whether the comments are displayed in a single-column layout.
-
#two_columns_layout? ⇒ Boolean
Public: Checks whether the comments are displayed in a two-column layout.
-
#update_comments_count ⇒ Object
Public: Updates the comments counter cache.
-
#users_to_notify_on_comment_created ⇒ Object
Public: Override Commentable concern method ‘users_to_notify_on_comment_created`.
Methods included from TranslatableAttributes
#attachment?, #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 HasAttachments
Methods included from Followable
Class Method Details
.export_serializer ⇒ Object
160 161 162 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 160 def self.export_serializer Decidim::Debates::DownloadYourDataDebateSerializer end |
.log_presenter_class_for(_log) ⇒ Object
69 70 71 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 69 def self.log_presenter_class_for(_log) Decidim::Debates::AdminLog::DebatePresenter end |
.newsletter_participant_ids(component) ⇒ Object
164 165 166 167 168 169 170 171 172 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 164 def self.(component) = 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:)) ( + commentators_ids).flatten.compact.uniq end |
.ransack(params = {}, options = {}) ⇒ Object
221 222 223 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 221 def self.ransack(params = {}, = {}) DebateSearch.new(self, params, ) end |
.ransackable_associations(_auth_object = nil) ⇒ Object
229 230 231 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 229 def self.ransackable_associations(_auth_object = nil) %w(taxonomies) end |
.ransackable_attributes(_auth_object = nil) ⇒ Object
225 226 227 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 225 def self.ransackable_attributes(_auth_object = nil) %w(search_text title description) end |
.ransackable_scopes(_auth_object = nil) ⇒ Object
217 218 219 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 217 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.
116 117 118 119 120 121 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 116 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.
156 157 158 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 156 def 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.
97 98 99 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 97 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
190 191 192 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 190 def closeable_by?(user) (user) end |
#closed? ⇒ Boolean
Checks whether the debate is closed or not.
183 184 185 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 183 def closed? closed_at.present? && conclusions.present? end |
#commentable_type ⇒ Object
Public: Identifies the commentable type in the API.
134 135 136 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 134 def commentable_type self.class.name end |
#comments_end_time ⇒ Object
77 78 79 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 77 def comments_end_time end_time end |
#comments_have_alignment? ⇒ Boolean
Public: Overrides the ‘comments_have_alignment?` Commentable concern method.
124 125 126 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 124 def comments_have_alignment? true end |
#comments_have_votes? ⇒ Boolean
Public: Overrides the ‘comments_have_votes?` Commentable concern method.
129 130 131 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 129 def comments_have_votes? true end |
#comments_start_time ⇒ Object
73 74 75 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 73 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
177 178 179 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 177 def editable_by?(user) !closed? && (user) end |
#open? ⇒ Boolean
Public: Checks if the debate is open or not.
Returns a boolean.
111 112 113 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 111 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.
104 105 106 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 104 def open_ama? ama? && Time.current.between?(start_time, end_time) end |
#presenter ⇒ Object
Returns the presenter for this debate, to be used in the views. Required by ResourceRenderer.
65 66 67 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 65 def presenter Decidim::Debates::DebatePresenter.new(self) end |
#reported_attributes ⇒ Object
Public: Overrides the ‘reported_attributes` Reportable concern method.
82 83 84 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 82 def reported_attributes [:title, :description] end |
#reported_searchable_content_extras ⇒ Object
Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method.
87 88 89 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 87 def reported_searchable_content_extras [] end |
#single_column_layout? ⇒ Boolean
Public: Checks whether the comments are displayed in a single-column layout.
139 140 141 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 139 def single_column_layout? comments_layout == "single_column" end |
#two_columns_layout? ⇒ Boolean
Public: Checks whether the comments are displayed in a two-column layout.
144 145 146 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 144 def two_columns_layout? comments_layout == "two_columns" 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
199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 199 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&., last_comment_by_type: last_comment&., comments_count:, updated_at: Time.current ) end |
#users_to_notify_on_comment_created ⇒ Object
Public: Override Commentable concern method ‘users_to_notify_on_comment_created`
149 150 151 152 153 |
# File 'decidim-debates/app/models/decidim/debates/debate.rb', line 149 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 |