Class: ActiveRecord::PredicateBuilder
- Inherits:
-
Object
- Object
- ActiveRecord::PredicateBuilder
show all
- Defined in:
- activerecord/lib/active_record/relation/predicate_builder.rb,
activerecord/lib/active_record/relation/predicate_builder/array_handler.rb,
activerecord/lib/active_record/relation/predicate_builder/range_handler.rb,
activerecord/lib/active_record/relation/predicate_builder/relation_handler.rb,
activerecord/lib/active_record/relation/predicate_builder/basic_object_handler.rb,
activerecord/lib/active_record/relation/predicate_builder/association_query_value.rb,
activerecord/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb
Overview
Defined Under Namespace
Classes: ArrayHandler, AssociationQueryValue, BasicObjectHandler, PolymorphicArrayValue, RangeHandler, RelationHandler
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of PredicateBuilder.
Class Method Details
.references(attributes) ⇒ Object
28
29
30
31
32
33
34
35
36
|
# File 'activerecord/lib/active_record/relation/predicate_builder.rb', line 28
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
53
54
55
|
# File 'activerecord/lib/active_record/relation/predicate_builder.rb', line 53
def [](attr_name, value, operator = nil)
build(table.arel_table[attr_name], value, operator)
end
|
#build(attribute, value, operator = nil) ⇒ Object
57
58
59
60
61
62
63
64
65
|
# File 'activerecord/lib/active_record/relation/predicate_builder.rb', line 57
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
67
68
69
70
|
# File 'activerecord/lib/active_record/relation/predicate_builder.rb', line 67
def build_bind_attribute(column_name, value)
type = table.type(column_name)
Relation::QueryAttribute.new(column_name, type.immutable_value(value), type)
end
|
#build_from_hash(attributes, &block) ⇒ Object
23
24
25
26
|
# File 'activerecord/lib/active_record/relation/predicate_builder.rb', line 23
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)
49
50
51
|
# File 'activerecord/lib/active_record/relation/predicate_builder.rb', line 49
def register_handler(klass, handler)
@handlers.unshift([klass, handler])
end
|
#resolve_arel_attribute(table_name, column_name, &block) ⇒ Object
72
73
74
|
# File 'activerecord/lib/active_record/relation/predicate_builder.rb', line 72
def resolve_arel_attribute(table_name, column_name, &block)
table.associated_table(table_name, &block).arel_table[column_name]
end
|