Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with allow_nil: true #997

Merged
merged 8 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ avoid raising `ActiveRecord::RecordNotFound` and accept a `nil`.
```ruby
MyModel.friendly.find("bad-slug") # where bad-slug is not a valid slug
MyModel.friendly.find(123) # where 123 is not a valid primary key ID
MyModel.friendly.find(nil) # when you have a variable/param that's possibly nil
MyModel.friendly.find(nil) # maybe you have a variable/param that's potentially nil
#=> raise ActiveRecord::RecordNotFound

MyModel.friendly.find("bad-slug", allow_nil: true)
Expand Down
4 changes: 3 additions & 1 deletion lib/friendly_id/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def find(*args, allow_nil: false)
return super(*args) if potential_primary_key?(id)

raise_not_found_exception(id) unless allow_nil
rescue ActiveRecord::RecordNotFound => exception
raise exception unless allow_nil
end

# Returns true if a record with the given id exists.
Expand All @@ -45,7 +47,7 @@ def exists?(conditions = :none)
# `find`.
# @raise ActiveRecord::RecordNotFound
def find_by_friendly_id(id)
first_by_friendly_id(id) or raise raise_not_found_exception(id)
first_by_friendly_id(id) or raise_not_found_exception(id)
end

def exists_by_friendly_id?(id)
Expand Down
36 changes: 36 additions & 0 deletions test/finders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,40 @@ def model_class
assert_nil model_class.existing.find("foo", allow_nil: true)
end
end

test "allows nil with a bad primary key ID and allow_nil: true" do
with_instance_of(model_class) do |record|
assert_nil model_class.find(0, allow_nil: true)
end
end

test "allows nil on relations with a bad primary key ID and allow_nil: true" do
with_instance_of(model_class) do |record|
assert_nil model_class.existing.find(0, allow_nil: true)
end
end

test "allows nil with a bad potential primary key ID and allow_nil: true" do
with_instance_of(model_class) do |record|
assert_nil model_class.find("0", allow_nil: true)
end
end

test "allows nil on relations with a bad potential primary key ID and allow_nil: true" do
with_instance_of(model_class) do |record|
assert_nil model_class.existing.find("0", allow_nil: true)
end
end

test "allows nil with nil ID and allow_nil: true" do
with_instance_of(model_class) do |record|
assert_nil model_class.find(nil, allow_nil: true)
end
end

test "allows nil on relations with nil ID and allow_nil: true" do
with_instance_of(model_class) do |record|
assert_nil model_class.existing.find(nil, allow_nil: true)
end
end
end