Skip to content

Commit 13efb05

Browse files
Correcting issue with caching and sql literals in joins in resource def
1 parent 637956d commit 13efb05

5 files changed

Lines changed: 25 additions & 10 deletions

File tree

lib/jsonapi/resource.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ def preload_included_fragments(resources, records, serializer, options)
11531153
target_resources = {}
11541154
include_directives.paths.each do |path|
11551155
# If path is [:posts, :comments, :author], then...
1156-
pluck_attrs = [] # [posts.id, comments.id, authors.id, authors.updated_at]
1156+
pluck_attrs = [] # ...will be [posts.id, comments.id, authors.id, authors.updated_at]
11571157
pluck_attrs << self._model_class.arel_table[self._primary_key]
11581158

11591159
relation = records
@@ -1179,7 +1179,10 @@ def preload_included_fragments(resources, records, serializer, options)
11791179
ar_hash = assocs_path.reverse.reduce{|memo, step| { step => memo } }
11801180
# We can't just look up the table name from the resource class, because Arel could
11811181
# have used a table alias if the relation includes a self-reference.
1182-
table = relation.joins(ar_hash).arel.source.right.last.left
1182+
join_source = relation.joins(ar_hash).arel.source.right.reverse.find do |arel_node|
1183+
arel_node.is_a?(Arel::Nodes::InnerJoin)
1184+
end
1185+
table = join_source.left
11831186
parent_klass = klass
11841187
klass = relationship.resource_klass
11851188
pluck_attrs << table[klass._primary_key]

lib/jsonapi/resource_serializer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def process_primary(source, include_directives)
179179

180180
def supplying_attribute_fields(resource_klass)
181181
@_supplying_attribute_fields.fetch resource_klass do
182-
attrs = Set.new(resource_klass._attributes.keys)
182+
attrs = Set.new(resource_klass._attributes.keys.map(&:to_sym))
183183
cur = resource_klass
184184
while cur != JSONAPI::Resource
185185
if @fields.has_key?(cur._type)
@@ -194,7 +194,7 @@ def supplying_attribute_fields(resource_klass)
194194

195195
def supplying_relationship_fields(resource_klass)
196196
@_supplying_relationship_fields.fetch resource_klass do
197-
relationships = Set.new(resource_klass._relationships.keys)
197+
relationships = Set.new(resource_klass._relationships.keys.map(&:to_sym))
198198
cur = resource_klass
199199
while cur != JSONAPI::Resource
200200
if @fields.has_key?(cur._type)

test/controllers/controller_test.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3577,13 +3577,15 @@ def test_whitelisted_error_in_controller
35773577
end
35783578

35793579
class Api::V6::PostsControllerTest < ActionController::TestCase
3580-
def test_with_sql_fragment
3581-
get :index, params: {include: 'section'}
3580+
def test_caching_with_join_from_resource_with_sql_fragment
3581+
assert_cacheable_get :index, params: {include: 'section'}
35823582
assert_response :success
35833583
end
3584+
end
35843585

3585-
def test_caching_with_sql_fragment
3586-
assert_cacheable_get :index, params: {include: 'section'}
3586+
class Api::V6::SectionsControllerTest < ActionController::TestCase
3587+
def test_caching_with_join_to_resource_with_sql_fragment
3588+
assert_cacheable_get :index, params: {include: 'posts'}
35873589
assert_response :success
35883590
end
3589-
end
3591+
end

test/fixtures/active_record.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
create_table :sections, force: true do |t|
5656
t.string :name
57+
t.timestamps null: false
5758
end
5859

5960
create_table :posts_tags, force: true do |t|
@@ -343,6 +344,7 @@ class Tag < ActiveRecord::Base
343344
end
344345

345346
class Section < ActiveRecord::Base
347+
has_many :posts
346348
end
347349

348350
class HairCut < ActiveRecord::Base
@@ -758,6 +760,9 @@ module V6
758760
class PostsController < JSONAPI::ResourceController
759761
end
760762

763+
class SectionsController < JSONAPI::ResourceController
764+
end
765+
761766
class CustomersController < JSONAPI::ResourceController
762767
end
763768

@@ -1464,7 +1469,11 @@ module Api
14641469
module V6
14651470
class PersonResource < PersonResource; end
14661471
class TagResource < TagResource; end
1467-
class SectionResource < SectionResource; end
1472+
1473+
class SectionResource < SectionResource
1474+
has_many :posts
1475+
end
1476+
14681477
class CommentResource < CommentResource; end
14691478

14701479
class PostResource < PostResource

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ class CatResource < JSONAPI::Resource
322322
JSONAPI.configuration.route_format = :dasherized_route
323323
namespace :v6 do
324324
jsonapi_resources :posts
325+
jsonapi_resources :sections
325326
jsonapi_resources :customers
326327
jsonapi_resources :purchase_orders
327328
jsonapi_resources :line_items

0 commit comments

Comments
 (0)