1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Subclasses share superclass site until explicitly set. This way you can set Superclass.site = ... after subclasses have been defined.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5767 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2006-12-21 22:12:21 +00:00
parent 93c816f0c2
commit e6988ef2ce
4 changed files with 45 additions and 18 deletions

View file

@ -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]

View file

@ -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

View file

@ -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

View file

@ -10,5 +10,5 @@ class Forum < BeastResource
end
class Topic < BeastResource
site << '/forums/:forum_id'
end
self.site += '/forums/:forum_id'
end