Enforce Schema Rules
This plugin provides a way to validate your model against databse 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
Download
Development is done at:
http://github.com/twinge/enforce_schema_rules
You can install as a gem with:
gem install enforce_schema_rules
Bugs & feedback
Please send bug reports, patches and feedback to the GitHub project.
Todo
The main goal was recent updates was to get it to work in Rails 3. Some cleanup was just a side-effect. If anybody is interested, additional cleanup items include:
* Make sure we don't need better test coverage. The upgrades to
Rails 3 caused some test fixes but I am not sure we have coverage so
a good eye towards that would be good.
* The smallest number of changes were done to get things working under
Rails 3. This plugin was written a while back. It might could be
refactored using some new goodness that comes with a more recent
ActiveRecord/Rails implementation.
* I most likely dropped support for Rails 2.x. The old version works
fine for those still on Rails 2.x so I say we just drop Rails 2.x
support (there are some warnings but they can be fixed fairly easily
and don't need to be fixed).
Credit
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 syle lesson.