Class: Decidim::Comments::Comment

Inherits:
ApplicationRecord show all
Includes:
Authorable, Commentable, DataPortability, 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

Instance Method Summary collapse

Methods included from TranslatableAttributes

#default_locale?

Methods included from Searchable

searchable_resources, 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

#friendly_created_at

Class Method Details

.export_serializerObject


149
150
151
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 149

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

.negativeObject


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

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

.neutralObject


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

def self.neutral
  where(alignment: 0)
end

.positiveObject


66
67
68
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 66

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


156
157
158
159
160
161
162
163
164
165
166
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 156

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

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

def accepts_new_comments?
  root_commentable.accepts_new_comments? && depth < MAX_DEPTH
end

#can_participate?(user) ⇒ Boolean

Returns:

  • (Boolean)

168
169
170
171
172
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 168

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

  root_commentable.can_participate?(user)
end

#comment_threadsObject

Public: Override comment threads to exclude hidden ones.

Returns comment.


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

def comment_threads
  super.reject(&:hidden?)
end

#componentObject


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

def component
  commentable.component if commentable.respond_to?(:component)
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)

124
125
126
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 124

def down_voted_by?(user)
  down_votes.any? { |vote| vote.author == user }
end

#formatted_bodyObject


174
175
176
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 174

def formatted_body
  Decidim::ContentProcessor.render(sanitize_content(render_markdown(translated_body)), "div")
end

#participatory_spaceObject


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

def participatory_space
  return root_commentable if root_commentable.is_a?(Decidim::Participable)

  root_commentable.participatory_space
end

#reported_attributesObject

Public: Overrides the `reported_attributes` Reportable concern method.


140
141
142
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 140

def reported_attributes
  [:body]
end

#reported_content_urlObject

Public: Overrides the `reported_content_url` Reportable concern method.


129
130
131
132
133
134
135
136
137
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 129

def reported_content_url
  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_extrasObject

Public: Overrides the `reported_searchable_content_extras` Reportable concern method.


145
146
147
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 145

def reported_searchable_content_extras
  [normalized_author.name]
end

#translated_bodyObject


178
179
180
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 178

def translated_body
  @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

Returns:

  • (Boolean)

117
118
119
# File 'decidim-comments/app/models/decidim/comments/comment.rb', line 117

def up_voted_by?(user)
  up_votes.any? { |vote| vote.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


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

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)

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

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