https://github.com/krisleech/not_found
Allows you to rescue ActiveRecord::RecordNotFound for a specific model
https://github.com/krisleech/not_found
Last synced: 8 months ago
JSON representation
Allows you to rescue ActiveRecord::RecordNotFound for a specific model
- Host: GitHub
- URL: https://github.com/krisleech/not_found
- Owner: krisleech
- License: mit
- Created: 2015-11-13T21:26:23.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-11-23T16:20:38.000Z (over 10 years ago)
- Last Synced: 2025-10-25T04:43:43.521Z (8 months ago)
- Language: Ruby
- Size: 12.7 KB
- Stars: 60
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# NotFound (alpha)
[](http://badge.fury.io/rb/not_found)
[](https://codeclimate.com/github/krisleech/not_found)
[](https://travis-ci.org/krisleech/not_found)
[](https://coveralls.io/r/krisleech/not_found?branch=master)
Allows you to rescue `ActiveRecord::RecordNotFound` for a specific model.
For example if you have a `User` model you can rescue `User::RecordNotFound`.
Code which rescue `ActiveRecord::Record` will still work as expected.
## Why?
Because a rescue block around code which rescues `ActiveRecord::RecordNotFound`
might work as expected today, but some time in the future when within that
block another model ends up raising `ActiveRecord::RecordNotFound` it leads to
unexpected behaviour which is diffcult to trace. This gem allows you to be
explicit about what you want to rescue.
## Installation
```ruby
gem 'not_found'
```
## Usage
Include the mixin for a single model
```ruby
class User < ActiveRecord::Base
include NotFound::Mixin
end
```
or every model
```ruby
ActiveRecord::Base.class_eval { include NotFound::Mixin }
```
and rescue away like this
```ruby
begin
user = User.find(id)
rescue User::RecordNotFound
# handle missing user
end
```
or this
```ruby
begin
user = User.find(id)
post = Post.find(id)
rescue User::RecordNotFound
# handle missing user
rescue Post::RecordNotFound
# handle missing post
end
```
or this
```ruby
begin
user = User.find(id)
post = Post.find(id)
rescue User::RecordNotFound
# handle missing user
rescue ActiveRecord::RecordNotFound
# handle missing posts and other models
end
```
## Compatibility
Tested with MRI 2.x, JRuby and Rubinius.
See the [build status](https://travis-ci.org/krisleech/not_found) for details.
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`.
To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
### Running Specs
```
bundle exec rspec
```
To run the specs on code changes try [entr](http://entrproject.org/):
```
ls **/*.rb | entr bundle exec rspec
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/krisleech/not_found.
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).