jsonapi-serializer/spec/lib/object_serializer_polymorphic_spec.rb
Christopher Sansone 6d01bec146
Improved relationship serializer options (#32)
* allow the relationship's serializer key to be a Proc

* fixes

* specifically test the relationship name and the resource type

* support object blocks without a serializer defined

* stop validation gracefully if the relationship is polymorphic

* improve performance by using instance variables instead of accessor methods

* force initialization up front to avoid the iterative calls

* serializer procs should act like polymorphic when determining the id_method_name

* specs for serializer procs

* updated with more details and examples for relationship serializer options

* adjust specs to define the serializers

* avoid extra method calls for performance

* name change

* one less function call for better performance

* do not require lazy loaded relationships to resolve the serializer

* give polymorphic precedence for backwards compatibility

* move serializer inference into ObjectSerializer to allow for overriding

* move method for better diff

* fix race condition in multi-threaded environments
2020-02-15 14:57:44 +00:00

100 lines
2.3 KiB
Ruby

require 'spec_helper'
describe FastJsonapi::ObjectSerializer do
class List
attr_accessor :id, :name, :items
end
class ChecklistItem
attr_accessor :id, :name
end
class Car
attr_accessor :id, :model, :year
end
class Animal
attr_accessor :id, :uuid, :species
end
class ChecklistItemSerializer
include FastJsonapi::ObjectSerializer
set_type :checklist_item
attributes :name
set_key_transform :dash
end
class CarSerializer
include FastJsonapi::ObjectSerializer
set_type :car
attributes :model, :year
set_key_transform :dash
end
class AnimalSerializer
include FastJsonapi::ObjectSerializer
set_type :checklist_item
attributes :uuid, :species
end
class ListSerializer
include FastJsonapi::ObjectSerializer
set_type :list
attributes :name
set_key_transform :dash
has_many :items, polymorphic: true
end
class ZooSerializer
include FastJsonapi::ObjectSerializer
set_type :list
attributes :name
has_many :items, polymorphic: true, id_method_name: :uuid
end
let(:car) do
car = Car.new
car.id = 1
car.model = 'Toyota Corolla'
car.year = 1987
car
end
let(:checklist_item) do
checklist_item = ChecklistItem.new
checklist_item.id = 2
checklist_item.name = 'Do this action!'
checklist_item
end
let(:animal) do
animal = Animal.new
animal.id = 1
animal.species = 'Mellivora capensis'
animal.uuid = SecureRandom.uuid
animal
end
context 'when serializing id and type of polymorphic relationships' do
it 'should return correct type when transform_method is specified' do
list = List.new
list.id = 1
list.items = [checklist_item, car]
list_hash = ListSerializer.new(list).to_hash
record_type = list_hash[:data][:relationships][:items][:data][0][:type]
expect(record_type).to eq 'checklist-item'.to_sym
record_type = list_hash[:data][:relationships][:items][:data][1][:type]
expect(record_type).to eq 'car'.to_sym
end
it 'should use the correct id method on associated objects' do
list = List.new
list.id = 1
list.items = [animal]
list_hash = ZooSerializer.new(list).to_hash
id = list_hash[:data][:relationships][:items][:data][0][:id]
expect(id).to eq animal.uuid
end
end
end