From 249d7af066c229c5291b68a42c2985252be3aa18 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Sun, 11 Apr 2010 12:51:31 +1000 Subject: [PATCH] Use to_param rather than id when using custom methods --- .../lib/active_resource/custom_methods.rb | 3 +-- activeresource/test/base/custom_methods_test.rb | 8 ++++++++ .../test/fixtures/person_with_to_param.rb | 5 +++++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 activeresource/test/fixtures/person_with_to_param.rb diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb index 4647e83..65d1e6d 100644 --- a/activeresource/lib/active_resource/custom_methods.rb +++ b/activeresource/lib/active_resource/custom_methods.rb @@ -106,10 +106,9 @@ module ActiveResource connection.delete(custom_method_element_url(method_name, options), self.class.headers) end - private def custom_method_element_url(method_name, options = {}) - "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}.#{self.class.format.extension}#{self.class.__send__(:query_string, options)}" + "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{to_param}/#{method_name}.#{self.class.format.extension}#{self.class.__send__(:query_string, options)}" end def custom_method_new_element_url(method_name, options = {}) diff --git a/activeresource/test/base/custom_methods_test.rb b/activeresource/test/base/custom_methods_test.rb index 61887f4..dc91923 100644 --- a/activeresource/test/base/custom_methods_test.rb +++ b/activeresource/test/base/custom_methods_test.rb @@ -14,6 +14,7 @@ class CustomMethodsTest < Test::Unit::TestCase ActiveResource::HttpMock.respond_to do |mock| mock.get "/people/1.xml", {}, @matz mock.get "/people/1/shallow.xml", {}, @matz + mock.get "/people/Matz/shallow.xml", {}, @matz mock.get "/people/1/deep.xml", {}, @matz_deep mock.get "/people/retrieve.xml?name=Matz", {}, @matz_array mock.get "/people/managers.xml", {}, @matz_array @@ -58,6 +59,13 @@ class CustomMethodsTest < Test::Unit::TestCase # Nested resource assert_equal ActiveResource::Response.new("", 204, {}), StreetAddress.put(:sort, :person_id => 1, :by => 'name') end + + def test_to_param_for_custom_elements + person = Person.find(1) + person.stubs(:to_param).returns("Matz") + assert_equal person.send(:custom_method_element_url, :shallow), "/people/Matz/shallow.xml" + assert_equal person.get(:shallow), {"id" => 1, "name" => 'Matz'} + end def test_custom_element_method # Test GET against an element URL diff --git a/activeresource/test/fixtures/person_with_to_param.rb b/activeresource/test/fixtures/person_with_to_param.rb new file mode 100644 index 0000000..15d5f6f --- /dev/null +++ b/activeresource/test/fixtures/person_with_to_param.rb @@ -0,0 +1,5 @@ +class Person < ActiveResource::Base + def to_param + name + end +end -- 1.6.4.1