# dont_fucking_repeat_yourself
- table :user, options => "TYPE=InnoDB" do |t|
- # Adds has_many :tasks to the model, and adds an index on the target column
- # in the associated table (tasks.user_id, in this case.)
- t.has_many :tasks
- # Create a VARCHAR(50) NOT NULL column, validates_length_of :login,
- # :in => 5..50, validates_presence_of :login, validates_uniqueness_of
- # :login and a unique index for the column.
- t.string :login, :limit => 5..50, :null => false, :unique => true
- # Create a VARCHAR(40) NOT NULL column ready for a SHA1 hash of the
- # :password field. Also adds validates_confirmation_of :password, and an
- # accessor for :password_confirmation. (:confirmation isn't limited to
- # passwords, although it's the main use of it.)
- t.hash :password, :limit => 4..16, :null => false, :confirmation => true
- # Create a DATETIME NOT NULL column for created_at
- t.created_at
- end
- table :task, :options => "TYPE=InnoDB" do |t|
- # Adds belongs_to :user, :allow_nil => true to the model. Also adds an
- # user_id INTEGER column, and creates an index for it.
- t.belongs_to :user, :allow_nil => true
- # Adds a VARCHAR(128) NOT NULL column, validates_length_of :title, :in
- # => 1..128, validates_presence_of :title, and an index for the column.
- t.string :title, :limit => 1..128, :null => false, :index => true
- # Adds a DATETIME column, no validation.
- t.datetime :due
- # Adds a BOOLEAN NOT NULL DEFAULT FALSE column. Doesn't add a
- # validates_presence_of validation.
- t.boolean :done, :default => false, :null => false
- # Create DATETIME NOT NULL columns for created_at and updated_at
- t.timestamps
- # Create a unique index for the two columns due and done.
- t.index :due, :done, :unique => true
- # Create an index for the two columns user_id and title.
- t.index :user_id, :title
- end
So, you’re a Rails developer? CHECK! You have this great idea for a new application, the one which will surely get Yahoo and Google involved in a bidding war? CHECK! But you don’t actually start developing it because you are fed up having to run script/generate model model_name, editing the migration for the table, writing the tests for all the rules which are implied by the database (Such as maximum lengths for columns and column uniqueness), and then editing the model to pass those tests? CHECK!
To solve this problem, you can see the proposed syntax for my (tentatively titled) dont_fucking_repeat_yourself plugin. After creating a file much in the manner of that above, you simply run the (tentatively titled) i_am_bored_of_repeating_myself rake task and all your models, migrations and tests are generated.
It will also allow you to add new models during development, so no longer will you think “No, I won’t add that feature because it requires a new table and I can’t be arsed doing the create-model-write-migration-write-tests-edit-model dance.”
Obviously this only helps with the initial creation of models and doesn’t attempt to help you when you want to change your title column to hold more than 128 characters, but man, it should really speed up those early days of development.
If anyone has any comments let me know, a link to my email is at the bottom of this page. Hell, if anyone reads this at all, it’ll be good to hear from you.
(And if anyone is offended by the swearing, I’m sorry, but DHH started it.)