Module: Ardm::ActiveRecord::PredicateBuilder::Rails3::ClassMethods

Defined in:
lib/ardm/active_record/predicate_builder/rails3.rb

Instance Method Summary collapse

Instance Method Details

#build_from_hash(klass, attributes, default_table) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ardm/active_record/predicate_builder/rails3.rb', line 41

def build_from_hash(klass, attributes, default_table)
  queries = []
  klass = attributes.klass # HAX (this method is added to the attributes hash by expand_hash_conditions_for_aggregates

  attributes.each do |column, value|
    table = default_table

    if value.is_a?(Hash)
      if value.empty?
        queries << '1=0'
      else
        table       = Arel::Table.new(column, default_table.engine)
        association = klass.reflect_on_association(column.to_sym)

        value.each do |k, v|
          queries.concat expand(association && association.klass, table, k, v)
        end
      end
    else
      if Ardm::Query::Operator === column
        original = column
        operator = column.operator
        column   = column.target.to_s
      else
        column = column.to_s
      end

      if column.include?('.')
        table_name, column = column.split('.', 2)
        table = Arel::Table.new(table_name, default_table.engine)
      end

      query = expand(klass, table, column, value)
      # TODO make nicer
      if operator == :not
        # Logical not factorization !(a && b) == (!a || !b)
        query.map! &:not
        query = [query.inject { |composite, predicate| composite.or(predicate) }]
      end
      queries.concat query
    end
  end

  queries
end

#expand(klass, table, column, value) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ardm/active_record/predicate_builder/rails3.rb', line 87

def expand(klass, table, column, value)
  queries = []

  # Find the foreign key when using queries such as:
  # Post.where(author: author)
  #
  # For polymorphic relationships, find the foreign key and type:
  # PriceEstimate.where(estimate_of: treasure)
  if klass && reflection = klass.reflect_on_association(column.to_sym)
    column = reflection.foreign_key
    if value.is_a?(Base) && reflection.respond_to?(:polymorphic?) && reflection.polymorphic?
      queries << build(table[reflection.foreign_type], value.class.base_class)
    end
  end

  #puts "expand(#{klass.name}, #{column.inspect}, #{value.inspect})"

  queries << build(table[column], value)
  queries
end

#references(attributes) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/ardm/active_record/predicate_builder/rails3.rb', line 108

def references(attributes)
  attributes.map do |key, value|
    if value.is_a?(Hash)
      key
    else
      key = key.to_s
      key.split('.').first if key.include?('.')
    end
  end.compact
end

#register_handler(klass, handler) ⇒ Object

Define how a class is converted to Arel nodes when passed to where. The handler can be any object that responds to call, and will be used for any value that === the class given. For example:

MyCustomDateRange = Struct.new(:start, :end)
handler = proc do |column, range|
  Arel::Nodes::Between.new(column,
    Arel::Nodes::And.new([range.start, range.end])
  )
end
ActiveRecord::PredicateBuilder.register_handler(MyCustomDateRange, handler)


130
131
132
# File 'lib/ardm/active_record/predicate_builder/rails3.rb', line 130

def register_handler(klass, handler)
  handlers.unshift([klass, handler])
end

#resolve_column_aliases(klass, hash) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/ardm/active_record/predicate_builder/rails3.rb', line 31

def resolve_column_aliases(klass, hash)
  hash = hash.dup
  hash.keys.grep(Symbol) do |key|
    if klass.attribute_alias? key
      hash[klass.attribute_alias(key)] = hash.delete key
    end
  end
  hash
end