From 1d5c34c2c27370356e8cd1ef478111802b6a5af4 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 27 Apr 2007 20:54:53 +0000 Subject: [PATCH] Added find-by-path options to ActiveResource::Base.find [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6595 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activeresource/CHANGELOG | 6 ++++++ activeresource/lib/active_resource/base.rb | 21 ++++++++++++++------- activeresource/test/abstract_unit.rb | 2 +- activeresource/test/base_test.rb | 15 +++++++++++++++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index e740af59be..403933f7aa 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,5 +1,11 @@ *SVN* +* Added find-by-path options to ActiveResource::Base.find [DHH]. Examples: + + employees = Person.find(:all, :from => "/companies/1/people.xml") # => GET /companies/1/people.xml + manager = Person.find("/companies/1/manager.xml") # => GET /companies/1/manager.xml + + * Added support for using classes from within a single nested module [DHH]. Example: module Highrise diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 3f7121a7ca..bcc1e8b497 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -113,11 +113,13 @@ def create(attributes = {}) end # 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 + # 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 + # Person.find(:all, :from => "/companies/1/people.xml") # => GET /companies/1/people.xml + # Person.find("/companies/1/manager.xml") # => GET /companies/1/manager.xml + # StreetAddress.find(1, :person_id => 1) # => GET /people/1/street_addresses/1.xml def find(*arguments) scope = arguments.slice!(0) options = arguments.slice!(0) || {} @@ -144,8 +146,11 @@ def exists?(id, options = {}) private # Find every resource. def find_every(options) + from = options.delete(:from) prefix_options, query_options = split_options(options) - instantiate_collection(connection.get(collection_path(prefix_options, query_options)) || []) + from ||= collection_path(prefix_options, query_options) + + instantiate_collection(connection.get(from) || []) end def instantiate_collection(collection, prefix_options = {}) @@ -160,7 +165,9 @@ def instantiate_collection(collection, prefix_options = {}) # { :person => person1 } def find_single(scope, options) prefix_options, query_options = split_options(options) - returning new(connection.get(element_path(scope, prefix_options, query_options))) do |resource| + from = scope.to_s.include?("/") ? scope : element_path(scope, prefix_options, query_options) + + returning new(connection.get(from)) do |resource| resource.prefix_options = prefix_options end end diff --git a/activeresource/test/abstract_unit.rb b/activeresource/test/abstract_unit.rb index 574080acc6..ca0d1631e9 100644 --- a/activeresource/test/abstract_unit.rb +++ b/activeresource/test/abstract_unit.rb @@ -8,4 +8,4 @@ $:.unshift "#{File.dirname(__FILE__)}/../test" require 'setter_trap' -ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log") +ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log") \ No newline at end of file diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index 374cb9481a..51e1dd3c04 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -203,6 +203,21 @@ def test_find_by_id_not_found assert_raises(ActiveResource::ResourceNotFound) { StreetAddress.find(1) } end + def test_find_all_by_from + ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, "#{@david}" } + + people = Person.find(:all, :from => "/companies/1/people.xml") + assert_equal 1, people.size + assert_equal "David", people.first.name + end + + def test_find_single_by_from + ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/manager.xml", {}, @david } + + david = Person.find("/companies/1/manager.xml") + assert_equal "David", david.name + end + def test_save rick = Person.new assert_equal true, rick.save