From 22671e1e8e47d2106744cc6e0f1ab8e58bc4fcba Mon Sep 17 00:00:00 2001 From: Rasik Pandey Date: Tue, 12 Aug 2008 16:21:17 -0400 Subject: [PATCH] loading/reloading perserves prefix options --- activeresource/lib/active_resource/base.rb | 8 +++----- activeresource/test/base_test.rb | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 4192fab..e25a8e0 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -599,10 +599,8 @@ module ActiveResource collection.collect! { |record| instantiate_record(record, prefix_options) } end - def instantiate_record(record, prefix_options = {}) - returning new(record) do |resource| - resource.prefix_options = prefix_options - end + def instantiate_record(record,prefix_options = {}) + new(prefix_options.stringify_keys.merge(record)) end @@ -914,7 +912,7 @@ module ActiveResource # my_branch.reload # my_branch.name # => "Wilson Road" def reload - self.load(self.class.find(to_param, :params => @prefix_options).attributes) + self.load(@prefix_options.stringify_keys.merge(self.class.find(to_param, :params => @prefix_options).attributes)) end # A method to manually load attributes from a \hash. Recursively loads collections of diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index d37a6d4..97b5a99 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -10,6 +10,7 @@ class BaseTest < Test::Unit::TestCase @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') @addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address') + @addy_with_person_id = { :id => 3, :street => '12345 Street', :person_id => 33 }.to_xml(:root => 'address') @default_request_headers = { 'Content-Type' => 'application/xml' } @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people') @@ -77,6 +78,9 @@ class BaseTest < Test::Unit::TestCase mock.get "/people/1/addresses/2.xml", {}, nil, 404 mock.get "/people/2/addresses/1.xml", {}, nil, 404 mock.get "/people/Greg/addresses/1.xml", {}, @addy + mock.get "/people/Greg/addresses/1.xml", {}, @addy + mock.get "/people/3/addresses/3.xml", {}, @addy_with_person_id + mock.get "/people/33/addresses/3.xml", {}, @addy_with_person_id mock.put "/people/1/addresses/1.xml", {}, nil, 204 mock.delete "/people/1/addresses/1.xml", {}, nil, 200 mock.post "/people/1/addresses.xml", {}, nil, 201, 'Location' => '/people/1/addresses/5' @@ -638,11 +642,27 @@ class BaseTest < Test::Unit::TestCase address = StreetAddress.find(1, :params => { :person_id => 1 }) ryan = Person.new(:id => 1, :name => 'Ryan', :address => address) assert_equal address.prefix_options, ryan.address.prefix_options + + #test loading a resource with prefix_options in the body + #preserves the prefix_options in the body and not those in the url + address = StreetAddress.find(3, :params => { :person_id => 3 }) + assert_equal 33, address.prefix_options[:person_id] end def test_reload_works_with_prefix_options address = StreetAddress.find(1, :params => { :person_id => 1 }) assert_equal address, address.reload + #and we still have the prefix options that were passed in + assert_not_nil address.prefix_options[:person_id] + assert_not_nil 1, address.prefix_options[:person_id] + + #we test that the prefix option doesn't override the value + #that comes back in the response body with the reload + #this example is contrived, but valid for batch requests + address = StreetAddress.find(3, :params => { :person_id => 3 }) + assert_equal address, address.reload + assert_equal 33, address.prefix_options[:person_id] + assert_not_equal 3, address.prefix_options[:person_id] end def test_reload_with_redefined_to_param @@ -668,6 +688,7 @@ class BaseTest < Test::Unit::TestCase def test_reload_works_without_prefix_options person = Person.find(:first) assert_equal person, person.reload + assert person.prefix_options.empty? end -- 1.5.5.3