From 80d539bd4daa180e0c792b3878b2250c9913c437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20L=C3=BCtke?= Date: Wed, 27 Jun 2007 16:00:07 +0000 Subject: Fixes that using a subclass of an ARes object would cache a connection object based of its parent's site variable. Changing the parent's site would have no effect on the descentent objects. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7143 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activeresource/lib/active_resource/base.rb | 10 +++++++--- activeresource/test/base_test.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 0255eefc55..d33195e261 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -164,15 +164,19 @@ module ActiveResource # The site variable is required ActiveResource's mapping to work. def site=(site) @connection = nil - @site = create_site_uri_from(site) + @site = site.nil? ? nil : create_site_uri_from(site) end # An instance of ActiveResource::Connection that is the base connection to the remote service. # The +refresh+ parameter toggles whether or not the connection is refreshed at every request # or not (defaults to +false+). def connection(refresh = false) - @connection = Connection.new(site) if refresh || @connection.nil? - @connection + if defined?(@connection) or superclass == Object + @connection = Connection.new(site) if refresh || @connection.nil? + @connection + else + superclass.connection + end end def headers diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index 48b1045b61..2c6b7c648f 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -54,6 +54,14 @@ class BaseTest < Test::Unit::TestCase assert_equal 'http://foo:bar@beast.caboo.se', Forum.site.to_s assert_equal 'http://foo:bar@beast.caboo.se/forums/:forum_id', Topic.site.to_s end + + def test_site_variable_can_be_reset + actor = Class.new(ActiveResource::Base) + assert_nil actor.site + actor.site = 'http://localhost:31337' + actor.site = nil + assert_nil actor.site + end def test_site_reader_uses_superclass_site_until_written # Superclass is Object so returns nil. @@ -84,6 +92,28 @@ class BaseTest < Test::Unit::TestCase actor.site = 'http://nomad' assert_equal actor.site, jester.site assert jester.site.frozen? + + # Subclasses are always equal to superclass site when not overridden + fruit = Class.new(ActiveResource::Base) + apple = Class.new(fruit) + + fruit.site = 'http://market' + assert_equal fruit.site, apple.site, 'subclass did not adopt changes to parent class' + + fruit.site = 'http://supermarket' + assert_equal fruit.site, apple.site, 'subclass did not adopt changes to parent class' + end + + def test_updating_baseclass_site_object_wipes_descendent_cached_connection_objects + # Subclasses are always equal to superclass site when not overridden + fruit = Class.new(ActiveResource::Base) + apple = Class.new(fruit) + + fruit.site = 'http://market' + assert_equal fruit.connection.site, apple.connection.site + + fruit.site = 'http://supermarket' + assert_equal fruit.connection.site, apple.connection.site end def test_collection_name -- cgit v1.2.3