Class: ActiveRecord::PredicateBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/relation/predicate_builder.rb,
lib/active_record/relation/predicate_builder/array_handler.rb,
lib/active_record/relation/predicate_builder/range_handler.rb,
lib/active_record/relation/predicate_builder/relation_handler.rb,
lib/active_record/relation/predicate_builder/basic_object_handler.rb,
lib/active_record/relation/predicate_builder/association_query_value.rb,
lib/active_record/relation/predicate_builder/polymorphic_array_value.rb

Overview

:nodoc:

Defined Under Namespace

Classes: ArrayHandler, AssociationQueryValue, BasicObjectHandler, PolymorphicArrayValue, RangeHandler, RelationHandler

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ PredicateBuilder

Returns a new instance of PredicateBuilder.



12
13
14
15
16
17
18
19
20
21
# File 'lib/active_record/relation/predicate_builder.rb', line 12

def initialize(table)
  @table = table
  @handlers = []

  register_handler(BasicObject, BasicObjectHandler.new(self))
  register_handler(Range, RangeHandler.new(self))
  register_handler(Relation, RelationHandler.new)
  register_handler(Array, ArrayHandler.new(self))
  register_handler(Set, ArrayHandler.new(self))
end

Class Method Details

.references(attributes) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/active_record/relation/predicate_builder.rb', line 28

def self.references(attributes)
  attributes.each_with_object([]) do |(key, value), result|
    if value.is_a?(Hash)
      result << Arel.sql(key, retryable: true)
    elsif (idx = key.rindex("."))
      result << Arel.sql(key[0, idx], retryable: true)
    end
  end
end

Instance Method Details

#[](attr_name, value, operator = nil) ⇒ Object



53
54
55
# File 'lib/active_record/relation/predicate_builder.rb', line 53

def [](attr_name, value, operator = nil)
  build(table.arel_table[attr_name], value, operator)
end

#build(attribute, value, operator = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/active_record/relation/predicate_builder.rb', line 57

def build(attribute, value, operator = nil)
  value = value.id if value.respond_to?(:id)
  if operator ||= table.type(attribute.name).force_equality?(value) && :eq
    bind = build_bind_attribute(attribute.name, value)
    attribute.public_send(operator, bind)
  else
    handler_for(value).call(attribute, value)
  end
end

#build_bind_attribute(column_name, value) ⇒ Object



67
68
69
# File 'lib/active_record/relation/predicate_builder.rb', line 67

def build_bind_attribute(column_name, value)
  Relation::QueryAttribute.new(column_name, value, table.type(column_name))
end

#build_from_hash(attributes, &block) ⇒ Object



23
24
25
26
# File 'lib/active_record/relation/predicate_builder.rb', line 23

def build_from_hash(attributes, &block)
  attributes = convert_dot_notation_to_hash(attributes)
  expand_from_hash(attributes, &block)
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.new("users").register_handler(MyCustomDateRange, handler)


49
50
51
# File 'lib/active_record/relation/predicate_builder.rb', line 49

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

#resolve_arel_attribute(table_name, column_name, &block) ⇒ Object



71
72
73
# File 'lib/active_record/relation/predicate_builder.rb', line 71

def resolve_arel_attribute(table_name, column_name, &block)
  table.associated_table(table_name, &block).arel_table[column_name]
end