Module: Filterable::Datable::Range

Defined in:
lib/filterable/datable/range.rb

Overview

Keeps rows whose datable attribute falls within an inclusive range. Reads filters[<attribute>][from] and filters[<attribute>][to], applied only when both bounds are present and parseable.

Class Method Summary collapse

Class Method Details

.call(params, scope) ⇒ ActiveRecord::Relation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Narrows the scope to rows within each declared attribute's inclusive +from+/+to+ range.

Parameters:

  • params (Hash, ActionController::Parameters)

    the nested filters params.

  • scope (ActiveRecord::Relation)

    the relation being filtered.

Returns:

  • (ActiveRecord::Relation)

    the narrowed relation.



17
18
19
20
21
22
23
24
25
26
# File 'lib/filterable/datable/range.rb', line 17

def call(params, scope)
  Filterable::Datable.columns(params, scope).reduce(scope) do |sub_scope, (column, bounds)|
    from = Filterable::Datable.parse(bounds[:from])
    to = Filterable::Datable.parse(bounds[:to])
    next sub_scope unless from && to

    field = scope.arel_table[column]
    sub_scope.where(field.gteq(from)).where(field.lteq(to))
  end
end