Class: Decidim::Comments::Comment

Inherits:
ApplicationRecord show all
Includes:
ActionView::Helpers::TextHelper, ActsAsTree, Authorable, Commentable, DownloadYourData, 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

Instance Method Summary collapse

Class Method Details

.export_serializerObject



164
165
166
# File 'app/models/decidim/comments/comment.rb', line 164

def self.export_serializer
  Decidim::Comments::CommentSerializer
end

.negativeObject



79
80
81
# File 'app/models/decidim/comments/comment.rb', line 79

def self.negative
  where(alignment: -1)
end

.neutralObject



75
76
77
# File 'app/models/decidim/comments/comment.rb', line 75

def self.neutral
  where(alignment: 0)
end

.positiveObject



71
72
73
# File 'app/models/decidim/comments/comment.rb', line 71

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



171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/decidim/comments/comment.rb', line 171

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

Returns:

  • (Boolean)


109
110
111
112
113
# File 'app/models/decidim/comments/comment.rb', line 109

def accepts_new_comments?
  return if deleted?

  root_commentable.accepts_new_comments? && depth < MAX_DEPTH
end

#can_participate?(user) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
# File 'app/models/decidim/comments/comment.rb', line 183

def can_participate?(user)
  return true unless root_commentable.respond_to?(:can_participate?)

  root_commentable.can_participate?(user)
end

#componentObject



104
105
106
# File 'app/models/decidim/comments/comment.rb', line 104

def component
  commentable.component if commentable.respond_to?(:component)
end

#delete!Object



200
201
202
203
204
205
206
# File 'app/models/decidim/comments/comment.rb', line 200

def delete!
  return if deleted?

  update(deleted_at: Time.current)

  update_counter
end

#deleted?Boolean

Returns:

  • (Boolean)


208
209
210
# File 'app/models/decidim/comments/comment.rb', line 208

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

Returns:

  • (Boolean)


135
136
137
# File 'app/models/decidim/comments/comment.rb', line 135

def down_voted_by?(user)
  down_votes.exists?(author: user)
end

#edited?Boolean

Returns:

  • (Boolean)


212
213
214
# File 'app/models/decidim/comments/comment.rb', line 212

def edited?
  Decidim::ActionLog.where(resource: self).exists?(["extra @> ?", { edit: true }.to_json])
end

#extra_actions_for(current_user) ⇒ Object



216
217
218
# File 'app/models/decidim/comments/comment.rb', line 216

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



192
193
194
# File 'app/models/decidim/comments/comment.rb', line 192

def formatted_body(override_translation = nil)
  Decidim::ContentProcessor.render(sanitize_content_for_comment(render_markdown(translated_body(override_translation))), "div")
end

#organizationObject



87
88
89
# File 'app/models/decidim/comments/comment.rb', line 87

def organization
  commentable&.organization || participatory_space&.organization
end

#original_participatory_spaceObject



95
# File 'app/models/decidim/comments/comment.rb', line 95

alias original_participatory_space participatory_space

#participatory_spaceObject



97
98
99
100
101
102
# File 'app/models/decidim/comments/comment.rb', line 97

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_attributesObject

Public: Overrides the ‘reported_attributes` Reportable concern method.



155
156
157
# File 'app/models/decidim/comments/comment.rb', line 155

def reported_attributes
  [:body]
end

#reported_content_urlObject

Public: Overrides the ‘reported_content_url` Reportable concern method.



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/decidim/comments/comment.rb', line 140

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)
  elsif root_commentable.respond_to?(:reported_content_url)
    root_commentable.reported_content_url(url_params)
  else
    "/"
  end
end

#reported_searchable_content_extrasObject

Public: Overrides the ‘reported_searchable_content_extras` Reportable concern method.



160
161
162
# File 'app/models/decidim/comments/comment.rb', line 160

def reported_searchable_content_extras
  [author.name]
end

#reported_titleObject



83
84
85
# File 'app/models/decidim/comments/comment.rb', line 83

def reported_title
  truncate(translated_attribute(body))
end

#translated_body(override_translation = nil) ⇒ Object



196
197
198
# File 'app/models/decidim/comments/comment.rb', line 196

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

Returns:

  • (Boolean)


128
129
130
# File 'app/models/decidim/comments/comment.rb', line 128

def up_voted_by?(user)
  up_votes.exists?(author: user)
end

#users_to_notify_on_comment_createdObject

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



119
120
121
122
123
# File 'app/models/decidim/comments/comment.rb', line 119

def users_to_notify_on_comment_created
  Decidim::User.where(id: commentable.users_to_notify_on_comment_created).or(
    Decidim::User.where(id: decidim_author_id)
  )
end

#visible?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/models/decidim/comments/comment.rb', line 91

def visible?
  participatory_space.try(:visible?) && component.try(:published?)
end