Class: Toller::Filters::ColumnHandler

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

Overview

Column handler for filter

Instance Method Summary collapse

Instance Method Details

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

Applies a plain where clause to collection for a non-scope filter.

If field isn't a real column on the collection's model, the filter is logged and skipped instead of raising once the relation is evaluated.

Parameters:

  • collection (ActiveRecord::Relation)

    the collection to filter

  • type (Symbol)

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

  • value (Object)

    the raw filter param value

  • properties (Hash)

    the filter's properties, used to resolve :field

Returns:

  • (ActiveRecord::Relation)

    the filtered collection, or collection unchanged if :field is unknown



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/toller/filters/column_handler.rb', line 20

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

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

  mutated_value = value_mutator(type, value)

  collection.where(field_name => mutated_value)
end