Params are now passed to nested includes

This commit is contained in:
Trevor Hinesley 2018-07-19 09:57:22 -05:00
parent dc2b78bbe4
commit 07b6e614ac
3 changed files with 25 additions and 2 deletions

View File

@ -120,7 +120,7 @@ module FastJsonapi
included_objects.each do |inc_obj|
if remaining_items(items)
serializer_records = serializer.get_included_records(inc_obj, remaining_items(items), known_included_objects)
serializer_records = serializer.get_included_records(inc_obj, remaining_items(items), known_included_objects, params)
included_records.concat(serializer_records) unless serializer_records.empty?
end

View File

@ -310,6 +310,23 @@ describe FastJsonapi::ObjectSerializer do
end
end
context 'when serializing included, params should be available in any serializer' do
subject(:serializable_hash) do
options = {}
options[:include] = [:"actors.awards"]
options[:params] = { include_award_year: true }
MovieSerializer.new(movie, options).serializable_hash
end
let(:actor) { movie.actors.first }
let(:award) { actor.awards.first }
let(:year) { award.year }
it 'passes params to deeply nested includes' do
expect(year).to_not be_blank
expect(serializable_hash[:included][0][:attributes][:year]).to eq year
end
end
context 'when is_collection option present' do
subject { MovieSerializer.new(resource, is_collection_options).serializable_hash }

View File

@ -76,6 +76,7 @@ RSpec.shared_context 'movie class' do
a.id = i
a.title = "Test Award #{i}"
a.actor_id = id
a.year = 1990 + i
end
end
end
@ -106,7 +107,7 @@ RSpec.shared_context 'movie class' do
end
class Award
attr_accessor :id, :title, :actor_id
attr_accessor :id, :title, :actor_id, :year
end
class State
@ -221,6 +222,11 @@ RSpec.shared_context 'movie class' do
class AwardSerializer
include FastJsonapi::ObjectSerializer
attributes :id, :title
attribute :year, if: Proc.new { |record, params|
params[:include_award_year].present? ?
params[:include_award_year] :
false
}
belongs_to :actor
end