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

Add allow_nil: true option to the finder #995

Merged
merged 8 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,27 @@ existing users, do this from the console, runner, or add a Rake task:
User.find_each(&:save)
```

## Options

### :allow_nil
joemsak marked this conversation as resolved.
Show resolved Hide resolved

You can pass `allow_nil: true` to the `friendly.find()` method if you're want to
avoid raising `ActiveRecord::RecordNotFound` and accept a `nil`.

#### Example

```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 don't know if you even have a slug or ID
#=> raise ActiveRecord::RecordNotFound

MyModel.friendly.find("bad-slug", allow_nil: true) # where bad-slug is not a valid slug
MyModel.friendly.find(123, allow_nil: true) # where 123 is not a valid primary key ID
MyModel.friendly.find(nil, allow_nil: true) # when you don't know if you even have a slug or ID
#=> nil
```

## Bugs

Please report them on the [Github issue
Expand Down
20 changes: 16 additions & 4 deletions lib/friendly_id/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@ module FinderMethods
# id matching '123' and then fall back to looking for a record with the
# numeric id '123'.
#
# @param [Boolean] allow_nil (default: false)
# Use allow_nil: true if you'd like the finder to return nil instead of
# raising ActivRecord::RecordNotFound
#
# ### Example
#
# MyModel.friendly.find("bad-slug")
# #=> raise ActiveRecord::RecordNotFound
#
# MyModel.friendly.find("bad-slug", allow_nil: true)
# #=> nil
#
# Since FriendlyId 5.0, if the id is a nonnumeric string like '123-foo' it
# will *only* search by friendly id and not fall back to the regular find
# method.
#
# If you want to search only by the friendly id, use {#find_by_friendly_id}.
# @raise ActiveRecord::RecordNotFound
def find(*args)
def find(*args, allow_nil: false)
id = args.first
return super if args.count != 1 || id.unfriendly_id?
return super(*args) if args.count != 1 || id.unfriendly_id?
first_by_friendly_id(id).tap { |result| return result unless result.nil? }
return super if potential_primary_key?(id)
return super(*args) if potential_primary_key?(id)

raise_not_found_exception(id)
raise_not_found_exception(id) unless allow_nil
end

# Returns true if a record with the given id exists.
Expand Down
12 changes: 12 additions & 0 deletions test/finders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ def model_class
assert model_class.existing.find(record.friendly_id)
end
end

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

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