Added short-hand declaration style to migrations (inspiration from Sexy...

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
上级 0306e4a2
*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]
......
......@@ -301,7 +301,7 @@ def [](name)
#
# 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 @@ def [](name)
# # 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 @@ def column(name, type, options = {})
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.
......
*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]
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册