Skip to content

Instantly share code, notes, and snippets.

@carllerche
Created May 16, 2013 20:28
Show Gist options
  • Save carllerche/5594832 to your computer and use it in GitHub Desktop.
Save carllerche/5594832 to your computer and use it in GitHub Desktop.
module SpecHelper
module Helpers
def default_serializer_options
{}
end
def serialization_scope
nil
end
alias _serialization_scope serialization_scope
def serialized(obj, opts = {})
opts = opts.merge(controller: self)
if obj.respond_to?(:active_model_serializer)
serializer = obj.active_model_serializer
end
if opts[:serializer]
serializer = opts[:serializer]
end
if serializer
unless opts.key?(:root)
if el = (obj.respond_to?(:to_ary) && obj.first)
opts[:root] ||= el.class.to_s.underscore.pluralize
end
end
obj = ActiveModel::Serializer.build_json(self, obj, opts)
obj = obj.as_json
end
obj
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment