Class: Decidim::Comments::SortedComments

Inherits:
Rectify::Query
  • Object
show all
Defined in:
app/queries/decidim/comments/sorted_comments.rb

Overview

A class used to find comments for a commentable resource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commentable, options = {}) ⇒ SortedComments

Initializes the class.

commentable = a resource that can have comments options - The Hash options is used to refine the selection ( default: {}):

:order_by - The string order_by to sort by ( optional )


23
24
25
26
27
# File 'app/queries/decidim/comments/sorted_comments.rb', line 23

def initialize(commentable, options = {})
  options[:order_by] ||= "older"
  @commentable = commentable
  @options = options
end

Instance Attribute Details

#commentableObject (readonly)

Returns the value of attribute commentable.



7
8
9
# File 'app/queries/decidim/comments/sorted_comments.rb', line 7

def commentable
  @commentable
end

Class Method Details

.for(commentable, options = {}) ⇒ Object

Syntactic sugar to initialize the class and return the queried objects.

commentable - a resource that can have comments options - The Hash options is used to refine the selection ( default: {}):

:order_by - The string order_by to sort by ( optional )


14
15
16
# File 'app/queries/decidim/comments/sorted_comments.rb', line 14

def self.for(commentable, options = {})
  new(commentable, options).query
end

Instance Method Details

#queryObject

Finds the Comments for a resource that can have comments and eager loads comments replies. It uses Comment’s MAX_DEPTH to load a maximum level of nested replies.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/queries/decidim/comments/sorted_comments.rb', line 32

def query
  scope = base_scope
          .includes(:author, :user_group, :up_votes, :down_votes)

  case @options[:order_by]
  when "older"
    order_by_older(scope)
  when "recent"
    order_by_recent(scope)
  when "best_rated"
    order_by_best_rated(scope)
  when "most_discussed"
    order_by_most_discussed(scope)
  else
    order_by_older(scope)
  end
end