Skip to content

Commit b0bf769

Browse files
committed
Use :unprocessable_content for scaffolds with Rack 3.1 or higher
1 parent 38339ad commit b0bf769

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

lib/generators/rails/scaffold_controller_generator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class ScaffoldControllerGenerator
1515
def permitted_params
1616
attributes_names.map { |name| ":#{name}" }.join(', ')
1717
end unless private_method_defined? :permitted_params
18+
19+
def status_unprocessable_content
20+
::Rack::Utils::SYMBOL_TO_STATUS_CODE.key(422) rescue :unprocessable_content
21+
end
1822
end
1923
end
2024
end

lib/generators/rails/templates/api_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create
2525
if @<%= orm_instance.save %>
2626
render :show, status: :created, location: <%= "@#{singular_table_name}" %>
2727
else
28-
render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
28+
render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %>
2929
end
3030
end
3131

@@ -35,7 +35,7 @@ def update
3535
if @<%= orm_instance.update("#{singular_table_name}_params") %>
3636
render :show, status: :ok, location: <%= "@#{singular_table_name}" %>
3737
else
38-
render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
38+
render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %>
3939
end
4040
end
4141

lib/generators/rails/templates/controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def create
3333
format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully created.") %> }
3434
format.json { render :show, status: :created, location: <%= "@#{singular_table_name}" %> }
3535
else
36-
format.html { render :new, status: :unprocessable_entity }
37-
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
36+
format.html { render :new, status: :<%= status_unprocessable_content.to_s %> }
37+
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %> }
3838
end
3939
end
4040
end
@@ -46,8 +46,8 @@ def update
4646
format.html { redirect_to <%= redirect_resource_name %>, notice: <%= %("#{human_name} was successfully updated.") %>, status: :see_other }
4747
format.json { render :show, status: :ok, location: <%= "@#{singular_table_name}" %> }
4848
else
49-
format.html { render :edit, status: :unprocessable_entity }
50-
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
49+
format.html { render :edit, status: :<%= status_unprocessable_content.to_s %> }
50+
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :<%= status_unprocessable_content.to_s %> }
5151
end
5252
end
5353
end

test/scaffold_api_controller_generator_test.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,20 @@ class ScaffoldApiControllerGeneratorTest < Rails::Generators::TestCase
2424
assert_match %r{@post = Post\.new\(post_params\)}, m
2525
assert_match %r{@post\.save}, m
2626
assert_match %r{render :show, status: :created, location: @post}, m
27-
assert_match %r{render json: @post\.errors, status: :unprocessable_entity}, m
27+
if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1")
28+
assert_match %r{render json: @post\.errors, status: :unprocessable_entity}, m
29+
else
30+
assert_match %r{render json: @post\.errors, status: :unprocessable_content}, m
31+
end
2832
end
2933

3034
assert_instance_method :update, content do |m|
3135
assert_match %r{render :show, status: :ok, location: @post}, m
32-
assert_match %r{render json: @post.errors, status: :unprocessable_entity}, m
36+
if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1")
37+
assert_match %r{render json: @post.errors, status: :unprocessable_entity}, m
38+
else
39+
assert_match %r{render json: @post.errors, status: :unprocessable_content}, m
40+
end
3341
end
3442

3543
assert_instance_method :destroy, content do |m|

test/scaffold_controller_generator_test.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,25 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
3333
assert_match %r{@post\.save}, m
3434
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully created\." \}}, m
3535
assert_match %r{format\.json \{ render :show, status: :created, location: @post \}}, m
36-
assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m
37-
assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m
36+
if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1")
37+
assert_match %r{format\.html \{ render :new, status: :unprocessable_entity \}}, m
38+
assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_entity \}}, m
39+
else
40+
assert_match %r{format\.html \{ render :new, status: :unprocessable_content \}}, m
41+
assert_match %r{format\.json \{ render json: @post\.errors, status: :unprocessable_content \}}, m
42+
end
3843
end
3944

4045
assert_instance_method :update, content do |m|
4146
assert_match %r{format\.html \{ redirect_to @post, notice: "Post was successfully updated\.", status: :see_other \}}, m
4247
assert_match %r{format\.json \{ render :show, status: :ok, location: @post \}}, m
43-
assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m
44-
assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m
48+
if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3.1")
49+
assert_match %r{format\.html \{ render :edit, status: :unprocessable_entity \}}, m
50+
assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_entity \}}, m
51+
else
52+
assert_match %r{format\.html \{ render :edit, status: :unprocessable_content \}}, m
53+
assert_match %r{format\.json \{ render json: @post.errors, status: :unprocessable_content \}}, m
54+
end
4555
end
4656

4757
assert_instance_method :destroy, content do |m|

0 commit comments

Comments
 (0)