Class: Toller::Sorts::ColumnHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/toller/sorts/column_handler.rb

Overview

Column handler for filter

Instance Method Summary collapse

Instance Method Details

#call(collection, type, direction, properties) ⇒ ActiveRecord::Relation

Applies a plain order clause to collection for a non-scope sort.

If field isn't a real column on the collection's model, or the column's actual type doesn't match the declared type:, the sort is logged and skipped instead of raising once the relation is evaluated.

Parameters:

  • collection (ActiveRecord::Relation)

    the collection to sort

  • type (Symbol)

    the sort type (e.g. :string, :integer, :boolean)

  • direction (Symbol)

    the sort direction, :asc or :desc

  • properties (Hash)

    the sort's properties, used to resolve :field

Returns:

  • (ActiveRecord::Relation)

    the sorted collection, or collection unchanged if :field is unknown or its actual column type doesn't match type



22
23
24
25
26
27
28
# File 'lib/toller/sorts/column_handler.rb', line 22

def call(collection, type, direction, properties)
  field_name = properties[:field]

  return collection unless resolvable_column?(collection, field_name, type)

  collection.order(field_name => direction)
end