Attempt to render the template inside the responder, so it can be used for caching and pagination.
Signed-off-by: Yehuda Katz <wycats@gmail.com>
This commit is contained in:
parent
dbf20c2dbb
commit
684a6b3c71
@ -178,23 +178,7 @@ def clear_respond_to
|
||||
#
|
||||
def respond_to(*mimes, &block)
|
||||
raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
|
||||
|
||||
collector = Collector.new
|
||||
mimes = collect_mimes_from_class_level if mimes.empty?
|
||||
mimes.each { |mime| collector.send(mime) }
|
||||
block.call(collector) if block_given?
|
||||
|
||||
if format = request.negotiate_mime(collector.order)
|
||||
self.formats = [format.to_sym]
|
||||
|
||||
if response = collector.response_for(format)
|
||||
response.call
|
||||
else
|
||||
default_render
|
||||
end
|
||||
else
|
||||
head :not_acceptable
|
||||
end
|
||||
collect_mimes_for_render(mimes, block){ default_render }
|
||||
end
|
||||
|
||||
# respond_with wraps a resource around a responder for default representation.
|
||||
@ -227,10 +211,10 @@ def respond_to(*mimes, &block)
|
||||
# a proc to it.
|
||||
#
|
||||
def respond_with(*resources, &block)
|
||||
respond_to(&block)
|
||||
rescue ActionView::MissingTemplate
|
||||
options = resources.extract_options!
|
||||
(options.delete(:responder) || responder).call(self, resources, options)
|
||||
collect_mimes_for_render([], block) do
|
||||
options = resources.extract_options!
|
||||
(options.delete(:responder) || responder).call(self, resources, options)
|
||||
end
|
||||
end
|
||||
|
||||
def responder
|
||||
@ -258,6 +242,29 @@ def collect_mimes_from_class_level #:nodoc:
|
||||
end
|
||||
end
|
||||
|
||||
# Receives a collection of mimes and a block with formats and initialize a
|
||||
# collector. If a response was added to the collector, uses it to satisfy
|
||||
# the request, otherwise yields the block given.
|
||||
#
|
||||
def collect_mimes_for_render(mimes, formats)
|
||||
collector = Collector.new
|
||||
mimes = collect_mimes_from_class_level if mimes.empty?
|
||||
mimes.each { |mime| collector.send(mime) }
|
||||
formats.call(collector) if formats
|
||||
|
||||
if format = request.negotiate_mime(collector.order)
|
||||
self.formats = [format.to_sym]
|
||||
|
||||
if response = collector.response_for(format)
|
||||
response.call
|
||||
else
|
||||
yield
|
||||
end
|
||||
else
|
||||
head :not_acceptable
|
||||
end
|
||||
end
|
||||
|
||||
class Collector #:nodoc:
|
||||
attr_accessor :order
|
||||
|
||||
|
@ -109,8 +109,10 @@ def self.call(*args)
|
||||
# template.
|
||||
#
|
||||
def to_html
|
||||
render
|
||||
rescue ActionView::MissingTemplate
|
||||
if get?
|
||||
render
|
||||
raise
|
||||
elsif has_errors?
|
||||
render :action => default_action
|
||||
else
|
||||
@ -118,12 +120,14 @@ def to_html
|
||||
end
|
||||
end
|
||||
|
||||
# All others formats try to render the resource given instead. For this
|
||||
# purpose a helper called display as a shortcut to render a resource with
|
||||
# the current format.
|
||||
# All others formats follow the procedure below. First we try to render a
|
||||
# template, if the template is not available, we verify if the resource
|
||||
# responds to :to_format and display it.
|
||||
#
|
||||
def to_format
|
||||
return render unless resourceful?
|
||||
render
|
||||
rescue ActionView::MissingTemplate
|
||||
raise unless resourceful?
|
||||
|
||||
if get?
|
||||
display resource
|
||||
|
Loading…
Reference in New Issue
Block a user