@@ -14,7 +14,8 @@ def initialize(serializer, options = {})
14
14
@options [ :include ] = @options [ :include ] . split ( ',' )
15
15
end
16
16
17
- if fields = options . delete ( :fields )
17
+ fields = options . delete ( :fields )
18
+ if fields
18
19
@fieldset = ActiveModel ::Serializer ::Fieldset . new ( fields , serializer . json_key )
19
20
else
20
21
@fieldset = options [ :fieldset ]
@@ -48,7 +49,7 @@ def serializable_hash(options = nil)
48
49
49
50
def fragment_cache ( cached_hash , non_cached_hash )
50
51
root = false if @options . include? ( :include )
51
- JsonApi ::FragmentCache . new ( ) . fragment_cache ( root , cached_hash , non_cached_hash )
52
+ JsonApi ::FragmentCache . new . fragment_cache ( root , cached_hash , non_cached_hash )
52
53
end
53
54
54
55
private
@@ -103,14 +104,12 @@ def relationship_value_for(serializer, options = {})
103
104
options [ :virtual_value ]
104
105
elsif serializer && serializer . object
105
106
resource_identifier_for ( serializer )
106
- else
107
- nil
108
107
end
109
108
end
110
109
end
111
110
112
111
def relationships_for ( serializer )
113
- Hash [ serializer . associations . map { |association | [ association . key , { data : relationship_value_for ( association . serializer , association . options ) } ] } ]
112
+ Hash [ serializer . associations . map { |association | [ association . key , { data : relationship_value_for ( association . serializer , association . options ) } ] } ]
114
113
end
115
114
116
115
def included_for ( serializer )
@@ -121,24 +120,23 @@ def _included_for(resource_name, serializer, parent = nil)
121
120
if serializer . respond_to? ( :each )
122
121
serializer . flat_map { |s | _included_for ( resource_name , s , parent ) } . uniq
123
122
else
123
+ return [ ] unless serializer && serializer . object
124
124
result = [ ]
125
- if serializer && serializer . object
126
- resource_path = [ parent , resource_name ] . compact . join ( '.' )
127
-
128
- if include_assoc? ( resource_path )
129
- primary_data = primary_data_for ( serializer , @options )
130
- relationships = relationships_for ( serializer )
131
- primary_data [ :relationships ] = relationships if relationships . any?
132
- result . push ( primary_data )
133
- end
125
+ resource_path = [ parent , resource_name ] . compact . join ( '.' )
126
+
127
+ if include_assoc? ( resource_path )
128
+ primary_data = primary_data_for ( serializer , @options )
129
+ relationships = relationships_for ( serializer )
130
+ primary_data [ :relationships ] = relationships if relationships . any?
131
+ result . push ( primary_data )
132
+ end
134
133
135
- if include_nested_assoc? ( resource_path )
136
- serializer . associations . each do |association |
137
- if association . serializer
138
- result . concat ( _included_for ( association . key , association . serializer , resource_path ) )
139
- result . uniq!
140
- end
141
- end
134
+ if include_nested_assoc? ( resource_path )
135
+ non_empty_associations = serializer . associations . select ( &:serializer )
136
+
137
+ non_empty_associations . each do |association |
138
+ result . concat ( _included_for ( association . key , association . serializer , resource_path ) )
139
+ result . uniq!
142
140
end
143
141
end
144
142
result
@@ -160,17 +158,15 @@ def check_assoc(assoc)
160
158
def add_links ( options )
161
159
links = @hash . fetch ( :links ) { { } }
162
160
collection = serializer . object
163
- if is_paginated? ( collection )
164
- @hash [ :links ] = add_pagination_links ( links , collection , options )
165
- end
161
+ @hash [ :links ] = add_pagination_links ( links , collection , options ) if paginated? ( collection )
166
162
end
167
163
168
164
def add_pagination_links ( links , collection , options )
169
165
pagination_links = JsonApi ::PaginationLinks . new ( collection , options [ :context ] ) . serializable_hash ( options )
170
166
links . update ( pagination_links )
171
167
end
172
168
173
- def is_paginated ?( collection )
169
+ def paginated ?( collection )
174
170
collection . respond_to? ( :current_page ) &&
175
171
collection . respond_to? ( :total_pages ) &&
176
172
collection . respond_to? ( :size )
0 commit comments