Skip to content

Commit 5cd0ed8

Browse files
authored
Merge pull request #24 from speee/rails-8.1-compatibility
Add Rails 8.1 compatibility improvements
2 parents 0513b04 + 4a10213 commit 5cd0ed8

File tree

6 files changed

+45
-24
lines changed

6 files changed

+45
-24
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ when 'master'
2828
gem 'arel', { git: 'https://github.com/rails/arel.git' }
2929
when 'default'
3030
gem 'railties', '>= 6.0'
31+
gem 'csv' # Required by Rails 8.1+ (removed from standard library)
3132
else
3233
gem 'railties', "~> #{version}"
3334
end

lib/jsonapi/exceptions.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ def errors
1818
raise NotImplementedError, "Subclass of Error must implement errors method"
1919
# :nocov:
2020
end
21+
22+
private
23+
24+
# Rails 8.0+ deprecated :unprocessable_entity in favor of :unprocessable_content
25+
# This helper maintains backward compatibility with earlier Rails versions
26+
def unprocessable_status
27+
if Rails::VERSION::MAJOR >= 8
28+
:unprocessable_content
29+
else
30+
:unprocessable_entity
31+
end
32+
end
2133
end
2234

2335
class Errors < Error
@@ -498,7 +510,7 @@ def errors
498510

499511
def json_api_error(attr_key, message)
500512
create_error_object(code: JSONAPI::VALIDATION_ERROR,
501-
status: :unprocessable_entity,
513+
status: unprocessable_status,
502514
title: message,
503515
detail: detail(attr_key, message),
504516
source: { pointer: pointer(attr_key) },
@@ -532,7 +544,7 @@ def general_error?(attr_key)
532544
class SaveFailed < Error
533545
def errors
534546
[create_error_object(code: JSONAPI::SAVE_FAILED,
535-
status: :unprocessable_entity,
547+
status: unprocessable_status,
536548
title: I18n.translate('jsonapi-resources.exceptions.save_failed.title',
537549
default: 'Save failed or was cancelled'),
538550
detail: I18n.translate('jsonapi-resources.exceptions.save_failed.detail',

test/controllers/controller_test.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ def test_create_link_to_missing_object
761761
}
762762
}
763763

764-
assert_response :unprocessable_entity
764+
assert_response unprocessable_status
765765
# TODO: check if this validation is working
766766
assert_match /author - can't be blank/, response.body
767767
assert_nil response.location
@@ -864,7 +864,7 @@ def test_create_with_invalid_data
864864
}
865865
}
866866

867-
assert_response :unprocessable_entity
867+
assert_response unprocessable_status
868868

869869
assert_equal "/data/relationships/author", json_response['errors'][0]['source']['pointer']
870870
assert_equal "can't be blank", json_response['errors'][0]['title']
@@ -2019,7 +2019,7 @@ def test_delete_with_validation_error_base
20192019

20202020
assert_equal "can't destroy me", json_response['errors'][0]['title']
20212021
assert_equal "/data", json_response['errors'][0]['source']['pointer']
2022-
assert_response :unprocessable_entity
2022+
assert_response unprocessable_status
20232023
end
20242024

20252025
def test_delete_with_validation_error_attr
@@ -2028,7 +2028,7 @@ def test_delete_with_validation_error_attr
20282028

20292029
assert_equal "is locked", json_response['errors'][0]['title']
20302030
assert_equal "/data/attributes/title", json_response['errors'][0]['source']['pointer']
2031-
assert_response :unprocessable_entity
2031+
assert_response unprocessable_status
20322032
end
20332033

20342034
def test_delete_single
@@ -2631,7 +2631,7 @@ def test_create_validations_missing_attribute
26312631
}
26322632
}
26332633

2634-
assert_response :unprocessable_entity
2634+
assert_response unprocessable_status
26352635
assert_equal 2, json_response['errors'].size
26362636
assert_equal JSONAPI::VALIDATION_ERROR, json_response['errors'][0]['code']
26372637
assert_equal JSONAPI::VALIDATION_ERROR, json_response['errors'][1]['code']
@@ -2653,7 +2653,7 @@ def test_update_validations_missing_attribute
26532653
}
26542654
}
26552655

2656-
assert_response :unprocessable_entity
2656+
assert_response unprocessable_status
26572657
assert_equal 1, json_response['errors'].size
26582658
assert_equal JSONAPI::VALIDATION_ERROR, json_response['errors'][0]['code']
26592659
assert_match /name - can't be blank/, response.body
@@ -3183,7 +3183,7 @@ def test_create_with_invalid_data
31833183
}
31843184
}
31853185

3186-
assert_response :unprocessable_entity
3186+
assert_response unprocessable_status
31873187

31883188
assert_equal "/data/attributes/spouse-name", json_response['errors'][0]['source']['pointer']
31893189
assert_equal "can't be blank", json_response['errors'][0]['title']
@@ -3781,7 +3781,7 @@ def test_save_model_callbacks_fail
37813781
}
37823782
}
37833783

3784-
assert_response :unprocessable_entity
3784+
assert_response unprocessable_status
37853785
assert_match /Save failed or was cancelled/, json_response['errors'][0]['detail']
37863786
end
37873787
end
@@ -4079,7 +4079,7 @@ def test_delete_with_validation_error_base_on_resource
40794079

40804080
assert_equal "can't destroy me", json_response['errors'][0]['title']
40814081
assert_equal "/data/attributes/base", json_response['errors'][0]['source']['pointer']
4082-
assert_response :unprocessable_entity
4082+
assert_response unprocessable_status
40834083
end
40844084
end
40854085

test/helpers/functional_helpers.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,15 @@ module FunctionalHelpers
5555
def json_response
5656
JSON.parse(@response.body)
5757
end
58+
59+
# Rails 8.0+ deprecated :unprocessable_entity in favor of :unprocessable_content
60+
# This helper maintains backward compatibility in tests
61+
def unprocessable_status
62+
if Rails::VERSION::MAJOR >= 8
63+
:unprocessable_content
64+
else
65+
:unprocessable_entity
66+
end
67+
end
5868
end
5969
end

test/test_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,16 @@ def assert_jsonapi_response(expected_status, msg = nil)
585585
assert_equal expected_status, status, msg
586586
end
587587

588+
# Rails 8.0+ deprecated :unprocessable_entity in favor of :unprocessable_content
589+
# This helper maintains backward compatibility in tests
590+
def unprocessable_status
591+
if Rails::VERSION::MAJOR >= 8
592+
:unprocessable_content
593+
else
594+
:unprocessable_entity
595+
end
596+
end
597+
588598
def assert_jsonapi_get(url, msg = "GET response must be 200")
589599
get url, headers: { 'Accept' => JSONAPI::MEDIA_TYPE }
590600
assert_jsonapi_response 200, msg

test/unit/active_relation_resource_finder/join_manager_test.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,7 @@
44
class JoinTreeTest < ActiveSupport::TestCase
55

66
def db_true
7-
case ActiveRecord::Base.connection.adapter_name
8-
when 'SQLite'
9-
if Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR >= 1
10-
# Rails 8.1+ SQLite uses TRUE instead of 1
11-
"TRUE"
12-
elsif Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
13-
"1"
14-
else
15-
"'t'"
16-
end
17-
when 'PostgreSQL'
18-
'TRUE'
19-
end
7+
ActiveRecord::Base.connection.quoted_true
208
end
219

2210
def test_no_added_joins

0 commit comments

Comments
 (0)