Skip to content

Commit 189b795

Browse files
committed
fixing array rendering when elements doesn't have a serializer
1 parent d589268 commit 189b795

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

lib/action_controller/serialization.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ def use_adapter?
3939
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
4040

4141
if use_adapter? && (serializer = get_serializer(resource))
42-
4342
@_serializer_opts[:scope] ||= serialization_scope
4443
@_serializer_opts[:scope_name] = _serialization_scope
4544

46-
# omg hax
47-
object = serializer.new(resource, @_serializer_opts)
48-
adapter = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts)
49-
super(adapter, options)
50-
else
51-
super(resource, options)
45+
object = serializer.new(resource, @_serializer_opts)
46+
47+
if serializer == ActiveModel::Serializer.config.array_serializer
48+
resource = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts) unless object.objects.all? {|i| i.nil?}
49+
else
50+
resource = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts)
51+
end
5252
end
53+
54+
super(resource, options)
5355
end
5456
end
5557

lib/active_model/serializer/array_serializer.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class ArraySerializer
44
include Enumerable
55
delegate :each, to: :@objects
66

7-
attr_reader :meta, :meta_key
7+
attr_reader :meta, :meta_key, :objects
88

99
def initialize(objects, options = {})
1010
@resource = objects
@@ -13,7 +13,10 @@ def initialize(objects, options = {})
1313
:serializer,
1414
ActiveModel::Serializer.serializer_for(object)
1515
)
16-
serializer_class.new(object, options.except(:serializer))
16+
17+
unless serializer_class.nil?
18+
serializer_class.new(object, options.except(:serializer))
19+
end
1720
end
1821
@meta = options[:meta]
1922
@meta_key = options[:meta_key]

0 commit comments

Comments
 (0)