Skip to content

Commit 42da699

Browse files
authored
Merge pull request #43 from richmolj/master
Allow sideload to be referenced multiple times
2 parents 51c1fc1 + 2a7cd64 commit 42da699

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/jsonapi_compliable/sideload.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,10 @@ def sideload(name)
310310
# @return [Hash] The nested include hash
311311
# @api private
312312
def to_hash(processed = [])
313-
return { name => {} } if processed.include?(self)
313+
# Cut off at 5 recursions
314+
if processed.select { |p| p == self }.length == 5
315+
return { name => {} }
316+
end
314317
processed << self
315318

316319
result = { name => {} }.tap do |hash|

spec/integration/rails/finders_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,35 @@ def json
295295
end
296296
end
297297

298+
context 'sideloading the same "type", then adding another sideload' do
299+
before do
300+
Author.class_eval do
301+
has_many :other_books, class_name: 'Book'
302+
end
303+
304+
SerializableAuthor.class_eval do
305+
has_many :other_books
306+
end
307+
308+
Integration::AuthorResource.class_eval do
309+
has_many :other_books,
310+
scope: -> { Book.all },
311+
foreign_key: :author_id,
312+
resource: Integration::BookResource
313+
end
314+
end
315+
316+
it 'works' do
317+
book2.genre = Genre.create! name: 'Comedy'
318+
book2.save!
319+
get :index, params: {
320+
filter: { books: { id: book1.id }, other_books: { id: book2.id } },
321+
include: 'books.genre,other_books.genre'
322+
}
323+
expect(json_includes('genres').length).to eq(2)
324+
end
325+
end
326+
298327
context 'sideloading polymorphic belongs_to' do
299328
it 'allows extra fields for the sideloaded resource' do
300329
get :index, params: {

0 commit comments

Comments
 (0)