This plugin provides a way to validate your model against database rules you’ve already created in your schema, including column lengths for strings, “not null” designations, unique index constraints, and ensuring integer input for integer columns.
The four methods currently supported are:
enforce_column_limits enforce_integer_columns enforce_not_null enforce_unique_indexes
You can also just call enforce_schema_rules
to enforce all the above.
Accepts the relevant validates_length_of options (i.e. :on and :message) and these assume the usual defaults.
In addition to the regular options, you can also have an :except
list enumerating the column that you don’t want to validate.
By default, magic columns (_at, _on, _id, id, position, etc) are skipped. If you’d like to override that behavior, you can define your own :exclusion_regexp
Examples:
class Person < ActiveRecord::Base enforce_schema_rules :except => :dhh end class Book < ActiveRecord::Base enforce_column_limits :message => "exceeds the %d character limit", :on => :update enforce_unique_indexes enforce_not_null :exclusion_regexp => /$fk_/ end
Development is done at:
http://github.com/twinge/enforce_schema_rules
You can install as a gem with:
gem install enforce_schema_rules
Please send bug reports, patches and feedback to the GitHub project.
The plugin was written by Josh Starcher <[email protected]>. It was updated to Rails 3 by Eric Anderson <[email protected]>. This plugin is basically an extension of David Easley’s enforce-limits plugin. Michael Schuerig provided contributed a patch and a style lesson.