No commit activity in last 3 years
No release in over 3 years
ErrorsFullDetails extends ActiveModel::Errors to allow adding error codes to error messages
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

>= 5.2.2.1
 Project Readme

ErrorsFullDetails

Adds a new method to ActiveModel::Errors to display messages with codes using details.

Usage

Add errors with codes to an object:

class Person < ApplicationRecord
  validate :username_is_cool
  validate :title_is_looking_good
  validate :default_code

  private

  def username_is_cool
    errors.add(:username, 'is not cool', code: 15)
  end

  def title_is_looking_good
    errors.add(:title, 'is not looking good', code: '210')
  end

  def default_code
    errors.add(:base, 'This error has a default code')
  end
end

Call object.errors.full_details to generate an array of error messages and codes:

[
  {
    message: "Username is not cool",
    code: "15"
  },
  {
    message: "Title is not looking good",
    code: "210"
  },
  {
    message: "This error has a default code",
    code: "0"
  }
]

Errors added without an error code will return '0' as a default error code.

Installation

Add this line to your application's Gemfile:

gem 'errors_full_details', '~> 0.0.4'

And then execute:

$ bundle

License

The gem is available as open source under the terms of the MIT License.