diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index 89878aa2145abe580e8a3dcf80020682a5a0729b..cbb56c5609677c9e915025f7bdd702a100ace226 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Use HEAD instead of GET in exists? [bscofield] + * Fix small documentation typo. Closes #10670 [l.guidi] * find_or_create_resource_for handles module nesting. #10646 [xavier] diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index eea6259a644b845a2d3ebebc803c17b4a2d606a4..97baa466820ba6a6f2858c67ccedd7cd532a2b9d 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -436,7 +436,13 @@ def delete(id, options = {}) # Note.exists(1349) # # => false def exists?(id, options = {}) - id && !find_single(id, options).nil? + if id + prefix_options, query_options = split_options(options[:params]) + path = element_path(id, prefix_options, query_options) + response = connection.head(path, headers) + response.code == 200 + end + # id && !find_single(id, options).nil? rescue ActiveResource::ResourceNotFound false end diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index 2bf83b1615fa02499300bd50e3ee8a1402133362..e4e6da50c92f9943e300008a2bef0a14349fb36d 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -101,6 +101,12 @@ def post(path, body = '', headers = {}) request(:post, path, body.to_s, build_request_headers(headers)) end + # Execute a HEAD request. + # Used to ... + def head(path, headers= {}) + request(:head, path, build_request_headers(headers)) + end + private # Makes request to remote service. diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb index d70364c5eb069c7ceabcf02b3cbc1ef7261298cf..d1c1412575ec318bed832db52be45bf99dc43c14 100644 --- a/activeresource/lib/active_resource/http_mock.rb +++ b/activeresource/lib/active_resource/http_mock.rb @@ -9,7 +9,7 @@ def initialize(responses) @responses = responses end - for method in [ :post, :put, :get, :delete ] + for method in [ :post, :put, :get, :delete, :head ] module_eval <<-EOE, __FILE__, __LINE__ def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {}) @responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || "", status, response_headers) @@ -56,7 +56,7 @@ def #{method}(path, body, headers) EOE end - for method in [ :get, :delete ] + for method in [ :get, :delete, :head ] module_eval <<-EOE, __FILE__, __LINE__ def #{method}(path, headers) request = ActiveResource::Request.new(:#{method}, path, nil, headers) diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index 3808209f79b24d8e3c106a2891b2a92ff55870cd..7606c56bcf58614c17fc7829fa68a7afa7a8526c 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -36,6 +36,11 @@ def setup mock.put "/people//addresses/1.xml", {}, nil, 404 mock.delete "/people//addresses/1.xml", {}, nil, 404 mock.post "/people//addresses.xml", {}, nil, 404 + mock.head "/people/1.xml", {}, nil, 200 + mock.head "/people/99.xml", {}, nil, 404 + mock.head "/people/1/addresses/1.xml", {}, nil, 200 + mock.head "/people/1/addresses/2.xml", {}, nil, 404 + mock.head "/people/2/addresses/1.xml", {}, nil, 404 end end diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb index 8ffc4dfbb20c83afc25558410e191566390aba2e..38fdbd3b2f9e5c27d5a79ceeb5f396f52e56bb29 100644 --- a/activeresource/test/connection_test.rb +++ b/activeresource/test/connection_test.rb @@ -27,6 +27,7 @@ def setup mock.delete "/people/2.xml", @header, nil, 200 mock.post "/people.xml", {}, nil, 201, 'Location' => '/people/5.xml' mock.post "/members.xml", {}, @header, 201, 'Location' => '/people/6.xml' + mock.head "/people/1.xml", {}, nil, 200 end end @@ -105,6 +106,12 @@ def test_get assert_equal "Matz", matz["name"] end + def test_head + response = @conn.head("/people/1.xml") + assert response.body.blank? + assert_equal 200, response.code + end + def test_get_with_header david = @conn.get("/people/2.xml", @header) assert_equal "David", david["name"]