aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-01-12 07:20:05 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-01-12 07:20:05 +0000
commit2bb33bbd590b1217f0a6576de34105ab1db3510c (patch)
tree4133c16561bfdb8f9c99b7e6f15b6b074826515e /activeresource
parentfa619b051b5e12c6b1e8b91a32d6738edafcc3af (diff)
downloadrails-2bb33bbd590b1217f0a6576de34105ab1db3510c.tar.gz
rails-2bb33bbd590b1217f0a6576de34105ab1db3510c.tar.bz2
rails-2bb33bbd590b1217f0a6576de34105ab1db3510c.zip
Base.exists?(id, options) and Base#exists? check whether the resource is found. Closes #6970.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5898 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activeresource')
-rw-r--r--activeresource/CHANGELOG2
-rw-r--r--activeresource/lib/active_resource/base.rb12
-rw-r--r--activeresource/test/base_test.rb24
3 files changed, 38 insertions, 0 deletions
diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG
index 3a2d70c387..44108fb8ce 100644
--- a/activeresource/CHANGELOG
+++ b/activeresource/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Base.exists?(id, options) and Base#exists? check whether the resource is found. #6970 [rwdaigle]
+
* Query string support. [untext, Jeremy Kemper]
# GET /forums/1/topics.xml?sort=created_at
Topic.find(:all, :forum_id => 1, :sort => 'created_at')
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 7f99e5cbf5..8babc18507 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -80,6 +80,13 @@ module ActiveResource
connection.delete(element_path(id))
end
+ # True if the resource is found.
+ def exists?(id, options = {})
+ id && !find_single(id, options).nil?
+ rescue ActiveResource::ResourceNotFound
+ false
+ end
+
private
def find_every(options)
collection = connection.get(collection_path(options)) || []
@@ -167,6 +174,11 @@ module ActiveResource
connection.delete(element_path)
end
+ # True if this resource is found.
+ def exists?
+ !new? && self.class.exists?(id, prefix_options)
+ end
+
def to_xml(options={})
attributes.to_xml({:root => self.class.element_name}.merge(options))
end
diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb
index a8c9454476..f9f2b24c72 100644
--- a/activeresource/test/base_test.rb
+++ b/activeresource/test/base_test.rb
@@ -21,6 +21,8 @@ class BaseTest < Test::Unit::TestCase
mock.get "/people.xml", {}, "<people>#{@matz}#{@david}</people>"
mock.get "/people/1/addresses.xml", {}, "<addresses>#{@addy}</addresses>"
mock.get "/people/1/addresses/1.xml", {}, @addy
+ mock.get "/people/1/addresses/2.xml", {}, nil, 404
+ mock.get "/people/2/addresses/1.xml", {}, nil, 404
mock.put "/people/1/addresses/1.xml", {}, nil, 204
mock.delete "/people/1/addresses/1.xml", {}, nil, 200
mock.post "/people/1/addresses.xml", {}, nil, 201, 'Location' => '/people/1/addresses/5'
@@ -237,4 +239,26 @@ class BaseTest < Test::Unit::TestCase
def test_delete
assert Person.delete(1)
end
+
+ def test_exists
+ # Class method.
+ assert !Person.exists?(nil)
+ assert Person.exists?(1)
+ assert !Person.exists?(99)
+
+ # Instance method.
+ assert !Person.new.exists?
+ assert Person.find(1).exists?
+ assert !Person.new(:id => 99).exists?
+
+ # Nested class method.
+ assert StreetAddress.exists?(1, :person_id => 1)
+ assert !StreetAddress.exists?(1, :person_id => 2)
+ assert !StreetAddress.exists?(2, :person_id => 1)
+
+ # Nested instance method.
+ assert StreetAddress.find(1, :person_id => 1).exists?
+ assert !StreetAddress.new({:id => 1}, {:person_id => 2}).exists?
+ assert !StreetAddress.new({:id => 2}, {:person_id => 1}).exists?
+ end
end