Skip to content

Commit 95a36c1

Browse files
authored
Merge pull request #1171 from cerebris/fixup_1164
Fixup of #1164 - Eager load includes for related resources (#1163)
2 parents 87db448 + 31ec00d commit 95a36c1

5 files changed

Lines changed: 35 additions & 2 deletions

File tree

lib/jsonapi/relationship_builder.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def define_resource_relationship_accessor(type, relationship_name)
6060

6161
resource_klass = relationship.resource_klass
6262

63+
records = resource_klass.apply_includes(records, options)
64+
6365
filters = options.fetch(:filters, {})
6466
unless filters.nil? || filters.empty?
6567
records = resource_klass.apply_filters(records, filters, options)

test/controllers/controller_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3173,7 +3173,7 @@ def test_books_banned_non_book_admin_includes_nested_includes
31733173
end
31743174
assert_response :success
31753175
assert_equal 12, json_response['data'].size
3176-
assert_equal 132, json_response['included'].size
3176+
assert_equal 135, json_response['included'].size
31773177
assert_equal 'Book 0', json_response['data'][0]['attributes']['title']
31783178
assert_equal 901, json_response['meta']['record-count']
31793179
ensure

test/fixtures/active_record.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,8 @@ class BookResource < JSONAPI::Resource
14651465
attribute "title"
14661466
attributes :isbn, :banned
14671467

1468+
paginator :offset
1469+
14681470
has_many "authors"
14691471

14701472
has_many "book_comments", relation_name: -> (options = {}) {
@@ -1523,6 +1525,8 @@ class BookCommentResource < JSONAPI::Resource
15231525
has_one :book
15241526
has_one :author, class_name: 'Person'
15251527

1528+
paginator :offset
1529+
15261530
filters :book
15271531
filter :approved, apply: ->(records, value, options) {
15281532
context = options[:context]

test/fixtures/book_comments.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
book_<%= book_num %>_comment_<%= comment_num %>:
55
id: <%= comment_id %>
66
body: This is comment <%= comment_num %> on book <%= book_num %>.
7-
author_id: <%= book_num.even? ? comment_id % 2 : (comment_id % 2) + 2 %>
7+
author_id: <%= (comment_id % 5) + 1 %>
88
book_id: <%= book_num %>
99
approved: <%= comment_num.even? %>
1010
<% comment_id = comment_id + 1 %>

test/integration/requests/request_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,33 @@ def test_pagination_empty_results
604604
# assert_equal 'This is comment 18 on book 1.', json_response['data'][9]['attributes']['body']
605605
# end
606606

607+
def test_query_count_related_resources
608+
# Expected Queries:
609+
# * Fetch specified book record
610+
# * Fetch book comment records associated with specified book
611+
# * Select count of book comment records for pagination
612+
Api::V2::BookCommentResource.paginator :offset
613+
assert_query_count 3 do
614+
get '/api/v2/books/1/book_comments?page[limit]=20'
615+
end
616+
assert_equal 20, json_response['data'].size
617+
end
618+
619+
def test_query_count_related_resources_with_includes
620+
# Expected Queries:
621+
# * Fetch specified book record
622+
# * Fetch book comment records associated with specified book
623+
# * Fetch all author records the book comments to be returned
624+
# * Select count of book comment records for pagination
625+
Api::V2::BookCommentResource.paginator :offset
626+
627+
assert_query_count 4 do
628+
get '/api/v2/books/1/book_comments?page[limit]=20&include=author'
629+
end
630+
assert_equal 20, json_response['data'].size
631+
assert_equal 5, json_response['included'].size
632+
end
633+
607634

608635
def test_flow_self
609636
assert_cacheable_jsonapi_get '/posts/1'

0 commit comments

Comments
 (0)