Module: Filterable::Datable

Defined in:
lib/filterable/datable.rb,
lib/filterable/datable/after.rb,
lib/filterable/datable/range.rb,
lib/filterable/datable/since.rb,
lib/filterable/datable/before.rb

Overview

Namespace for the date filters and the helpers they share.

Defined Under Namespace

Modules: After, Before, Range, Since

Class Method Summary collapse

Class Method Details

.columns(params, scope) ⇒ Hash{Symbol, String => Hash}

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.

Whitelist the params to the declared datable attributes and resolve each one to its DB column, so a filter only ever touches columns the model opted in.

Parameters:

  • params (Hash, ActionController::Parameters)

    the nested filters params.

  • scope (ActiveRecord::Relation)

    the relation being filtered.

Returns:

  • (Hash{Symbol, String => Hash})

    db_column => raw bounds for that attribute.



34
35
36
37
38
39
# File 'lib/filterable/datable.rb', line 34

def columns(params, scope)
  declared = scope.datable_attribute_names
  sliced = params.slice(*declared.keys)
  sliced = sliced.to_unsafe_h if sliced.respond_to?(:to_unsafe_h)
  sliced.transform_keys { |name| declared[name] }
end

.parse(value) ⇒ Date, ...

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.

Coerce a raw filter value into a Date/Time, or nil when it cannot be parsed, so a malformed query param narrows nothing instead of raising.

Parameters:

  • value (Date, Time, String)

    the raw bound from the params.

Returns:

  • (Date, Time, nil)

    the parsed value, or nil when unparseable.



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

def parse(value)
  case value
  when Date, Time
    value
  else
    begin
      Time.parse(value.to_s)
    rescue ArgumentError
      nil
    end
  end
end