mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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
This commit is contained in:
parent
2f184c338b
commit
011f4e7413
4 changed files with 49 additions and 1 deletions
|
@ -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]
|
||||
|
|
|
@ -6,11 +6,16 @@ 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=(site)
|
||||
@site = site.is_a?(URI) ? site : URI.parse(site)
|
||||
@site = create_site_uri_from(site)
|
||||
@connection = nil
|
||||
@site
|
||||
end
|
||||
|
@ -78,6 +83,14 @@ 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
|
||||
end
|
||||
end
|
||||
|
||||
attr_accessor :attributes
|
||||
|
|
|
@ -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 @@ class BaseTest < Test::Unit::TestCase
|
|||
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
|
||||
|
|
14
activeresource/test/fixtures/beast.rb
vendored
Normal file
14
activeresource/test/fixtures/beast.rb
vendored
Normal file
|
@ -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
|
Loading…
Reference in a new issue