@@ -16,16 +16,14 @@ def initialize(serializer, options = {})
16
16
end
17
17
18
18
def serializable_hash ( options = { } )
19
- @root = ( @options [ :root ] || serializer . json_key . to_s . pluralize ) . to_sym
20
-
21
19
if serializer . respond_to? ( :each )
22
- @hash [ @root ] = serializer . map do |s |
23
- self . class . new ( s , @options . merge ( top : @top , fieldset : @fieldset ) ) . serializable_hash [ @root ]
20
+ @hash [ :data ] = serializer . map do |s |
21
+ self . class . new ( s , @options . merge ( top : @top , fieldset : @fieldset ) ) . serializable_hash [ :data ]
24
22
end
25
23
else
26
24
@hash = cached_object do
27
- @hash [ @root ] = attributes_for_serializer ( serializer , @options )
28
- add_resource_links ( @hash [ @root ] , serializer )
25
+ @hash [ :data ] = attributes_for_serializer ( serializer , @options )
26
+ add_resource_links ( @hash [ :data ] , serializer )
29
27
@hash
30
28
end
31
29
end
@@ -35,58 +33,40 @@ def serializable_hash(options = {})
35
33
private
36
34
37
35
def add_links ( resource , name , serializers )
38
- type = serialized_object_type ( serializers )
39
36
resource [ :links ] ||= { }
40
-
41
- if name . to_s == type || !type
42
- resource [ :links ] [ name ] ||= [ ]
43
- resource [ :links ] [ name ] += serializers . map { |serializer | serializer . id . to_s }
44
- else
45
- resource [ :links ] [ name ] ||= { }
46
- resource [ :links ] [ name ] [ :type ] = type
47
- resource [ :links ] [ name ] [ :ids ] ||= [ ]
48
- resource [ :links ] [ name ] [ :ids ] += serializers . map { |serializer | serializer . id . to_s }
49
- end
37
+ resource [ :links ] [ name ] ||= { linkage : [ ] }
38
+ resource [ :links ] [ name ] [ :linkage ] += serializers . map { |serializer | { type : serializer . type , id : serializer . id . to_s } }
50
39
end
51
40
52
41
def add_link ( resource , name , serializer )
53
42
resource [ :links ] ||= { }
54
- resource [ :links ] [ name ] = nil
43
+ resource [ :links ] [ name ] = { linkage : nil }
55
44
56
45
if serializer && serializer . object
57
- type = serialized_object_type ( serializer )
58
- if name . to_s == type || !type
59
- resource [ :links ] [ name ] = serializer . id . to_s
60
- else
61
- resource [ :links ] [ name ] ||= { }
62
- resource [ :links ] [ name ] [ :type ] = type
63
- resource [ :links ] [ name ] [ :id ] = serializer . id . to_s
64
- end
46
+ resource [ :links ] [ name ] [ :linkage ] = { type : serializer . type , id : serializer . id . to_s }
65
47
end
66
48
end
67
49
68
- def add_linked ( resource_name , serializers , parent = nil )
50
+ def add_included ( resource_name , serializers , parent = nil )
69
51
serializers = Array ( serializers ) unless serializers . respond_to? ( :each )
70
52
71
53
resource_path = [ parent , resource_name ] . compact . join ( '.' )
72
54
73
- if include_assoc? ( resource_path ) && resource_type = serialized_object_type ( serializers )
74
- plural_name = resource_type . pluralize . to_sym
75
- @top [ :linked ] ||= { }
76
- @top [ :linked ] [ plural_name ] ||= [ ]
55
+ if include_assoc? ( resource_path )
56
+ @top [ :included ] ||= [ ]
77
57
78
58
serializers . each do |serializer |
79
59
attrs = attributes_for_serializer ( serializer , @options )
80
60
81
- add_resource_links ( attrs , serializer , add_linked : false )
61
+ add_resource_links ( attrs , serializer , add_included : false )
82
62
83
- @top [ :linked ] [ plural_name ] . push ( attrs ) unless @top [ :linked ] [ plural_name ] . include? ( attrs )
63
+ @top [ :included ] . push ( attrs ) unless @top [ :included ] . include? ( attrs )
84
64
end
85
65
end
86
66
87
67
serializers . each do |serializer |
88
68
serializer . each_association do |name , association , opts |
89
- add_linked ( name , association , resource_path ) if association
69
+ add_included ( name , association , resource_path ) if association
90
70
end if include_nested_assoc? resource_path
91
71
end
92
72
end
@@ -97,14 +77,16 @@ def attributes_for_serializer(serializer, options)
97
77
result = [ ]
98
78
serializer . each do |object |
99
79
options [ :fields ] = @fieldset && @fieldset . fields_for ( serializer )
80
+ options [ :required_fields ] = [ :id , :type ]
100
81
attributes = object . attributes ( options )
101
- attributes [ :id ] = attributes [ :id ] . to_s if attributes [ :id ]
82
+ attributes [ :id ] = attributes [ :id ] . to_s
102
83
result << attributes
103
84
end
104
85
else
105
86
options [ :fields ] = @fieldset && @fieldset . fields_for ( serializer )
87
+ options [ :required_fields ] = [ :id , :type ]
106
88
result = serializer . attributes ( options )
107
- result [ :id ] = result [ :id ] . to_s if result [ :id ]
89
+ result [ :id ] = result [ :id ] . to_s
108
90
end
109
91
110
92
result
@@ -128,18 +110,8 @@ def check_assoc(assoc)
128
110
end
129
111
end
130
112
131
- def serialized_object_type ( serializer )
132
- return false unless Array ( serializer ) . first
133
- type_name = Array ( serializer ) . first . object . class . to_s . demodulize . underscore
134
- if serializer . respond_to? ( :first )
135
- type_name . pluralize
136
- else
137
- type_name
138
- end
139
- end
140
-
141
113
def add_resource_links ( attrs , serializer , options = { } )
142
- options [ :add_linked ] = options . fetch ( :add_linked , true )
114
+ options [ :add_included ] = options . fetch ( :add_included , true )
143
115
144
116
serializer . each_association do |name , association , opts |
145
117
attrs [ :links ] ||= { }
@@ -150,9 +122,9 @@ def add_resource_links(attrs, serializer, options = {})
150
122
add_link ( attrs , name , association )
151
123
end
152
124
153
- if @ options[ :embed ] != :ids && options [ :add_linked ]
125
+ if options [ :add_included ]
154
126
Array ( association ) . each do |association |
155
- add_linked ( name , association )
127
+ add_included ( name , association )
156
128
end
157
129
end
158
130
end
0 commit comments