diff options
author | eileencodes <eileencodes@gmail.com> | 2014-11-04 13:58:15 -0500 |
---|---|---|
committer | eileencodes <eileencodes@gmail.com> | 2015-01-02 17:15:30 -0500 |
commit | 076682692cd363765e6e5235c691474a071a01de (patch) | |
tree | 5933c5c5bf5795a1a2dd02afa47800e3b390e6dd | |
parent | 69d05ae3e52fa382748982ad06c0d0a1014ebc91 (diff) | |
download | rails-076682692cd363765e6e5235c691474a071a01de.tar.gz rails-076682692cd363765e6e5235c691474a071a01de.tar.bz2 rails-076682692cd363765e6e5235c691474a071a01de.zip |
Refactor construct_tables method
Move method structure into reflection classes for accessibly on each
reflection rather than by traversing the chain.
-rw-r--r-- | activerecord/lib/active_record/associations/association_scope.rb | 46 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 10 |
2 files changed, 20 insertions, 36 deletions
diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 95c4cf2500..4907d1cf6c 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -63,22 +63,12 @@ module ActiveRecord private - def construct_tables(chain, klass, refl, alias_tracker) + def construct_tables(chain, alias_tracker, name) chain.map do |reflection| - alias_tracker.aliased_table_for( - table_name_for(reflection, klass, refl), - table_alias_for(reflection, refl, reflection != refl), - type_caster: klass.type_caster, - ) + reflection.alias_name(name, alias_tracker) end end - def table_alias_for(reflection, refl, join = false) - name = "#{reflection.plural_name}_#{alias_suffix(refl)}" - name << "_join" if join - name - end - def join(table, constraint) table.create_join(table, table.create_on(constraint), join_type) end @@ -145,14 +135,7 @@ module ActiveRecord end def table_name - if @reflection.polymorphic? - # If this is a polymorphic belongs_to, we want to get the klass from the - # association because it depends on the polymorphic_type attribute of - # the owner - klass.table_name - else - @reflection.table_name - end + klass.table_name end def plural_name @@ -174,6 +157,12 @@ module ActiveRecord def source_type_info @reflection.source_type_info end + + def alias_name(name, alias_tracker) + alias_name = "#{plural_name}_#{name}_join" + table_name = klass.table_name + alias_tracker.aliased_table_for(table_name, alias_name) + end end def get_chain(reflection, association) @@ -183,7 +172,7 @@ module ActiveRecord end def add_constraints(scope, owner, assoc_klass, refl, tracker, chain) - tables = construct_tables(chain, assoc_klass, refl, tracker) + tables = construct_tables(chain, tracker, refl.name) owner_reflection = chain.last table = tables.last @@ -225,21 +214,6 @@ module ActiveRecord scope end - def alias_suffix(refl) - refl.name - end - - def table_name_for(reflection, klass, refl) - if reflection == refl - # If this is a polymorphic belongs_to, we want to get the klass from the - # association because it depends on the polymorphic_type attribute of - # the owner - klass.table_name - else - reflection.table_name - end - end - def eval_scope(klass, scope, owner) klass.unscoped.instance_exec(owner, &scope) end diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 1bc92b1587..83771bd5dc 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -165,6 +165,11 @@ module ActiveRecord def constraints scope ? [scope] : [] end + + def alias_name(name, alias_tracker) + alias_name = "#{plural_name}_#{name}" + alias_tracker.aliased_table_for(table_name, alias_name) + end end # Base class for AggregateReflection and AssociationReflection. Objects of @@ -753,6 +758,11 @@ module ActiveRecord source_type = @prev_reflection.options[:source_type] lambda { |object| where(type => source_type) } end + + def alias_name(name, alias_tracker) + alias_name = "#{plural_name}_#{name}" + alias_tracker.aliased_table_for(table_name, alias_name) + end end # Consider the following example: |