diff options
-rw-r--r-- | activeresource/CHANGELOG | 8 | ||||
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 21 | ||||
-rw-r--r-- | activeresource/test/base_test.rb | 30 | ||||
-rw-r--r-- | activeresource/test/fixtures/beast.rb | 4 |
4 files changed, 45 insertions, 18 deletions
diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index 679fb832d4..1079156fdc 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,18 +1,18 @@ *SVN* -* Allow subclassed resources to share the site info [Rick] +* Allow subclassed resources to share the site info [Rick, Jeremy Kemper] class BeastResource < ActiveResource::Base self.site = 'http://beast.caboo.se' end - + class Forum < BeastResource # taken from BeastResource # self.site = 'http://beast.caboo.se' end - + class Topic < BeastResource - site << '/forums/:forum_id' + self.site += '/forums/:forum_id' end * Fix issues with ActiveResource collection handling. Closes #6291. [bmilekic] diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index fd583d40e9..9a810e4adf 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -6,13 +6,14 @@ module ActiveResource # calls. cattr_accessor :logger - def self.inherited(base) - base.site = site.to_s if site - super - end - class << self - attr_reader :site + def site + if defined?(@site) + @site + elsif superclass != Object and superclass.site + superclass.site.dup.freeze + end + end def site=(site) @site = create_site_uri_from(site) @@ -83,13 +84,9 @@ module ActiveResource def find_single(scope, options) new(connection.get(element_path(scope, options)), options) end - + def create_site_uri_from(site) - returning site.is_a?(URI) ? site : URI.parse(site) do |uri| - def uri.<<(extra) - path << extra - end unless uri.respond_to?(:<<) - end + site.is_a?(URI) ? site.dup : URI.parse(site) end end diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index e0bbdc00bf..ad799806e5 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -43,6 +43,36 @@ class BaseTest < Test::Unit::TestCase assert_equal site, Person.site end + def test_site_reader_uses_superclass_site_until_written + # Superclass is Object so returns nil. + assert_nil ActiveResource::Base.site + assert_nil Class.new(ActiveResource::Base).site + + # Subclass uses superclass site. + actor = Class.new(Person) + assert_equal Person.site, actor.site + + # Subclass returns frozen superclass copy. + assert !Person.site.frozen? + assert actor.site.frozen? + + # Changing subclass site doesn't change superclass site. + actor.site = 'http://localhost:31337' + assert_not_equal Person.site, actor.site + + # Changed subclass site is not frozen. + assert !actor.site.frozen? + + # Changing superclass site doesn't overwrite subclass site. + Person.site = 'http://somewhere.else' + assert_not_equal Person.site, actor.site + + # Changing superclass site after subclassing changes subclass site. + jester = Class.new(actor) + actor.site = 'http://nomad' + assert_equal actor.site, jester.site + assert jester.site.frozen? + end def test_collection_name assert_equal "people", Person.collection_name diff --git a/activeresource/test/fixtures/beast.rb b/activeresource/test/fixtures/beast.rb index 1fe81da425..e31ec58346 100644 --- a/activeresource/test/fixtures/beast.rb +++ b/activeresource/test/fixtures/beast.rb @@ -10,5 +10,5 @@ class Forum < BeastResource end class Topic < BeastResource - site << '/forums/:forum_id' -end
\ No newline at end of file + self.site += '/forums/:forum_id' +end |