Ticket #8501: add_timestamps_to_model_generator_by_default3.diff
| File add_timestamps_to_model_generator_by_default3.diff, 7.7 kB (added by shane, 2 years ago) |
|---|
-
railties/test/generators/generator_test_helper.rb
old new 128 128 # the parsed yaml tree is passed to a block. 129 129 def assert_generated_fixtures_for(name) 130 130 assert_generated_yaml "test/fixtures/#{name.to_s.underscore}" do |yaml| 131 assert_generated_timestamps(yaml) 131 132 yield yaml if block_given? 132 133 end 133 134 end … … 147 148 # It takes the name of the migration as a parameter. 148 149 # the migration body is passed to a block. 149 150 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" 151 153 yield body if block_given? 152 154 end 153 155 end … … 174 176 assert body=~/t\.#{type.to_s} :#{name.to_s}/, "should have column #{name.to_s} defined" 175 177 end 176 178 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 177 188 end -
railties/lib/rails_generator/generators/components/model/USAGE
old new 5 5 should not be suffixed with 'Model'. 6 6 7 7 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. 11 12 12 13 The generator creates a model class in app/models, a test suite in test/unit, test fixtures in 13 14 test/fixtures/singular_name.yml, and a migration in db/migrate. … … 21 22 Fixtures: test/fixtures/accounts.yml 22 23 Migration: db/migrate/XXX_add_accounts.rb 23 24 24 ./script/generate model post title:string created_on:datebody:text published:boolean25 ./script/generate model post title:string body:text published:boolean 25 26 26 27 Creates post model with predefined attributes. -
railties/lib/rails_generator/generators/components/model/templates/fixtures.yml
old new 4 4 <% for attribute in attributes -%> 5 5 <%= attribute.name %>: <%= attribute.default %> 6 6 <% end -%> 7 created_at: <%= Time.now.to_s(:db) %> 8 updated_at: <%= Time.now.to_s(:db) %> 7 9 two: 8 10 id: 2 9 11 <% for attribute in attributes -%> 10 12 <%= attribute.name %>: <%= attribute.default %> 11 13 <% end -%> 14 created_at: <%= Time.now.to_s(:db) %> 15 updated_at: <%= Time.now.to_s(:db) %> -
railties/lib/rails_generator/generators/components/model/templates/migration.rb
old new 4 4 <% for attribute in attributes -%> 5 5 t.<%= attribute.type %> :<%= attribute.name %> 6 6 <% end -%> 7 t.timestamps 7 8 end 8 9 end 9 10 -
railties/lib/rails_generator/generators/components/scaffold/USAGE
old new 16 16 As additional parameters, the generator will take attribute pairs 17 17 described by name and type. These attributes will be used to 18 18 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 24 27 You don't have to think up all attributes up front, but it's a good 25 28 idea of adding just the baseline of what's needed to start really 26 29 working with the resource. … … 33 36 34 37 Examples: 35 38 ./script/generate scaffold post # no attributes, view will be anemic 36 ./script/generate scaffold post title:string created_on:datebody:text published:boolean37 ./script/generate scaffold purchase order_id:integer created_at:datetimeamount:decimal39 ./script/generate scaffold post title:string body:text published:boolean 40 ./script/generate scaffold purchase order_id:integer amount:decimal -
railties/lib/rails_generator/generators/components/resource/USAGE
old new 10 10 As additional parameters, the generator will take attribute pairs 11 11 described by name and type. These attributes will be used to 12 12 prepopulate the migration to create the table for the model. For 13 example, "resource post title:string created_on:date body:text14 published:boolean" will give you a Post model with those fourattributes.13 example, "resource post title:string body:text published:boolean" will 14 give you a Post model with those three attributes. 15 15 16 By default, timestamps will be added to the migration for you, giving 17 you created_at and updated_at fields. 18 16 19 You don't have to think up all attributes up front, but it's a good 17 20 idea of adding just the baseline of what's needed to start really 18 21 working with the resource. … … 26 29 27 30 Examples: 28 31 ./script/generate resource post # no attributes 29 ./script/generate resource post title:string created_on:datebody:text published:boolean30 ./script/generate resource purchase order_id:integer created_at:datetimeamount:decimal32 ./script/generate resource post title:string body:text published:boolean 33 ./script/generate resource purchase order_id:integer amount:decimal