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

#sorted?Boolean

Returns true if the scope is paged.

Returns:

  • (Boolean)

    true if the scope is paged.



49
50
51
# File 'lib/shamu/entities/list_scope/sorting.rb', line 49

def sorted?
  !!sort_by
end