Sha256: 7efe188bbca35a7cca2319a4585ff69f32a1412a3d120fcf07556362c0740ab0

Contents?: true

Size: 581 Bytes

Versions: 9

Compression:

Stored size: 581 Bytes

Contents

class BMC::Filter::ByKeyValue
  attr_reader :key

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

  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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
bmc-1.3.3 app/filters/bmc/filter/by_key_value.rb
bmc-1.3.2 app/filters/bmc/filter/by_key_value.rb
bmc-1.3.1 app/filters/bmc/filter/by_key_value.rb
bmc-1.3.0 app/filters/bmc/filter/by_key_value.rb
bmc-1.2.1 app/filters/bmc/filter/by_key_value.rb
bmc-1.2.0 app/filters/bmc/filter/by_key_value.rb
bmc-1.1.0 app/filters/bmc/filter/by_key_value.rb
bmc-1.0.1 app/filters/bmc/filter/by_key_value.rb
bmc-1.0.0 app/filters/bmc/filter/by_key_value.rb