@@ -17,18 +17,8 @@ def serializable_hash(options = nil)
1717
1818 private
1919
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-
3020 def serializable_hash_for_collection ( serializers , options )
31- include_directive = include_directive_from_options ( instance_options )
21+ include_directive = ActiveModel :: Serializer . include_directive_from_options ( instance_options )
3222 instance_options [ :cached_attributes ] ||= ActiveModel ::Serializer . cache_read_multi ( serializers , self , include_directive )
3323 instance_opts = instance_options . merge ( include_directive : include_directive )
3424 serializers . map do |serializer |
@@ -37,37 +27,8 @@ def serializable_hash_for_collection(serializers, options)
3727 end
3828
3929 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 ] ||= { }
42- resource = serializer . cached_attributes ( options [ :fields ] , cached_attributes , self )
43- relationships = resource_relationships ( serializer , options )
44- resource . merge ( relationships )
45- end
46-
47- def resource_relationships ( serializer , options )
48- relationships = { }
49- include_directive = options . fetch ( :include_directive )
50- serializer . associations ( include_directive ) . each do |association |
51- relationships [ association . key ] ||= relationship_value_for ( association , options )
52- end
53-
54- relationships
55- end
56-
57- def relationship_value_for ( association , options )
58- return association . options [ :virtual_value ] if association . options [ :virtual_value ]
59- return unless association . serializer && association . serializer . object
60-
61- include_directive = options . fetch ( :include_directive )
62- opts = instance_options . merge ( include_directive : include_directive [ association . key ] )
63- relationship_value = Attributes . new ( association . serializer , opts ) . serializable_hash ( options )
64-
65- if association . options [ :polymorphic ] && relationship_value
66- polymorphic_type = association . serializer . object . class . name . underscore
67- relationship_value = { type : polymorphic_type , polymorphic_type . to_sym => relationship_value }
68- end
69-
70- relationship_value
30+ options [ :include_directive ] ||= ActiveModel ::Serializer . include_directive_from_options ( instance_options )
31+ serializer . serializable_hash_for_single_resource ( instance_options , options , self )
7132 end
7233 end
7334 end
0 commit comments