Module: SortingTableFor::ModelScope::SingletonMethods

Defined in:
lib/sorting_table_for/model_scope.rb

Instance Method Summary collapse

Instance Method Details

#sorting_table(*args) ⇒ Object

Return a scope of the object with an order

Usage

sorting_table(the params) - Sorting by the given parameters
sorting_table(the params, column name) - Sort by the column name with direction ascending, if no parameters
sorting_table(the params, column name, direction) - Sort by the column name with the given direction, if no parameters

Exemples

User.sorting_table(params)
User.sorting_table(params, :username)
User.sorting_table(params, :username, :desc)

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
# File 'lib/sorting_table_for/model_scope.rb', line 29

def sorting_table(*args)
  raise ArgumentError, 'sorting_table: Too many arguments (max : 3)' if args.size > 3 
  sort_table_param = get_sorting_table_params(args)
  return scoped({})  if !sort_table_param and args.size == 1
  sort, direction = get_sort_and_direction(sort_table_param, args)
  return scoped({}) if !sort or !valid_column?(sort) or !valid_direction?(direction)
  scoped({ :order => "#{sort} #{direction}" })
end