Class: Decidim::Comments::SortedComments

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

Overview

A class used to find comments for a commentable resource

Constant Summary collapse

DEFAULT_COMMENTS_LIMIT =
20

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 )
:limit - The number of items to load ( optional )
:offset - The number of items to skip ( optional )
:alignment - Filter by alignment: 1 (in_favor), -1 (against), 0 (neutral) ( optional )


30
31
32
33
34
# File 'app/queries/decidim/comments/sorted_comments.rb', line 30

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

Instance Attribute Details

#commentableObject (readonly)

Returns the value of attribute commentable.



9
10
11
# File 'app/queries/decidim/comments/sorted_comments.rb', line 9

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 )
:limit - The number of items to load ( optional )
:offset - The number of items to skip ( optional )


18
19
20
# File 'app/queries/decidim/comments/sorted_comments.rb', line 18

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

Instance Method Details

#has_more?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'app/queries/decidim/comments/sorted_comments.rb', line 61

def has_more?
  return false unless limited?

  total_count > offset + limit
end

#limitObject



71
72
73
# File 'app/queries/decidim/comments/sorted_comments.rb', line 71

def limit
  @options[:limit]&.to_i || default_limit
end

#offsetObject



67
68
69
# File 'app/queries/decidim/comments/sorted_comments.rb', line 67

def offset
  @options[:offset].to_i
end

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/queries/decidim/comments/sorted_comments.rb', line 39

def query
  scope = base_scope
          .includes(:author)

  sorted_scope = case @options[:order_by]
                 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

  apply_limit(sorted_scope)
end

#total_countObject



57
58
59
# File 'app/queries/decidim/comments/sorted_comments.rb', line 57

def total_count
  base_scope.count
end