Skip to content
This repository
Browse code

Added short-hand declaration style to migrations (inspiration from Se…

…xy 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
  • Loading branch information...
commit 4cbbebb1c3385a232a6943b92ff41697ab97cb27 1 parent 0306e4a
David Heinemeier Hansson dhh authored
23 activerecord/CHANGELOG
... ... @@ -1,6 +1,27 @@
1 1 *SVN*
2 2
3   -* 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]
  3 +* Added short-hand declaration style to migrations (inspiration from Sexy Migrations, http://errtheblog.com/post/2381) [DHH]. Example:
  4 +
  5 + create_table "products" do |t|
  6 + t.column "shop_id", :integer
  7 + t.column "creator_id", :integer
  8 + t.column "name", :string, :default => "Untitled"
  9 + t.column "value", :string, :default => "Untitled"
  10 + t.column "created_at", :datetime
  11 + t.column "updated_at", :datetime
  12 + end
  13 +
  14 + ...can now be written as:
  15 +
  16 + create_table :products do |t|
  17 + t.integer :shop_id, :creator_id
  18 + t.string :name, :value, :default => "Untitled"
  19 + t.timestamps
  20 + end
  21 +
  22 + Note: Schema dumping still happens in the old style -- someone care to update it?
  23 +
  24 +* 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]
4 25
5 26 * Improve performance of calling .create on has_many :through associations. [evan]
6 27
46 activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -301,7 +301,7 @@ def [](name)
301 301 #
302 302 # This method returns <tt>self</tt>.
303 303 #
304   - # ===== Examples
  304 + # == Examples
305 305 # # Assuming td is an instance of TableDefinition
306 306 # td.column(:granted, :boolean)
307 307 # #=> granted BOOLEAN
@@ -322,6 +322,34 @@ def [](name)
322 322 # # probably wouldn't hurt to include it.
323 323 # def.column(:huge_integer, :decimal, :precision => 30)
324 324 # #=> huge_integer DECIMAL(30)
  325 + #
  326 + # == Short-hand examples
  327 + #
  328 + # Instead of calling column directly, you can also work with the short-hand definitions for the default types.
  329 + # They use the type as the method name instead of as a parameter and allow for multiple columns to be defined
  330 + # in a single statement.
  331 + #
  332 + # What can be written like this with the regular calls to column:
  333 + #
  334 + # create_table "products", :force => true do |t|
  335 + # t.column "shop_id", :integer
  336 + # t.column "creator_id", :integer
  337 + # t.column "name", :string, :default => "Untitled"
  338 + # t.column "value", :string, :default => "Untitled"
  339 + # t.column "created_at", :datetime
  340 + # t.column "updated_at", :datetime
  341 + # end
  342 + #
  343 + # Can also be written as follows using the short-hand:
  344 + #
  345 + # create_table :products do |t|
  346 + # t.integer :shop_id, :creator_id
  347 + # t.string :name, :value, :default => "Untitled"
  348 + # t.timestamps
  349 + # end
  350 + #
  351 + # There's a short-hand method for each of the type values declared at the top. And then there's
  352 + # TableDefinition#timestamps that'll add created_at and updated_at as datetimes.
325 353 def column(name, type, options = {})
326 354 column = self[name] || ColumnDefinition.new(@base, name, type)
327 355 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 = {})
333 361 self
334 362 end
335 363
  364 + %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
  365 + class_eval <<-EOV
  366 + def #{column_type}(*args)
  367 + options = args.last.is_a?(Hash) ? args.pop : {}
  368 + column_names = args
  369 +
  370 + column_names.each { |name| column(name, '#{column_type}', options) }
  371 + end
  372 + EOV
  373 + end
  374 +
  375 + def timestamps
  376 + column(:created_at, :datetime)
  377 + column(:updated_at, :datetime)
  378 + end
  379 +
336 380 # Returns a String whose contents are the column definitions
337 381 # concatenated together. This string can then be pre and appended to
338 382 # to generate the final SQL to create the table.
2  railties/CHANGELOG
... ... @@ -1,5 +1,7 @@
1 1 *SVN*
2 2
  3 +* Updated resource_scaffold and model generators to use short-hand style migrations [DHH]
  4 +
3 5 * Updated initializer to only load #{RAILS_ENV}.rb once. Added deprecation warning for config.breakpoint_server. [Nicholas Seckar]
4 6
5 7 * 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  railties/lib/rails_generator/generators/components/model/templates/migration.rb
@@ -2,7 +2,7 @@ class <%= migration_name %> < ActiveRecord::Migration
2 2 def self.up
3 3 create_table :<%= table_name %> do |t|
4 4 <% for attribute in attributes -%>
5   - t.column :<%= attribute.name %>, :<%= attribute.type %>
  5 + t.<%= attribute.type %> :<%= attribute.name %>
6 6 <% end -%>
7 7 end
8 8 end
2  railties/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb
@@ -2,7 +2,7 @@ class <%= migration_name %> < ActiveRecord::Migration
2 2 def self.up
3 3 create_table :<%= table_name %> do |t|
4 4 <% for attribute in attributes -%>
5   - t.column :<%= attribute.name %>, :<%= attribute.type %>
  5 + t.<%= attribute.type %> :<%= attribute.name %>
6 6 <% end -%>
7 7 end
8 8 end

0 comments on commit 4cbbebb

Please sign in to comment.
Something went wrong with that request. Please try again.