Gracefully handle extra "controller" when generating controller

This commit is contained in:
fatkodima 2017-12-24 04:19:33 +02:00 committed by fatkodima
parent 6aa5cf03ea
commit 6df0bd156a
2 changed files with 33 additions and 1 deletions

@ -20,10 +20,20 @@ def add_routes
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 @@ def test_does_not_add_routes_when_action_is_not_specified
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