diff --git a/lib/jsonapi/routing_ext.rb b/lib/jsonapi/routing_ext.rb index b0b94013..6ff8d08f 100644 --- a/lib/jsonapi/routing_ext.rb +++ b/lib/jsonapi/routing_ext.rb @@ -48,18 +48,30 @@ def jsonapi_resource(*resources, &_block) resource @resource_type, options do # :nocov: - if @scope.respond_to? :[]= + if @scope.respond_to?(:[]=) # Rails 4 @scope[:jsonapi_resource] = @resource_type + if block_given? + yield + else + jsonapi_relationships + end + elsif Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR >= 1 + # Rails 8.1+ + # Rails 8.1 changed Scope to not support []= and Resource.new signature + # Use instance variable to track resource type + @jsonapi_resource_type = @resource_type if block_given? yield else jsonapi_relationships end else - # Rails 5 - jsonapi_resource_scope(SingletonResource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do + # Rails 5-8.0 + resource_arg = SingletonResource.new(@resource_type, api_only?, @scope[:shallow], options) + + jsonapi_resource_scope(resource_arg, @resource_type) do if block_given? yield else @@ -123,7 +135,7 @@ def jsonapi_resources(*resources, &_block) resources @resource_type, options do # :nocov: - if @scope.respond_to? :[]= + if @scope.respond_to?(:[]=) # Rails 4 @scope[:jsonapi_resource] = @resource_type if block_given? @@ -131,9 +143,21 @@ def jsonapi_resources(*resources, &_block) else jsonapi_relationships end + elsif Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR >= 1 + # Rails 8.1+ + # Rails 8.1 changed Scope to not support []= and Resource.new signature + # Use instance variable to track resource type + @jsonapi_resource_type = @resource_type + if block_given? + yield + else + jsonapi_relationships + end else - # Rails 5 - jsonapi_resource_scope(Resource.new(@resource_type, api_only?, @scope[:shallow], options), @resource_type) do + # Rails 5-8.0 + resource_arg = Resource.new(@resource_type, api_only?, @scope[:shallow], options) + + jsonapi_resource_scope(resource_arg, @resource_type) do if block_given? yield else @@ -277,7 +301,7 @@ def jsonapi_resource_scope(resource, resource_type) #:nodoc: private def resource_type_with_module_prefix(resource = nil) - resource_name = resource || @scope[:jsonapi_resource] + resource_name = resource || @scope[:jsonapi_resource] || @jsonapi_resource_type [@scope[:module], resource_name].compact.collect(&:to_s).join('/') end end diff --git a/test/integration/requests/request_test.rb b/test/integration/requests/request_test.rb index a0e7f963..ce55697e 100644 --- a/test/integration/requests/request_test.rb +++ b/test/integration/requests/request_test.rb @@ -578,7 +578,8 @@ def test_put_invalid_json assert_equal 400, status assert_equal 'Bad Request', json_response['errors'][0]['title'] - assert_match 'unexpected token at', json_response['errors'][0]['detail'] + # Rails 8.1+ has more detailed JSON error messages + assert_match(/unexpected token at|expected .* got:|parse error/i, json_response['errors'][0]['detail']) end def test_put_valid_json_but_array diff --git a/test/test_db-shm b/test/test_db-shm deleted file mode 100644 index 42eee55d..00000000 Binary files a/test/test_db-shm and /dev/null differ diff --git a/test/test_db-wal b/test/test_db-wal deleted file mode 100644 index d750fe47..00000000 Binary files a/test/test_db-wal and /dev/null differ diff --git a/test/unit/active_relation_resource_finder/join_manager_test.rb b/test/unit/active_relation_resource_finder/join_manager_test.rb index 840c90ee..19ba1a83 100644 --- a/test/unit/active_relation_resource_finder/join_manager_test.rb +++ b/test/unit/active_relation_resource_finder/join_manager_test.rb @@ -6,7 +6,10 @@ class JoinTreeTest < ActiveSupport::TestCase def db_true case ActiveRecord::Base.connection.adapter_name when 'SQLite' - if Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2) + if Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR >= 1 + # Rails 8.1+ SQLite uses TRUE instead of 1 + "TRUE" + elsif Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2) "1" else "'t'"