mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-27 17:46:05 -05:00
Detect arrays for serialization using respond_to?(:to_ary).
This is the way AMS detects arrays, and is more robust than checking is_a? for whitelisted classes. For example, this works for ActiveRecord::AssociationRelation which the current logic does not handle.
This commit is contained in:
parent
cba7d3fc82
commit
8f53b7a65b
1 changed files with 2 additions and 2 deletions
|
@ -169,9 +169,9 @@ class ApplicationController < ActionController::Base
|
||||||
def serialize_data(obj, serializer, opts={})
|
def serialize_data(obj, serializer, opts={})
|
||||||
# If it's an array, apply the serializer as an each_serializer to the elements
|
# If it's an array, apply the serializer as an each_serializer to the elements
|
||||||
serializer_opts = {scope: guardian}.merge!(opts)
|
serializer_opts = {scope: guardian}.merge!(opts)
|
||||||
if obj.is_a?(Array) or obj.is_a?(ActiveRecord::Associations::CollectionProxy)
|
if obj.respond_to?(:to_ary)
|
||||||
serializer_opts[:each_serializer] = serializer
|
serializer_opts[:each_serializer] = serializer
|
||||||
ActiveModel::ArraySerializer.new(obj, serializer_opts).as_json
|
ActiveModel::ArraySerializer.new(obj.to_ary, serializer_opts).as_json
|
||||||
else
|
else
|
||||||
serializer.new(obj, serializer_opts).as_json
|
serializer.new(obj, serializer_opts).as_json
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue