Class: Decidim::Comments::SortedComments
- Inherits:
-
Query
- Object
- Query
- Decidim::Comments::SortedComments
- 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
-
#commentable ⇒ Object
readonly
Returns the value of attribute commentable.
Class Method Summary collapse
-
.for(commentable, options = {}) ⇒ Object
Syntactic sugar to initialize the class and return the queried objects.
Instance Method Summary collapse
- #has_more? ⇒ Boolean
-
#initialize(commentable, options = {}) ⇒ SortedComments
constructor
Initializes the class.
- #limit ⇒ Object
- #offset ⇒ Object
-
#query ⇒ Object
Finds the Comments for a resource that can have comments and eager loads comments replies.
- #total_count ⇒ Object
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, = {}) [:order_by] ||= "older" @commentable = commentable @options = end |
Instance Attribute Details
#commentable ⇒ Object (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, = {}) new(commentable, ).query end |
Instance Method Details
#has_more? ⇒ 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 |
#limit ⇒ Object
71 72 73 |
# File 'app/queries/decidim/comments/sorted_comments.rb', line 71 def limit @options[:limit]&.to_i || default_limit end |
#offset ⇒ Object
67 68 69 |
# File 'app/queries/decidim/comments/sorted_comments.rb', line 67 def offset @options[:offset].to_i end |
#query ⇒ Object
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_count ⇒ Object
57 58 59 |
# File 'app/queries/decidim/comments/sorted_comments.rb', line 57 def total_count base_scope.count end |