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
38 changes: 31 additions & 7 deletions lib/jsonapi/routing_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -123,17 +135,29 @@ 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?
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(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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/integration/requests/request_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file removed test/test_db-shm
Binary file not shown.
Binary file removed test/test_db-wal
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
Expand Down