Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/generators/rails/scaffold_controller_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/generators/rails/templates/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions lib/generators/rails/templates/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions test/scaffold_api_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
18 changes: 14 additions & 4 deletions test/scaffold_controller_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down