Module: Shamu::Entities::ListScope::Sorting
- Defined in:
- lib/shamu/entities/list_scope/sorting.rb
Overview
Include sorting parameters and parsing.
class UsersListScope < Shamu::Entities::ListScope
include Shamu::Entities::ListScope::Sorting
end
scope = UserListScope.coerce!( sort_by: { first_name: :desc } )
scope.sort_by #=> { first_name: :desc }
scope = UserListScope.coerce!( sort_by: :first_name )
scope.sort_by #=> { first_name: :asc }
scope = UserListScope.coerce!( sort_by: [ :first_name, :last_name ] )
scope.sort_by #=> { first_name: :asc, last_name: :asc }
Attributes collapse
-
#sort_by ⇒ Hash
The sort attribute is coerced by converting arrays to a hash with a default direction of :asc for each attribute.
Instance Method Summary collapse
-
#sort_by_resolved ⇒ Hash
all transforms applied.
-
#sorted? ⇒ Boolean
True if the scope is paged.
Instance Attribute Details
#sort_by ⇒ Hash
The sort attribute is coerced by converting arrays to a hash with a default direction of :asc for each attribute.
scope.sort_by :name # => { name: :asc }
scope.sort_by :name, :created_at # => { name: :asc, created_at: :asc }
scope.sort_by :count, rating: :desc # => { count: :asc, rating: :desc }
|
# File 'lib/shamu/entities/list_scope/sorting.rb', line 27
|
Instance Method Details
#sort_by_resolved ⇒ Hash
all transforms applied.
55 56 57 58 59 60 61 |
# File 'lib/shamu/entities/list_scope/sorting.rb', line 55 def sort_by_resolved return sort_by unless reverse_sort? sort_by.each_with_object( {} ) do |( attribute, direction ), resolved| resolved[ attribute ] = direction == :asc ? :desc : :asc end end |
#sorted? ⇒ Boolean
Returns true if the scope is paged.
49 50 51 |
# File 'lib/shamu/entities/list_scope/sorting.rb', line 49 def sorted? !!sort_by end |