aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorfatkodima <fatkodima@rambler.ru>2017-12-24 04:19:33 +0200
committerfatkodima <fatkodima123@gmail.com>2018-03-23 19:09:59 +0200
commit6df0bd156af8e9229dd3069260a469c29a1e0fe3 (patch)
tree0af7cad1459668bf5dfa0129ddda07d24c8e0ad5 /railties
parent6aa5cf03ea8232180ffbbae4c130b051f813c670 (diff)
downloadrails-6df0bd156af8e9229dd3069260a469c29a1e0fe3.tar.gz
rails-6df0bd156af8e9229dd3069260a469c29a1e0fe3.tar.bz2
rails-6df0bd156af8e9229dd3069260a469c29a1e0fe3.zip
Gracefully handle extra "controller" when generating controller
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/generators/rails/controller/controller_generator.rb12
-rw-r--r--railties/test/generators/controller_generator_test.rb22
2 files changed, 33 insertions, 1 deletions
diff --git a/railties/lib/rails/generators/rails/controller/controller_generator.rb b/railties/lib/rails/generators/rails/controller/controller_generator.rb
index 6e2495d45f..eb75e7e661 100644
--- a/railties/lib/rails/generators/rails/controller/controller_generator.rb
+++ b/railties/lib/rails/generators/rails/controller/controller_generator.rb
@@ -20,10 +20,20 @@ module Rails
route generate_routing_code
end
- hook_for :template_engine, :test_framework, :helper, :assets
+ hook_for :template_engine, :test_framework, :helper, :assets do |generator|
+ invoke generator, [ remove_possible_suffix(name), actions ]
+ end
private
+ def file_name
+ @_file_name ||= remove_possible_suffix(super)
+ end
+
+ def remove_possible_suffix(name)
+ name.sub(/_?controller$/i, "")
+ end
+
# This method creates nested route entry for namespaced resources.
# For eg. rails g controller foo/bar/baz index show
# Will generate -
diff --git a/railties/test/generators/controller_generator_test.rb b/railties/test/generators/controller_generator_test.rb
index 91e4a86775..021004c9b8 100644
--- a/railties/test/generators/controller_generator_test.rb
+++ b/railties/test/generators/controller_generator_test.rb
@@ -116,4 +116,26 @@ class ControllerGeneratorTest < Rails::Generators::TestCase
assert_no_match(/namespace :admin/, routes)
end
end
+
+ def test_controller_suffix_is_not_duplicated
+ run_generator ["account_controller"]
+
+ assert_no_file "app/controllers/account_controller_controller.rb"
+ assert_file "app/controllers/account_controller.rb"
+
+ assert_no_file "app/views/account_controller/"
+ assert_file "app/views/account/"
+
+ assert_no_file "test/controllers/account_controller_controller_test.rb"
+ assert_file "test/controllers/account_controller_test.rb"
+
+ assert_no_file "app/helpers/account_controller_helper.rb"
+ assert_file "app/helpers/account_helper.rb"
+
+ assert_no_file "app/assets/javascripts/account_controller.js"
+ assert_file "app/assets/javascripts/account.js"
+
+ assert_no_file "app/assets/stylesheets/account_controller.css"
+ assert_file "app/assets/stylesheets/account.css"
+ end
end