Skip to content
This repository
Browse code

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
risk danger olson authored December 13, 2006
15  activeresource/CHANGELOG
... ...
@@ -1,5 +1,20 @@
1 1
 *SVN*
2 2
 
  3
+* Allow subclassed resources to share the site info [Rick]
  4
+
  5
+    class BeastResource < ActiveResource::Base
  6
+      self.site = 'http://beast.caboo.se'
  7
+    end
  8
+    
  9
+    class Forum < BeastResource
  10
+      # taken from BeastResource
  11
+      # self.site = 'http://beast.caboo.se'
  12
+    end
  13
+    
  14
+    class Topic < BeastResource
  15
+      site << '/forums/:forum_id'
  16
+    end
  17
+
3 18
 * Fix issues with ActiveResource collection handling.  Closes #6291. [bmilekic]
4 19
 
5 20
 * Use attr_accessor_with_default to dry up attribute initialization. References #6538. [Stuart Halloway]
15  activeresource/lib/active_resource/base.rb
@@ -6,11 +6,16 @@ class Base
6 6
     # calls.
7 7
     cattr_accessor :logger
8 8
 
  9
+    def self.inherited(base)
  10
+      base.site = site.to_s if site
  11
+      super
  12
+    end
  13
+
9 14
     class << self
10 15
       attr_reader :site
11 16
 
12 17
       def site=(site)
13  
-        @site = site.is_a?(URI) ? site : URI.parse(site)
  18
+        @site = create_site_uri_from(site)
14 19
         @connection = nil
15 20
         @site
16 21
       end
@@ -78,6 +83,14 @@ def find_every(options)
78 83
         def find_single(scope, options)
79 84
           new(connection.get(element_path(scope, options)), options)
80 85
         end
  86
+      
  87
+        def create_site_uri_from(site)
  88
+          returning site.is_a?(URI) ? site : URI.parse(site) do |uri|
  89
+            def uri.<<(extra)
  90
+              path << extra
  91
+            end unless uri.respond_to?(:<<)
  92
+          end
  93
+        end
81 94
     end
82 95
 
83 96
     attr_accessor :attributes
6  activeresource/test/base_test.rb
... ...
@@ -1,6 +1,7 @@
1 1
 require "#{File.dirname(__FILE__)}/abstract_unit"
2 2
 require "fixtures/person"
3 3
 require "fixtures/street_address"
  4
+require "fixtures/beast"
4 5
 
5 6
 class BaseTest < Test::Unit::TestCase
6 7
   def setup
@@ -171,4 +172,9 @@ def test_destroy_with_custom_prefix
171 172
   def test_delete
172 173
     assert Person.delete(1)
173 174
   end
  175
+
  176
+  def test_should_use_site_prefix_and_credentials
  177
+    assert_equal 'http://foo:bar@beast.caboo.se', Forum.site.to_s
  178
+    assert_equal 'http://foo:bar@beast.caboo.se/forums/:forum_id', Topic.site.to_s
  179
+  end
174 180
 end
14  activeresource/test/fixtures/beast.rb
... ...
@@ -0,0 +1,14 @@
  1
+class BeastResource < ActiveResource::Base
  2
+  self.site = 'http://beast.caboo.se'
  3
+  site.user = 'foo'
  4
+  site.password = 'bar'
  5
+end
  6
+
  7
+class Forum < BeastResource
  8
+  # taken from BeastResource
  9
+  # self.site = 'http://beast.caboo.se'
  10
+end
  11
+
  12
+class Topic < BeastResource
  13
+  site << '/forums/:forum_id'
  14
+end

0 notes on commit 011f4e7

Please sign in to comment.
Something went wrong with that request. Please try again.