Skip to content
This repository
Browse code

Subclasses share superclass site until explicitly set. This way you c…

…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...
commit e6988ef2ce31553c9c5f52a8d34899e39df67694 1 parent 93c816f
Jeremy Kemper authored December 21, 2006
8  activeresource/CHANGELOG
... ...
@@ -1,18 +1,18 @@
1 1
 *SVN*
2 2
 
3  
-* Allow subclassed resources to share the site info [Rick]
  3
+* Allow subclassed resources to share the site info [Rick, Jeremy Kemper]
4 4
 
5 5
     class BeastResource < ActiveResource::Base
6 6
       self.site = 'http://beast.caboo.se'
7 7
     end
8  
-    
  8
+
9 9
     class Forum < BeastResource
10 10
       # taken from BeastResource
11 11
       # self.site = 'http://beast.caboo.se'
12 12
     end
13  
-    
  13
+
14 14
     class Topic < BeastResource
15  
-      site << '/forums/:forum_id'
  15
+      self.site += '/forums/:forum_id'
16 16
     end
17 17
 
18 18
 * Fix issues with ActiveResource collection handling.  Closes #6291. [bmilekic]
21  activeresource/lib/active_resource/base.rb
@@ -6,13 +6,14 @@ 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  
-
14 9
     class << self
15  
-      attr_reader :site
  10
+      def site
  11
+        if defined?(@site)
  12
+          @site
  13
+        elsif superclass != Object and superclass.site
  14
+          superclass.site.dup.freeze
  15
+        end
  16
+      end
16 17
 
17 18
       def site=(site)
18 19
         @site = create_site_uri_from(site)
@@ -83,13 +84,9 @@ def find_every(options)
83 84
         def find_single(scope, options)
84 85
           new(connection.get(element_path(scope, options)), options)
85 86
         end
86  
-      
  87
+
87 88
         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
  89
+          site.is_a?(URI) ? site.dup : URI.parse(site)
93 90
         end
94 91
     end
95 92
 
30  activeresource/test/base_test.rb
@@ -43,6 +43,36 @@ def test_site_accessor_accepts_uri_or_string_argument
43 43
     assert_equal site, Person.site
44 44
   end
45 45
 
  46
+  def test_site_reader_uses_superclass_site_until_written
  47
+    # Superclass is Object so returns nil.
  48
+    assert_nil ActiveResource::Base.site
  49
+    assert_nil Class.new(ActiveResource::Base).site
  50
+
  51
+    # Subclass uses superclass site.
  52
+    actor = Class.new(Person)
  53
+    assert_equal Person.site, actor.site
  54
+
  55
+    # Subclass returns frozen superclass copy.
  56
+    assert !Person.site.frozen?
  57
+    assert actor.site.frozen?
  58
+
  59
+    # Changing subclass site doesn't change superclass site.
  60
+    actor.site = 'http://localhost:31337'
  61
+    assert_not_equal Person.site, actor.site
  62
+
  63
+    # Changed subclass site is not frozen.
  64
+    assert !actor.site.frozen?
  65
+
  66
+    # Changing superclass site doesn't overwrite subclass site.
  67
+    Person.site = 'http://somewhere.else'
  68
+    assert_not_equal Person.site, actor.site
  69
+
  70
+    # Changing superclass site after subclassing changes subclass site.
  71
+    jester = Class.new(actor)
  72
+    actor.site = 'http://nomad'
  73
+    assert_equal actor.site, jester.site
  74
+    assert jester.site.frozen?
  75
+  end
46 76
 
47 77
   def test_collection_name
48 78
     assert_equal "people", Person.collection_name
4  activeresource/test/fixtures/beast.rb
@@ -10,5 +10,5 @@ class Forum < BeastResource
10 10
 end
11 11
 
12 12
 class Topic < BeastResource
13  
-  site << '/forums/:forum_id'
14  
-end
  13
+  self.site += '/forums/:forum_id'
  14
+end

0 notes on commit e6988ef

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