public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Warn and uses singularized ModelName if a plural ModelName is given to 
script/generate. Override with --force-plural. [#333 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
pcg79 (author)
Wed Jun 04 12:13:50 -0700 2008
lifo (committer)
Fri Jun 27 08:35:26 -0700 2008
commit  4ddca325eeef46e640a225efb9b297260e1e8e7d
tree    aac70c659ec7c6e2b50ad1740708601edda8f651
parent  3c1e8ab0fecd5ee7f9328578016ef1e61214eeee
...
1
2
 
3
4
5
...
16
17
18
 
 
 
 
 
19
20
21
...
81
82
83
 
 
84
85
86
...
1
 
2
3
4
5
...
16
17
18
19
20
21
22
23
24
25
26
...
86
87
88
89
90
91
92
93
0
@@ -1,5 +1,5 @@
0
 class ScaffoldGenerator < Rails::Generator::NamedBase
0
- default_options :skip_timestamps => false, :skip_migration => false
0
+ default_options :skip_timestamps => false, :skip_migration => false, :force_plural => false
0
 
0
   attr_reader :controller_name,
0
                 :controller_class_path,
0
@@ -16,6 +16,11 @@ class ScaffoldGenerator < Rails::Generator::NamedBase
0
   def initialize(runtime_args, runtime_options = {})
0
     super
0
 
0
+ if @name == @name.pluralize && !options[:force_plural]
0
+ logger.warning "Plural version of the model detected, using singularized version. Override with --force-plural."
0
+ @name = @name.singularize
0
+ end
0
+
0
     @controller_name = @name.pluralize
0
 
0
     base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
0
@@ -81,6 +86,8 @@ class ScaffoldGenerator < Rails::Generator::NamedBase
0
              "Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
0
       opt.on("--skip-migration",
0
              "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
0
+ opt.on("--force-plural",
0
+ "Forces the generation of a plural ModelName") { |v| options[:force_plural] = v }
0
     end
0
 
0
     def scaffold_views
...
1
 
2
3
4
...
104
105
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
...
1
2
3
4
5
...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
0
@@ -1,4 +1,5 @@
0
 require 'generators/generator_test_helper'
0
+require 'abstract_unit'
0
 
0
 class RailsScaffoldGeneratorTest < GeneratorTestCase
0
   
0
@@ -104,4 +105,45 @@ class RailsScaffoldGeneratorTest < GeneratorTestCase
0
     assert_added_route_for :products
0
   end
0
 
0
+ uses_mocha("scaffold_force_plural_names") do
0
+ def test_scaffolded_plural_names
0
+ Rails::Generator::Base.logger.expects(:warning)
0
+ g = Rails::Generator::Base.instance('scaffold', %w(ProductLines))
0
+ assert_equal "ProductLines", g.controller_name
0
+ assert_equal "ProductLines", g.controller_class_name
0
+ assert_equal "ProductLine", g.controller_singular_name
0
+ assert_equal "product_lines", g.controller_plural_name
0
+ assert_equal "product_lines", g.controller_file_name
0
+ assert_equal "product_lines", g.controller_table_name
0
+ end
0
+ end
0
+
0
+ def test_scaffold_plural_model_name_without_force_plural_generates_singular_model
0
+ run_generator('scaffold', %w(Products name:string))
0
+
0
+ assert_generated_model_for :product
0
+ assert_generated_functional_test_for :products
0
+ assert_generated_unit_test_for :product
0
+ assert_generated_fixtures_for :products
0
+ assert_generated_helper_for :products
0
+ assert_generated_stylesheet :scaffold
0
+ assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb"
0
+ assert_skipped_migration :create_products
0
+ assert_added_route_for :products
0
+ end
0
+
0
+ def test_scaffold_plural_model_name_with_force_plural_forces_plural_model
0
+ run_generator('scaffold', %w(Products name:string --force-plural))
0
+
0
+ assert_generated_model_for :products
0
+ assert_generated_functional_test_for :products
0
+ assert_generated_unit_test_for :products
0
+ assert_generated_fixtures_for :products
0
+ assert_generated_helper_for :products
0
+ assert_generated_stylesheet :scaffold
0
+ assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb"
0
+ assert_skipped_migration :create_products
0
+ assert_added_route_for :products
0
+ end
0
+
0
 end

Comments

    No one has commented yet.