diff options
author | Matthew Draper <matthew@trebex.net> | 2014-06-12 01:52:15 +0930 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2014-06-12 01:52:15 +0930 |
commit | e5203391040340a8c3b7959b7850c0e0a1eccb15 (patch) | |
tree | 6cd5d7ad526d5c918cc6e3b02fce579e1a4fd9db /activerecord | |
parent | 70b931f846cb212f3db16f35a10094fb727a57e2 (diff) | |
parent | 5823e429981dc74f8f53187d2ab573823381bf28 (diff) | |
download | rails-e5203391040340a8c3b7959b7850c0e0a1eccb15.tar.gz rails-e5203391040340a8c3b7959b7850c0e0a1eccb15.tar.bz2 rails-e5203391040340a8c3b7959b7850c0e0a1eccb15.zip |
Merge pull request #15630 from eileencodes/refactor-join-keys-on-add_constraints
begin refactoring add_constraints by moving join keys
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/association_scope.rb | 15 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 17 |
2 files changed, 20 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 31108cc1aa..519d4d8651 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -105,18 +105,9 @@ module ActiveRecord chain.each_with_index do |reflection, i| table, foreign_table = tables.shift, tables.first - if reflection.source_macro == :belongs_to - if reflection.polymorphic? - key = reflection.association_primary_key(assoc_klass) - else - key = reflection.association_primary_key - end - - foreign_key = reflection.foreign_key - else - key = reflection.foreign_key - foreign_key = reflection.active_record_primary_key - end + join_keys = reflection.join_keys(assoc_klass) + key = join_keys.key + foreign_key = join_keys.foreign_key if reflection == chain.last bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key], tracker diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 6a098e44ca..95a3c70c93 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -188,6 +188,23 @@ module ActiveRecord active_record == other_aggregation.active_record end + JoinKeys = Struct.new(:key, :foreign_key) # :nodoc: + + def join_keys(assoc_klass) + if source_macro == :belongs_to + if polymorphic? + reflection_key = association_primary_key(assoc_klass) + else + reflection_key = association_primary_key + end + reflection_foreign_key = foreign_key + else + reflection_key = foreign_key + reflection_foreign_key = active_record_primary_key + end + JoinKeys.new(reflection_key, reflection_foreign_key) + end + private def derive_class_name name.to_s.camelize |