Skip to content

Commit 1d93405

Browse files
authored
Merge pull request #375 from stefanvermaas/main
Always return a boolean value for `ActiveResource::Base#exists?`
2 parents 1840215 + 7892b1a commit 1d93405

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

lib/active_resource/base.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -1072,13 +1072,13 @@ def delete(id, options = {})
10721072
#
10731073
# Note.exists(1349) # => false
10741074
def exists?(id, options = {})
1075-
if id
1076-
prefix_options, query_options = split_options(options[:params])
1077-
path = element_path(id, prefix_options, query_options)
1078-
response = connection.head(path, headers)
1079-
(200..206).include? response.code.to_i
1080-
end
1081-
# id && !find_single(id, options).nil?
1075+
return false unless id
1076+
1077+
prefix_options, query_options = split_options(options[:params])
1078+
path = element_path(id, prefix_options, query_options)
1079+
response = connection.head(path, headers)
1080+
1081+
(200..206).include?(response.code.to_i)
10821082
rescue ActiveResource::ResourceNotFound, ActiveResource::ResourceGone
10831083
false
10841084
end

test/cases/base_test.rb

+12-12
Original file line numberDiff line numberDiff line change
@@ -1288,24 +1288,24 @@ def test_delete_with_custom_header
12881288
########################################################################
12891289
def test_exists
12901290
# Class method.
1291-
assert_not Person.exists?(nil)
1292-
assert Person.exists?(1)
1293-
assert_not Person.exists?(99)
1291+
assert_equal false, Person.exists?(nil)
1292+
assert_equal true, Person.exists?(1)
1293+
assert_equal false, Person.exists?(99)
12941294

12951295
# Instance method.
1296-
assert_not Person.new.exists?
1297-
assert Person.find(1).exists?
1298-
assert_not Person.new(id: 99).exists?
1296+
assert_equal false, Person.new.exists?
1297+
assert_equal true, Person.find(1).exists?
1298+
assert_equal false, Person.new(id: 99).exists?
12991299

13001300
# Nested class method.
1301-
assert StreetAddress.exists?(1, params: { person_id: 1 })
1302-
assert_not StreetAddress.exists?(1, params: { person_id: 2 })
1303-
assert_not StreetAddress.exists?(2, params: { person_id: 1 })
1301+
assert_equal true, StreetAddress.exists?(1, params: { person_id: 1 })
1302+
assert_equal false, StreetAddress.exists?(1, params: { person_id: 2 })
1303+
assert_equal false, StreetAddress.exists?(2, params: { person_id: 1 })
13041304

13051305
# Nested instance method.
1306-
assert StreetAddress.find(1, params: { person_id: 1 }).exists?
1307-
assert_not StreetAddress.new(id: 1, person_id: 2).exists?
1308-
assert_not StreetAddress.new(id: 2, person_id: 1).exists?
1306+
assert_equal true, StreetAddress.find(1, params: { person_id: 1 }).exists?
1307+
assert_equal false, StreetAddress.new(id: 1, person_id: 2).exists?
1308+
assert_equal false, StreetAddress.new(id: 2, person_id: 1).exists?
13091309
end
13101310

13111311
def test_exists_with_redefined_to_param

0 commit comments

Comments
 (0)