Permalink
Browse files

Warn and uses singularized ModelName if a plural ModelName is given t…

…o script/generate. Override with --force-plural. [#333 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
1 parent 3c1e8ab commit 4ddca325eeef46e640a225efb9b297260e1e8e7d @pcg79 pcg79 committed with lifo Jun 4, 2008
@@ -1,5 +1,5 @@
class ScaffoldGenerator < Rails::Generator::NamedBase
- default_options :skip_timestamps => false, :skip_migration => false
+ default_options :skip_timestamps => false, :skip_migration => false, :force_plural => false
attr_reader :controller_name,
:controller_class_path,
@@ -16,6 +16,11 @@ class ScaffoldGenerator < Rails::Generator::NamedBase
def initialize(runtime_args, runtime_options = {})
super
+ if @name == @name.pluralize && !options[:force_plural]
+ logger.warning "Plural version of the model detected, using singularized version. Override with --force-plural."
+ @name = @name.singularize
+ end
+
@controller_name = @name.pluralize
base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
@@ -81,6 +86,8 @@ def add_options!(opt)
"Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
opt.on("--skip-migration",
"Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
+ opt.on("--force-plural",
+ "Forces the generation of a plural ModelName") { |v| options[:force_plural] = v }
end
def scaffold_views
@@ -1,4 +1,5 @@
require 'generators/generator_test_helper'
+require 'abstract_unit'
class RailsScaffoldGeneratorTest < GeneratorTestCase
@@ -104,4 +105,45 @@ def test_scaffold_generates_resources_with_attributes
assert_added_route_for :products
end
+ uses_mocha("scaffold_force_plural_names") do
+ def test_scaffolded_plural_names
+ Rails::Generator::Base.logger.expects(:warning)
+ g = Rails::Generator::Base.instance('scaffold', %w(ProductLines))
+ assert_equal "ProductLines", g.controller_name
+ assert_equal "ProductLines", g.controller_class_name
+ assert_equal "ProductLine", g.controller_singular_name
+ assert_equal "product_lines", g.controller_plural_name
+ assert_equal "product_lines", g.controller_file_name
+ assert_equal "product_lines", g.controller_table_name
+ end
+ end
+
+ def test_scaffold_plural_model_name_without_force_plural_generates_singular_model
+ run_generator('scaffold', %w(Products name:string))
+
+ 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_plural_model_name_with_force_plural_forces_plural_model
+ run_generator('scaffold', %w(Products name:string --force-plural))
+
+ assert_generated_model_for :products
+ assert_generated_functional_test_for :products
+ assert_generated_unit_test_for :products
+ 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
+
end

0 comments on commit 4ddca32

Please sign in to comment.