From 8f53b7a65bacffecb25581ee3f7aa46b01214878 Mon Sep 17 00:00:00 2001 From: Vikhyat Korrapati Date: Wed, 16 Apr 2014 20:48:09 +0530 Subject: [PATCH] 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. --- app/controllers/application_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ce55c6060e4..7bae7c5fde1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -169,9 +169,9 @@ class ApplicationController < ActionController::Base def serialize_data(obj, serializer, opts={}) # If it's an array, apply the serializer as an each_serializer to the elements 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 - ActiveModel::ArraySerializer.new(obj, serializer_opts).as_json + ActiveModel::ArraySerializer.new(obj.to_ary, serializer_opts).as_json else serializer.new(obj, serializer_opts).as_json end