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

Changeset 6883

Show
Ignore:
Timestamp:
05/29/07 00:11:56 (1 year ago)
Author:
bitsweat
Message:

Generated migrations include timestamps by default. Closes #8501.

Files:

Legend:

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

    r6872 r6883  
    11*SVN* 
     2 
     3* Generated migrations include timestamps by default.  #8501 [shane] 
    24 
    35* Drop Action Web Service from rails:freeze:edge.  [Jeremy Kemper] 
  • trunk/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml

    r6869 r6883  
    55  <%= attribute.name %>: <%= attribute.default %> 
    66<% end -%> 
     7  created_at: <%= Time.now.to_s(:db) %> 
     8  updated_at: <%= Time.now.to_s(:db) %> 
    79two: 
    810  id: 2 
     
    1012  <%= attribute.name %>: <%= attribute.default %> 
    1113<% end -%> 
     14  created_at: <%= Time.now.to_s(:db) %> 
     15  updated_at: <%= Time.now.to_s(:db) %> 
  • trunk/railties/lib/rails_generator/generators/components/model/templates/migration.rb

    r6667 r6883  
    55      t.<%= attribute.type %> :<%= attribute.name %> 
    66<% end -%> 
     7    t.timestamps  
    78    end 
    89  end 
  • trunk/railties/lib/rails_generator/generators/components/model/USAGE

    r5236 r6883  
    66 
    77    As additional parameters, the generator will take attribute pairs described by name and type. These attributes will 
    8     be used to prepopulate the migration to create the table for the model and give you a set of predefined fixture.     
    9     You don't have to think up all attributes up front, but it's a good idea of adding just the baseline of what's  
     8    be used to prepopulate the migration to create the table for the model and give you a set of predefined fixture.  By 
     9    default, created_at and updated_at timestamps are added to migration for you, so you needn't specify them by hand. 
     10    You don't have to think up all attributes up front, but it's a good idea of adding just the baseline of what's 
    1011    needed to start really working with the resource. 
    1112 
     
    2223            Migration:  db/migrate/XXX_add_accounts.rb 
    2324 
    24     ./script/generate model post title:string created_on:date body:text published:boolean 
    25      
     25    ./script/generate model post title:string body:text published:boolean 
     26 
    2627        Creates post model with predefined attributes. 
  • trunk/railties/lib/rails_generator/generators/components/resource/USAGE

    r6795 r6883  
    1111    described by name and type. These attributes will be used to 
    1212    prepopulate the migration to create the table for the model. For 
    13     example, "resource post title:string created_on:date body:text 
    14     published:boolean" will give you a Post model with those four attributes. 
     13    example, "resource post title:string body:text published:boolean" will 
     14    give you a Post model with those three attributes. 
     15 
     16    By default, created_at and updated_at timestamps are added to migration 
     17    for you, so you needn't specify them by hand. 
    1518 
    1619    You don't have to think up all attributes up front, but it's a good 
     
    2730Examples: 
    2831    ./script/generate resource post # no attributes 
    29     ./script/generate resource post title:string created_on:date body:text published:boolean 
    30     ./script/generate resource purchase order_id:integer created_at:datetime amount:decimal 
     32    ./script/generate resource post title:string body:text published:boolean 
     33    ./script/generate resource purchase order_id:integer amount:decimal 
  • trunk/railties/lib/rails_generator/generators/components/scaffold/USAGE

    r6768 r6883  
    77    view for HTML, one for an XML API, one for ATOM, etc). Everything comes 
    88    with sample unit and functional tests as well. 
    9      
     9 
    1010    The generator takes the name of the model as its first argument. This 
    1111    model name is then pluralized to get the controller name. So 
     
    1313    PostsController and will be intended for URLs like /posts and 
    1414    /posts/45. 
    15      
     15 
    1616    As additional parameters, the generator will take attribute pairs 
    1717    described by name and type. These attributes will be used to 
    1818    prepopulate the migration to create the table for the model and to give 
    19     you a set of templates for the view. For example, "scaffold 
    20     post title:string created_on:date body:text published:boolean" will 
    21     give you a model with those four attributes, forms to create and edit 
    22     those models from, and an index that'll list them all. 
    23      
     19    you a set of templates for the view. For example, "scaffold post 
     20    title:string body:text published:boolean" will give you a model with 
     21    those three attributes, forms to create and edit those models from, 
     22    and an index that'll list them all. 
     23 
     24    By default, created_at and updated_at timestamps are added to migration 
     25    for you, so you needn't specify them by hand. 
     26 
    2427    You don't have to think up all attributes up front, but it's a good 
    2528    idea of adding just the baseline of what's needed to start really 
    2629    working with the resource. 
    27      
     30 
    2831    The generator also adds a declaration to your config/routes.rb file 
    29     to hook up the rules that'll point URLs to this new resource. If you  
     32    to hook up the rules that'll point URLs to this new resource. If you 
    3033    create a resource like "scaffold post", it will add 
    31     "map.resources :posts" (notice the plural form) in the routes file,  
    32     making your new resource accessible from /posts.  
     34    "map.resources :posts" (notice the plural form) in the routes file, 
     35    making your new resource accessible from /posts. 
    3336 
    3437Examples: 
    3538    ./script/generate scaffold post # no attributes, view will be anemic 
    36     ./script/generate scaffold post title:string created_on:date body:text published:boolean 
    37     ./script/generate scaffold purchase order_id:integer created_at:datetime amount:decimal 
     39    ./script/generate scaffold post title:string body:text published:boolean 
     40    ./script/generate scaffold purchase order_id:integer amount:decimal 
  • trunk/railties/test/generators/generator_test_helper.rb

    r6824 r6883  
    129129  def assert_generated_fixtures_for(name) 
    130130    assert_generated_yaml "test/fixtures/#{name.to_s.underscore}" do |yaml| 
     131      assert_generated_timestamps(yaml) 
    131132      yield yaml if block_given? 
    132133    end 
     
    148149  # the migration body is passed to a block. 
    149150  def assert_generated_migration(name,parent="ActiveRecord::Migration") 
    150     assert_generated_class "db/migrate/001_#{name.to_s.underscore}",parent do |body|       
     151    assert_generated_class "db/migrate/001_#{name.to_s.underscore}",parent do |body|    
     152      assert body=~/timestamps/, "should have timestamps defined"    
    151153      yield body if block_given? 
    152154    end 
     
    175177  end 
    176178   
     179  private 
     180    # asserts that the default timestamps are created in the fixture 
     181    def assert_generated_timestamps(yaml) 
     182      yaml.values.each do |v| 
     183        ["created_at", "updated_at"].each do |field| 
     184          assert v.keys.include?(field), "should have #{field} field by default" 
     185        end 
     186      end             
     187    end 
    177188end