Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow subclassed resources to share the site info [Rick]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5717 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 011f4e7413f1bde83aa62a5fc46afd6957ff9b3e 1 parent 2f184c3
@technoweenie technoweenie authored
View
15 activeresource/CHANGELOG
@@ -1,5 +1,20 @@
*SVN*
+* Allow subclassed resources to share the site info [Rick]
+
+ 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'
+ end
+
* Fix issues with ActiveResource collection handling. Closes #6291. [bmilekic]
* Use attr_accessor_with_default to dry up attribute initialization. References #6538. [Stuart Halloway]
View
15 activeresource/lib/active_resource/base.rb
@@ -6,11 +6,16 @@ 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=(site)
- @site = site.is_a?(URI) ? site : URI.parse(site)
+ @site = create_site_uri_from(site)
@connection = nil
@site
end
@@ -78,6 +83,14 @@ 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
+ end
end
attr_accessor :attributes
View
6 activeresource/test/base_test.rb
@@ -1,6 +1,7 @@
require "#{File.dirname(__FILE__)}/abstract_unit"
require "fixtures/person"
require "fixtures/street_address"
+require "fixtures/beast"
class BaseTest < Test::Unit::TestCase
def setup
@@ -171,4 +172,9 @@ def test_destroy_with_custom_prefix
def test_delete
assert Person.delete(1)
end
+
+ def test_should_use_site_prefix_and_credentials
+ 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
end
View
14 activeresource/test/fixtures/beast.rb
@@ -0,0 +1,14 @@
+class BeastResource < ActiveResource::Base
+ self.site = 'http://beast.caboo.se'
+ site.user = 'foo'
+ site.password = 'bar'
+end
+
+class Forum < BeastResource
+ # taken from BeastResource
+ # self.site = 'http://beast.caboo.se'
+end
+
+class Topic < BeastResource
+ site << '/forums/:forum_id'
+end
Please sign in to comment.
Something went wrong with that request. Please try again.