From 51a181e2c6da3edf97fb9d0d0788c7af3b176121 Mon Sep 17 00:00:00 2001 From: Ryan Bigg Date: Sun, 11 Apr 2010 13:04:46 +1000 Subject: [PATCH] use to_param rather than hardcoding id - #1717 --- .../lib/active_resource/custom_methods.rb | 2 +- .../test/cases/base/custom_methods_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletions(-) diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb index dd3e35d..f3712ca 100644 --- a/activeresource/lib/active_resource/custom_methods.rb +++ b/activeresource/lib/active_resource/custom_methods.rb @@ -108,7 +108,7 @@ module ActiveResource 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/cases/base/custom_methods_test.rb b/activeresource/test/cases/base/custom_methods_test.rb index 2d81549..77f59b1 100644 --- a/activeresource/test/cases/base/custom_methods_test.rb +++ b/activeresource/test/cases/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/Matz/shallow.xml", {}, @matz mock.get "/people/1/shallow.xml", {}, @matz mock.get "/people/1/deep.xml", {}, @matz_deep mock.get "/people/retrieve.xml?name=Matz", {}, @matz_array @@ -77,6 +78,13 @@ class CustomMethodsTest < Test::Unit::TestCase assert_equal ActiveResource::Response.new("", 204, {}), StreetAddress.find(1, :params => { :person_id => 1 }).put(:normalize_phone, :locale => 'US') 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_new_element_method # Test POST against a new element URL -- 1.6.4.1