Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Subclasses share superclass site until explicitly set. This way you c…

…an set Superclass.site = ... after subclasses have been defined.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5767 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit e6988ef2ce31553c9c5f52a8d34899e39df67694 1 parent 93c816f
Jeremy Kemper jeremy authored
8 activeresource/CHANGELOG
View
@@ -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]
21 activeresource/lib/active_resource/base.rb
View
@@ -6,13 +6,14 @@ class Base
# 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 @@ def find_every(options)
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
30 activeresource/test/base_test.rb
View
@@ -43,6 +43,36 @@ def test_site_accessor_accepts_uri_or_string_argument
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
4 activeresource/test/fixtures/beast.rb
View
@@ -10,5 +10,5 @@ class Forum < BeastResource
end
class Topic < BeastResource
- site << '/forums/:forum_id'
-end
+ self.site += '/forums/:forum_id'
+end
Please sign in to comment.
Something went wrong with that request. Please try again.