diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-05-06 01:48:19 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-05-06 01:48:19 +0000 |
commit | 4cbbebb1c3385a232a6943b92ff41697ab97cb27 (patch) | |
tree | 6075f7bfaa234138f44183181d1bcc8ab6836098 /activerecord/lib | |
parent | 0306e4a20483a91d9288ac6f20e6b79db6eca7a7 (diff) | |
download | rails-4cbbebb1c3385a232a6943b92ff41697ab97cb27.tar.gz rails-4cbbebb1c3385a232a6943b92ff41697ab97cb27.tar.bz2 rails-4cbbebb1c3385a232a6943b92ff41697ab97cb27.zip |
Added short-hand declaration style to migrations (inspiration from Sexy Migrations, http://errtheblog.com/post/2381) [DHH] Updated resource_scaffold and model generators to use short-hand style migrations [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6667 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index ebde8c83f4..b6c3949a5a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -301,7 +301,7 @@ module ActiveRecord # # This method returns <tt>self</tt>. # - # ===== Examples + # == Examples # # Assuming td is an instance of TableDefinition # td.column(:granted, :boolean) # #=> granted BOOLEAN @@ -322,6 +322,34 @@ module ActiveRecord # # probably wouldn't hurt to include it. # def.column(:huge_integer, :decimal, :precision => 30) # #=> huge_integer DECIMAL(30) + # + # == Short-hand examples + # + # Instead of calling column directly, you can also work with the short-hand definitions for the default types. + # They use the type as the method name instead of as a parameter and allow for multiple columns to be defined + # in a single statement. + # + # What can be written like this with the regular calls to column: + # + # create_table "products", :force => true do |t| + # t.column "shop_id", :integer + # t.column "creator_id", :integer + # t.column "name", :string, :default => "Untitled" + # t.column "value", :string, :default => "Untitled" + # t.column "created_at", :datetime + # t.column "updated_at", :datetime + # end + # + # Can also be written as follows using the short-hand: + # + # create_table :products do |t| + # t.integer :shop_id, :creator_id + # t.string :name, :value, :default => "Untitled" + # t.timestamps + # end + # + # There's a short-hand method for each of the type values declared at the top. And then there's + # TableDefinition#timestamps that'll add created_at and updated_at as datetimes. def column(name, type, options = {}) column = self[name] || ColumnDefinition.new(@base, name, type) column.limit = options[:limit] || native[type.to_sym][:limit] if options[:limit] or native[type.to_sym] @@ -333,6 +361,22 @@ module ActiveRecord self end + %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type| + class_eval <<-EOV + def #{column_type}(*args) + options = args.last.is_a?(Hash) ? args.pop : {} + column_names = args + + column_names.each { |name| column(name, '#{column_type}', options) } + end + EOV + end + + def timestamps + column(:created_at, :datetime) + column(:updated_at, :datetime) + end + # Returns a String whose contents are the column definitions # concatenated together. This string can then be pre and appended to # to generate the final SQL to create the table. |