Class: Decidim::Comments::Comment
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Comments::Comment
- Includes:
- ActsAsTree, Authorable, Commentable, DownloadYourData, FriendlyDates, Loggable, Reportable, Searchable, Traceable, TranslatableAttributes, TranslatableResource
- Defined in:
- decidim-comments/app/models/decidim/comments/comment.rb
Overview
Some resources will be configured as commentable objects so users can comment on them. The will be able to create conversations between users to discuss or share their thoughts about the resource.
Constant Summary collapse
- MAX_DEPTH =
Limit the max depth of a comment tree. If C is a comment and R is a reply: C (depth 0) |–R (depth 1) |–R (depth 1)
|--R (depth 2) |--R (depth 3)
3
Class Method Summary collapse
- .export_serializer ⇒ Object
- .negative ⇒ Object
- .neutral ⇒ Object
- .positive ⇒ Object
-
.user_commentators_ids_in(resources) ⇒ Object
Public: Returns the list of author IDs of type ‘UserBaseEntity` that commented in one of the
resources
.
Instance Method Summary collapse
-
#accepts_new_comments? ⇒ Boolean
Public: Override Commentable concern method ‘accepts_new_comments?`.
- #can_participate?(user) ⇒ Boolean
- #component ⇒ Object
- #delete! ⇒ Object
- #deleted? ⇒ Boolean
-
#down_voted_by?(user) ⇒ Boolean
Public: Check if the user has downvoted the comment.
- #edited? ⇒ Boolean
- #extra_actions_for(current_user) ⇒ Object
-
#formatted_body(override_translation = nil) ⇒ Object
The override_translation argument has been added to be able to use this method from comment event in the resource_text method which requires the use of this argument in translated_attribute of body.
- #organization ⇒ Object
- #original_participatory_space ⇒ Object
- #participatory_space ⇒ Object
-
#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.
- #translated_body(override_translation = nil) ⇒ Object
-
#up_voted_by?(user) ⇒ Boolean
Public: Check if the user has upvoted the comment.
-
#users_to_notify_on_comment_created ⇒ Object
Public: Override Commentable concern method ‘users_to_notify_on_comment_created`.
- #visible? ⇒ Boolean
Methods included from ActsAsTree
#descendants, #self_and_descendants
Methods included from TranslatableAttributes
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 FriendlyDates
Class Method Details
.export_serializer ⇒ Object
157 158 159 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 157 def self.export_serializer Decidim::Comments::CommentSerializer end |
.negative ⇒ Object
78 79 80 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 78 def self.negative where(alignment: -1) end |
.neutral ⇒ Object
74 75 76 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 74 def self.neutral where(alignment: 0) end |
.positive ⇒ Object
70 71 72 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 70 def self.positive where(alignment: 1) end |
.user_commentators_ids_in(resources) ⇒ Object
Public: Returns the list of author IDs of type ‘UserBaseEntity` that commented in one of the resources
. Expects all resources
to be of the same “commentable_type”. If the result is not `Decidim::Comments::Commentable` returns `nil`.
164 165 166 167 168 169 170 171 172 173 174 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 164 def self.user_commentators_ids_in(resources) if resources.first.is_a?(Decidim::Comments::Commentable) commentable_type = resources.first.class.name Decidim::Comments::Comment.select("DISTINCT decidim_author_id").not_hidden.not_deleted .where(decidim_commentable_id: resources.pluck(:id)) .where(decidim_commentable_type: commentable_type) .where("decidim_author_type" => "Decidim::UserBaseEntity").pluck(:decidim_author_id) else [] end end |
Instance Method Details
#accepts_new_comments? ⇒ Boolean
Public: Override Commentable concern method ‘accepts_new_comments?`
104 105 106 107 108 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 104 def accepts_new_comments? return if deleted? root_commentable.accepts_new_comments? && depth < MAX_DEPTH end |
#can_participate?(user) ⇒ Boolean
176 177 178 179 180 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 176 def can_participate?(user) return true unless root_commentable.respond_to?(:can_participate?) root_commentable.can_participate?(user) end |
#component ⇒ Object
99 100 101 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 99 def component commentable.component if commentable.respond_to?(:component) end |
#delete! ⇒ Object
193 194 195 196 197 198 199 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 193 def delete! return if deleted? update(deleted_at: Time.current) update_counter end |
#deleted? ⇒ Boolean
201 202 203 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 201 def deleted? deleted_at.present? end |
#down_voted_by?(user) ⇒ Boolean
Public: Check if the user has downvoted the comment
Returns a bool value to indicate if the condition is truthy or not
130 131 132 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 130 def down_voted_by?(user) down_votes.exists?(author: user) end |
#edited? ⇒ Boolean
205 206 207 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 205 def edited? Decidim::ActionLog.where(resource: self).exists?(["extra @> ?", Arel.sql("{\"edit\":true}")]) end |
#extra_actions_for(current_user) ⇒ Object
209 210 211 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 209 def extra_actions_for(current_user) root_commentable.try(:actions_for_comment, self, current_user) end |
#formatted_body(override_translation = nil) ⇒ Object
The override_translation argument has been added to be able to use this method from comment event in the resource_text method which requires the use of this argument in translated_attribute of body
185 186 187 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 185 def formatted_body(override_translation = nil) Decidim::ContentProcessor.render(sanitize_content_for_comment(render_markdown(translated_body(override_translation))), "div") end |
#organization ⇒ Object
82 83 84 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 82 def organization commentable&.organization || participatory_space&.organization end |
#original_participatory_space ⇒ Object
90 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 90 alias original_participatory_space participatory_space |
#participatory_space ⇒ Object
92 93 94 95 96 97 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 92 def participatory_space return original_participatory_space if original_participatory_space.present? return root_commentable unless root_commentable.respond_to?(:participatory_space) root_commentable.participatory_space end |
#reported_attributes ⇒ Object
Public: Overrides the ‘reported_attributes` Reportable concern method.
148 149 150 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 148 def reported_attributes [:body] end |
#reported_content_url ⇒ Object
Public: Overrides the ‘reported_content_url` Reportable concern method.
135 136 137 138 139 140 141 142 143 144 145 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 135 def reported_content_url return unless root_commentable url_params = { anchor: "comment_#{id}" } if root_commentable.respond_to?(:polymorphic_resource_url) root_commentable.polymorphic_resource_url(url_params) else ResourceLocatorPresenter.new(root_commentable).url(url_params) end end |
#reported_searchable_content_extras ⇒ Object
Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method.
153 154 155 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 153 def reported_searchable_content_extras [.name] end |
#translated_body(override_translation = nil) ⇒ Object
189 190 191 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 189 def translated_body(override_translation = nil) translated_attribute(body, organization, override_translation) end |
#up_voted_by?(user) ⇒ Boolean
Public: Check if the user has upvoted the comment
Returns a bool value to indicate if the condition is truthy or not
123 124 125 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 123 def up_voted_by?(user) up_votes.exists?(author: user) end |
#users_to_notify_on_comment_created ⇒ Object
Public: Override Commentable concern method ‘users_to_notify_on_comment_created`. Return the comment author together with whatever ActiveRecord::Relation is returned by the `commentable`. This will cause the comment author to be notified when the comment is replied
114 115 116 117 118 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 114 def users_to_notify_on_comment_created Decidim::User.where(id: commentable.users_to_notify_on_comment_created).or( Decidim::User.where(id: ) ) end |
#visible? ⇒ Boolean
86 87 88 |
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 86 def visible? participatory_space.try(:visible?) && component.try(:published?) end |