forked from darkside/fast_jsonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonapi_group_context.rb
112 lines (95 loc) · 2.78 KB
/
jsonapi_group_context.rb
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
RSpec.shared_context 'jsonapi group class' do
# Person, Group Classes and serializers
before(:context) do
# models
class JSONAPIPerson
attr_accessor :id, :first_name, :last_name
end
class JSONAPIGroup
attr_accessor :id, :name, :groupees # Let's assume groupees can be Person or Group objects
end
# serializers
class JSONAPIPersonSerializer < JSONAPI::Serializable::Resource
type 'person'
attributes :first_name, :last_name
end
class JSONAPIGroupSerializer < JSONAPI::Serializable::Resource
type 'group'
attributes :name
has_many :groupees
end
class JSONAPISerializerB
def initialize(data, options = {})
@serializer = JSONAPI::Serializable::Renderer.new
@options = options.merge(class: {
JSONAPIPerson: JSONAPIPersonSerializer,
JSONAPIGroup: JSONAPIGroupSerializer
})
@data = data
end
def to_json
@serializer.render(@data, @options).to_json
end
def to_hash
@serializer.render(@data, @options)
end
end
end
after :context do
classes_to_remove = %i[
JSONAPIPerson
JSONAPIGroup
JSONAPIPersonSerializer
JSONAPIGroupSerializer]
classes_to_remove.each do |klass_name|
Object.send(:remove_const, klass_name) if Object.constants.include?(klass_name)
end
end
let(:jsonapi_groups) do
group_count = 0
person_count = 0
3.times.map do |i|
group = JSONAPIGroup.new
group.id = group_count + 1
group.name = "Test Group #{group.id}"
group_count = group.id
person = JSONAPIPerson.new
person.id = person_count + 1
person.last_name = "Last Name #{person.id}"
person.first_name = "First Name #{person.id}"
person_count = person.id
child_group = JSONAPIGroup.new
child_group.id = group_count + 1
child_group.name = "Test Group #{child_group.id}"
group_count = child_group.id
group.groupees = [person, child_group]
group
end
end
let(:jsonapi_person) do
person = JSONAPIPerson.new
person.id = 3
person
end
def build_jsonapi_groups(count)
group_count = 0
person_count = 0
count.times.map do |i|
group = JSONAPIGroup.new
group.id = group_count + 1
group.name = "Test Group #{group.id}"
group_count = group.id
person = JSONAPIPerson.new
person.id = person_count + 1
person.last_name = "Last Name #{person.id}"
person.first_name = "First Name #{person.id}"
person_count = person.id
child_group = JSONAPIGroup.new
child_group.id = group_count + 1
child_group.name = "Test Group #{child_group.id}"
group_count = child_group.id
group.groupees = [person, child_group]
group
end
end
end