aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2014-11-04 13:58:15 -0500
committereileencodes <eileencodes@gmail.com>2015-01-02 17:15:30 -0500
commit076682692cd363765e6e5235c691474a071a01de (patch)
tree5933c5c5bf5795a1a2dd02afa47800e3b390e6dd /activerecord/lib/active_record/associations
parent69d05ae3e52fa382748982ad06c0d0a1014ebc91 (diff)
downloadrails-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.
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r--activerecord/lib/active_record/associations/association_scope.rb46
1 files changed, 10 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