Class: JsonapiCompliable::Scoping::Sort
- Defined in:
- lib/jsonapi_compliable/scoping/sort.rb
Overview
Apply sorting logic to the scope.
By default, sorting comes 'for free'. To specify a custom sorting proc:
class PostResource < ApplicationResource
sort do |scope, att, dir|
int = dir == :desc ? -1 : 1
scope.sort_by { |x| x[att] * int }
end
end
The sorting proc will be called once for each sort att/dir requested.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#apply_custom_scope ⇒ Object
Apply custom scoping proc configured via Resource DSL.
-
#apply_standard_scope ⇒ Object
Apply default scope logic via Resource adapter.
-
#custom_scope ⇒ Proc, Nil
The custom proc specified by Resource DSL.
Methods inherited from Base
Constructor Details
This class inherits a constructor from JsonapiCompliable::Scoping::Base
Instance Method Details
#apply_custom_scope ⇒ Object
Apply custom scoping proc configured via Resource DSL
32 33 34 35 36 37 38 |
# File 'lib/jsonapi_compliable/scoping/sort.rb', line 32 def apply_custom_scope each_sort do |attribute, direction| @scope = custom_scope .call(@scope, attribute, direction, resource.context) end @scope end |
#apply_standard_scope ⇒ Object
Apply default scope logic via Resource adapter
23 24 25 26 27 28 |
# File 'lib/jsonapi_compliable/scoping/sort.rb', line 23 def apply_standard_scope each_sort do |attribute, direction| @scope = resource.adapter.order(@scope, attribute, direction) end @scope end |
#custom_scope ⇒ Proc, Nil
Returns The custom proc specified by Resource DSL
17 18 19 |
# File 'lib/jsonapi_compliable/scoping/sort.rb', line 17 def custom_scope resource.sorting end |