Skip to content

Commit 6d03db3

Browse files
authored
Merge branch 'dev' into master
2 parents 0c367d2 + 4012507 commit 6d03db3

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

lib/fast_jsonapi/serialization_core.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def record_hash(record, fieldset, params = {})
6767
temp_hash[:links] = links_hash(record, params) if data_links.present?
6868
temp_hash
6969
end
70-
record_hash[:relationships] = record_hash[:relationships].merge(relationships_hash(record, uncachable_relationships_to_serialize, params)) if uncachable_relationships_to_serialize.present?
70+
record_hash[:relationships] = record_hash[:relationships].merge(relationships_hash(record, uncachable_relationships_to_serialize, fieldset, params)) if uncachable_relationships_to_serialize.present?
7171
record_hash
7272
else
7373
record_hash = id_hash(id_from_record(record), record_type, true)
@@ -123,7 +123,7 @@ def get_included_records(record, includes_list, known_included_objects, fieldset
123123

124124
included_objects.each do |inc_obj|
125125
if remaining_items(items)
126-
serializer_records = serializer.get_included_records(inc_obj, remaining_items(items), known_included_objects, fieldsets)
126+
serializer_records = serializer.get_included_records(inc_obj, remaining_items(items), known_included_objects, fieldsets, params)
127127
included_records.concat(serializer_records) unless serializer_records.empty?
128128
end
129129

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
@@ -81,6 +81,7 @@ def awards
8181
a.title = "Test Award #{i}"
8282
a.actor_id = id
8383
a.imdb_award_id = i * 10
84+
a.year = 1990 + i
8485
end
8586
end
8687
end
@@ -111,7 +112,7 @@ def state
111112
end
112113

113114
class Award
114-
attr_accessor :id, :title, :actor_id, :imdb_award_id
115+
attr_accessor :id, :title, :actor_id, :year, :imdb_award_id
115116
end
116117

117118
class State
@@ -226,6 +227,11 @@ class AgencySerializer
226227
class AwardSerializer
227228
include FastJsonapi::ObjectSerializer
228229
attributes :id, :title
230+
attribute :year, if: Proc.new { |record, params|
231+
params[:include_award_year].present? ?
232+
params[:include_award_year] :
233+
false
234+
}
229235
belongs_to :actor
230236
end
231237

0 commit comments

Comments
 (0)