Rails validation generator
If you’re writing Rails apps on the regular a good amount of your time is going to be spent writing ActiveRecord validations. You’re probably also defining many of the core restrictions in the underlying SQL table structure. Perhaps, like me, you have grown to loath having to define your tables in your migrations, complete with
:limit and :null => false options, only to have to repeat yourself in the models’ validations with a bunch of validates_length_of and validates_presence_of declarations.The larger the table definition the worse it gets. Some strings can be null, some can’t. That’ll mean careful placement of
:allow_blank => true in the models, attached to the necessary validates_length_of, :maximum => expressions. That kind of thing takes forever, but it’s necessary unless you want flies in your soup.So here’s a Rake task to do this for you. It’s like an intern. It’s not perfect and you’ll probably want to tailor its deliverables before you call it a day, but it does a decent job.
Consider a table definition (from my
schema.rb) that looks like this:
# id :integer(4) not null, primary key@
# firstname :string(30) default(""), not null
# lastname :string(30) default(""), not null
# title :string(30)
# organization :string(30)
# address_1 :string(50) default(""), not null
# address_2 :string(50)
# im_name :string(50)
# state :string(20)
# city :string(50) default(""), not null
# postal_code :string(15) default(""), not null
# province :string(20)
# country :string(30) default(""), not null
# telephone :string(20) default(""), not null
# fax :string(20)
# system_os :string(20)
# browser :string(20)
# email :string(255) default(""), not null
# assignment_type :string(10) default(""), not null
# qualifications :string(500) default(""), not null
# resume_url :string(255)
# resume_file_name :string(255)
# resume_content_type :string(50)
# resume_file_size :integer(4)
# cluster_1_id :integer(4) not null
# cluster_2_id :integer(4)
# cluster_3_id :integer(4)
# cluster_1_quals :string(500) default(""), not null
# cluster_2_quals :string(500)
# cluster_3_quals :string(500)
# activated_by_admin_at :datetime
# deactivated_by_admin_at :datetime
# approved_by_admin_id :integer(4)
# points :integer(4) default(0), not null
# last_logged_in_at :datetime
# year :integer(4) not null
# created_at :datetime
# updated_at :datetime
That would probably have taken about four months to declare in ActiveRecord validations, and it’s fucking boring. Now it can be side stepped a bit. Given that table, the following is generated:
validates_presence_of :firstname, :lastname, :address_1, :city, :postal_code, :country, :telephone, :email, :assignment_type, :qualifications, :cluster_1_id, :cluster_1_quals, :points, :year validates_length_of :cluster_2_quals, :cluster_3_quals, :maximum => 500, :allow_blank => true validates_length_of :address_2, :im_name, :resume_content_type, :maximum => 50, :allow_blank => true validates_length_of :resume_url, :resume_file_name, :maximum => 255, :allow_blank => true validates_length_of :title, :organization, :maximum => 30, :allow_blank => true validates_length_of :state, :province, :fax, :system_os, :browser, :maximum => 20, :allow_blank => true validates_length_of :qualifications, :cluster_1_quals, :maximum => 500 validates_length_of :address_1, :city, :maximum => 50 validates_length_of :email, :maximum => 255 validates_length_of :firstname, :lastname, :country, :maximum => 30 validates_length_of :telephone, :maximum => 20 validates_length_of :postal_code, :maximum => 15 validates_length_of :assignment_type, :maximum => 10 validates_numericality_of :resume_file_size, :cluster_2_id, :cluster_3_id, :approved_by_admin_id, :allow_blank => true validates_numericality_of :cluster_1_id, :points, :year@
That’s not bad. It built out the list of required fields. All strings got length validations, with
:allow_blanks where they were allowed to be null in the DB. Strings with like lengths get grouped together. validates_numericality_of for integer fields.http://pastie.org/342875
0 comments