Skip to content

Commit 7254d34

Browse files
committed
Move Serializer#serialize into Serializer#serializable_hash
1 parent caf4910 commit 7254d34

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed

lib/active_model/serializer.rb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def self.include_directive_from_options(options)
109109
end
110110
end
111111

112+
# @api private
112113
def self.serialization_adapter_instance
113114
@serialization_adapter_instance ||= ActiveModelSerializers::Adapter::Attributes
114115
end
@@ -138,9 +139,7 @@ def success?
138139
# associations, similar to how ActiveModel::Serializers::JSON is used
139140
# in ActiveRecord::Base.
140141
#
141-
# TODO: Move to here the Attributes adapter logic for
142-
# +serializable_hash_for_single_resource(options)+
143-
# and include <tt>ActiveModel::Serializers::JSON</tt>.
142+
# TODO: Include <tt>ActiveModel::Serializers::JSON</tt>.
144143
# So that the below is true:
145144
# @param options [nil, Hash] The same valid options passed to `serializable_hash`
146145
# (:only, :except, :methods, and :include).
@@ -164,10 +163,13 @@ def success?
164163
# serializer.as_json(include: :posts)
165164
# # Second level and higher order associations work as well:
166165
# serializer.as_json(include: { posts: { include: { comments: { only: :body } }, only: :title } })
167-
def serializable_hash(adapter_opts = nil)
168-
adapter_opts ||= {}
169-
adapter_opts = { include: '*' }.merge!(adapter_opts)
170-
serialize(adapter_opts)
166+
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
167+
adapter_options ||= {}
168+
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
169+
cached_attributes = adapter_options[:cached_attributes] ||= {}
170+
resource = cached_attributes(options[:fields], cached_attributes, adapter_instance)
171+
relationships = resource_relationships(adapter_options, options, adapter_instance)
172+
resource.merge(relationships)
171173
end
172174
alias to_hash serializable_hash
173175
alias to_h serializable_hash
@@ -199,15 +201,6 @@ def read_attribute_for_serialization(attr)
199201
end
200202
end
201203

202-
# @api private
203-
def serialize(adapter_options, options = {}, adapter_instance = self.class.serialization_adapter_instance)
204-
options[:include_directive] ||= ActiveModel::Serializer.include_directive_from_options(adapter_options)
205-
cached_attributes = adapter_options[:cached_attributes] ||= {}
206-
resource = cached_attributes(options[:fields], cached_attributes, adapter_instance)
207-
relationships = resource_relationships(adapter_options, options, adapter_instance)
208-
resource.merge(relationships)
209-
end
210-
211204
# @api private
212205
def resource_relationships(adapter_options, options, adapter_instance)
213206
relationships = {}
@@ -227,7 +220,7 @@ def relationship_value_for(association, adapter_options, adapter_instance)
227220
association_object = association_serializer && association_serializer.object
228221
return unless association_object
229222

230-
relationship_value = association_serializer.serialize(adapter_options, {}, adapter_instance)
223+
relationship_value = association_serializer.serializable_hash(adapter_options, {}, adapter_instance)
231224

232225
if association.options[:polymorphic] && relationship_value
233226
polymorphic_type = association_object.class.name.underscore

lib/active_model/serializer/collection_serializer.rb

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@ def initialize(resources, options = {})
1111
@object = resources
1212
@options = options
1313
@root = options[:root]
14-
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
15-
@serializers = resources.map do |resource|
16-
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
17-
18-
if serializer_class.nil? # rubocop:disable Style/GuardClause
19-
fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
20-
else
21-
serializer_class.new(resource, options.except(:serializer))
22-
end
23-
end
14+
@serializers = serializers_from_resources
2415
end
2516

2617
def success?
2718
true
2819
end
2920

21+
# @api private
22+
def serializable_hash(adapter_options, options, adapter_instance)
23+
include_directive = ActiveModel::Serializer.include_directive_from_options(adapter_options)
24+
adapter_options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, include_directive)
25+
adapter_opts = adapter_options.merge(include_directive: include_directive)
26+
serializers.map do |serializer|
27+
serializer.serializable_hash(adapter_opts, options, adapter_instance)
28+
end
29+
end
30+
3031
# TODO: unify naming of root, json_key, and _type. Right now, a serializer's
3132
# json_key comes from the root option or the object's model name, by default.
3233
# But, if a dev defines a custom `json_key` method with an explicit value,
@@ -56,19 +57,28 @@ def paginated?
5657
object.respond_to?(:size)
5758
end
5859

59-
# @api private
60-
def serialize(adapter_options, options, adapter_instance)
61-
include_directive = ActiveModel::Serializer.include_directive_from_options(adapter_options)
62-
adapter_options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(self, adapter_instance, include_directive)
63-
adapter_opts = adapter_options.merge(include_directive: include_directive)
64-
serializers.map do |serializer|
65-
serializer.serialize(adapter_opts, options, adapter_instance)
60+
protected
61+
62+
attr_reader :serializers, :options
63+
64+
private
65+
66+
def serializers_from_resources
67+
serializer_context_class = options.fetch(:serializer_context_class, ActiveModel::Serializer)
68+
object.map do |resource|
69+
serializer_from_resource(resource, serializer_context_class, options)
6670
end
6771
end
6872

69-
protected
73+
def serializer_from_resource(resource, serializer_context_class, options)
74+
serializer_class = options.fetch(:serializer) { serializer_context_class.serializer_for(resource) }
7075

71-
attr_reader :serializers, :options
76+
if serializer_class.nil? # rubocop:disable Style/GuardClause
77+
fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
78+
else
79+
serializer_class.new(resource, options.except(:serializer))
80+
end
81+
end
7282
end
7383
end
7484
end

lib/active_model_serializers/adapter/attributes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Adapter
33
class Attributes < Base
44
def serializable_hash(options = nil)
55
options = serialization_options(options)
6-
serializer.serialize(instance_options, options, self)
6+
serializer.serializable_hash(instance_options, options, self)
77
end
88
end
99
end

0 commit comments

Comments
 (0)