Sha256: 871f0cfc853f477d30f7ba0310660930eacd3016dd684bf9879f688bb13f17bb

Contents?: true

Size: 617 Bytes

Versions: 6

Compression:

Stored size: 617 Bytes

Contents

class BMC::Filter::ByKeyValue
  attr_reader :key

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

  def apply(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

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
bmc-1.4.3 app/filters/bmc/filter/by_key_value.rb
bmc-1.4.2 app/filters/bmc/filter/by_key_value.rb
bmc-1.4.1 app/filters/bmc/filter/by_key_value.rb
bmc-1.4.0 app/filters/bmc/filter/by_key_value.rb
bmc-1.3.5 app/filters/bmc/filter/by_key_value.rb
bmc-1.3.4 app/filters/bmc/filter/by_key_value.rb