ErrorMerger
Adds a #merge method to ActiveModel-compliant models.
Allows the merging/consolidation of errors on multiple models to make it easy to pass into some kind of error renderer.
Also adds #full_sentence and #full_sentences methods, with are comparable to #full_message and #full_messages respectively, except they ensure each error message always ends with a period.
Installation
Add this line to your application's Gemfile:
gem 'error_merger'
And then execute:
$ bundle
Or install it yourself as:
$ gem install error_merger
Usage
@user = User.new user_params
@user.valid? # => false
@account = Account.new account_params
@account.valid? # => false
@account.errors.merge @user
@account.errors. # => will include errors for both Account and User
@user.errors. # => ["Username can't be blank"]
@user.errors.full_sentences # => ["Username can't be blank."]
By default merged errors are prefixed with the model name. In the above example, an error on User might look like: "User: First name must not be blank".
The default behavior is the equivalent to: @account.errors.merge @user, 'User: '
To skip prefixing, pass an empty string: ''.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request