0.0
No release in over 3 years
Low commit activity in last 3 years
Enhances the Error class on ActiveModel-compliant models with merge() and full_sentences() methods.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0

Runtime

 Project Readme

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

In your Gemfile, add:

gem 'error_merger'

Usage

Merging

@user = User.new name: nil
@user.valid? # => false
@account = Account.new group: 'invalid'
@account.valid? # => false

## By default, will prefix with the merged model's name:
@account.errors.merge @user
@account.errors.full_messages  # will include errors for both Account and User
# => ["Group is invalid", "User: Name can't be blank"]

## Disable the prefix:
@account.errors.merge @user, ''
# @account.errors.merge @user, false  # equivalent to ''
@account.errors.full_messages
# => ["Group is invalid", "Name can't be blank"]

## Or change it:
@account.errors.merge @user, 'Member'
@account.errors.full_messages
# => ["Group is invalid", "Member Name can't be blank"]

## By default, merged errors are associated with the model, not an association
## (by adding the errors to :base). An attribute may be specified. Since the
## standard rendering of the error message will use the attribute name as part
## of the error message, auto-prefixing is disabled.
@account.errors.merge @user, attribute: :user
@account.errors.messages
# => {group: ["is invalid"], user: ["Name can't be blank"]}
@account.errors.full_messages
# => ["Group is invalid", "User Name can't be blank"]

## A prefix can still be added, however:
@account.errors.merge @user, 'Member', attribute: :user
@account.errors.full_messages
# => ["Group is invalid", "User Member Name can't be blank"]

Hint: to merge the actual attributes directly, use the built-in merge! method instead.

## This may cause weird behavior if both models have the same attribute (eg: both
## have a :name attribute).
@account.errors.merge! @user.errors
@account.errors.messages
# => {group: ["is invalid"], name: ["can't be blank"]}
@account.errors.full_messages
# => ["Group is invalid", "Name can't be blank"]

Sentences

## Rails default behavior:
@user.errors.full_messages
# => ["Name can't be blank"]

## Make errors nicer for display to users:
@user.errors.full_sentences
# => ["Name can't be blank."]

## Combine sentences into a single string:
@user.errors.join_sentences
# => "First name can't be blank. Last name can't be blank."

Contributing

Contributions are welcomed. If unsure about the scope or reach of a potential PR, feel free to open a discussion Issue first (not required though).

Please submit human-written contributions only. No contributions containing output generated by LLMs or other non-deterministic systems are allowed.

  • Code, tests, docs, PRs: must be 100% human written.
  • Issues, security reports: must originally be 100% human written. If not originally in English, an LLM may be used to translate to English provided that this translation is disclosed.

We recognize that opinions about the use of LLMs and non-deterministic systems vary widely. Still, we respectfully ask that you honor this policy when interacting with this project.

Q: May I use LLMs apart from contributions?

All usage, training or otherwise, must respect copyrights. Beyond that, we've found that LLMs are prone to errors and misunderstandings--sometimes obvious, sometimes subtle. Please verify everything before trusting an LLM's output.

Running tests

bundle install
rake [test]

License

MIT