Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6587

Show
Ignore:
Timestamp:
04/26/07 19:38:16 (1 year ago)
Author:
david
Message:

Added support for using classes from within a single nested module [DHH]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activeresource/CHANGELOG

    r6586 r6587  
    11*SVN* 
     2 
     3* Added support for using classes from within a single nested module [DHH]. Example: 
     4 
     5    module Highrise 
     6      class Note < ActiveResource::Base 
     7        self.site = "http://37s.sunrise.i:3000" 
     8      end 
     9 
     10      class Comment < ActiveResource::Base 
     11        self.site = "http://37s.sunrise.i:3000" 
     12      end 
     13    end 
     14 
     15  assert_kind_of Highrise::Comment, Note.find(1).comments.first 
     16     
    217 
    318* Added load_attributes_from_response as a way of loading attributes from other responses than just create [DHH] 
  • trunk/activeresource/lib/active_resource/base.rb

    r6586 r6587  
    113113      end 
    114114 
    115       # Core method for finding resources.  Used similarly to ActiveRecord's find method. 
    116       #  Person.find(1) # => GET /people/1.xml 
     115      # Core method for finding resources.  Used similarly to Active Record's find method. 
     116      #  Person.find(1)                     # => GET /people/1.xml 
     117      #  Person.find(:all)                  # => GET /people.xml 
     118      #  Person.find(:all, :title => "CEO") # => GET /people.xml?title=CEO 
     119      #  Person.find(:managers)             # => GET /people/managers.xml 
    117120      #  StreetAddress.find(1, :person_id => 1) # => GET /people/1/street_addresses/1.xml 
    118121      def find(*arguments) 
     
    291294      # Update the resource on the remote service. 
    292295      def update 
    293         connection.put(element_path(prefix_options), to_xml) 
     296        returning connection.put(element_path(prefix_options), to_xml) do |response| 
     297          load_attributes_from_response(response) 
     298        end 
    294299      end 
    295300 
     
    330335      def find_or_create_resource_for(name) 
    331336        resource_name = name.to_s.camelize 
    332         self.class.const_get(resource_name) 
     337 
     338        # FIXME: Make it generic enough to support any depth of module nesting 
     339        if (ancestors = self.class.name.split("::")).size > 1 
     340          ancestors.first.constantize.const_get(resource_name) 
     341        else 
     342          self.class.const_get(resource_name) 
     343        end 
    333344      rescue NameError 
    334345        resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base)) 
  • trunk/activeresource/test/base/load_test.rb

    r5714 r6587  
    22require "fixtures/person" 
    33require "fixtures/street_address" 
     4 
     5module Highrise 
     6  class Note < ActiveResource::Base 
     7    self.site = "http://37s.sunrise.i:3000" 
     8  end 
     9 
     10  class Comment < ActiveResource::Base 
     11    self.site = "http://37s.sunrise.i:3000" 
     12  end 
     13end 
     14 
    415 
    516class BaseLoadTest < Test::Unit::TestCase 
     
    93104    assert_equal @matz[:id], rivers.last.rafted_by.id 
    94105  end 
     106   
     107  def test_nested_collections_within_the_same_namespace 
     108    n = Highrise::Note.new(:comments => [{ :name => "1" }]) 
     109    assert_kind_of Highrise::Comment, n.comments.first 
     110  end 
    95111end