Module: Jsonapi::QueryBuilder::Mixins::Sort

Extended by:
ActiveSupport::Concern
Included in:
BaseQuery
Defined in:
lib/jsonapi/query_builder/mixins/sort.rb,
lib/jsonapi/query_builder/mixins/sort/param.rb

Defined Under Namespace

Classes: Param

Instance Method Summary collapse

Instance Method Details

#sort(collection, sort_params = send(:sort_params)) ⇒ ActiveRecord::Relation

Sorts the passed relation with the default sort params (parsed from the queries params) or with explicitly passed sort parameters. Parses each sort parameter and looks for the sorting strategy for it, if the strategy responds to a call method it calls it with the collection and parameter’s parsed sort direction, otherwise it instantiates the sort class with the collection and the parameter’s parsed sort direction and calls for the results. Finally it adds the unique sort attributes to enforce deterministic results. If sort params are blank, it adds the default sort attributes before setting the unique sort attributes.

Parameters:

  • collection (ActiveRecord::Relation)
  • sort_params (Object) (defaults to: send(:sort_params))

    Optional explicit sort params

Returns:

  • (ActiveRecord::Relation)

    Sorted relation

Raises:



63
64
65
66
67
68
69
70
# File 'lib/jsonapi/query_builder/mixins/sort.rb', line 63

def sort(collection, sort_params = send(:sort_params))
  sort_params = Param.deserialize_params(sort_params)
  ensure_permitted_sort_params!(sort_params) if sort_params

  collection
    .yield_self { |c| add_order_attributes(c, sort_params) }
    .yield_self(&method(:add_unique_order_attributes))
end