Skip to content

Commit 07b6e61

Browse files
Params are now passed to nested includes
1 parent dc2b78b commit 07b6e61

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

lib/fast_jsonapi/serialization_core.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def get_included_records(record, includes_list, known_included_objects, params =
120120

121121
included_objects.each do |inc_obj|
122122
if remaining_items(items)
123-
serializer_records = serializer.get_included_records(inc_obj, remaining_items(items), known_included_objects)
123+
serializer_records = serializer.get_included_records(inc_obj, remaining_items(items), known_included_objects, params)
124124
included_records.concat(serializer_records) unless serializer_records.empty?
125125
end
126126

spec/lib/object_serializer_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,23 @@ class BlahSerializer
310310
end
311311
end
312312

313+
context 'when serializing included, params should be available in any serializer' do
314+
subject(:serializable_hash) do
315+
options = {}
316+
options[:include] = [:"actors.awards"]
317+
options[:params] = { include_award_year: true }
318+
MovieSerializer.new(movie, options).serializable_hash
319+
end
320+
let(:actor) { movie.actors.first }
321+
let(:award) { actor.awards.first }
322+
let(:year) { award.year }
323+
324+
it 'passes params to deeply nested includes' do
325+
expect(year).to_not be_blank
326+
expect(serializable_hash[:included][0][:attributes][:year]).to eq year
327+
end
328+
end
329+
313330
context 'when is_collection option present' do
314331
subject { MovieSerializer.new(resource, is_collection_options).serializable_hash }
315332

spec/shared/contexts/movie_context.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def awards
7676
a.id = i
7777
a.title = "Test Award #{i}"
7878
a.actor_id = id
79+
a.year = 1990 + i
7980
end
8081
end
8182
end
@@ -106,7 +107,7 @@ def state
106107
end
107108

108109
class Award
109-
attr_accessor :id, :title, :actor_id
110+
attr_accessor :id, :title, :actor_id, :year
110111
end
111112

112113
class State
@@ -221,6 +222,11 @@ class AgencySerializer
221222
class AwardSerializer
222223
include FastJsonapi::ObjectSerializer
223224
attributes :id, :title
225+
attribute :year, if: Proc.new { |record, params|
226+
params[:include_award_year].present? ?
227+
params[:include_award_year] :
228+
false
229+
}
224230
belongs_to :actor
225231
end
226232

0 commit comments

Comments
 (0)