Class: JSONAPI::CollectionQuery
- Inherits:
-
Object
- Object
- JSONAPI::CollectionQuery
- Defined in:
- lib/json_api/support/collection_query.rb
Instance Attribute Summary collapse
-
#pagination_applied ⇒ Object
readonly
Returns the value of attribute pagination_applied.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#total_count ⇒ Object
readonly
Returns the value of attribute total_count.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(scope, definition:, model_class:, filter_params:, sort_params:, page_params:) ⇒ CollectionQuery
constructor
A new instance of CollectionQuery.
Constructor Details
#initialize(scope, definition:, model_class:, filter_params:, sort_params:, page_params:) ⇒ CollectionQuery
Returns a new instance of CollectionQuery.
7 8 9 10 11 12 13 14 15 |
# File 'lib/json_api/support/collection_query.rb', line 7 def initialize(scope, definition:, model_class:, filter_params:, sort_params:, page_params:) @scope = scope @definition = definition @model_class = model_class @filter_params = filter_params @sort_params = sort_params @page_params = page_params @pagination_applied = page_params.present? end |
Instance Attribute Details
#pagination_applied ⇒ Object (readonly)
Returns the value of attribute pagination_applied.
5 6 7 |
# File 'lib/json_api/support/collection_query.rb', line 5 def pagination_applied @pagination_applied end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
5 6 7 |
# File 'lib/json_api/support/collection_query.rb', line 5 def scope @scope end |
#total_count ⇒ Object (readonly)
Returns the value of attribute total_count.
5 6 7 |
# File 'lib/json_api/support/collection_query.rb', line 5 def total_count @total_count end |
Instance Method Details
#execute ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/json_api/support/collection_query.rb', line 17 def execute # Apply filtering @scope = apply_filtering # Get total count before sorting (for virtual attributes, we need to load records) # If we have virtual attribute sorting, we'll recalculate after sorting has_virtual_sort = sort_params.any? { |sort_field| virtual_attribute_sort?(sort_field) } @total_count = @scope.count unless has_virtual_sort # Apply sorting (may convert scope to array if virtual attributes are involved) @scope = apply_sorting(@scope) # Recalculate total count if we converted to array for virtual sorting @total_count = @scope.count if has_virtual_sort && @scope.is_a?(Array) # Apply pagination @scope = apply_pagination self end |