diff options
author | Xavier Noria <fxn@hashref.com> | 2012-02-22 09:00:53 -0800 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2012-02-22 09:00:53 -0800 |
commit | 7f2548e34d79c47ca138ca0378f4a06390c407f1 (patch) | |
tree | 4807b806fe8f3cfe30314fc522599b776e4a6d36 /activeresource | |
parent | f28d9f15482addeb95ab05545b7a8467a4e0182d (diff) | |
parent | 002713c64568114f3754799acc0723ea0d442f7a (diff) | |
download | rails-7f2548e34d79c47ca138ca0378f4a06390c407f1.tar.gz rails-7f2548e34d79c47ca138ca0378f4a06390c407f1.tar.bz2 rails-7f2548e34d79c47ca138ca0378f4a06390c407f1.zip |
Merge pull request #5130 from dlee/revised_patch_verb
Add config.default_method_for_update to support PATCH
Diffstat (limited to 'activeresource')
-rw-r--r-- | activeresource/lib/active_resource/connection.rb | 7 | ||||
-rw-r--r-- | activeresource/lib/active_resource/custom_methods.rb | 16 | ||||
-rw-r--r-- | activeresource/lib/active_resource/http_mock.rb | 6 | ||||
-rw-r--r-- | activeresource/test/cases/format_test.rb | 12 | ||||
-rw-r--r-- | activeresource/test/cases/http_mock_test.rb | 4 |
5 files changed, 32 insertions, 13 deletions
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index 46060b6f74..8a8dc3146d 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -15,6 +15,7 @@ module ActiveResource HTTP_FORMAT_HEADER_NAMES = { :get => 'Accept', :put => 'Content-Type', :post => 'Content-Type', + :patch => 'Content-Type', :delete => 'Accept', :head => 'Accept' } @@ -86,6 +87,12 @@ module ActiveResource with_auth { request(:delete, path, build_request_headers(headers, :delete, self.site.merge(path))) } end + # Executes a PATCH request (see HTTP protocol documentation if unfamiliar). + # Used to update resources. + def patch(path, body = '', headers = {}) + with_auth { request(:patch, path, body.to_s, build_request_headers(headers, :patch, self.site.merge(path))) } + end + # Executes a PUT request (see HTTP protocol documentation if unfamiliar). # Used to update resources. def put(path, body = '', headers = {}) diff --git a/activeresource/lib/active_resource/custom_methods.rb b/activeresource/lib/active_resource/custom_methods.rb index a0eb28ed13..095dcd2a8e 100644 --- a/activeresource/lib/active_resource/custom_methods.rb +++ b/activeresource/lib/active_resource/custom_methods.rb @@ -11,10 +11,10 @@ module ActiveResource # # This route set creates routes for the following HTTP requests: # - # POST /people/new/register.json # PeopleController.register - # PUT /people/1/promote.json # PeopleController.promote with :id => 1 - # DELETE /people/1/deactivate.json # PeopleController.deactivate with :id => 1 - # GET /people/active.json # PeopleController.active + # POST /people/new/register.json # PeopleController.register + # PUT/PATCH /people/1/promote.json # PeopleController.promote with :id => 1 + # DELETE /people/1/deactivate.json # PeopleController.deactivate with :id => 1 + # GET /people/active.json # PeopleController.active # # Using this module, Active Resource can use these custom REST methods just like the # standard methods. @@ -63,6 +63,10 @@ module ActiveResource connection.post(custom_method_collection_url(custom_method_name, options), body, headers) end + def patch(custom_method_name, options = {}, body = '') + connection.patch(custom_method_collection_url(custom_method_name, options), body, headers) + end + def put(custom_method_name, options = {}, body = '') connection.put(custom_method_collection_url(custom_method_name, options), body, headers) end @@ -98,6 +102,10 @@ module ActiveResource end end + def patch(method_name, options = {}, body = '') + connection.patch(custom_method_element_url(method_name, options), body, self.class.headers) + end + def put(method_name, options = {}, body = '') connection.put(custom_method_element_url(method_name, options), body, self.class.headers) end diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb index 666b961f87..820b178d4b 100644 --- a/activeresource/lib/active_resource/http_mock.rb +++ b/activeresource/lib/active_resource/http_mock.rb @@ -15,7 +15,7 @@ module ActiveResource # # mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {}) # - # * <tt>http_method</tt> - The HTTP method to listen for. This can be +get+, +post+, +put+, +delete+ or + # * <tt>http_method</tt> - The HTTP method to listen for. This can be +get+, +post+, +patch+, +put+, +delete+ or # +head+. # * <tt>path</tt> - A string, starting with a "/", defining the URI that is expected to be # called. @@ -55,7 +55,7 @@ module ActiveResource @responses = responses end - [ :post, :put, :get, :delete, :head ].each do |method| + [ :post, :put, :patch, :get, :delete, :head ].each do |method| # def post(path, request_headers = {}, body = nil, status = 200, response_headers = {}) # @responses[Request.new(:post, path, nil, request_headers)] = Response.new(body || "", status, response_headers) # end @@ -217,7 +217,7 @@ module ActiveResource end # body? methods - { true => %w(post put), + { true => %w(post patch put), false => %w(get delete head) }.each do |has_body, methods| methods.each do |method| # def post(path, body, headers) diff --git a/activeresource/test/cases/format_test.rb b/activeresource/test/cases/format_test.rb index 30342ecc74..315f9db1eb 100644 --- a/activeresource/test/cases/format_test.rb +++ b/activeresource/test/cases/format_test.rb @@ -11,11 +11,15 @@ class FormatTest < ActiveSupport::TestCase end def test_http_format_header_name - header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:get] - assert_equal 'Accept', header_name + [:get, :head].each do |verb| + header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[verb] + assert_equal 'Accept', header_name + end - headers_names = [ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:put], ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:post]] - headers_names.each{ |name| assert_equal 'Content-Type', name } + [:patch, :put, :post].each do |verb| + header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[verb] + assert_equal 'Content-Type', header_name + end end def test_formats_on_single_element diff --git a/activeresource/test/cases/http_mock_test.rb b/activeresource/test/cases/http_mock_test.rb index d2fd911314..d13d9258ce 100644 --- a/activeresource/test/cases/http_mock_test.rb +++ b/activeresource/test/cases/http_mock_test.rb @@ -8,7 +8,7 @@ class HttpMockTest < ActiveSupport::TestCase FORMAT_HEADER = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES - [:post, :put, :get, :delete, :head].each do |method| + [:post, :patch, :put, :get, :delete, :head].each do |method| test "responds to simple #{method} request" do ActiveResource::HttpMock.respond_to do |mock| mock.send(method, "/people/1", { FORMAT_HEADER[method] => "application/json" }, "Response") @@ -193,7 +193,7 @@ class HttpMockTest < ActiveSupport::TestCase end def request(method, path, headers = {}, body = nil) - if method.in?([:put, :post]) + if method.in?([:patch, :put, :post]) @http.send(method, path, body, headers) else @http.send(method, path, headers) |