@@ -8,7 +8,8 @@ class JsonApi < Base
88
99 def initialize ( serializer , options = { } )
1010 super
11- @included = ActiveModel ::Serializer ::Utils . include_args_to_hash ( instance_options [ :include ] )
11+ @include_tree = IncludeTree . from_include_args ( options [ :include ] )
12+
1213 fields = options . delete ( :fields )
1314 if fields
1415 @fieldset = ActiveModel ::Serializer ::Fieldset . new ( fields , serializer . json_key )
@@ -19,10 +20,11 @@ def initialize(serializer, options = {})
1920
2021 def serializable_hash ( options = nil )
2122 options ||= { }
23+
2224 if serializer . respond_to? ( :each )
23- serializable_hash_for_collection ( serializer , options )
25+ serializable_hash_for_collection ( options )
2426 else
25- serializable_hash_for_single_resource ( serializer , options )
27+ serializable_hash_for_single_resource ( options )
2628 end
2729 end
2830
@@ -34,10 +36,10 @@ def fragment_cache(cached_hash, non_cached_hash)
3436 private
3537
3638 ActiveModel . silence_warnings do
37- attr_reader :included , : fieldset
39+ attr_reader :fieldset
3840 end
3941
40- def serializable_hash_for_collection ( serializer , options )
42+ def serializable_hash_for_collection ( options )
4143 hash = { data : [ ] }
4244 serializer . each do |s |
4345 result = self . class . new ( s , instance_options . merge ( fieldset : fieldset ) ) . serializable_hash ( options )
@@ -57,10 +59,10 @@ def serializable_hash_for_collection(serializer, options)
5759 hash
5860 end
5961
60- def serializable_hash_for_single_resource ( serializer , options )
62+ def serializable_hash_for_single_resource ( options )
6163 primary_data = primary_data_for ( serializer , options )
6264 relationships = relationships_for ( serializer )
63- included = included_for ( serializer )
65+ included = included_resources ( @include_tree )
6466 hash = { data : primary_data }
6567 hash [ :data ] [ :relationships ] = relationships if relationships . any?
6668 hash [ :included ] = included if included . any?
@@ -123,37 +125,37 @@ def relationship_value_for(serializer, options = {})
123125 end
124126
125127 def relationships_for ( serializer )
126- Hash [ serializer . associations . map { |association | [ association . key , { data : relationship_value_for ( association . serializer , association . options ) } ] } ]
128+ serializer . associations . each_with_object ( { } ) do |association , hash |
129+ hash [ association . key ] = { data : relationship_value_for ( association . serializer , association . options ) }
130+ end
127131 end
128132
129- def included_for ( serializer )
130- included . flat_map { |inc |
131- association = serializer . associations . find { |assoc | assoc . key == inc . first }
132- _included_for ( association . serializer , inc . second ) if association
133- } . uniq
133+ def included_resources ( include_tree )
134+ included = [ ]
135+
136+ serializer . associations ( include_tree ) . each do |association |
137+ add_included_resources_for ( association . serializer , include_tree [ association . key ] , included )
138+ end
139+
140+ included
134141 end
135142
136- def _included_for ( serializer , includes )
143+ def add_included_resources_for ( serializer , include_tree , included )
137144 if serializer . respond_to? ( :each )
138- serializer . flat_map { |s | _included_for ( s , includes ) } . uniq
145+ serializer . each { |s | add_included_resources_for ( s , include_tree , included ) }
139146 else
140- return [ ] unless serializer && serializer . object
147+ return unless serializer && serializer . object
141148
142149 primary_data = primary_data_for ( serializer , instance_options )
143150 relationships = relationships_for ( serializer )
144151 primary_data [ :relationships ] = relationships if relationships . any?
145152
146- included = [ primary_data ]
153+ return if included . include? ( primary_data )
154+ included . push ( primary_data )
147155
148- includes . each do |inc |
149- association = serializer . associations . find { |assoc | assoc . key == inc . first }
150- if association
151- included . concat ( _included_for ( association . serializer , inc . second ) )
152- included . uniq!
153- end
156+ serializer . associations ( include_tree ) . each do |association |
157+ add_included_resources_for ( association . serializer , include_tree [ association . key ] , included )
154158 end
155-
156- included
157159 end
158160 end
159161
0 commit comments