Class: Agilibox::SmallData::FilterStrategyByKeyValue

Inherits:
FilterStrategy
  • Object
show all
Defined in:
app/filters/agilibox/small_data/filter_strategy_by_key_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ FilterStrategyByKeyValue

Returns a new instance of FilterStrategyByKeyValue.



4
5
6
7
# File 'app/filters/agilibox/small_data/filter_strategy_by_key_value.rb', line 4

def initialize(key = nil)
  super()
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



2
3
4
# File 'app/filters/agilibox/small_data/filter_strategy_by_key_value.rb', line 2

def key
  @key
end

Instance Method Details

#apply(query, value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/filters/agilibox/small_data/filter_strategy_by_key_value.rb', line 9

def apply(query, value)
  value = true  if value == "true"
  value = false if value == "false"

  column = column_for(query)

  if value.to_s.in?(%w(nil null))
    query.where("#{column} IS NULL")
  elsif value.to_s.in?(%w(not_nil not_null))
    query.where("#{column} IS NOT NULL")
  else
    query.where("#{column} = ?", value)
  end
end

#column_for(query) ⇒ Object



24
25
26
# File 'app/filters/agilibox/small_data/filter_strategy_by_key_value.rb', line 24

def column_for(query)
  key.is_a?(Symbol) ? "#{query.model.table_name}.#{key}" : key.to_s
end