Update: this Gem is being modernized to support Rails 6.1, Ruby 2.7 and other modern versions. You can see the progress on: https://github.com/pupeno/validation_auditor/tree/modernize
A user visits your web app, tries to do something with it but it fails due to a validation error. Generally, the validation is stopping a user from doing something bad, but every now and then it's the validation that is bad. Don't you hate it when the credit card processor won't accept your name as written in the credit card due to an unexpected character and won't accept anything else because that's not your name? We all do.
This gem allows you to easily keep a log of validation errors, so you can later inspect them to try to find those cases where things are going wrong.
This gem supports Rails 4.0 through 4.2 running on Ruby 1.9 through 2.3 (latest stable version of each).
Ruby 1.9 and 2.1 are not being actively tested due to issues with bundler in the continuous integration server. Tests for Ruby 2.0 should ensure 2.1 works fine anyway, Ruby 1.9 might accidentally become unsupported, please, report a bug if you experience.
Rails 3.2 is not being actively supported because of the time investment required to maintain the test suite but no specific action against Rails 3.2 is being taken and re-enabling the 3.2 test suite is not discarded. The last branch to test and support Rails 3.2 was v1.0 and the last release was v1.0.0.
Add this line to your application's Gemfile:
gem "validation_auditor"
If you are on Rails < 4.0, then you need to install the latest version from the 1.0 branch:
gem "validation_auditor", "~> 1.0.0"
And then execute:
$ bundle
You need to install the migration file in your Rails project, which you can do by:
$ rails generate validation_auditor:install
After you run the migration, you need to enable the validation auditor in each model by calling the class method:
audit_validation_errors
, for example:
class Blog < ActiveRecord::Base
audit_validation_errors
end
From then on, every time saving that record fails, a record will be saved to validation_audits with the failure message and some extra information.
If you enable validation audit on the controller, by calling audit_validation_errors
as in:
class BlogsController < ApplicationController
audit_validation_errors
end
then you'll also get params, url and user agent in the validation audit. This breaks the model-controller separation, so it's optional.
If for some reason saving a validation audit fails, the exception will be left to propagate into your application so that no exception is silently swallowed. You may not want to let a secondary system, like auditing, stop your application from working (depending on how critical auditing is for you). If that's the case, you can define an exception handler that can report the exception in whatever fashion you normally report exceptions to your dev team and then swallow the exception. This may or may not work in Rails < 4.
ValidationAuditor.exception_handler = lambda do |e, va|
puts "When trying to save validation audit #{va}, exception #{e} was encountered."
end
This gem is being used by:
- Watu
- Qredo
- You? please, let me know if you are using this gem.
- Dropped testing and official support for Rails 3.2.
- Added testing and official support for Rails 4.2.
- Added testing and official support for Ruby 2.2.X.
- Added testing and official support for Ruby 2.3.X
- Started testing Ruby 2.1.3 and 2.1.4.
- Refactoring to make code more readable (increased code climate to 4.0).
- Marked internal methods as private.
- Improved documentation.
- When cleaning params to save them as yaml, also clean each element of arrays.
- Respect the filter_parameters configuration from Rails.
- Don't crash in the presence of file uploads when reporting validation errors.
- Everything.
- 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