Class: ActiveRecord::PredicateBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/relation/predicate_builder.rb,
lib/active_record/relation/predicate_builder/base_handler.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, BaseHandler, BasicObjectHandler, PolymorphicArrayValue, RangeHandler, RelationHandler

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ PredicateBuilder

Returns a new instance of PredicateBuilder.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/active_record/relation/predicate_builder.rb', line 7

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

  register_handler(BasicObject, BasicObjectHandler.new(self))
  register_handler(Base, BaseHandler.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



24
25
26
27
28
29
30
31
32
33
# File 'lib/active_record/relation/predicate_builder.rb', line 24

def self.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

Instance Method Details

#build(attribute, value) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/active_record/relation/predicate_builder.rb', line 50

def build(attribute, value)
  if table.type(attribute.name).force_equality?(value)
    bind = build_bind_attribute(attribute.name, value)
    attribute.eq(bind)
  else
    handler_for(value).call(attribute, value)
  end
end

#build_bind_attribute(column_name, value) ⇒ Object



59
60
61
62
# File 'lib/active_record/relation/predicate_builder.rb', line 59

def build_bind_attribute(column_name, value)
  attr = Relation::QueryAttribute.new(column_name.to_s, value, table.type(column_name))
  Arel::Nodes::BindParam.new(attr)
end

#build_from_hash(attributes) ⇒ Object



19
20
21
22
# File 'lib/active_record/relation/predicate_builder.rb', line 19

def build_from_hash(attributes)
  attributes = convert_dot_notation_to_hash(attributes)
  expand_from_hash(attributes)
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)


46
47
48
# File 'lib/active_record/relation/predicate_builder.rb', line 46

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