module Ardm::Ar::Query::ClassMethods

Public Instance Methods

execute_sql(sql) click to toggle source
# File lib/ardm/ar/query.rb, line 29
def execute_sql(sql)
  connection.execute(sql)
end
expand_hash_conditions_for_aggregates(options) click to toggle source

hook into query engine in the most general way possible

Calls superclass method
# File lib/ardm/ar/query.rb, line 34
def expand_hash_conditions_for_aggregates(options)
  complex, simple = options.partition {|k,v| Ardm::Query::Operator === k }
  result = super(Hash[simple]) # send simple all at once to save computation
  complex.each do |(operator, value)|
    expanded_opts = super(operator.target => value)

    if expanded_opts.size > 1
      $stderr.puts "WARNING: Operator #{operator.target.inspect} on multiple attribute aggregate #{expanded_opts.inspect} might be totally crazyballs."
    end

    expanded_opts.each do |new_key, new_val|
      new_operator = Ardm::Query::Operator.new(new_key, operator.operator)
      result[new_operator] = new_val
    end
  end

  # This hack allows access to the original class from within the PredicateBuilder (so hax)
  class << result
    attr_accessor :klass
  end
  result.klass = self
  result
end
first_or_create(find_params) click to toggle source
# File lib/ardm/ar/query.rb, line 74
def first_or_create(find_params)
  all(find_params).first_or_create
end
first_or_create!(find_params) click to toggle source
# File lib/ardm/ar/query.rb, line 78
def first_or_create!(find_params)
  all(find_params).first_or_create!
end
first_or_initialize(find_params) click to toggle source
# File lib/ardm/ar/query.rb, line 82
def first_or_initialize(find_params)
  all(find_params).first_or_initialize
end
get(id) click to toggle source
# File lib/ardm/ar/query.rb, line 58
def get(id)
  if Array === id && id.size == 1
    # Model#key returns an array
    id = id.first
  end
  where(primary_key => id).first
end
get!(id) click to toggle source
# File lib/ardm/ar/query.rb, line 66
def get!(id)
  if Array === id && id.size == 1
    # Model#key returns an array
    id = id.first
  end
  find(id)
end