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

Ticket #8501: add_timestamps_to_model_generator_by_default.diff

File add_timestamps_to_model_generator_by_default.diff, 8.6 kB (added by shane, 2 years ago)
  • vendor/rails/railties/test/generators/generator_test_helper.rb

    old new  
    128128  # the parsed yaml tree is passed to a block. 
    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 
    133134  end 
     
    147148  # It takes the name of the migration as a parameter. 
    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 
    153155  end 
     
    174176      assert body=~/t\.#{type.to_s} :#{name.to_s}/, "should have column #{name.to_s} defined" 
    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 
  • vendor/rails/railties/lib/rails_generator/generators/components/model/USAGE

    old new  
    55    should not be suffixed with 'Model'. 
    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  
    10     needed to start really working with the resource. 
     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, timestamps will be added to the migration for you, giving you created_at and updated_at fields. You don't 
     10    have to think up all attributes up front, but it's a good idea of adding just the baseline of what's needed to 
     11    start really working with the resource. 
    1112 
    1213    The generator creates a model class in app/models, a test suite in test/unit, test fixtures in 
    1314    test/fixtures/singular_name.yml, and a migration in db/migrate. 
  • vendor/rails/railties/lib/rails_generator/generators/components/model/model_generator.rb

    old new  
    11class ModelGenerator < Rails::Generator::NamedBase 
    22  default_options :skip_migration => false 
     3  default_options :skip_timestamps => false 
    34 
    45  def manifest 
    56    record do |m| 
     
    3435      opt.separator 'Options:' 
    3536      opt.on("--skip-migration",  
    3637             "Don't generate a migration file for this model") { |v| options[:skip_migration] = v } 
     38      opt.on("--skip-timestamps",  
     39             "Don't add timestamps to the migration") { |v| options[:skip_timestamps] = v } 
    3740    end 
    3841end 
  • vendor/rails/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml

    old new  
    44<% for attribute in attributes -%> 
    55  <%= attribute.name %>: <%= attribute.default %> 
    66<% end -%> 
     7<% unless options[:skip_timestamps] -%> 
     8  created_at: <%= Time.now.to_s(:db) %> 
     9  updated_at: <%= Time.now.to_s(:db) %> 
     10<% end -%> 
    711two: 
    812  id: 2 
    913<% for attribute in attributes -%> 
    1014  <%= attribute.name %>: <%= attribute.default %> 
    1115<% end -%> 
     16<% unless options[:skip_timestamps] -%> 
     17  created_at: <%= Time.now.to_s(:db) %> 
     18  updated_at: <%= Time.now.to_s(:db) %> 
     19<% end -%> 
  • vendor/rails/railties/lib/rails_generator/generators/components/model/templates/migration.rb

    old new  
    44<% for attribute in attributes -%> 
    55      t.<%= attribute.type %> :<%= attribute.name %> 
    66<% end -%> 
     7    <% unless options[:skip_timestamps] -%>t.timestamps <% end -%>  
    78    end 
    89  end 
    910 
  • vendor/rails/railties/lib/rails_generator/generators/components/scaffold/USAGE

    old new  
    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, timestamps will be added to the migration for you, giving 
     25    you created_at and updated_at fields. 
     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. 
     
    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 
  • vendor/rails/railties/lib/rails_generator/generators/components/resource/USAGE

    old new  
    1010    As additional parameters, the generator will take attribute pairs 
    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. 
    1515 
     16    By default, timestamps will be added to the migration for you, giving 
     17    you created_at and updated_at fields. 
     18 
    1619    You don't have to think up all attributes up front, but it's a good 
    1720    idea of adding just the baseline of what's needed to start really 
    1821    working with the resource. 
     
    2629 
    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