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/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, BasicObjectHandler, PolymorphicArrayValue, RangeHandler, RelationHandler
Constant Summary
collapse
- BaseHandler =
No-op BaseHandler to work Mashal.load(File.read(“legacy_relation.dump”)). TODO: Remove the constant alias once Rails 6.1 has released.
BasicObjectHandler
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of PredicateBuilder.
Class Method Details
.references(attributes) ⇒ Object
32
33
34
35
36
37
38
39
40
|
# File 'lib/active_record/relation/predicate_builder.rb', line 32
def self.references(attributes)
attributes.each_with_object([]) do |(key, value), result|
if value.is_a?(Hash)
result << Arel.sql(key)
elsif key.include?(".")
result << Arel.sql(key.split(".").first)
end
end
end
|
Instance Method Details
#[](attr_name, value, operator = nil) ⇒ Object
57
58
59
|
# File 'lib/active_record/relation/predicate_builder.rb', line 57
def [](attr_name, value, operator = nil)
build(table.arel_table[attr_name], value, operator)
end
|
#build(attribute, value, operator = nil) ⇒ Object
61
62
63
64
65
66
67
68
69
|
# File 'lib/active_record/relation/predicate_builder.rb', line 61
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
71
72
73
74
|
# File 'lib/active_record/relation/predicate_builder.rb', line 71
def build_bind_attribute(column_name, value)
attr = Relation::QueryAttribute.new(column_name, value, table.type(column_name))
Arel::Nodes::BindParam.new(attr)
end
|
#build_from_hash(attributes, &block) ⇒ Object
27
28
29
30
|
# File 'lib/active_record/relation/predicate_builder.rb', line 27
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)
53
54
55
|
# File 'lib/active_record/relation/predicate_builder.rb', line 53
def register_handler(klass, handler)
@handlers.unshift([klass, handler])
end
|
#resolve_arel_attribute(table_name, column_name, &block) ⇒ Object
76
77
78
|
# File 'lib/active_record/relation/predicate_builder.rb', line 76
def resolve_arel_attribute(table_name, column_name, &block)
table.associated_table(table_name, &block).arel_table[column_name]
end
|