https://github.com/segoy89/errors_to_sentences
Simple gem to transform error messages into sentences
https://github.com/segoy89/errors_to_sentences
activemodel errors gem rails rails-gem ruby-on-rails
Last synced: 4 months ago
JSON representation
Simple gem to transform error messages into sentences
- Host: GitHub
- URL: https://github.com/segoy89/errors_to_sentences
- Owner: segoy89
- License: mit
- Created: 2020-08-22T07:54:12.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-08-23T08:24:51.000Z (almost 6 years ago)
- Last Synced: 2025-12-01T07:35:22.910Z (6 months ago)
- Topics: activemodel, errors, gem, rails, rails-gem, ruby-on-rails
- Language: Ruby
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ErrorsToSentences
This gem allows combining multiple error messages of one attribute into one sentence.
For more, see [Usage](#usage) section
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'errors_to_sentences'
```
And then execute:
```shell
$ bundle install
```
Or install it yourself as:
```shell
$ gem install errors_to_sentences
```
## Usage
To generate sentence errors just call `.to_sentences` method on instance errors. For an instance:
Let's say we have a model that looks like:
```ruby
class Refueling < ApplicationRecord
belongs_to :user
with_options numericality: { greater_than: 0 } do
validates :liters, presence: true
validates :kilometers, presence: true
validates :cost, allow_nil: true
end
end
```
```shell
$ refueling = Refueling.new
=> #
$ refueling.validate!
ActiveRecord::RecordInvalid (Validation failed: User must exist, Liters is not a number, Liters can't be blank, Kilometers is not a number, Kilometers can't be blank)
$ refueling.errors.full_messages
=> ["User must exist", "Liters is not a number", "Liters can't be blank", "Kilometers is not a number", "Kilometers can't be blank"]
```
and now **`.to_sentences`** method
```shell
$ refueling.errors.to_sentences
=> ["User must exist", "Liters is not a number and can't be blank", "Kilometers is not a number and can't be blank"]
```
## Development
After checking out the repo, run `bin/setup` to install dependencies. 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).
## Contributing
Bug reports and pull requests are welcome on GitHub at .