Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix caching for namespaced polymorphic associations #1080

Closed
Closed
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
3 changes: 2 additions & 1 deletion lib/jsonapi/cached_resource_fragment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def to_cache_value
end

def to_real_resource
rs = Resource.resource_klass_for(self.type).find_by_keys([self.id], {context: self.context})
rs_klass = @resource_klass || Resource.resource_klass_for(self.type)
rs = rs_klass.find_by_keys([self.id], {context: self.context})
return rs.try(:first)
end

Expand Down
15 changes: 15 additions & 0 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3520,6 +3520,21 @@ def test_show_relationship
end
end

class Api::V1::PicturesControllerTest < ActionController::TestCase
def test_pictures_index
assert_cacheable_get :index
assert_response :success
assert_equal 3, json_response['data'].size
end

def test_pictures_index_with_polymorphic_include_one_level
assert_cacheable_get :index, params: {include: 'imageable'}
assert_response :success
assert_equal 3, json_response['data'].size
assert_equal 2, json_response['included'].size
end
end

class CarsControllerTest < ActionController::TestCase
def setup
JSONAPI.configuration.json_key_format = :camelized_key
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,9 @@ def context

class LikesController < JSONAPI::ResourceController
end

class PicturesController < JSONAPI::ResourceController
end
end

module V2
Expand Down Expand Up @@ -1492,6 +1495,10 @@ class HairCutResource < HairCutResource; end
class VehicleResource < VehicleResource; end
class CarResource < CarResource; end
class BoatResource < BoatResource; end
class ImageableResource < ImageableResource; end
class PictureResource < PictureResource; end
class ProductResource < ProductResource; end
class DocumentResource < DocumentResource; end
end
end

Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ class CatResource < JSONAPI::Resource
jsonapi_resources :craters
jsonapi_resources :preferences
jsonapi_resources :likes
jsonapi_resources :pictures
end

JSONAPI.configuration.route_format = :underscored_route
Expand Down