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_handler.rb,
lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb

Overview

:nodoc:

Defined Under Namespace

Classes: ArrayHandler, AssociationQueryHandler, AssociationQueryValue, BaseHandler, BasicObjectHandler, PolymorphicArrayHandler, PolymorphicArrayValue, RangeHandler, RelationHandler

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ PredicateBuilder

Returns a new instance of PredicateBuilder.



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

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

  register_handler(BasicObject, BasicObjectHandler.new)
  register_handler(Base, BaseHandler.new(self))
  register_handler(Range, RangeHandler.new)
  register_handler(RangeHandler::RangeWithBinds, RangeHandler.new)
  register_handler(Relation, RelationHandler.new)
  register_handler(Array, ArrayHandler.new(self))
  register_handler(AssociationQueryValue, AssociationQueryHandler.new(self))
  register_handler(PolymorphicArrayValue, PolymorphicArrayHandler.new(self))
end

Class Method Details

.references(attributes) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/active_record/relation/predicate_builder.rb', line 37

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

Instance Method Details

#build(attribute, value) ⇒ Object



63
64
65
# File 'lib/active_record/relation/predicate_builder.rb', line 63

def build(attribute, value)
  handler_for(value).call(attribute, value)
end

#build_from_hash(attributes) ⇒ Object



27
28
29
30
# File 'lib/active_record/relation/predicate_builder.rb', line 27

def build_from_hash(attributes)
  attributes = convert_dot_notation_to_hash(attributes)
  expand_from_hash(attributes)
end

#create_binds(attributes) ⇒ Object



32
33
34
35
# File 'lib/active_record/relation/predicate_builder.rb', line 32

def create_binds(attributes)
  attributes = convert_dot_notation_to_hash(attributes)
  create_binds_for_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)


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

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