Module: Ardm::ActiveRecord::Query::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#expand_hash_conditions_for_aggregates(options) ⇒ Object

hook into query engine in the most general way possible



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ardm/active_record/query.rb', line 30

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



70
71
72
# File 'lib/ardm/active_record/query.rb', line 70

def first_or_create(find_params)
  all(find_params).first_or_create
end

#first_or_create!(find_params) ⇒ Object



74
75
76
# File 'lib/ardm/active_record/query.rb', line 74

def first_or_create!(find_params)
  all(find_params).first_or_create!
end

#first_or_initialize(find_params) ⇒ Object



78
79
80
# File 'lib/ardm/active_record/query.rb', line 78

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

#get(id) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/ardm/active_record/query.rb', line 54

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



62
63
64
65
66
67
68
# File 'lib/ardm/active_record/query.rb', line 62

def get!(id)
  if Array === id && id.size == 1
    # Model#key returns an array
    id = id.first
  end
  find(id)
end