From 496a2f97e3b41ffb1e39f2db8d8792d9e505a354 Mon Sep 17 00:00:00 2001 From: Jose Galisteo Date: Fri, 17 Dec 2021 00:19:43 +0100 Subject: [PATCH] Fix scaffold controller generator with namespace (#512) * Fix scaffold controller generator with namespace For example: ``` bundle exec rails g scaffold_controller Admin::Post title:string content:text --model-name=Post ``` It fixes the code generated by jbuilder, other changes are required on railties gem. * Specify main branch for rails-head * Add test for controller scaffold with namespace Only for Rails::VERSION::MAJOR >= 6 because the method `show_helper` doesn't handle well the namespaces on previous versions. Co-authored-by: David Heinemeier Hansson --- lib/generators/rails/templates/controller.rb | 4 ++-- test/scaffold_controller_generator_test.rb | 23 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/generators/rails/templates/controller.rb b/lib/generators/rails/templates/controller.rb index b314e96..92f5da7 100644 --- a/lib/generators/rails/templates/controller.rb +++ b/lib/generators/rails/templates/controller.rb @@ -30,7 +30,7 @@ def create respond_to do |format| if @<%= orm_instance.save %> - format.html { redirect_to @<%= singular_table_name %>, notice: <%= %("#{human_name} was successfully created.") %> } + format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully created.") %> } format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> } else format.html { render :new, status: :unprocessable_entity } @@ -43,7 +43,7 @@ def create def update respond_to do |format| if @<%= orm_instance.update("#{singular_table_name}_params") %> - format.html { redirect_to @<%= singular_table_name %>, notice: <%= %("#{human_name} was successfully updated.") %> } + format.html { redirect_to <%= show_helper %>, notice: <%= %("#{human_name} was successfully updated.") %> } format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> } else format.html { render :edit, status: :unprocessable_entity } diff --git a/test/scaffold_controller_generator_test.rb b/test/scaffold_controller_generator_test.rb index f378300..10bf26b 100644 --- a/test/scaffold_controller_generator_test.rb +++ b/test/scaffold_controller_generator_test.rb @@ -31,14 +31,14 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_instance_method :create, content do |m| assert_match %r{@post = Post\.new\(post_params\)}, m assert_match %r{@post\.save}, m - assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully created\." \}}, m + assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully created\." \}}, m assert_match %r{format\.json \{ render :show, status: :created, location: @post \}}, m assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m end assert_instance_method :update, content do |m| - assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully updated\." \}}, m + assert_match %r{format\.html \{ redirect_to post_url\(@post\), notice: "Post was successfully updated\." \}}, m assert_match %r{format\.json \{ render :show, status: :ok, location: @post \}}, m assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m @@ -59,6 +59,25 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end end + if Rails::VERSION::MAJOR >= 6 + test 'controller with namespace' do + run_generator %w(Admin::Post --model-name=Post) + assert_file 'app/controllers/admin/posts_controller.rb' do |content| + assert_instance_method :create, content do |m| + assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully created\." \}}, m + end + + assert_instance_method :update, content do |m| + assert_match %r{format\.html \{ redirect_to admin_post_url\(@post\), notice: "Post was successfully updated\." \}}, m + end + + assert_instance_method :destroy, content do |m| + assert_match %r{format\.html \{ redirect_to admin_posts_url, notice: "Post was successfully destroyed\." \}}, m + end + end + end + end + test "don't use require and permit if there are no attributes" do run_generator %w(Post)