Skip to content

Commit e5a66b5

Browse files
committed
feat: Replace :unprocessable_entity
With :unprocessable_content
1 parent b4870a8 commit e5a66b5

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

app/controllers/active_storage_db/files_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ def show
1717
def update
1818
if (token = decode_verified_token)
1919
file_uploaded = upload_file(token, body: request.body)
20-
head(file_uploaded ? :no_content : :unprocessable_entity)
20+
head(file_uploaded ? :no_content : unprocessable)
2121
else
2222
head(:not_found)
2323
end
2424
rescue ActiveStorage::IntegrityError
25-
head(:unprocessable_entity)
25+
head(unprocessable)
2626
end
2727

2828
private
@@ -59,5 +59,9 @@ def upload_file(token, body:)
5959
db_service.upload(token[:key], request.body, checksum: token[:checksum])
6060
true
6161
end
62+
63+
def unprocessable
64+
Gem::Version.new(Rails.version) >= Gem::Version.new("7.1") ? :unprocessable_content : :unprocessable_entity
65+
end
6266
end
6367
end

spec/dummy/app/controllers/posts_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def create
3232
format.json { render :show, status: :created, location: @post }
3333
else
3434
format.html { render :new }
35-
format.json { render json: { errors: @post.errors }, status: :unprocessable_entity }
35+
format.json { render json: { errors: @post.errors }, status: unprocessable }
3636
end
3737
end
3838
end
@@ -46,7 +46,7 @@ def update
4646
format.json { render :show, status: :ok, location: @post }
4747
else
4848
format.html { render :edit }
49-
format.json { render json: { errors: @post.errors }, status: :unprocessable_entity }
49+
format.json { render json: { errors: @post.errors }, status: unprocessable }
5050
end
5151
end
5252
end
@@ -70,4 +70,8 @@ def load_post
7070
def post_params
7171
params.fetch(:post) { {} }.permit!
7272
end
73+
74+
def unprocessable
75+
Gem::Version.new(Rails.version) >= Gem::Version.new("7.1") ? :unprocessable_content : :unprocessable_entity
76+
end
7377
end

spec/requests/file_controller_spec.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def create_blob_before_direct_upload(byte_size:, checksum:, filename: 'hello.txt
2020
)
2121
end
2222

23+
def unprocessable
24+
Gem::Version.new(Rails.version) >= Gem::Version.new("7.1") ? :unprocessable_content : :unprocessable_entity
25+
end
26+
2327
let(:blob) { create_blob(filename: 'img.jpg', content_type: 'image/jpeg') }
2428
let(:url_options) do
2529
{
@@ -106,7 +110,7 @@ def create_blob_before_direct_upload(byte_size:, checksum:, filename: 'hello.txt
106110
it 'uses blob direct upload with mismatched content type' do
107111
put blob.service_url_for_direct_upload, params: data, headers: { 'Content-Type' => 'application/octet-stream' }
108112

109-
expect(response).to have_http_status(:unprocessable_entity)
113+
expect(response).to have_http_status(unprocessable)
110114
expect(blob.service).not_to exist(blob.key)
111115
end
112116

@@ -118,7 +122,7 @@ def create_blob_before_direct_upload(byte_size:, checksum:, filename: 'hello.txt
118122
it 'fails to upload' do
119123
put blob.service_url_for_direct_upload, params: data
120124

121-
expect(response).to have_http_status(:unprocessable_entity)
125+
expect(response).to have_http_status(unprocessable)
122126
expect(blob.service).not_to exist(blob.key)
123127
end
124128
end
@@ -129,7 +133,7 @@ def create_blob_before_direct_upload(byte_size:, checksum:, filename: 'hello.txt
129133
it 'fails to upload' do
130134
put blob.service_url_for_direct_upload, params: data, headers: { 'Content-Type' => 'text/plain' }
131135

132-
expect(response).to have_http_status(:unprocessable_entity)
136+
expect(response).to have_http_status(unprocessable)
133137
expect(blob.service).not_to exist(blob.key)
134138
end
135139
end
@@ -154,7 +158,7 @@ def create_blob_before_direct_upload(byte_size:, checksum:, filename: 'hello.txt
154158
it 'fails to upload' do
155159
put blob.service_url_for_direct_upload, params: data, headers: { 'Content-Type' => 'text/plain' }
156160

157-
expect(response).to have_http_status(:unprocessable_entity)
161+
expect(response).to have_http_status(unprocessable)
158162
end
159163
end
160164
end

0 commit comments

Comments
 (0)