Allow fieldsets to specify no attributes/relationships
This commit is contained in:
parent
1a407c0030
commit
21ae4aaa0a
@ -44,6 +44,8 @@ module FastJsonapi
|
|||||||
def attributes_hash(record, fieldset = nil, params = {})
|
def attributes_hash(record, fieldset = nil, params = {})
|
||||||
attributes = attributes_to_serialize
|
attributes = attributes_to_serialize
|
||||||
attributes = attributes.slice(*fieldset) if fieldset.present?
|
attributes = attributes.slice(*fieldset) if fieldset.present?
|
||||||
|
attributes = {} if fieldset == []
|
||||||
|
|
||||||
attributes.each_with_object({}) do |(_k, attribute), hash|
|
attributes.each_with_object({}) do |(_k, attribute), hash|
|
||||||
attribute.serialize(record, params, hash)
|
attribute.serialize(record, params, hash)
|
||||||
end
|
end
|
||||||
@ -52,6 +54,7 @@ module FastJsonapi
|
|||||||
def relationships_hash(record, relationships = nil, fieldset = nil, params = {})
|
def relationships_hash(record, relationships = nil, fieldset = nil, params = {})
|
||||||
relationships = relationships_to_serialize if relationships.nil?
|
relationships = relationships_to_serialize if relationships.nil?
|
||||||
relationships = relationships.slice(*fieldset) if fieldset.present?
|
relationships = relationships.slice(*fieldset) if fieldset.present?
|
||||||
|
relationships = {} if fieldset == []
|
||||||
|
|
||||||
relationships.each_with_object({}) do |(_k, relationship), hash|
|
relationships.each_with_object({}) do |(_k, relationship), hash|
|
||||||
relationship.serialize(record, params, hash)
|
relationship.serialize(record, params, hash)
|
||||||
|
@ -22,6 +22,18 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
expect(hash[:data][:relationships].keys.sort).to eq %i[actors advertising_campaign]
|
expect(hash[:data][:relationships].keys.sort).to eq %i[actors advertising_campaign]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'returns no fields when none are specified' do
|
||||||
|
hash = MovieSerializer.new(movie, fields: { movie: [] }).serializable_hash
|
||||||
|
|
||||||
|
expect(hash[:data][:attributes].keys).to eq []
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns no relationships when none are specified' do
|
||||||
|
hash = MovieSerializer.new(movie, fields: { movie: [] }).serializable_hash
|
||||||
|
|
||||||
|
expect(hash[:data][:relationships].keys).to eq []
|
||||||
|
end
|
||||||
|
|
||||||
it 'only returns specified fields for included relationships' do
|
it 'only returns specified fields for included relationships' do
|
||||||
hash = MovieSerializer.new(movie, fields: fields, include: %i[actors]).serializable_hash
|
hash = MovieSerializer.new(movie, fields: fields, include: %i[actors]).serializable_hash
|
||||||
|
|
||||||
@ -45,4 +57,25 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
|
|
||||||
expect(hash[:included][3][:relationships].keys.sort).to eq %i[movie]
|
expect(hash[:included][3][:relationships].keys.sort).to eq %i[movie]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'with no included fields specified' do
|
||||||
|
let(:fields) do
|
||||||
|
{
|
||||||
|
movie: %i[name actors advertising_campaign],
|
||||||
|
actor: []
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns no fields for included relationships when none are specified' do
|
||||||
|
hash = MovieSerializer.new(movie, fields: fields, include: %i[actors advertising_campaign]).serializable_hash
|
||||||
|
|
||||||
|
expect(hash[:included][2][:attributes].keys).to eq []
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns no relationships when none are specified' do
|
||||||
|
hash = MovieSerializer.new(movie, fields: fields, include: %i[actors advertising_campaign]).serializable_hash
|
||||||
|
|
||||||
|
expect(hash[:included][2][:relationships].keys).to eq []
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user