Class: Decidim::Comments::Comment
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::Comments::Comment
- Includes:
- Authorable, Commentable, DataPortability, FriendlyDates, Loggable, Reportable, Searchable, Traceable, TranslatableAttributes, TranslatableResource
- Defined in:
- 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
- #formatted_body ⇒ Object
- #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 ⇒ 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
Class Method Details
.export_serializer ⇒ Object
154 155 156 |
# File 'app/models/decidim/comments/comment.rb', line 154 def self.export_serializer Decidim::Comments::CommentSerializer end |
.negative ⇒ Object
75 76 77 |
# File 'app/models/decidim/comments/comment.rb', line 75 def self.negative where(alignment: -1) end |
.neutral ⇒ Object
71 72 73 |
# File 'app/models/decidim/comments/comment.rb', line 71 def self.neutral where(alignment: 0) end |
.positive ⇒ Object
67 68 69 |
# File 'app/models/decidim/comments/comment.rb', line 67 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`.
161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/models/decidim/comments/comment.rb', line 161 def self.user_commentators_ids_in(resources) if resources.first&.kind_of?(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?`
101 102 103 104 105 |
# File 'app/models/decidim/comments/comment.rb', line 101 def accepts_new_comments? return if deleted? root_commentable.accepts_new_comments? && depth < MAX_DEPTH end |
#can_participate?(user) ⇒ Boolean
173 174 175 176 177 |
# File 'app/models/decidim/comments/comment.rb', line 173 def can_participate?(user) return true unless root_commentable&.respond_to?(:can_participate?) root_commentable.can_participate?(user) end |
#component ⇒ Object
96 97 98 |
# File 'app/models/decidim/comments/comment.rb', line 96 def component commentable.component if commentable.respond_to?(:component) end |
#delete! ⇒ Object
187 188 189 190 191 192 193 |
# File 'app/models/decidim/comments/comment.rb', line 187 def delete! return if deleted? update(deleted_at: Time.current) update_counter end |
#deleted? ⇒ Boolean
195 196 197 |
# File 'app/models/decidim/comments/comment.rb', line 195 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
127 128 129 |
# File 'app/models/decidim/comments/comment.rb', line 127 def down_voted_by?(user) down_votes.any? { |vote| vote. == user } end |
#edited? ⇒ Boolean
199 200 201 |
# File 'app/models/decidim/comments/comment.rb', line 199 def edited? Decidim::ActionLog.where(resource: self).exists?(["extra @> ?", Arel.sql("{\"edit\":true}")]) end |
#formatted_body ⇒ Object
179 180 181 |
# File 'app/models/decidim/comments/comment.rb', line 179 def formatted_body Decidim::ContentProcessor.render(sanitize_content_for_comment(render_markdown(translated_body)), "div") end |
#organization ⇒ Object
79 80 81 |
# File 'app/models/decidim/comments/comment.rb', line 79 def organization commentable&.organization || participatory_space&.organization end |
#original_participatory_space ⇒ Object
87 |
# File 'app/models/decidim/comments/comment.rb', line 87 alias original_participatory_space participatory_space |
#participatory_space ⇒ Object
89 90 91 92 93 94 |
# File 'app/models/decidim/comments/comment.rb', line 89 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.
145 146 147 |
# File 'app/models/decidim/comments/comment.rb', line 145 def reported_attributes [:body] end |
#reported_content_url ⇒ Object
Public: Overrides the ‘reported_content_url` Reportable concern method.
132 133 134 135 136 137 138 139 140 141 142 |
# File 'app/models/decidim/comments/comment.rb', line 132 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.
150 151 152 |
# File 'app/models/decidim/comments/comment.rb', line 150 def reported_searchable_content_extras [.name] end |
#translated_body ⇒ Object
183 184 185 |
# File 'app/models/decidim/comments/comment.rb', line 183 def translated_body translated_attribute(body, organization) 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
120 121 122 |
# File 'app/models/decidim/comments/comment.rb', line 120 def up_voted_by?(user) up_votes.any? { |vote| vote. == 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
111 112 113 114 115 |
# File 'app/models/decidim/comments/comment.rb', line 111 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
83 84 85 |
# File 'app/models/decidim/comments/comment.rb', line 83 def visible? participatory_space.try(:visible?) && component.try(:published?) end |