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/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
Defined Under Namespace
Classes: ArrayHandler, AssociationQueryValue, BaseHandler, BasicObjectHandler, PolymorphicArrayValue, RangeHandler, RelationHandler
Class Method Summary
collapse
Instance Method Summary
collapse
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(".".freeze).first if key.include?(".".freeze)
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
|