diff --git a/lib/generators/rails/scaffold_controller_generator.rb b/lib/generators/rails/scaffold_controller_generator.rb index 42407c0..3665899 100644 --- a/lib/generators/rails/scaffold_controller_generator.rb +++ b/lib/generators/rails/scaffold_controller_generator.rb @@ -15,6 +15,10 @@ class ScaffoldControllerGenerator def permitted_params attributes_names.map { |name| ":#{name}" }.join(', ') end unless private_method_defined? :permitted_params + + def status_unprocessable_content + ::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422) rescue :unprocessable_content + end end end end diff --git a/lib/generators/rails/templates/api_controller.rb b/lib/generators/rails/templates/api_controller.rb index 5c2222c..62cb8a6 100644 --- a/lib/generators/rails/templates/api_controller.rb +++ b/lib/generators/rails/templates/api_controller.rb @@ -25,7 +25,7 @@ def create if @<%= orm_instance.save %> render :show, status: :created, location: <%= "@#{singular_table_name}" %> else - render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity + render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %> end end @@ -35,7 +35,7 @@ def update if @<%= orm_instance.update("#{singular_table_name}_params") %> render :show, status: :ok, location: <%= "@#{singular_table_name}" %> else - render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity + render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %> end end diff --git a/lib/generators/rails/templates/controller.rb b/lib/generators/rails/templates/controller.rb index 0aff821..b0b56d9 100644 --- a/lib/generators/rails/templates/controller.rb +++ b/lib/generators/rails/templates/controller.rb @@ -33,8 +33,8 @@ def create format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully created.") %> } format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> } else - format.html { render :new, status: :unprocessable_entity } - format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } + format.html { render :new, status: :<%= status_unprocessable_content.to_s %> } + format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %> } end end end @@ -46,8 +46,8 @@ def update format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully updated.") %>, status: :see_other } format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> } else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } + format.html { render :edit, status: :<%= status_unprocessable_content.to_s %> } + format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %> } end end end diff --git a/test/scaffold_api_controller_generator_test.rb b/test/scaffold_api_controller_generator_test.rb index 546749b..932ad1b 100644 --- a/test/scaffold_api_controller_generator_test.rb +++ b/test/scaffold_api_controller_generator_test.rb @@ -24,12 +24,20 @@ class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase assert_match %r{@post = Post\.new\(post_params\)}, m assert_match %r{@post\.save}, m assert_match %r{render :show, status: :created, location: @post}, m - assert_match %r{render json: @post\.errors, status: :unprocessable_entity}, m + if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1") + assert_match %r{render json: @post\.errors, status: :unprocessable_entity}, m + else + assert_match %r{render json: @post\.errors, status: :unprocessable_content}, m + end end assert_instance_method :update, content do |m| assert_match %r{render :show, status: :ok, location: @post}, m - assert_match %r{render json: @post.errors, status: :unprocessable_entity}, m + if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1") + assert_match %r{render json: @post.errors, status: :unprocessable_entity}, m + else + assert_match %r{render json: @post.errors, status: :unprocessable_content}, m + end end assert_instance_method :destroy, content do |m| diff --git a/test/scaffold_controller_generator_test.rb b/test/scaffold_controller_generator_test.rb index 048df2a..e68aa25 100644 --- a/test/scaffold_controller_generator_test.rb +++ b/test/scaffold_controller_generator_test.rb @@ -33,15 +33,25 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_match %r{@post\.save}, m assert_match %r{format\.html \{ redirect_to @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 + if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1") + assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m + assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m + else + assert_match %r{format\.html \{ render :new, status: :unprocessable_content \}}, m + assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_content \}}, m + end end assert_instance_method :update, content do |m| assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully updated\.", status: :see_other \}}, 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 + if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1") + assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m + assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m + else + assert_match %r{format\.html \{ render :edit, status: :unprocessable_content \}}, m + assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_content \}}, m + end end assert_instance_method :destroy, content do |m|