Permalink
Browse files

Support agnostic formats when calling custom methods. Closes #10635 […

…joerichsen]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8502 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 90d75e5 commit c54b915825b034b431b4b0cdb28f8ca00fca808d @technoweenie technoweenie committed Dec 28, 2007
@@ -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]
@@ -27,7 +27,7 @@
unless defined?(ActiveSupport)
begin
$:.unshift(File.dirname(__FILE__) + "/../../activesupport/lib")
- require 'active_support'
+ require 'active_support'
rescue LoadError
require 'rubygems'
gem 'activesupport'
@@ -80,7 +80,7 @@ def delete(custom_method_name, options = {})
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 @@ def delete(method_name, options = {})
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
@@ -29,6 +29,40 @@ def test_formats_on_collection
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)

0 comments on commit c54b915

Please sign in to comment.