Changeset 6883
- Timestamp:
- 05/29/07 00:11:56 (3 years ago)
- Files:
-
- trunk/railties/CHANGELOG (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml (modified) (2 diffs)
- trunk/railties/lib/rails_generator/generators/components/model/templates/migration.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/model/USAGE (modified) (2 diffs)
- trunk/railties/lib/rails_generator/generators/components/resource/USAGE (modified) (2 diffs)
- trunk/railties/lib/rails_generator/generators/components/scaffold/USAGE (modified) (2 diffs)
- trunk/railties/test/generators/generator_test_helper.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/railties/CHANGELOG
r6872 r6883 1 1 *SVN* 2 3 * Generated migrations include timestamps by default. #8501 [shane] 2 4 3 5 * Drop Action Web Service from rails:freeze:edge. [Jeremy Kemper] trunk/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml
r6869 r6883 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 … … 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) %> trunk/railties/lib/rails_generator/generators/components/model/templates/migration.rb
r6667 r6883 5 5 t.<%= attribute.type %> :<%= attribute.name %> 6 6 <% end -%> 7 t.timestamps 7 8 end 8 9 end trunk/railties/lib/rails_generator/generators/components/model/USAGE
r5236 r6883 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 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 10 11 needed to start really working with the resource. 11 12 … … 22 23 Migration: db/migrate/XXX_add_accounts.rb 23 24 24 ./script/generate model post title:string created_on:datebody:text published:boolean25 25 ./script/generate model post title:string body:text published:boolean 26 26 27 Creates post model with predefined attributes. trunk/railties/lib/rails_generator/generators/components/resource/USAGE
r6795 r6883 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: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. 15 18 16 19 You don't have to think up all attributes up front, but it's a good … … 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 trunk/railties/lib/rails_generator/generators/components/scaffold/USAGE
r6768 r6883 7 7 view for HTML, one for an XML API, one for ATOM, etc). Everything comes 8 8 with sample unit and functional tests as well. 9 9 10 10 The generator takes the name of the model as its first argument. This 11 11 model name is then pluralized to get the controller name. So … … 13 13 PostsController and will be intended for URLs like /posts and 14 14 /posts/45. 15 15 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, created_at and updated_at timestamps are added to migration 25 for you, so you needn't specify them by hand. 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. 27 30 28 31 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 30 33 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. 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 trunk/railties/test/generators/generator_test_helper.rb
r6824 r6883 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 … … 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 … … 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