Add a Responder to handle respond_with(resource)

This commit is contained in:
Michael Rykov
2013-03-13 16:45:23 -07:00
parent ad886495a1
commit 37b0690fb8
8 changed files with 463 additions and 21 deletions

View File

@@ -30,6 +30,12 @@ module ActionController
included do
class_attribute :_serialization_scope
self._serialization_scope = :current_user
self.responder = ActiveModel::Serializer::Responder
self.respond_to :json
unless ActiveModel::Serializer.use_default_render_json
self.send(:include, RenderJsonOverride)
end
end
def serialization_scope
@@ -39,31 +45,33 @@ module ActionController
def default_serializer_options
end
def _render_option_json(json, options)
options = default_serializer_options.merge(options) if default_serializer_options
module RenderJsonOverride
def _render_option_json(json, options)
options = default_serializer_options.merge(options) if default_serializer_options
serializer = options.delete(:serializer) ||
(json.respond_to?(:active_model_serializer) && json.active_model_serializer)
serializer = options.delete(:serializer) ||
(json.respond_to?(:active_model_serializer) && json.active_model_serializer)
if json.respond_to?(:to_ary)
unless serializer <= ActiveModel::ArraySerializer
raise ArgumentError.new("#{serializer.name} is not an ArraySerializer. " +
"You may want to use the :each_serializer option instead.")
if json.respond_to?(:to_ary)
unless serializer <= ActiveModel::ArraySerializer
raise ArgumentError.new("#{serializer.name} is not an ArraySerializer. " +
"You may want to use the :each_serializer option instead.")
end
if options[:root] != false && serializer.root != false
# default root element for arrays is serializer's root or the controller name
# the serializer for an Array is ActiveModel::ArraySerializer
options[:root] ||= serializer.root || controller_name
end
end
if options[:root] != false && serializer.root != false
# default root element for arrays is serializer's root or the controller name
# the serializer for an Array is ActiveModel::ArraySerializer
options[:root] ||= serializer.root || controller_name
if serializer
options[:scope] = serialization_scope unless options.has_key?(:scope)
options[:url_options] = url_options
json = serializer.new(json, options)
end
super
end
if serializer
options[:scope] = serialization_scope unless options.has_key?(:scope)
options[:url_options] = url_options
json = serializer.new(json, options)
end
super
end
module ClassMethods

View File

@@ -66,6 +66,9 @@ module ActiveModel
self._embed = :objects
class_attribute :_root_embed
class_attribute :use_default_render_json
self.use_default_render_json = false
class << self
# Define attributes to be used in the serialization.
def attributes(*attrs)

View File

@@ -0,0 +1,43 @@
module ActiveModel
class Serializer
class Responder < ::ActionController::Responder #:nodoc:
attr_reader :serializer
protected
def display(resource, given_options = {})
if format != :json
super
else
default_options = controller.send(:default_serializer_options)
options = self.options.reverse_merge(default_options || {})
serializer = options[:serializer] ||
(resource.respond_to?(:active_model_serializer) &&
resource.active_model_serializer)
if resource.respond_to?(:to_ary)
unless serializer <= ActiveModel::ArraySerializer
raise ArgumentError.new("#{serializer.name} is not an ArraySerializer. " +
"You may want to use the :each_serializer option instead.")
end
if options[:root] != false && serializer.root != false
# default root element for arrays is serializer's root or the controller name
# the serializer for an Array is ActiveModel::ArraySerializer
options[:root] ||= serializer.root || controller.send(:controller_name)
end
end
if serializer
serialization_scope = controller.send(:serialization_scope)
options[:scope] = serialization_scope unless options.has_key?(:scope)
options[:url_options] = controller.send(:url_options)
render(given_options.merge(:json => serializer.new(resource, options)))
else
super
end
end
end
end
end
end

View File

@@ -76,6 +76,7 @@ begin
require 'action_controller/serialization'
ActiveSupport.on_load(:action_controller) do
require 'active_model/serializer/responder'
include ::ActionController::Serialization
end
rescue LoadError => ex