diff options
-rw-r--r-- | activeresource/CHANGELOG | 2 | ||||
-rw-r--r-- | activeresource/lib/active_resource.rb | 2 | ||||
-rw-r--r-- | activeresource/lib/active_resource/custom_methods.rb | 6 | ||||
-rw-r--r-- | activeresource/test/format_test.rb | 34 |
4 files changed, 40 insertions, 4 deletions
diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index 69b387d9b9..ebabbf59c0 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Support agnostic formats when calling custom methods. Closes #10635 [joerichsen] + * Document custom methods. #10589 [Cheah Chu Yeow] * Ruby 1.9 compatibility. [Jeremy Kemper] diff --git a/activeresource/lib/active_resource.rb b/activeresource/lib/active_resource.rb index 96f2257a04..18347457aa 100644 --- a/activeresource/lib/active_resource.rb +++ b/activeresource/lib/active_resource.rb @@ -27,7 +27,7 @@ $:.unshift(File.dirname(__FILE__)) unless unless defined?(ActiveSupport) begin $:.unshift(File.dirname(__FILE__) + "/../../activesupport/lib") - require 'active_support' + require 'active_support' rescue LoadError require 'rubygems' gem 'activesupport' diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb index 4f5697fcf1..70e7ac9ab6 100644 --- a/activeresource/lib/active_resource/custom_methods.rb +++ b/activeresource/lib/active_resource/custom_methods.rb @@ -80,7 +80,7 @@ module ActiveResource module ClassMethods def custom_method_collection_url(method_name, options = {}) prefix_options, query_options = split_options(options) - "#{prefix(prefix_options)}#{collection_name}/#{method_name}.xml#{query_string(query_options)}" + "#{prefix(prefix_options)}#{collection_name}/#{method_name}.#{format.extension}#{query_string(query_options)}" end end @@ -108,11 +108,11 @@ module ActiveResource private def custom_method_element_url(method_name, options = {}) - "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}.xml#{self.class.send!(:query_string, options)}" + "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}.#{self.class.format.extension}#{self.class.send!(:query_string, options)}" end def custom_method_new_element_url(method_name, options = {}) - "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}.xml#{self.class.send!(:query_string, options)}" + "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}.#{self.class.format.extension}#{self.class.send!(:query_string, options)}" end end end diff --git a/activeresource/test/format_test.rb b/activeresource/test/format_test.rb index 609030250e..c2d3a08682 100644 --- a/activeresource/test/format_test.rb +++ b/activeresource/test/format_test.rb @@ -29,6 +29,40 @@ class FormatTest < Test::Unit::TestCase end end + def test_formats_on_custom_collection_method + for format in [ :json, :xml ] + using_format(Person, format) do + ActiveResource::HttpMock.respond_to.get "/people/retrieve.#{format}?name=David", {}, ActiveResource::Formats[format].encode([@david]) + remote_programmers = Person.get(:retrieve, :name => 'David') + assert_equal 1, remote_programmers.size + assert_equal @david[:id], remote_programmers[0]['id'] + assert_equal @david[:name], remote_programmers[0]['name'] + end + end + end + + def test_formats_on_custom_element_method + for format in [ :json, :xml ] + using_format(Person, format) do + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/people/2.#{format}", {}, ActiveResource::Formats[format].encode(@david) + mock.get "/people/2/shallow.#{format}", {}, ActiveResource::Formats[format].encode(@david) + end + remote_programmer = Person.find(2).get(:shallow) + assert_equal @david[:id], remote_programmer['id'] + assert_equal @david[:name], remote_programmer['name'] + end + end + + for format in [ :json, :xml ] + ryan = ActiveResource::Formats[format].encode({ :name => 'Ryan' }) + using_format(Person, format) do + ActiveResource::HttpMock.respond_to.post "/people/new/register.#{format}", {}, ryan, 201, 'Location' => "/people/5.#{format}" + remote_ryan = Person.new(:name => 'Ryan') + assert_equal ActiveResource::Response.new(ryan, 201, {'Location' => "/people/5.#{format}"}), remote_ryan.post(:register) + end + end + end private def using_format(klass, mime_type_reference) |