Class: Filtrate::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/filtrate/filter.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope, attr, val) ⇒ Filter

Returns a new instance of Filter.



5
6
7
8
9
10
11
# File 'lib/filtrate/filter.rb', line 5

def initialize(scope, attr, val)
  @scope = scope
  @attr = attr
  @value = val
  @type = @scope.columns_hash[attr].try(:type)
  @col = @scope.table[attr]
end

Instance Method Details

#to_scopeObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/filtrate/filter.rb', line 13

def to_scope
  case @type
  when :string
    @scope.where(@col.matches("%#{@value}%"))
  when :date
    @scope.where(@attr => @value['from']..@value['to'])
  else
    @scope
  end
end