Class: BMC::Filter::ByKeyValue

Inherits:
Object
  • Object
show all
Defined in:
app/filters/bmc/filter/by_key_value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ ByKeyValue

Returns a new instance of ByKeyValue.



4
5
6
7
# File 'app/filters/bmc/filter/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/bmc/filter/by_key_value.rb', line 2

def key
  @key
end

Instance Method Details

#call(query, value) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/filters/bmc/filter/by_key_value.rb', line 9

def call(query, value)
  return query if value == "all"

  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



26
27
28
# File 'app/filters/bmc/filter/by_key_value.rb', line 26

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