aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-05-06 01:48:19 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-05-06 01:48:19 +0000
commit4cbbebb1c3385a232a6943b92ff41697ab97cb27 (patch)
tree6075f7bfaa234138f44183181d1bcc8ab6836098
parent0306e4a20483a91d9288ac6f20e6b79db6eca7a7 (diff)
downloadrails-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
-rw-r--r--activerecord/CHANGELOG23
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb46
-rw-r--r--railties/CHANGELOG2
-rw-r--r--railties/lib/rails_generator/generators/components/model/templates/migration.rb2
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb2
5 files changed, 71 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 9190fcbb7d..a9c1b03fa1 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,6 +1,27 @@
*SVN*
-* Use association name for the wrapper element when using .to_xml. Previous behaviour lead to non-deterministic situations with STI and polymorphic associations. [Koz, jstrachan]
+* Added short-hand declaration style to migrations (inspiration from Sexy Migrations, http://errtheblog.com/post/2381) [DHH]. Example:
+
+ create_table "products" 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 now be written as:
+
+ create_table :products do |t|
+ t.integer :shop_id, :creator_id
+ t.string :name, :value, :default => "Untitled"
+ t.timestamps
+ end
+
+ Note: Schema dumping still happens in the old style -- someone care to update it?
+
+* Use association name for the wrapper element when using .to_xml. Previous behavior lead to non-deterministic situations with STI and polymorphic associations. [Koz, jstrachan]
* Improve performance of calling .create on has_many :through associations. [evan]
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.
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index 153d49af75..ee72dec0bc 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Updated resource_scaffold and model generators to use short-hand style migrations [DHH]
+
* Updated initializer to only load #{RAILS_ENV}.rb once. Added deprecation warning for config.breakpoint_server. [Nicholas Seckar]
* Removed breakpointer and Binding.of_caller in favor of relying on ruby-debug by Kent Sibilev since the breakpointer has been broken since Ruby 1.8.4 and will not be coming back [DHH]
diff --git a/railties/lib/rails_generator/generators/components/model/templates/migration.rb b/railties/lib/rails_generator/generators/components/model/templates/migration.rb
index 2a305a6a9d..cb1ddd399e 100644
--- a/railties/lib/rails_generator/generators/components/model/templates/migration.rb
+++ b/railties/lib/rails_generator/generators/components/model/templates/migration.rb
@@ -2,7 +2,7 @@ class <%= migration_name %> < ActiveRecord::Migration
def self.up
create_table :<%= table_name %> do |t|
<% for attribute in attributes -%>
- t.column :<%= attribute.name %>, :<%= attribute.type %>
+ t.<%= attribute.type %> :<%= attribute.name %>
<% end -%>
end
end
diff --git a/railties/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb b/railties/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb
index 2a305a6a9d..cb1ddd399e 100644
--- a/railties/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb
+++ b/railties/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb
@@ -2,7 +2,7 @@ class <%= migration_name %> < ActiveRecord::Migration
def self.up
create_table :<%= table_name %> do |t|
<% for attribute in attributes -%>
- t.column :<%= attribute.name %>, :<%= attribute.type %>
+ t.<%= attribute.type %> :<%= attribute.name %>
<% end -%>
end
end