This is a simple plugin that reads the metadata of your models and autogenerats validators in order to prevent posible validation errors generated by the database.
This works nicely for cases such as having a field with a defined lenght, let's say an varchar field with a length of 50, by the default if we don't add a presence validator on our model for the given field, ActiveRecord won't execute any validations over the provided value, if the value has a length greater than 50 characters, our storage engine will raise an exception and our rails app might display that lovable error screen we all like and love.
Add this line to your application's Gemfile:
gem 'active_record_validations_inflector', github: 'markmercedes/active_record_validations_inflector'
And then execute:
$ bundle
Or install it yourself as:
$ gem install active_record_validations_inflector
Inflecting validators in your classes
class Post < ActiveRecord::Base
# only presence validators
infer_validators :presence
# only length validators
infer_validators :length
# multiple validators
infer_validators :presence, :length
extend AttributeValidatorFinder
end
After checking out the repo, run bundle exec appraisal install
to install dependencies. Then, run bundle exec appraisal rspec
to run the tests. 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.
Bug reports and pull requests are welcome on GitHub at https://github.com/markmercedes/active_record_validations_inflector. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the ActiveRecordValidationsInflector project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.