@@ -3,44 +3,51 @@ module Adapter
33 class Attributes < Base
44 def initialize ( serializer , options = { } )
55 super
6- @include_directive =
7- if options [ :include_directive ]
8- options [ :include_directive ]
9- elsif options [ :include ]
10- JSONAPI ::IncludeDirective . new ( options [ :include ] , allow_wildcard : true )
11- else
12- ActiveModelSerializers . default_include_directive
13- end
146 end
157
168 def serializable_hash ( options = nil )
179 options = serialization_options ( options )
1810
1911 if serializer . respond_to? ( :each )
20- serializable_hash_for_collection ( options )
12+ serializable_hash_for_collection ( serializer , options )
2113 else
22- serializable_hash_for_single_resource ( options )
14+ serializable_hash_for_single_resource ( serializer , instance_options , options )
2315 end
2416 end
2517
2618 private
2719
28- def serializable_hash_for_collection ( options )
29- instance_options [ :cached_attributes ] ||= ActiveModel ::Serializer . cache_read_multi ( serializer , self , @include_directive )
30- opts = instance_options . merge ( include_directive : @include_directive )
31- serializer . map { |s | Attributes . new ( s , opts ) . serializable_hash ( options ) }
20+ def include_directive_from_options ( options )
21+ if options [ :include_directive ]
22+ options [ :include_directive ]
23+ elsif options [ :include ]
24+ JSONAPI ::IncludeDirective . new ( options [ :include ] , allow_wildcard : true )
25+ else
26+ ActiveModelSerializers . default_include_directive
27+ end
28+ end
29+
30+ def serializable_hash_for_collection ( serializers , options )
31+ include_directive = include_directive_from_options ( instance_options )
32+ instance_options [ :cached_attributes ] ||= ActiveModel ::Serializer . cache_read_multi ( serializers , self , include_directive )
33+ instance_opts = instance_options . merge ( include_directive : include_directive )
34+ serializers . map do |serializer |
35+ serializable_hash_for_single_resource ( serializer , instance_opts , options )
36+ end
3237 end
3338
34- def serializable_hash_for_single_resource ( options )
35- cached_attributes = instance_options [ :cached_attributes ] || { }
39+ def serializable_hash_for_single_resource ( serializer , instance_options , options )
40+ options [ :include_directive ] ||= include_directive_from_options ( instance_options )
41+ cached_attributes = instance_options [ :cached_attributes ] ||= { }
3642 resource = serializer . cached_attributes ( options [ :fields ] , cached_attributes , self )
37- relationships = resource_relationships ( options )
43+ relationships = resource_relationships ( serializer , options )
3844 resource . merge ( relationships )
3945 end
4046
41- def resource_relationships ( options )
47+ def resource_relationships ( serializer , options )
4248 relationships = { }
43- serializer . associations ( @include_directive ) . each do |association |
49+ include_directive = options . fetch ( :include_directive )
50+ serializer . associations ( include_directive ) . each do |association |
4451 relationships [ association . key ] ||= relationship_value_for ( association , options )
4552 end
4653
@@ -51,7 +58,8 @@ def relationship_value_for(association, options)
5158 return association . options [ :virtual_value ] if association . options [ :virtual_value ]
5259 return unless association . serializer && association . serializer . object
5360
54- opts = instance_options . merge ( include_directive : @include_directive [ association . key ] )
61+ include_directive = options . fetch ( :include_directive )
62+ opts = instance_options . merge ( include_directive : include_directive [ association . key ] )
5563 relationship_value = Attributes . new ( association . serializer , opts ) . serializable_hash ( options )
5664
5765 if association . options [ :polymorphic ] && relationship_value
0 commit comments