Skip to content

Commit

Permalink
Subclasses share superclass site until explicitly set. This way you c…
Browse files Browse the repository at this point in the history
…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
jeremy committed Dec 21, 2006
1 parent 93c816f commit e6988ef
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
8 changes: 4 additions & 4 deletions activeresource/CHANGELOG
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
21 changes: 9 additions & 12 deletions activeresource/lib/active_resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions activeresource/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions activeresource/test/fixtures/beast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ class Forum < BeastResource
end

class Topic < BeastResource
site << '/forums/:forum_id'
end
self.site += '/forums/:forum_id'
end

0 comments on commit e6988ef

Please sign in to comment.