-
Notifications
You must be signed in to change notification settings - Fork 422
Expand file tree
/
Copy pathobject_serializer_polymorphic_spec.rb
More file actions
79 lines (68 loc) · 1.88 KB
/
object_serializer_polymorphic_spec.rb
File metadata and controls
79 lines (68 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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 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 = 'sdfsdfds'
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