Skip to content

Commit 41575e3

Browse files
committed
Moving Attributes#serializable_hash_for_single_resource to Serializer
1 parent f28e486 commit 41575e3

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

lib/active_model/serializer.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ def self.get_serializer_for(klass)
9898
end
9999
end
100100

101+
def self.include_directive_from_options(options)
102+
if options[:include_directive]
103+
options[:include_directive]
104+
elsif options[:include]
105+
JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
106+
else
107+
ActiveModelSerializers.default_include_directive
108+
end
109+
end
110+
101111
attr_accessor :object, :root, :scope
102112

103113
# `scope_name` is set as :current_user by default in the controller.
@@ -185,6 +195,39 @@ def read_attribute_for_serialization(attr)
185195
end
186196
end
187197

198+
def serializable_hash_for_single_resource(adapter_options, options, adapter_instance)
199+
cached_attributes = adapter_options[:cached_attributes] ||= {}
200+
resource = cached_attributes(options[:fields], cached_attributes, adapter_instance)
201+
relationships = resource_relationships(options)
202+
resource.merge(relationships)
203+
end
204+
205+
def resource_relationships(options)
206+
relationships = {}
207+
include_directive = options.fetch(:include_directive)
208+
associations(include_directive).each do |association|
209+
relationships[association.key] ||= relationship_value_for(association, options)
210+
end
211+
212+
relationships
213+
end
214+
215+
def relationship_value_for(association, options)
216+
return association.options[:virtual_value] if association.options[:virtual_value]
217+
return unless association.serializer && association.serializer.object
218+
219+
include_directive = options.fetch(:include_directive)
220+
opts = instance_options.merge(include_directive: include_directive[association.key])
221+
relationship_value = ActiveModelSerializers::Adapter::Attributes.new(association.serializer, opts).serializable_hash(options)
222+
223+
if association.options[:polymorphic] && relationship_value
224+
polymorphic_type = association.serializer.object.class.name.underscore
225+
relationship_value = { type: polymorphic_type, polymorphic_type.to_sym => relationship_value }
226+
end
227+
228+
relationship_value
229+
end
230+
188231
protected
189232

190233
attr_accessor :instance_options

lib/active_model_serializers/adapter/attributes.rb

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)