Conditional relationships should be removed from included when proc evaluates to false
This commit is contained in:
parent
0b70657a41
commit
f864099761
@ -146,6 +146,8 @@ module FastJsonapi
|
|||||||
items = parse_include_item(include_item)
|
items = parse_include_item(include_item)
|
||||||
items.each do |item|
|
items.each do |item|
|
||||||
next unless relationships_to_serialize && relationships_to_serialize[item]
|
next unless relationships_to_serialize && relationships_to_serialize[item]
|
||||||
|
conditional_proc = relationships_to_serialize[item][:conditional_proc]
|
||||||
|
next if conditional_proc && !conditional_proc.call(record, params)
|
||||||
raise NotImplementedError if @relationships_to_serialize[item][:polymorphic].is_a?(Hash)
|
raise NotImplementedError if @relationships_to_serialize[item][:polymorphic].is_a?(Hash)
|
||||||
record_type = @relationships_to_serialize[item][:record_type]
|
record_type = @relationships_to_serialize[item][:record_type]
|
||||||
serializer = @relationships_to_serialize[item][:serializer].to_s.constantize
|
serializer = @relationships_to_serialize[item][:serializer].to_s.constantize
|
||||||
|
@ -349,12 +349,28 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_truthy
|
expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_truthy
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't return optional relationship when relationship is not included" do
|
context "when relationship is not included" do
|
||||||
|
let(:json) {
|
||||||
|
MovieOptionalRelationshipSerializer.new(movie, options).serialized_json
|
||||||
|
}
|
||||||
|
let(:options) {
|
||||||
|
{}
|
||||||
|
}
|
||||||
|
let(:serializable_hash) {
|
||||||
|
JSON.parse(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
it "doesn't return optional relationship" do
|
||||||
movie.actor_ids = []
|
movie.actor_ids = []
|
||||||
json = MovieOptionalRelationshipSerializer.new(movie).serialized_json
|
|
||||||
serializable_hash = JSON.parse(json)
|
|
||||||
expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_falsey
|
expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_falsey
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "doesn't include optional relationship" do
|
||||||
|
movie.actor_ids = []
|
||||||
|
options[:include] = [:actors]
|
||||||
|
expect(serializable_hash['included']).to be_blank
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when optional relationships are determined by params data' do
|
context 'when optional relationships are determined by params data' do
|
||||||
@ -364,10 +380,25 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
expect(serializable_hash['data']['relationships'].has_key?('owner')).to be_truthy
|
expect(serializable_hash['data']['relationships'].has_key?('owner')).to be_truthy
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't return optional relationship when relationship is not included" do
|
context "when relationship is not included" do
|
||||||
json = MovieOptionalRelationshipWithParamsSerializer.new(movie, { params: { admin: false }}).serialized_json
|
let(:json) {
|
||||||
serializable_hash = JSON.parse(json)
|
MovieOptionalRelationshipWithParamsSerializer.new(movie, options).serialized_json
|
||||||
|
}
|
||||||
|
let(:options) {
|
||||||
|
{ params: { admin: false }}
|
||||||
|
}
|
||||||
|
let(:serializable_hash) {
|
||||||
|
JSON.parse(json)
|
||||||
|
}
|
||||||
|
|
||||||
|
it "doesn't return optional relationship" do
|
||||||
expect(serializable_hash['data']['relationships'].has_key?('owner')).to be_falsey
|
expect(serializable_hash['data']['relationships'].has_key?('owner')).to be_falsey
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "doesn't include optional relationship" do
|
||||||
|
options[:include] = [:owner]
|
||||||
|
expect(serializable_hash['included']).to be_blank
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user