From 55b2264746e6511c5b2d81c1a08e96010aa929ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Ram=C3=ADrez=20Norambuena?= Date: Tue, 11 Feb 2020 19:37:36 -0300 Subject: [PATCH] scaffold_controller: Include the routes when create a generator Controller When is use a scaffold_controller add the routes as resources to the config/route.rb Also enable to use --skip-routes if doesn't want include the resources into the config/routes.rb file. --- .../scaffold_controller_generator.rb | 6 ++++++ .../scaffold_controller_generator_test.rb | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb index b2cdeee6d1..a4d82bc07c 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb @@ -15,6 +15,8 @@ module Rails class_option :api, type: :boolean, desc: "Generates API controller" + class_option :skip_routes, type: :boolean, desc: "Don't add routes to config/routes.rb." + argument :attributes, type: :array, default: [], banner: "field:type field:type" def create_controller_files @@ -26,6 +28,10 @@ module Rails invoke template_engine unless options.api? end + hook_for :resource_route, required: true do |route| + invoke route unless options.skip_routes? + end + hook_for :test_framework, as: :scaffold # Invoke the helper using the controller name (pluralized) diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 1f29dc057b..cdf6e34ee4 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -12,6 +12,8 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase include GeneratorsTestHelper arguments %w(User name:string age:integer) + setup :copy_routes + def test_controller_skeleton_is_created run_generator @@ -98,6 +100,22 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end end + def test_controller_route_are_added + run_generator ["Message", "photos:attachments"] + + assert_file "config/routes.rb" do |route| + assert_match(/resources :messages$/, route) + end + end + + def test_controller_route_are_skipped + run_generator ["Message", "photos:attachments", "--skip-routes"] + + assert_file "config/routes.rb" do |route| + assert_no_match(/resources :messages$/, route) + end + end + def test_helper_are_invoked_with_a_pluralized_name run_generator assert_file "app/helpers/users_helper.rb", /module UsersHelper/