Class: ActiveRecord::PredicateBuilder
- Inherits:
-
Object
- Object
- ActiveRecord::PredicateBuilder
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/class_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
Defined Under Namespace
Classes: ArrayHandler, AssociationQueryHandler, AssociationQueryValue, BaseHandler, BasicObjectHandler, ClassHandler, PolymorphicArrayHandler, PolymorphicArrayValue, RangeHandler, RelationHandler
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of PredicateBuilder.
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/active_record/relation/predicate_builder.rb', line 14
def initialize(table)
@table = table
@handlers = []
register_handler(BasicObject, BasicObjectHandler.new(self))
register_handler(Class, ClassHandler.new(self))
register_handler(Base, BaseHandler.new(self))
register_handler(Range, RangeHandler.new(self))
register_handler(RangeHandler::RangeWithBinds, RangeHandler.new(self))
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
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/active_record/relation/predicate_builder.rb', line 49
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
75
76
77
|
# File 'lib/active_record/relation/predicate_builder.rb', line 75
def build(attribute, value)
handler_for(value).call(attribute, value)
end
|
#build_from_hash(attributes) ⇒ Object
29
30
31
32
|
# File 'lib/active_record/relation/predicate_builder.rb', line 29
def build_from_hash(attributes)
attributes = convert_dot_notation_to_hash(attributes)
expand_from_hash(attributes)
end
|
#create_binds(attributes) ⇒ Object
34
35
36
37
|
# File 'lib/active_record/relation/predicate_builder.rb', line 34
def create_binds(attributes)
attributes = convert_dot_notation_to_hash(attributes)
create_binds_for_hash(attributes)
end
|
#expand(column, value) ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'lib/active_record/relation/predicate_builder.rb', line 39
def expand(column, value)
value = AssociationQueryHandler.value_for(table, column, value) if table.associated_with?(column)
build(table.arel_attribute(column), value)
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)
71
72
73
|
# File 'lib/active_record/relation/predicate_builder.rb', line 71
def register_handler(klass, handler)
@handlers.unshift([klass, handler])
end
|