Class: ActiveRecord::PredicateBuilder
- Inherits:
-
Object
- Object
- ActiveRecord::PredicateBuilder
- Defined in:
- lib/active_record/relation/predicate_builder.rb,
lib/active_record/relation/predicate_builder/array_handler.rb,
lib/active_record/relation/predicate_builder/relation_handler.rb
Overview
:nodoc:
Defined Under Namespace
Classes: ArrayHandler, RelationHandler
Constant Summary collapse
- BASIC_OBJECT_HANDLER =
:nodoc:
->(attribute, value) { attribute.eq(value) }
Class Method Summary collapse
- .build_from_hash(klass, attributes, default_table) ⇒ Object
-
.can_be_bound?(value) ⇒ Boolean
:nodoc:.
- .convert_value_to_association_ids(value, primary_key) ⇒ Object
- .expand(klass, table, column, value) ⇒ Object
- .polymorphic_base_class_from_value(value) ⇒ Object
- .references(attributes) ⇒ Object
-
.register_handler(klass, handler) ⇒ Object
Define how a class is converted to Arel nodes when passed to
where
. - .resolve_column_aliases(klass, hash) ⇒ Object
Class Method Details
.build_from_hash(klass, attributes, default_table) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_record/relation/predicate_builder.rb', line 20 def self.build_from_hash(klass, attributes, default_table) queries = [] attributes.each do |column, value| table = default_table if value.is_a?(Hash) if value.empty? queries << '1=0' else table = Arel::Table.new(column, default_table.engine) association = klass._reflect_on_association(column) value.each do |k, v| queries.concat (association && association.klass, table, k, v) end end else column = column.to_s if column.include?('.') table_name, column = column.split('.', 2) table = Arel::Table.new(table_name, default_table.engine) end queries.concat (klass, table, column, value) end end queries end |
.can_be_bound?(value) ⇒ Boolean
:nodoc:
149 150 151 152 153 |
# File 'lib/active_record/relation/predicate_builder.rb', line 149 def self.can_be_bound?(value) # :nodoc: !value.nil? && !value.is_a?(Hash) && handler_for(value) == BASIC_OBJECT_HANDLER end |
.convert_value_to_association_ids(value, primary_key) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/active_record/relation/predicate_builder.rb', line 136 def self.convert_value_to_association_ids(value, primary_key) case value when Relation value.select(primary_key) when Array value.map { |v| convert_value_to_association_ids(v, primary_key) } when Base value._read_attribute(primary_key) else value end end |
.expand(klass, table, column, value) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/active_record/relation/predicate_builder.rb', line 52 def self.(klass, table, column, value) queries = [] # Find the foreign key when using queries such as: # Post.where(author: author) # # For polymorphic relationships, find the foreign key and type: # PriceEstimate.where(estimate_of: treasure) if klass && reflection = klass._reflect_on_association(column) base_class = polymorphic_base_class_from_value(value) if reflection.polymorphic? && base_class queries << build(table[reflection.foreign_type], base_class) end column = reflection.foreign_key if base_class primary_key = reflection.association_primary_key(base_class) value = convert_value_to_association_ids(value, primary_key) end end queries << build(table[column], value) queries end |
.polymorphic_base_class_from_value(value) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/active_record/relation/predicate_builder.rb', line 79 def self.polymorphic_base_class_from_value(value) case value when Relation value.klass.base_class when Array val = value.compact.first val.class.base_class if val.is_a?(Base) when Base value.class.base_class end end |
.references(attributes) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/active_record/relation/predicate_builder.rb', line 91 def self.references(attributes) attributes.map do |key, value| if value.is_a?(Hash) key else key = key.to_s key.split('.').first if key.include?('.') end end.compact 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.register_handler(MyCustomDateRange, handler)
113 114 115 |
# File 'lib/active_record/relation/predicate_builder.rb', line 113 def self.register_handler(klass, handler) @handlers.unshift([klass, handler]) end |
.resolve_column_aliases(klass, hash) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/active_record/relation/predicate_builder.rb', line 8 def self.resolve_column_aliases(klass, hash) # This method is a hot spot, so for now, use Hash[] to dup the hash. # https://bugs.ruby-lang.org/issues/7166 hash = Hash[hash] hash.keys.grep(Symbol) do |key| if klass.attribute_alias? key hash[klass.attribute_alias(key)] = hash.delete key end end hash end |