Module: Ardm::Ar::Query::ClassMethods

Defined in:
lib/ardm/ar/query.rb

Instance Method Summary collapse

Instance Method Details

#execute_sql(sql) ⇒ Object



29
30
31
# File 'lib/ardm/ar/query.rb', line 29

def execute_sql(sql)
  connection.execute(sql)
end

#expand_hash_conditions_for_aggregates(options) ⇒ Object

hook into query engine in the most general way possible



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



74
75
76
# 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) ⇒ Object



78
79
80
# 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) ⇒ Object



82
83
84
# File 'lib/ardm/ar/query.rb', line 82

def first_or_initialize(find_params)
  all(find_params).first_or_initialize
end

#get(id) ⇒ Object



58
59
60
61
62
63
64
# 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) ⇒ Object



66
67
68
69
70
71
72
# 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