Class: ActiveRecord::Filters::Filter

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/active_record/filters/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#association, #association?, #column_struct, #find_column, #scoped

Constructor Details

#initialize(klass, attributes, options) ⇒ Filter

Returns a new instance of Filter.



10
11
12
13
14
# File 'lib/active_record/filters/filter.rb', line 10

def initialize(klass, attributes, options)
  self.klass      = klass
  self.attributes = attributes
  self.options    = options
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



7
8
9
# File 'lib/active_record/filters/filter.rb', line 7

def attributes
  @attributes
end

#klassObject

Returns the value of attribute klass.



6
7
8
# File 'lib/active_record/filters/filter.rb', line 6

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/active_record/filters/filter.rb', line 8

def options
  @options
end

Instance Method Details

#call(params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_record/filters/filter.rb', line 16

def call(params)
  relation = scoped

  columns.inject(relation) do |relation, column|
    # TODO: have a better way to compare this?
    param = if association? && relation.arel_table != column.relation
              key = "#{association.name}_attributes"
              params[key][column.name] if params[key]
            else
              params[column.name]
    end

    next relation if param.blank?

    column_name = klass.arel_table[column.name]

    case column.type
    when :string
      relation.where(column_name.matches("#{param}%"))
    when :decimal, :float, :integer
      relation.where(column_name.eq(I18n::Alchemy::NumericParser.parse(param)))
    when :time
      # TODO: why arel uses arel/attributes/time for date columns?
      relation.where(column_name.eq(I18n::Alchemy::DateParser.parse(param)))
    else
      relation.where(column_name.eq(param))
    end
  end
end