aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--railties/CHANGELOG2
-rw-r--r--railties/lib/rails_generator/generators/components/model/USAGE9
-rw-r--r--railties/lib/rails_generator/generators/components/model/templates/fixtures.yml4
-rw-r--r--railties/lib/rails_generator/generators/components/model/templates/migration.rb1
-rw-r--r--railties/lib/rails_generator/generators/components/resource/USAGE11
-rw-r--r--railties/lib/rails_generator/generators/components/scaffold/USAGE29
-rw-r--r--railties/test/generators/generator_test_helper.rb13
7 files changed, 47 insertions, 22 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index 7f906d55af..f40fc77ab7 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Generated migrations include timestamps by default. #8501 [shane]
+
* Drop Action Web Service from rails:freeze:edge. [Jeremy Kemper]
* Add db:create, drop, reset, charset, and collation tasks. #8448 [matt]
diff --git a/railties/lib/rails_generator/generators/components/model/USAGE b/railties/lib/rails_generator/generators/components/model/USAGE
index d9efa7a000..b93cf73716 100644
--- a/railties/lib/rails_generator/generators/components/model/USAGE
+++ b/railties/lib/rails_generator/generators/components/model/USAGE
@@ -5,8 +5,9 @@ Description:
should not be suffixed with 'Model'.
As additional parameters, the generator will take attribute pairs described by name and type. These attributes will
- be used to prepopulate the migration to create the table for the model and give you a set of predefined fixture.
- 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
+ be used to prepopulate the migration to create the table for the model and give you a set of predefined fixture. By
+ default, created_at and updated_at timestamps are added to migration for you, so you needn't specify them by hand.
+ 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
needed to start really working with the resource.
The generator creates a model class in app/models, a test suite in test/unit, test fixtures in
@@ -21,6 +22,6 @@ Examples:
Fixtures: test/fixtures/accounts.yml
Migration: db/migrate/XXX_add_accounts.rb
- ./script/generate model post title:string created_on:date body:text published:boolean
-
+ ./script/generate model post title:string body:text published:boolean
+
Creates post model with predefined attributes.
diff --git a/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml b/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml
index 6be3c81bee..eb3ab81ab8 100644
--- a/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml
+++ b/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml
@@ -4,8 +4,12 @@ one:
<% for attribute in attributes -%>
<%= attribute.name %>: <%= attribute.default %>
<% end -%>
+ created_at: <%= Time.now.to_s(:db) %>
+ updated_at: <%= Time.now.to_s(:db) %>
two:
id: 2
<% for attribute in attributes -%>
<%= attribute.name %>: <%= attribute.default %>
<% end -%>
+ created_at: <%= Time.now.to_s(:db) %>
+ updated_at: <%= Time.now.to_s(:db) %>
diff --git a/railties/lib/rails_generator/generators/components/model/templates/migration.rb b/railties/lib/rails_generator/generators/components/model/templates/migration.rb
index cb1ddd399e..b42aa81262 100644
--- a/railties/lib/rails_generator/generators/components/model/templates/migration.rb
+++ b/railties/lib/rails_generator/generators/components/model/templates/migration.rb
@@ -4,6 +4,7 @@ class <%= migration_name %> < ActiveRecord::Migration
<% for attribute in attributes -%>
t.<%= attribute.type %> :<%= attribute.name %>
<% end -%>
+ t.timestamps
end
end
diff --git a/railties/lib/rails_generator/generators/components/resource/USAGE b/railties/lib/rails_generator/generators/components/resource/USAGE
index 37faf2aba9..d396775a41 100644
--- a/railties/lib/rails_generator/generators/components/resource/USAGE
+++ b/railties/lib/rails_generator/generators/components/resource/USAGE
@@ -10,8 +10,11 @@ Description:
As additional parameters, the generator will take attribute pairs
described by name and type. These attributes will be used to
prepopulate the migration to create the table for the model. For
- example, "resource post title:string created_on:date body:text
- published:boolean" will give you a Post model with those four attributes.
+ example, "resource post title:string body:text published:boolean" will
+ give you a Post model with those three attributes.
+
+ By default, created_at and updated_at timestamps are added to migration
+ for you, so you needn't specify them by hand.
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 needed to start really
@@ -26,5 +29,5 @@ Description:
Examples:
./script/generate resource post # no attributes
- ./script/generate resource post title:string created_on:date body:text published:boolean
- ./script/generate resource purchase order_id:integer created_at:datetime amount:decimal
+ ./script/generate resource post title:string body:text published:boolean
+ ./script/generate resource purchase order_id:integer amount:decimal
diff --git a/railties/lib/rails_generator/generators/components/scaffold/USAGE b/railties/lib/rails_generator/generators/components/scaffold/USAGE
index a194f8d415..ecbb98300d 100644
--- a/railties/lib/rails_generator/generators/components/scaffold/USAGE
+++ b/railties/lib/rails_generator/generators/components/scaffold/USAGE
@@ -6,32 +6,35 @@ Description:
(GET/POST/PUT/DELETE) and is prepared for multi-client access (like one
view for HTML, one for an XML API, one for ATOM, etc). Everything comes
with sample unit and functional tests as well.
-
+
The generator takes the name of the model as its first argument. This
model name is then pluralized to get the controller name. So
"scaffold post" will generate a Post model and a
PostsController and will be intended for URLs like /posts and
/posts/45.
-
+
As additional parameters, the generator will take attribute pairs
described by name and type. These attributes will be used to
prepopulate the migration to create the table for the model and to give
- you a set of templates for the view. For example, "scaffold
- post title:string created_on:date body:text published:boolean" will
- give you a model with those four attributes, forms to create and edit
- those models from, and an index that'll list them all.
-
+ you a set of templates for the view. For example, "scaffold post
+ title:string body:text published:boolean" will give you a model with
+ those three attributes, forms to create and edit those models from,
+ and an index that'll list them all.
+
+ By default, created_at and updated_at timestamps are added to migration
+ for you, so you needn't specify them by hand.
+
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 needed to start really
working with the resource.
-
+
The generator also adds a declaration to your config/routes.rb file
- to hook up the rules that'll point URLs to this new resource. If you
+ to hook up the rules that'll point URLs to this new resource. If you
create a resource like "scaffold post", it will add
- "map.resources :posts" (notice the plural form) in the routes file,
- making your new resource accessible from /posts.
+ "map.resources :posts" (notice the plural form) in the routes file,
+ making your new resource accessible from /posts.
Examples:
./script/generate scaffold post # no attributes, view will be anemic
- ./script/generate scaffold post title:string created_on:date body:text published:boolean
- ./script/generate scaffold purchase order_id:integer created_at:datetime amount:decimal
+ ./script/generate scaffold post title:string body:text published:boolean
+ ./script/generate scaffold purchase order_id:integer amount:decimal
diff --git a/railties/test/generators/generator_test_helper.rb b/railties/test/generators/generator_test_helper.rb
index 0bfb2a0964..ee971d9669 100644
--- a/railties/test/generators/generator_test_helper.rb
+++ b/railties/test/generators/generator_test_helper.rb
@@ -128,6 +128,7 @@ module GeneratorTestHelper
# the parsed yaml tree is passed to a block.
def assert_generated_fixtures_for(name)
assert_generated_yaml "test/fixtures/#{name.to_s.underscore}" do |yaml|
+ assert_generated_timestamps(yaml)
yield yaml if block_given?
end
end
@@ -147,7 +148,8 @@ module GeneratorTestHelper
# It takes the name of the migration as a parameter.
# the migration body is passed to a block.
def assert_generated_migration(name,parent="ActiveRecord::Migration")
- assert_generated_class "db/migrate/001_#{name.to_s.underscore}",parent do |body|
+ assert_generated_class "db/migrate/001_#{name.to_s.underscore}",parent do |body|
+ assert body=~/timestamps/, "should have timestamps defined"
yield body if block_given?
end
end
@@ -174,4 +176,13 @@ module GeneratorTestHelper
assert body=~/t\.#{type.to_s} :#{name.to_s}/, "should have column #{name.to_s} defined"
end
+ private
+ # asserts that the default timestamps are created in the fixture
+ def assert_generated_timestamps(yaml)
+ yaml.values.each do |v|
+ ["created_at", "updated_at"].each do |field|
+ assert v.keys.include?(field), "should have #{field} field by default"
+ end
+ end
+ end
end \ No newline at end of file