mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #31554 from fatkodima/controller_generator_suffix
Gracefully handle extra "controller" when generating controller
This commit is contained in:
commit
7316f3696a
2 changed files with 33 additions and 1 deletions
|
@ -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 -
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue