diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-09-07 08:37:58 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-09-07 08:37:58 +0900 |
commit | 0a1112fc910005023fb8764d6b86ae448b449b22 (patch) | |
tree | 4ed0db1264b3f99f6f2d82a86ff133bb89eff30c /activerecord/lib/active_record | |
parent | 89dc8e1ff87d86c4253fa6dcd0002eb64fa4e922 (diff) | |
download | rails-0a1112fc910005023fb8764d6b86ae448b449b22.tar.gz rails-0a1112fc910005023fb8764d6b86ae448b449b22.tar.bz2 rails-0a1112fc910005023fb8764d6b86ae448b449b22.zip |
Don't pass `table` to `last_chain_scope` and `next_chain_scope`
Because `table` is part of `reflection`, don't need to pass it
explicitly. And also, naming `alias_name` to `table` is a little
confusing. `aliased_table` is preferable than `alias_name`.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/association_scope.rb | 32 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 4 |
2 files changed, 17 insertions, 19 deletions
diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 4220a1499a..ae3767466d 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -62,11 +62,12 @@ module ActiveRecord table.create_join(table, table.create_on(constraint), join_type) end - def last_chain_scope(scope, table, reflection, owner) + def last_chain_scope(scope, reflection, owner) join_keys = reflection.join_keys key = join_keys.key foreign_key = join_keys.foreign_key + table = reflection.aliased_table value = transform_value(owner[foreign_key]) scope = apply_scope(scope, table, key, value) @@ -82,11 +83,13 @@ module ActiveRecord value_transformation.call(value) end - def next_chain_scope(scope, table, reflection, foreign_table, next_reflection) + def next_chain_scope(scope, reflection, next_reflection) join_keys = reflection.join_keys key = join_keys.key foreign_key = join_keys.foreign_key + table = reflection.aliased_table + foreign_table = next_reflection.aliased_table constraint = table[key].eq(foreign_table[foreign_key]) if reflection.type @@ -98,11 +101,11 @@ module ActiveRecord end class ReflectionProxy < SimpleDelegator # :nodoc: - attr_reader :alias_name + attr_reader :aliased_table - def initialize(reflection, alias_name) + def initialize(reflection, aliased_table) super(reflection) - @alias_name = alias_name + @aliased_table = aliased_table end def all_includes; nil; end @@ -112,34 +115,29 @@ module ActiveRecord name = reflection.name chain = [Reflection::RuntimeReflection.new(reflection, association)] reflection.chain.drop(1).each do |refl| - alias_name = tracker.aliased_table_for( + aliased_table = tracker.aliased_table_for( refl.table_name, refl.alias_candidate(name), refl.klass.type_caster ) - chain << ReflectionProxy.new(refl, alias_name) + chain << ReflectionProxy.new(refl, aliased_table) end chain end def add_constraints(scope, owner, chain) - owner_reflection = chain.last - table = owner_reflection.alias_name - scope = last_chain_scope(scope, table, owner_reflection, owner) + scope = last_chain_scope(scope, chain.last, owner) chain.each_cons(2) do |reflection, next_reflection| - table = reflection.alias_name - foreign_table = next_reflection.alias_name - scope = next_chain_scope(scope, table, reflection, foreign_table, next_reflection) + scope = next_chain_scope(scope, reflection, next_reflection) end chain_head = chain.first chain.reverse_each do |reflection| - table = reflection.alias_name # Exclude the scope of the association itself, because that # was already merged in the #scope method. reflection.constraints.each do |scope_chain_item| - item = eval_scope(reflection, table, scope_chain_item, owner) + item = eval_scope(reflection, scope_chain_item, owner) if scope_chain_item == chain_head.scope scope.merge! item.except(:where, :includes) @@ -166,8 +164,8 @@ module ActiveRecord end end - def eval_scope(reflection, table, scope, owner) - relation = reflection.build_scope(table) + def eval_scope(reflection, scope, owner) + relation = reflection.build_scope(reflection.aliased_table) relation.instance_exec(owner, &scope) || relation end end diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 7b3bcc33bf..484cbc82f7 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -1070,8 +1070,8 @@ module ActiveRecord @association.klass end - def alias_name - Arel::Table.new(table_name, type_caster: klass.type_caster) + def aliased_table + @aliased_table ||= Arel::Table.new(table_name, type_caster: klass.type_caster) end def all_includes; yield; end |