@@ -31,8 +31,10 @@ def serializable_hash(options = nil)
31
31
32
32
add_links ( options )
33
33
else
34
- @hash [ :data ] = attributes_for_serializer ( serializer , options )
35
- add_resource_relationships ( @hash [ :data ] , serializer )
34
+ @hash [ :data ] = attributes_for ( serializer , options )
35
+ relationships = relationships_for ( serializer )
36
+ @hash [ :data ] [ :relationships ] = relationships if relationships . any?
37
+ add_included_relationships ( serializer )
36
38
end
37
39
@hash
38
40
end
@@ -44,27 +46,77 @@ def fragment_cache(cached_hash, non_cached_hash)
44
46
45
47
private
46
48
47
- def resource_identifier_type ( serializer )
49
+ def resource_identifier_type_for ( serializer )
48
50
if ActiveModel ::Serializer . config . jsonapi_resource_type == :singular
49
51
serializer . object . class . model_name . singular
50
52
else
51
53
serializer . object . class . model_name . plural
52
54
end
53
55
end
54
56
55
- def resource_identifier_id ( serializer )
57
+ def resource_identifier_id_for ( serializer )
56
58
if serializer . respond_to? ( :id )
57
- serializer . id . to_s
59
+ serializer . id
58
60
else
59
- serializer . object . id . to_s
61
+ serializer . object . id
60
62
end
61
63
end
62
64
63
- def resource_identifier ( serializer )
64
- type = resource_identifier_type ( serializer )
65
- id = resource_identifier_id ( serializer )
65
+ def resource_identifier_for ( serializer )
66
+ type = resource_identifier_type_for ( serializer )
67
+ id = resource_identifier_id_for ( serializer )
66
68
67
- { id : id , type : type }
69
+ { id : id . to_s , type : type }
70
+ end
71
+
72
+ def attributes_for ( serializer , options )
73
+ if serializer . respond_to? ( :each )
74
+ serializer . map { |s | resource_object_for ( s , options ) }
75
+ else
76
+ resource_object_for ( serializer , options )
77
+ end
78
+ end
79
+
80
+ def resource_object_for ( serializer , options )
81
+ options [ :fields ] = @fieldset && @fieldset . fields_for ( serializer )
82
+
83
+ cache_check ( serializer ) do
84
+ result = resource_identifier_for ( serializer )
85
+ attributes = serializer . attributes ( options ) . except ( :id )
86
+ result [ :attributes ] = attributes if attributes . any?
87
+ result
88
+ end
89
+ end
90
+
91
+ def relationship_value_for ( serializer , options = { } )
92
+ if serializer . respond_to? ( :each )
93
+ serializer . map { |s | resource_identifier_for ( s ) }
94
+ else
95
+ if options [ :virtual_value ]
96
+ options [ :virtual_value ]
97
+ elsif serializer && serializer . object
98
+ resource_identifier_for ( serializer )
99
+ else
100
+ nil
101
+ end
102
+ end
103
+ end
104
+
105
+ def relationships_for ( serializer )
106
+ relationships = { }
107
+ serializer . associations . each do |association |
108
+ value = relationship_value_for ( association . serializer , association . options )
109
+ relationships [ association . key ] = { data : value }
110
+ end
111
+ relationships
112
+ end
113
+
114
+ def add_included_relationships ( serializer )
115
+ serializer . associations . each do |association |
116
+ Array ( association . serializer ) . each do |assoc_serializer |
117
+ add_included ( association . key , assoc_serializer )
118
+ end
119
+ end
68
120
end
69
121
70
122
def add_included ( resource_name , serializers , parent = nil )
@@ -77,39 +129,18 @@ def add_included(resource_name, serializers, parent = nil)
77
129
@hash [ :included ] ||= [ ]
78
130
79
131
serializers . each do |serializer |
80
- attrs = attributes_for_serializer ( serializer , @options )
81
-
82
- add_resource_relationships ( attrs , serializer , add_included : false )
132
+ attrs = attributes_for ( serializer , @options )
133
+ relationships = relationships_for ( serializer )
134
+ attrs [ :relationships ] = relationships if relationships . any?
83
135
84
136
@hash [ :included ] . push ( attrs ) unless @hash [ :included ] . include? ( attrs )
85
137
end
86
138
end
87
139
88
140
serializers . each do |serializer |
89
141
serializer . associations . each do |association |
90
- serializer = association . serializer
91
-
92
- add_included ( association . key , serializer , resource_path ) if serializer
93
- end if include_nested_assoc? resource_path
94
- end
95
- end
96
-
97
- def attributes_for_serializer ( serializer , options )
98
- if serializer . respond_to? ( :each )
99
- serializer . map { |s | resource_object_for ( s , options ) }
100
- else
101
- resource_object_for ( serializer , options )
102
- end
103
- end
104
-
105
- def resource_object_for ( serializer , options )
106
- options [ :fields ] = @fieldset && @fieldset . fields_for ( serializer )
107
-
108
- cache_check ( serializer ) do
109
- result = resource_identifier ( serializer )
110
- attributes = serializer . attributes ( options ) . except ( :id )
111
- result [ :attributes ] = attributes if attributes . any?
112
- result
142
+ add_included ( association . key , association . serializer , resource_path ) if association . serializer
143
+ end if include_nested_assoc? ( resource_path )
113
144
end
114
145
end
115
146
@@ -131,34 +162,6 @@ def check_assoc(assoc)
131
162
end
132
163
end
133
164
134
- def resource_relationship_value ( serializer , options = { } )
135
- if serializer . respond_to? ( :each )
136
- serializer . map { |s | resource_identifier ( s ) }
137
- else
138
- if options [ :virtual_value ]
139
- options [ :virtual_value ]
140
- elsif serializer && serializer . object
141
- resource_identifier ( serializer )
142
- else
143
- nil
144
- end
145
- end
146
- end
147
-
148
- def add_resource_relationships ( attrs , serializer , options = { } )
149
- options [ :add_included ] = options . fetch ( :add_included , true )
150
- attrs [ :relationships ] = { } if serializer . associations . any?
151
- serializer . associations . each do |association |
152
- value = resource_relationship_value ( association . serializer , association . options )
153
- attrs [ :relationships ] [ association . key ] = { data : value }
154
- if options [ :add_included ]
155
- Array ( association . serializer ) . each do |s |
156
- add_included ( association . key , s )
157
- end
158
- end
159
- end
160
- end
161
-
162
165
def add_links ( options )
163
166
links = @hash . fetch ( :links ) { { } }
164
167
resources = serializer . instance_variable_get ( :@resource )
0 commit comments