module Shamu::Entities::ListScope::Sorting
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 } “`
Public Class Methods
included( base )
click to toggle source
@!endgroup Attributes
Calls superclass method
# File lib/shamu/entities/list_scope/sorting.rb, line 42 def self.included( base ) super base.attribute :default_sort_by, as: :default_order, coerce: ->( *values ) { parse_sort_by( values ) } base.attribute :sort_by, as: :order, coerce: ->( *values ) { parse_sort_by( values ) }, default: ->() { default_sort_by } end
Public Instance Methods
sort_by_resolved()
click to toggle source
@return [Hash] gets a normalized hash of attribute to direction with all transforms applied.
# File lib/shamu/entities/list_scope/sorting.rb, line 60 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?()
click to toggle source
@return [Boolean] true if the scope is paged.
# File lib/shamu/entities/list_scope/sorting.rb, line 54 def sorted? !!sort_by end
Private Instance Methods
parse_sort_by( arguments )
click to toggle source
# File lib/shamu/entities/list_scope/sorting.rb, line 79 def parse_sort_by( arguments ) Array( arguments ).each_with_object( {} ) do |arg, sorted| case arg when Array then sorted.merge!( parse_sort_by( arg ) ) when Hash then arg.each do |attr, direction| case direction when :asc, :desc, "asc", "desc" then sorted[attr] = direction.to_sym when Array, Hash then sorted[attr] = parse_sort_by( direction ) else fail ArgumentError end end when String, Symbol then sorted[arg.to_sym] = :asc else fail ArgumentError end end end
reverse_sort!()
click to toggle source
# File lib/shamu/entities/list_scope/sorting.rb, line 74 def reverse_sort! @reverse_sort = true self.sort_by = { id: :asc } unless sort_by_set? end
reverse_sort?()
click to toggle source
# File lib/shamu/entities/list_scope/sorting.rb, line 70 def reverse_sort? @reverse_sort end