Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ def cached_resources_for(records, serializer, options)
cache_ids = pluck_arel_attributes(records, t[_primary_key], t[_cache_field])
resources = CachedResourceFragment.fetch_fragments(self, serializer, options[:context], cache_ids)
else
resources = resources_for(records, options).map{|r| [r.id, r] }.to_h
resources = resources_for(records, options[:context]).map{|r| [r.id, r] }.to_h
end

preload_included_fragments(resources, records, serializer, options)
Expand Down
6 changes: 6 additions & 0 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,12 @@ def test_destroy_relationship_has_and_belongs_to_many_refect
ensure
JSONAPI.configuration.use_relationship_reflection = false
end

def test_index_with_caching_enabled_uses_context
assert_cacheable_get :index
assert_response :success
assert json_response['data'][0]['attributes']['title'] = 'Title'
end
end

class Api::V5::AuthorsControllerTest < ActionController::TestCase
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,9 @@ class BoatsController < JSONAPI::ResourceController
end

class BooksController < JSONAPI::ResourceController
def context
{ title: 'Title' }
end
end

### CONTROLLERS
Expand Down Expand Up @@ -1246,7 +1249,13 @@ class AuthorResource < JSONAPI::Resource
end

class BookResource < JSONAPI::Resource
attribute :title

has_many :authors, class_name: 'Author', inverse_relationship: :books

def title
context[:title]
end
end

class AuthorDetailResource < JSONAPI::Resource
Expand Down