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

Instance Method Summary collapse

Instance Attribute Details

#sort_byHash

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 }

Returns:

  • (Hash)

    the attributes and directions to sort by.



# File 'lib/shamu/entities/list_scope/sorting.rb', line 27

Instance Method Details

#sort_by_resolvedHash

all transforms applied.

Returns:

  • (Hash)

    gets a normalized hash of attribute to direction with



60
61
62
63
64
65
66
# 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?Boolean

Returns true if the scope is paged.

Returns:

  • (Boolean)

    true if the scope is paged.



54
55
56
# File 'lib/shamu/entities/list_scope/sorting.rb', line 54

def sorted?
  !!sort_by
end