diff options
-rw-r--r-- | activeresource/CHANGELOG | 15 | ||||
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 19 | ||||
-rw-r--r-- | activeresource/test/base/load_test.rb | 18 |
3 files changed, 47 insertions, 5 deletions
diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index 07c48505d1..e740af59be 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,5 +1,20 @@ *SVN* +* Added support for using classes from within a single nested module [DHH]. Example: + + module Highrise + class Note < ActiveResource::Base + self.site = "http://37s.sunrise.i:3000" + end + + class Comment < ActiveResource::Base + self.site = "http://37s.sunrise.i:3000" + end + end + + assert_kind_of Highrise::Comment, Note.find(1).comments.first + + * Added load_attributes_from_response as a way of loading attributes from other responses than just create [DHH] class Highrise::Task < ActiveResource::Base diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 4635a5009e..3f7121a7ca 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -112,8 +112,11 @@ module ActiveResource returning(self.new(attributes)) { |res| res.save } end - # Core method for finding resources. Used similarly to ActiveRecord's find method. - # Person.find(1) # => GET /people/1.xml + # Core method for finding resources. Used similarly to Active Record's find method. + # Person.find(1) # => GET /people/1.xml + # Person.find(:all) # => GET /people.xml + # Person.find(:all, :title => "CEO") # => GET /people.xml?title=CEO + # Person.find(:managers) # => GET /people/managers.xml # StreetAddress.find(1, :person_id => 1) # => GET /people/1/street_addresses/1.xml def find(*arguments) scope = arguments.slice!(0) @@ -290,7 +293,9 @@ module ActiveResource # Update the resource on the remote service. def update - connection.put(element_path(prefix_options), to_xml) + returning connection.put(element_path(prefix_options), to_xml) do |response| + load_attributes_from_response(response) + end end # Create (i.e., save to the remote service) the new resource. @@ -329,7 +334,13 @@ module ActiveResource # Tries to find a resource for a given name; if it fails, then the resource is created def find_or_create_resource_for(name) resource_name = name.to_s.camelize - self.class.const_get(resource_name) + + # FIXME: Make it generic enough to support any depth of module nesting + if (ancestors = self.class.name.split("::")).size > 1 + ancestors.first.constantize.const_get(resource_name) + else + self.class.const_get(resource_name) + end rescue NameError resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base)) resource.prefix = self.class.prefix diff --git a/activeresource/test/base/load_test.rb b/activeresource/test/base/load_test.rb index 6d2f65e1c5..28f1cfc97c 100644 --- a/activeresource/test/base/load_test.rb +++ b/activeresource/test/base/load_test.rb @@ -2,6 +2,17 @@ require "#{File.dirname(__FILE__)}/../abstract_unit" require "fixtures/person" require "fixtures/street_address" +module Highrise + class Note < ActiveResource::Base + self.site = "http://37s.sunrise.i:3000" + end + + class Comment < ActiveResource::Base + self.site = "http://37s.sunrise.i:3000" + end +end + + class BaseLoadTest < Test::Unit::TestCase def setup @matz = { :id => 1, :name => 'Matz' } @@ -92,4 +103,9 @@ class BaseLoadTest < Test::Unit::TestCase assert_equal @deep[:street][:state][:notable_rivers].first[:id], rivers.first.id assert_equal @matz[:id], rivers.last.rafted_by.id end -end + + def test_nested_collections_within_the_same_namespace + n = Highrise::Note.new(:comments => [{ :name => "1" }]) + assert_kind_of Highrise::Comment, n.comments.first + end +end
\ No newline at end of file |