提交 a60770d3 编写于 作者: S Sean Griffin

Remove `klass` and `arel_table` as a dependency of `PredicateBuilder`

This class cares far too much about the internals of other parts of
Active Record. This is an attempt to break out a meaningful object which
represents the needs of the predicate builder. I'm not fully satisfied
with the name, but the general concept is an object which represents a
table, the associations to/from that table, and the types associated
with it. Many of these exist at the `ActiveRecord::Base` class level,
not as properties of the table itself, hence the need for another
object. Currently it provides these by holding a reference to the class,
but that will likely change in the future. This allows the predicate
builder to remain wholy concerned with building predicates.

/cc @mrgilman
上级 35362817
......@@ -62,6 +62,7 @@ module ActiveRecord
autoload :Serialization
autoload :StatementCache
autoload :Store
autoload :TableMetadata
autoload :Timestamp
autoload :Transactions
autoload :Translation
......
......@@ -43,7 +43,7 @@ def join_constraints(foreign_table, foreign_klass, node, join_type, tables, scop
constraint = build_constraint(klass, table, key, foreign_table, foreign_key)
predicate_builder = PredicateBuilder.new(klass, table)
predicate_builder = PredicateBuilder.new(TableMetadata.new(klass, table))
scope_chain_items = scope_chain[scope_chain_index].map do |item|
if item.is_a?(Relation)
item
......
......@@ -249,12 +249,12 @@ def arel_engine # :nodoc:
end
def predicate_builder # :nodoc:
@predicate_builder ||= PredicateBuilder.new(self, arel_table)
@predicate_builder ||= PredicateBuilder.new(table_metadata)
end
private
def relation #:nodoc:
def relation # :nodoc:
relation = Relation.create(self, arel_table, predicate_builder)
if finder_needs_type_condition?
......@@ -263,6 +263,10 @@ def relation #:nodoc:
relation
end
end
def table_metadata # :nodoc:
TableMetadata.new(self, arel_table)
end
end
# New objects can be instantiated as either empty (pass no construction parameter) or pre-set with
......
......@@ -8,8 +8,9 @@ class PredicateBuilder # :nodoc:
require 'active_record/relation/predicate_builder/range_handler'
require 'active_record/relation/predicate_builder/relation_handler'
def initialize(klass, table)
@klass = klass
delegate :resolve_column_aliases, to: :table
def initialize(table)
@table = table
@handlers = []
......@@ -22,16 +23,6 @@ def initialize(klass, table)
register_handler(AssociationQueryValue, AssociationQueryHandler.new(self))
end
def resolve_column_aliases(hash)
hash = hash.dup
hash.keys.grep(Symbol) do |key|
if klass.attribute_alias? key
hash[klass.attribute_alias(key)] = hash.delete key
end
end
hash
end
def build_from_hash(attributes)
attributes = convert_dot_notation_to_hash(attributes.stringify_keys)
expand_from_hash(attributes)
......@@ -43,11 +34,11 @@ def expand(column, value)
#
# For polymorphic relationships, find the foreign key and type:
# PriceEstimate.where(estimate_of: treasure)
if klass && reflection = klass._reflect_on_association(column)
value = AssociationQueryValue.new(reflection, value)
if table.associated_with?(column)
value = AssociationQueryValue.new(table.associated_table(column), value)
end
build(table[column], value)
build(table.arel_attribute(column), value)
end
def self.references(attributes)
......@@ -82,17 +73,14 @@ def build(attribute, value)
protected
attr_reader :klass, :table
attr_reader :table
def expand_from_hash(attributes)
return ["1=0"] if attributes.empty?
attributes.flat_map do |key, value|
if value.is_a?(Hash)
arel_table = Arel::Table.new(key)
association = klass._reflect_on_association(key)
builder = self.class.new(association && association.klass, arel_table)
builder = self.class.new(table.associated_table(key))
builder.expand_from_hash(value)
else
expand(key, value)
......
......@@ -8,11 +8,12 @@ def initialize(predicate_builder)
def call(attribute, value)
queries = {}
table = value.associated_table
if value.base_class
queries[value.association.foreign_type] = value.base_class.name
queries[table.association_foreign_type] = value.base_class.name
end
queries[value.association.foreign_key] = value.ids
queries[table.association_foreign_key] = value.ids
predicate_builder.build_from_hash(queries)
end
......@@ -22,10 +23,10 @@ def call(attribute, value)
end
class AssociationQueryValue # :nodoc:
attr_reader :association, :value
attr_reader :associated_table, :value
def initialize(association, value)
@association = association
def initialize(associated_table, value)
@associated_table = associated_table
@value = value
end
......@@ -34,7 +35,7 @@ def ids
end
def base_class
if association.polymorphic?
if associated_table.polymorphic_association?
@base_class ||= polymorphic_base_class_from_value
end
end
......
......@@ -88,7 +88,7 @@ def expand_hash_conditions_for_aggregates(attrs)
# # => "address_street='123 abc st.' and address_city='chicago'"
def sanitize_sql_hash_for_conditions(attrs, default_table_name = self.table_name)
table = Arel::Table.new(table_name).alias(default_table_name)
predicate_builder = PredicateBuilder.new(self, table)
predicate_builder = PredicateBuilder.new(TableMetadata.new(self, table))
ActiveSupport::Deprecation.warn(<<-EOWARN)
sanitize_sql_hash_for_conditions is deprecated, and will be removed in Rails 5.0
EOWARN
......
module ActiveRecord
class TableMetadata # :nodoc:
delegate :foreign_type, :foreign_key, to: :association, prefix: true
def initialize(klass, arel_table, association = nil)
@klass = klass
@arel_table = arel_table
@association = association
end
def resolve_column_aliases(hash)
hash = hash.dup
hash.keys.grep(Symbol) do |key|
if klass.attribute_alias? key
hash[klass.attribute_alias(key)] = hash.delete key
end
end
hash
end
def arel_attribute(column_name)
arel_table[column_name]
end
def associated_with?(association_name)
klass && klass._reflect_on_association(association_name)
end
def associated_table(table_name)
arel_table = Arel::Table.new(table_name)
association = klass._reflect_on_association(table_name)
if association && !association.polymorphic?
klass = association.klass
end
TableMetadata.new(klass, arel_table, association)
end
def polymorphic_association?
association && association.polymorphic?
end
protected
attr_reader :klass, :arel_table, :association
end
end
......@@ -1663,7 +1663,8 @@ def test_presence
test 'using a custom table affects the wheres' do
table_alias = Post.arel_table.alias('omg_posts')
predicate_builder = ActiveRecord::PredicateBuilder.new(Post, table_alias)
table_metadata = ActiveRecord::TableMetadata.new(Post, table_alias)
predicate_builder = ActiveRecord::PredicateBuilder.new(table_metadata)
relation = ActiveRecord::Relation.new(Post, table_alias, predicate_builder)
relation.where!(:foo => "bar")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册