Migratory Shortcuts
Migrations are truly a great addition to Rails. That said, if you have a boat load of tables in your project it can be pretty tedious to keep repeating the same values again and again. Thanks to some good feedback there is an even nice syntax now.
Updated example:
Somewhere in the middle of doing this…
create_table :actors do |t|
t.column "created_at", :datetime
t.column "updated_at", :datetime
t.column "deleted", :boolean, :null => false, :default => false
end
create_table :names do |t|
t.column "name", :string, :null => false
t.column "created_at", :datetime
t.column "updated_at", :datetime
t.column "created_by", :integer, :null => false
t.column "updated_by", :integer, :null => false
t.column "deleted", :boolean, :null => false, :default => false
end
create_table :actors_names do |t|
t.column "actor_id", :integer, :null => false
t.column "name_id", :integer, :null => false
t.column "position", :integer, :null => false, :default => 0
t.column "created_at", :datetime
t.column "updated_at", :datetime
t.column "created_by", :integer, :null => false
t.column "updated_by", :integer, :null => false
t.column "deleted", :boolean, :null => false, :default => false
end
for the tenth time, you begin to wonder if there is a better way.
Introducing.. a better way.
The above becomes this…
create_table :actors, :with => :timestamps, :acts_as_paranoid do |t|
t.column "name", :string, :null => false
end
create_table :names, :with =>
and my favorite…
create_link_table :actors, :names, :with =>
[:timestamps, :userstamps, :acts_as_list, :acts_as_paranoid]
New addtion: You can now set defaults for your migrations!
So using defaults the above becomes:
migratory_defaults [:userstamps, :timestamps, :acts_as_paranoid]
create_table :actors
create_table :names do |t|
t.column "name", :string, :null => false
end
create_link_table :actors, :names, :with =>[:acts_as_list]
This makes dealing with migrations just a bit more managable, and you no longer need to remember all the “magic” column names. Just say “acts_as_list” and the “position” column is added for you.
Thus far the following options are supported:
acts_as_list
acts_as_tree
acts_as_nested_set
acts_as_paranoid
timestamps
userstamps
Enjoy!
- Repository Path: svn://rubyforge.org/var/svn/rubybrain/rails/plugins/migratory_shortcuts
- Homepage: http://www.rubybrain.org/articles/4-DRYing-up-migrations-revisited