From 1c07b84df95e932d50376c1d0a13585b2e2ef868 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 23 Dec 2010 13:36:25 +0000 Subject: If a has_many goes :through a belongs_to, and the foreign key of the belongs_to changes, then the has_many should be considered stale. --- .../lib/active_record/associations/has_one_through_association.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activerecord/lib/active_record/associations/has_one_through_association.rb') diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index e8cf73976b..eb17935d6a 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -33,6 +33,7 @@ module ActiveRecord private def find_target + update_stale_state scoped.first end end -- cgit v1.2.3 From 3eef0977e15d74518673e0bb3a9305cb41682dac Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 23 Dec 2010 19:53:52 +0000 Subject: Use the through association proxy for operations on the through record, so that those operations are automatically scoped and therefore construct_join_attributes does not need to use construct_owner_attributes. --- .../associations/has_one_through_association.rb | 24 ++++++++++++---------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'activerecord/lib/active_record/associations/has_one_through_association.rb') diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index eb17935d6a..2558941476 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -13,20 +13,22 @@ module ActiveRecord private - def create_through_record(new_value) #nodoc: - klass = @reflection.through_reflection.klass + def create_through_record(new_value) + proxy = @owner.send(@reflection.through_reflection.name) || + @owner.send(:association_instance_get, @reflection.through_reflection.name) + record = proxy.target - current_object = @owner.send(@reflection.through_reflection.name) - - if current_object - new_value ? current_object.update_attributes(construct_join_attributes(new_value)) : current_object.destroy + if record && !new_value + record.destroy elsif new_value - if @owner.new_record? - self.target = new_value - through_association = @owner.send(:association_instance_get, @reflection.through_reflection.name) - through_association.build(construct_join_attributes(new_value)) + attributes = construct_join_attributes(new_value) + + if record + record.update_attributes(attributes) + elsif @owner.new_record? + proxy.build(attributes) else - @owner.send(@reflection.through_reflection.name, klass.create(construct_join_attributes(new_value))) + proxy.create(attributes) end end end -- cgit v1.2.3 From 99db97a32237876f65468aa116441a621b80cc01 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Thu, 23 Dec 2010 19:54:43 +0000 Subject: Remove pointless use of 'private' --- .../lib/active_record/associations/has_one_through_association.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activerecord/lib/active_record/associations/has_one_through_association.rb') diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index 2558941476..8c8c9fbe5d 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -33,7 +33,6 @@ module ActiveRecord end end - private def find_target update_stale_state scoped.first -- cgit v1.2.3 From b0498372a10bda006350af42708a5588ab28ffcb Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 26 Dec 2010 19:37:35 +0000 Subject: Add a HasAssociation module for common code for has_* associations --- .../lib/active_record/associations/has_one_through_association.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/associations/has_one_through_association.rb') diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index 8c8c9fbe5d..c9ae930e93 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -1,10 +1,8 @@ -require "active_record/associations/through_association_scope" - module ActiveRecord # = Active Record Has One Through Association module Associations class HasOneThroughAssociation < HasOneAssociation - include ThroughAssociationScope + include ThroughAssociation def replace(new_value) create_through_record(new_value) -- cgit v1.2.3 From a0be389d39b790e0625339251d2674b8250b16b1 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 2 Jan 2011 14:28:53 +0000 Subject: Allow assignment on has_one :through where the owner is a new record [#5137 state:resolved] This required changing the code to keep the association proxy for a belongs_to around, despite its target being nil. Which in turn required various changes to the way that stale target checking is handled, in order to support various edge cases (loaded target is nil then foreign key added, foreign key is changed and then changed back, etc). A side effect is that the code is nicer and more succinct. Note that I am removing test_no_unexpected_aliasing since that is basically checking that the proxy for a belongs_to *does* change, which is the exact opposite of the intention of this commit. Also adding various tests for various edge cases and related things. Phew, long commit message! --- .../lib/active_record/associations/has_one_through_association.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/associations/has_one_through_association.rb') diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index c9ae930e93..11fa40a5c4 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -7,6 +7,7 @@ module ActiveRecord def replace(new_value) create_through_record(new_value) @target = new_value + loaded end private @@ -32,7 +33,6 @@ module ActiveRecord end def find_target - update_stale_state scoped.first end end -- cgit v1.2.3 From 4754018272d576590693e5418936db23c7d13172 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 9 Jan 2011 16:45:22 +0000 Subject: find_target can be inherited --- .../lib/active_record/associations/has_one_through_association.rb | 4 ---- 1 file changed, 4 deletions(-) (limited to 'activerecord/lib/active_record/associations/has_one_through_association.rb') diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index 11fa40a5c4..ab8543e4af 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -31,10 +31,6 @@ module ActiveRecord end end end - - def find_target - scoped.first - end end end end -- cgit v1.2.3 From 42b2e4f85bef64b7aa1382e96c79db1d4f318a56 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 9 Jan 2011 18:33:51 +0000 Subject: We can use the association_proxy method directly in HasOneThroughAssociation now --- .../lib/active_record/associations/has_one_through_association.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/associations/has_one_through_association.rb') diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index ab8543e4af..59a704b7bf 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -13,9 +13,8 @@ module ActiveRecord private def create_through_record(new_value) - proxy = @owner.send(@reflection.through_reflection.name) || - @owner.send(:association_instance_get, @reflection.through_reflection.name) - record = proxy.target + proxy = @owner.send(:association_proxy, @reflection.through_reflection.name) + record = proxy.send(:load_target) if record && !new_value record.destroy -- cgit v1.2.3 From 8aedd722e119770908eb3956ad91f4c88f425eb5 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 16 Jan 2011 19:51:50 +0000 Subject: Use self.target= in HasOneThroughAssociation too --- .../lib/active_record/associations/has_one_through_association.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record/associations/has_one_through_association.rb') diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index 59a704b7bf..6c455c8716 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -4,10 +4,9 @@ module ActiveRecord class HasOneThroughAssociation < HasOneAssociation include ThroughAssociation - def replace(new_value) - create_through_record(new_value) - @target = new_value - loaded + def replace(record) + create_through_record(record) + self.target = record end private -- cgit v1.2.3 From c4458b36024a20abacc39c069444bb0ac202778a Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 16 Jan 2011 19:53:40 +0000 Subject: Rename some variables --- .../associations/has_one_through_association.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'activerecord/lib/active_record/associations/has_one_through_association.rb') diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index 6c455c8716..dcd74e7346 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -11,21 +11,21 @@ module ActiveRecord private - def create_through_record(new_value) - proxy = @owner.send(:association_proxy, @reflection.through_reflection.name) - record = proxy.send(:load_target) + def create_through_record(record) + through_proxy = @owner.send(:association_proxy, @reflection.through_reflection.name) + through_record = through_proxy.send(:load_target) - if record && !new_value - record.destroy - elsif new_value - attributes = construct_join_attributes(new_value) + if through_record && !record + through_record.destroy + elsif record + attributes = construct_join_attributes(record) - if record - record.update_attributes(attributes) + if through_record + through_record.update_attributes(attributes) elsif @owner.new_record? - proxy.build(attributes) + through_proxy.build(attributes) else - proxy.create(attributes) + through_proxy.create(attributes) end end end -- cgit v1.2.3 From d85ee50eda88fc1d486140b63d5c6826b7f3671b Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 24 Jan 2011 19:32:34 +0000 Subject: Indent methods under private/protected sections --- .../associations/has_one_through_association.rb | 28 +++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'activerecord/lib/active_record/associations/has_one_through_association.rb') diff --git a/activerecord/lib/active_record/associations/has_one_through_association.rb b/activerecord/lib/active_record/associations/has_one_through_association.rb index dcd74e7346..69771afe50 100644 --- a/activerecord/lib/active_record/associations/has_one_through_association.rb +++ b/activerecord/lib/active_record/associations/has_one_through_association.rb @@ -11,24 +11,24 @@ module ActiveRecord private - def create_through_record(record) - through_proxy = @owner.send(:association_proxy, @reflection.through_reflection.name) - through_record = through_proxy.send(:load_target) + def create_through_record(record) + through_proxy = @owner.send(:association_proxy, @reflection.through_reflection.name) + through_record = through_proxy.send(:load_target) - if through_record && !record - through_record.destroy - elsif record - attributes = construct_join_attributes(record) + if through_record && !record + through_record.destroy + elsif record + attributes = construct_join_attributes(record) - if through_record - through_record.update_attributes(attributes) - elsif @owner.new_record? - through_proxy.build(attributes) - else - through_proxy.create(attributes) + if through_record + through_record.update_attributes(attributes) + elsif @owner.new_record? + through_proxy.build(attributes) + else + through_proxy.create(attributes) + end end end - end end end end -- cgit v1.2.3