Class: JSONAPI::CollectionQuery

Inherits:
Object
  • Object
show all
Includes:
Support::ConditionBuilding, Support::NestedFilters, Support::Pagination, Support::PolymorphicFilters, Support::RegularFilters, Support::Sorting
Defined in:
lib/json_api/support/collection_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Pagination

#apply_pagination

Methods included from Support::PolymorphicFilters

#apply_polymorphic_nested_filters

Methods included from Support::NestedFilters

#apply_nested_relationship_filters

Methods included from Support::RegularFilters

#apply_regular_filters

Methods included from Support::Sorting

#apply_sorting

Constructor Details

#initialize(scope, definition:, model_class:, filter_params:, sort_params:, page_params:) ⇒ CollectionQuery

Returns a new instance of CollectionQuery.



21
22
23
24
25
26
27
28
29
# File 'lib/json_api/support/collection_query.rb', line 21

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_appliedObject (readonly)

Returns the value of attribute pagination_applied.



19
20
21
# File 'lib/json_api/support/collection_query.rb', line 19

def pagination_applied
  @pagination_applied
end

#scopeObject (readonly)

Returns the value of attribute scope.



19
20
21
# File 'lib/json_api/support/collection_query.rb', line 19

def scope
  @scope
end

#total_countObject (readonly)

Returns the value of attribute total_count.



19
20
21
# File 'lib/json_api/support/collection_query.rb', line 19

def total_count
  @total_count
end

Instance Method Details

#executeObject



31
32
33
34
35
36
37
38
39
# File 'lib/json_api/support/collection_query.rb', line 31

def execute
  @scope = apply_filtering
  has_virtual_sort = sort_params.any? { |sort_field| virtual_attribute_sort?(sort_field) }
  @total_count = @scope.count unless has_virtual_sort
  @scope = apply_sorting(@scope)
  @total_count = @scope.count if has_virtual_sort && @scope.is_a?(Array)
  @scope = apply_pagination
  self
end