From 277b53fa7a3af93a0cadd61ff28788f26cf5f106 Mon Sep 17 00:00:00 2001 From: Javier Saldana Date: Thu, 25 Apr 2013 18:28:31 -0500 Subject: [PATCH 1/2] Ensure belongs_to finder method sends a non-nil key --- lib/active_resource/associations.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/active_resource/associations.rb b/lib/active_resource/associations.rb index 7d8f9078ec..1ae3a0f125 100644 --- a/lib/active_resource/associations.rb +++ b/lib/active_resource/associations.rb @@ -14,7 +14,7 @@ module Builder # === Options # [:class_name] # Specify the class name of the association. This class name would - # be used for resolving the association class. + # be used for resolving the association class. # # ==== Example for [:class_name] - option # GET /posts/123.json delivers following response body: @@ -48,7 +48,7 @@ def has_many(name, options = {}) # === Options # [:class_name] # Specify the class name of the association. This class name would - # be used for resolving the association class. + # be used for resolving the association class. # # ==== Example for [:class_name] - option # GET /posts/1.json delivers following response body: @@ -74,7 +74,7 @@ def has_one(name, options = {}) end # Specifies a one-to-one association with another class. This class should only be used - # if this class contains the foreign key. + # if this class contains the foreign key. # # Methods will be added for retrieval and query for a single associated object, for which # this object holds an id: @@ -83,7 +83,7 @@ def has_one(name, options = {}) # Returns the associated object. +nil+ is returned if the foreign key is +nil+. # Throws a ActiveResource::ResourceNotFound exception if the foreign key is not +nil+ # and the resource is not found. - # + # # (+association+ is replaced with the symbol passed as the first argument, so # belongs_to :post would add among others post.nil?. # @@ -130,8 +130,8 @@ def defines_belongs_to_finder_method(method_name, association_model, finder_key) instance_variable_get(ivar_name) elsif attributes.include?(method_name) attributes[method_name] - else - instance_variable_set(ivar_name, association_model.find(send(finder_key))) + elsif association_id = send(finder_key) + instance_variable_set(ivar_name, association_model.find(association_id)) end end end @@ -149,7 +149,7 @@ def defines_has_many_finder_method(method_name, association_model) end end end - + # Defines the has_one association def defines_has_one_finder_method(method_name, association_model) ivar_name = :"@#{method_name}" From 59568acef86fe3c39f39f7382ba043f1a3b6968e Mon Sep 17 00:00:00 2001 From: Javier Saldana Date: Tue, 25 Jun 2013 22:35:08 -0500 Subject: [PATCH 2/2] Add belongs_to tests for nil finder key case --- test/cases/association_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/cases/association_test.rb b/test/cases/association_test.rb index 8ab284116a..e8f8c91f13 100644 --- a/test/cases/association_test.rb +++ b/test/cases/association_test.rb @@ -58,4 +58,22 @@ def test_defines_belongs_to_finder_method_with_instance_variable_cache 2.times{person.customer} assert person.instance_variable_defined?(:@customer) end + + def test_belongs_to_with_finder_key + Person.defines_belongs_to_finder_method(:customer, Customer, 'customer_id') + + person = Person.new + person.stubs(:customer_id).returns(1) + Customer.expects(:find).with(1).once() + person.customer + end + + def test_belongs_to_with_nil_finder_key + Person.defines_belongs_to_finder_method(:customer, Customer, 'customer_id') + + person = Person.new + person.stubs(:customer_id).returns(nil) + Customer.expects(:find).with(nil).never() + person.customer + end end