diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index 9d96aa9e6ea099832c6558bd9cafc7bcb4ee1571..cfbf52de0bac31dbd1402b4f61596aea7be50238 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added --skip-migration option to scaffold and resource generators #8656 [Michael Glaesemann]
+
* Fix that FCGIs would leave log files open when asked to shut down by USR2 #3028 [sebastian.kanthak/josh]
* Fixed that dispatcher preparation callbacks only run once in production mode. Mock Routes.reload so that dispatcher preparation callback tests run. [Rick]
diff --git a/railties/lib/rails_generator/generators/components/resource/resource_generator.rb b/railties/lib/rails_generator/generators/components/resource/resource_generator.rb
index 5d9eefb5b22fb8e73d6956b590d016bd3e73d716..cb0952dab88112046844f29f6297bccd0dcd9cf2 100644
--- a/railties/lib/rails_generator/generators/components/resource/resource_generator.rb
+++ b/railties/lib/rails_generator/generators/components/resource/resource_generator.rb
@@ -1,4 +1,6 @@
class ResourceGenerator < Rails::Generator::NamedBase
+ default_options :skip_migration => false
+
attr_reader :controller_name,
:controller_class_path,
:controller_file_path,
@@ -57,7 +59,14 @@ def banner
"Usage: #{$0} resource ModelName [field:type, field:type]"
end
- def model_name
+ def add_options!(opt)
+ opt.separator ''
+ opt.separator 'Options:'
+ opt.on("--skip-migration",
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
+ end
+
+ def model_name
class_name.demodulize
end
end
diff --git a/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb b/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb
index 11c80d20204ce007dcefc6340b0837812d867359..b26300e024aa4003d6fd403ced3e836834b6bd28 100644
--- a/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb
+++ b/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb
@@ -1,4 +1,6 @@
class ScaffoldGenerator < Rails::Generator::NamedBase
+ default_options :skip_migration => false
+
attr_reader :controller_name,
:controller_class_path,
:controller_file_path,
@@ -51,7 +53,7 @@ def manifest
m.template('layout.html.erb', File.join('app/views/layouts', controller_class_path, "#{controller_file_name}.html.erb"))
m.template('style.css', 'public/stylesheets/scaffold.css')
- m.dependency 'model', [singular_name] + @args, :collision => :skip
+ m.dependency 'model', [singular_name] + @args, :collision => :skip
m.template(
'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
@@ -70,11 +72,18 @@ def banner
"Usage: #{$0} scaffold ModelName [field:type, field:type]"
end
+ def add_options!(opt)
+ opt.separator ''
+ opt.separator 'Options:'
+ opt.on("--skip-migration",
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
+ end
+
def scaffold_views
%w[ index show new edit ]
end
- def model_name
+ def model_name
class_name.demodulize
end
end
diff --git a/railties/test/generators/generator_test_helper.rb b/railties/test/generators/generator_test_helper.rb
index ee971d9669a8b21c15f1f0c99f4e4e53c575c7df..be23f5c6eb51481731e6867b0e1cb9d26dcca963 100644
--- a/railties/test/generators/generator_test_helper.rb
+++ b/railties/test/generators/generator_test_helper.rb
@@ -3,14 +3,14 @@ module GeneratorTestHelper
def build_generator(name,params)
Rails::Generator::Base.instance(name,params)
end
-
- # Runs the create command (like the command line does)
+
+ # Runs the create command (like the command line does)
def run_generator(name,params)
- silence_generator do
- build_generator(name,params).command(:create).invoke!
- end
+ silence_generator do
+ build_generator(name,params).command(:create).invoke!
+ end
end
-
+
# Silences the logger temporarily and returns the output as a String
def silence_generator
logger_original=Rails::Generator::Base.logger
@@ -20,7 +20,7 @@ def silence_generator
Rails::Generator::Base.logger=logger_original
myout.string
end
-
+
# asserts that the given controller was generated.
# It takes a name or symbol without the _controller part and an optional super class.
# The contents of the class source file is passed to a block.
@@ -29,7 +29,7 @@ def assert_generated_controller_for(name,parent="ApplicationController")
yield body if block_given?
end
end
-
+
# asserts that the given model was generated.
# It takes a name or symbol and an optional super class.
# the contents of the class source file is passed to a block.
@@ -47,7 +47,7 @@ def assert_generated_helper_for(name)
yield body if block_given?
end
end
-
+
# asserts that the given functional test was generated.
# It takes a name or symbol without the _controller_test part and an optional super class.
# the contents of the class source file is passed to a block.
@@ -74,45 +74,45 @@ def assert_generated_file(path)
yield f.read if block_given?
end
end
-
+
# asserts that the given file exists
def assert_file_exists(path)
assert File.exists?("#{RAILS_ROOT}/#{path}"),"The file '#{path}' should exist"
end
-
+
# asserts that the given class source file was generated.
# It takes a path without the .rb part and an optional super class.
# the contents of the class source file is passed to a block.
def assert_generated_class(path,parent=nil)
path=~/\/?(\d+_)?(\w+)$/
class_name=$2.camelize
- assert_generated_file("#{path}.rb") do |body|
+ assert_generated_file("#{path}.rb") do |body|
assert body=~/class #{class_name}#{parent.nil? ? '':" < #{parent}"}/,"the file '#{path}.rb' should be a class"
yield body if block_given?
end
end
-
+
# asserts that the given module source file was generated.
# It takes a path without the .rb part.
# the contents of the class source file is passed to a block.
- def assert_generated_module(path)
+ def assert_generated_module(path)
path=~/\/?(\w+)$/
module_name=$1.camelize
- assert_generated_file("#{path}.rb") do |body|
+ assert_generated_file("#{path}.rb") do |body|
assert body=~/module #{module_name}/,"the file '#{path}.rb' should be a module"
yield body if block_given?
end
end
-
+
# asserts that the given css stylesheet file was generated.
# It takes a path without the .css part.
# the contents of the stylesheet source file is passed to a block.
def assert_generated_stylesheet(path)
- assert_generated_file("public/stylesheets/#{path}.css") do |body|
+ assert_generated_file("public/stylesheets/#{path}.css") do |body|
yield body if block_given?
- end
+ end
end
-
+
# asserts that the given yaml file was generated.
# It takes a path without the .yml part.
# the parsed yaml tree is passed to a block.
@@ -120,9 +120,9 @@ def assert_generated_yaml(path)
assert_generated_file("#{path}.yml") do |body|
assert yaml=YAML.load(body)
yield yaml if block_given?
- end
+ end
end
-
+
# asserts that the given fixtures yaml file was generated.
# It takes a fixture name without the .yml part.
# the parsed yaml tree is passed to a block.
@@ -132,36 +132,43 @@ def assert_generated_fixtures_for(name)
yield yaml if block_given?
end
end
-
+
# asserts that the given views were generated.
# It takes a controller name and a list of views (including extensions).
# The body of each view is passed to a block
def assert_generated_views_for(name,*actions)
actions.each do |action|
- assert_generated_file("app/views/#{name.to_s.underscore}/#{action.to_s}") do |body|
+ assert_generated_file("app/views/#{name.to_s.underscore}/#{action.to_s}") do |body|
yield body if block_given?
- end
+ end
end
end
-
+
# asserts that the given migration file was generated.
# It takes the name of the migration as a parameter.
- # the migration body is passed to a block.
+ # 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 body=~/timestamps/, "should have timestamps defined"
+ 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
-
+
+ # Asserts that the given migration file was not generated.
+ # It takes the name of the migration as a parameter.
+ def assert_skipped_migration(name)
+ migration_file = "#{RAILS_ROOT}/db/migrate/001_#{name.to_s.underscore}.rb"
+ assert !File.exists?(migration_file), "should not create migration #{migration_file}"
+ end
+
# asserts that the given resource was added to the routes.
def assert_added_route_for(name)
assert_generated_file("config/routes.rb") do |body|
assert body=~/map.resources :#{name.to_s.underscore}/,"should add route for :#{name.to_s.underscore}"
end
end
-
- # asserts that the given methods are defined in the body.
+
+ # asserts that the given methods are defined in the body.
# This does assume standard rails code conventions with regards to the source code.
# The body of each individual method is passed to a block.
def assert_has_method(body,*methods)
@@ -170,12 +177,12 @@ def assert_has_method(body,*methods)
yield( name, $1 ) if block_given?
end
end
-
+
# asserts that the given column is defined in the migration
def assert_generated_column(body,name,type)
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)
@@ -183,6 +190,6 @@ def assert_generated_timestamps(yaml)
["created_at", "updated_at"].each do |field|
assert v.keys.include?(field), "should have #{field} field by default"
end
- end
+ end
end
-end
\ No newline at end of file
+end
diff --git a/railties/test/generators/rails_scaffold_generator_test.rb b/railties/test/generators/rails_scaffold_generator_test.rb
index 419ff2b6ff058e484bfd7606d2a265d9d2eab345..4de175b45e3f39bd4470a0b7484e19e45cb3b6a3 100644
--- a/railties/test/generators/rails_scaffold_generator_test.rb
+++ b/railties/test/generators/rails_scaffold_generator_test.rb
@@ -42,10 +42,11 @@ class InstanceTag; end
# Must set before requiring generator libs.
+tmp_dir="#{File.dirname(__FILE__)}/../fixtures/tmp"
if defined?(RAILS_ROOT)
- RAILS_ROOT.replace "#{File.dirname(__FILE__)}/../fixtures/tmp"
+ RAILS_ROOT.replace(tmp_dir)
else
- RAILS_ROOT = "#{File.dirname(__FILE__)}/../fixtures/tmp"
+ RAILS_ROOT=tmp_dir
end
Dir.mkdir(RAILS_ROOT) unless File.exists?(RAILS_ROOT)
@@ -127,6 +128,20 @@ def test_scaffold_generates_resources
assert_added_route_for :products
end
+ def test_scaffold_skip_migration_skips_migration
+ run_generator('scaffold', %w(Product --skip-migration))
+
+ assert_generated_model_for :product
+ assert_generated_functional_test_for :products
+ assert_generated_unit_test_for :product
+ assert_generated_fixtures_for :products
+ assert_generated_helper_for :products
+ assert_generated_stylesheet :scaffold
+ assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb"
+ assert_skipped_migration :create_products
+ assert_added_route_for :products
+ end
+
def test_scaffold_generates_resources_with_attributes
run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp))
@@ -166,4 +181,5 @@ def test_scaffold_generates_resources_with_attributes
assert_added_route_for :products
end
+
end