Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6667

Show
Ignore:
Timestamp:
05/06/07 01:48:19 (1 year ago)
Author:
david
Message:

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]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r6654 r6667  
    11*SVN* 
    22 
    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] 
    425 
    526* Improve performance of calling .create on has_many :through associations. [evan] 
  • trunk/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb

    r6359 r6667  
    302302      # This method returns <tt>self</tt>. 
    303303      # 
    304       # ===== Examples 
     304      # == Examples 
    305305      #  # Assuming td is an instance of TableDefinition 
    306306      #  td.column(:granted, :boolean) 
     
    323323      #  def.column(:huge_integer, :decimal, :precision => 30) 
    324324      #    #=> 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. 
    325353      def column(name, type, options = {}) 
    326354        column = self[name] || ColumnDefinition.new(@base, name, type) 
     
    334362      end 
    335363 
     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 
    336380      # Returns a String whose contents are the column definitions 
    337381      # concatenated together.  This string can then be pre and appended to 
  • trunk/railties/CHANGELOG

    r6627 r6667  
    11*SVN* 
     2 
     3* Updated resource_scaffold and model generators to use short-hand style migrations [DHH] 
    24 
    35* Updated initializer to only load #{RAILS_ENV}.rb once. Added deprecation warning for config.breakpoint_server. [Nicholas Seckar] 
  • trunk/railties/lib/rails_generator/generators/components/model/templates/migration.rb

    r5236 r6667  
    33    create_table :<%= table_name %> do |t| 
    44<% for attribute in attributes -%> 
    5       t.column :<%= attribute.name %>, :<%= attribute.type %> 
     5      t.<%= attribute.type %> :<%= attribute.name %> 
    66<% end -%> 
    77    end 
  • trunk/railties/lib/rails_generator/generators/components/scaffold_resource/templates/migration.rb

    r5147 r6667  
    33    create_table :<%= table_name %> do |t| 
    44<% for attribute in attributes -%> 
    5       t.column :<%= attribute.name %>, :<%= attribute.type %> 
     5      t.<%= attribute.type %> :<%= attribute.name %> 
    66<% end -%> 
    77    end