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, 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, the sort is logged and skipped instead of raising once the relation is evaluated.

Parameters:

  • collection (ActiveRecord::Relation)

    the collection to sort

  • 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



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

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

  unless collection.klass.column_names.include?(field_name.to_s)
    Rails.logger.warn("[Toller] Skipping sort: #{collection.klass} has no column `#{field_name}`")
    return collection
  end

  collection.order(field_name => direction)
end