@@ -3,44 +3,51 @@ module Adapter
3
3
class Attributes < Base
4
4
def initialize ( serializer , options = { } )
5
5
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
14
6
end
15
7
16
8
def serializable_hash ( options = nil )
17
9
options = serialization_options ( options )
18
10
19
11
if serializer . respond_to? ( :each )
20
- serializable_hash_for_collection ( options )
12
+ serializable_hash_for_collection ( serializer , options )
21
13
else
22
- serializable_hash_for_single_resource ( options )
14
+ serializable_hash_for_single_resource ( serializer , instance_options , options )
23
15
end
24
16
end
25
17
26
18
private
27
19
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
32
37
end
33
38
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 ] ||= { }
36
42
resource = serializer . cached_attributes ( options [ :fields ] , cached_attributes , self )
37
- relationships = resource_relationships ( options )
43
+ relationships = resource_relationships ( serializer , options )
38
44
resource . merge ( relationships )
39
45
end
40
46
41
- def resource_relationships ( options )
47
+ def resource_relationships ( serializer , options )
42
48
relationships = { }
43
- serializer . associations ( @include_directive ) . each do |association |
49
+ include_directive = options . fetch ( :include_directive )
50
+ serializer . associations ( include_directive ) . each do |association |
44
51
relationships [ association . key ] ||= relationship_value_for ( association , options )
45
52
end
46
53
@@ -51,7 +58,8 @@ def relationship_value_for(association, options)
51
58
return association . options [ :virtual_value ] if association . options [ :virtual_value ]
52
59
return unless association . serializer && association . serializer . object
53
60
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 ] )
55
63
relationship_value = Attributes . new ( association . serializer , opts ) . serializable_hash ( options )
56
64
57
65
if association . options [ :polymorphic ] && relationship_value
0 commit comments