diff options
author | José Valim <jose.valim@gmail.com> | 2009-08-29 16:18:37 +0200 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2009-08-29 11:01:00 -0500 |
commit | 684a6b3c71110b018199bf412e485189343e1f6d (patch) | |
tree | 3a004aedfa41b4dbe3d80f92c5d6be7348785837 /actionpack/lib/action_controller | |
parent | dbf20c2dbb5d1f2640517c468aa7c269d93414b9 (diff) | |
download | rails-684a6b3c71110b018199bf412e485189343e1f6d.tar.gz rails-684a6b3c71110b018199bf412e485189343e1f6d.tar.bz2 rails-684a6b3c71110b018199bf412e485189343e1f6d.zip |
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>
Diffstat (limited to 'actionpack/lib/action_controller')
-rw-r--r-- | actionpack/lib/action_controller/metal/mime_responds.rb | 49 | ||||
-rw-r--r-- | actionpack/lib/action_controller/metal/responder.rb | 14 |
2 files changed, 37 insertions, 26 deletions
diff --git a/actionpack/lib/action_controller/metal/mime_responds.rb b/actionpack/lib/action_controller/metal/mime_responds.rb index 950105e63f..6a32aeae24 100644 --- a/actionpack/lib/action_controller/metal/mime_responds.rb +++ b/actionpack/lib/action_controller/metal/mime_responds.rb @@ -178,23 +178,7 @@ module ActionController #:nodoc: # 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 @@ module ActionController #:nodoc: # 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 @@ module ActionController #: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 diff --git a/actionpack/lib/action_controller/metal/responder.rb b/actionpack/lib/action_controller/metal/responder.rb index fc01a0924a..ac7f5dafe9 100644 --- a/actionpack/lib/action_controller/metal/responder.rb +++ b/actionpack/lib/action_controller/metal/responder.rb @@ -109,8 +109,10 @@ module ActionController #:nodoc: # template. # def to_html + render + rescue ActionView::MissingTemplate if get? - render + raise elsif has_errors? render :action => default_action else @@ -118,12 +120,14 @@ module ActionController #:nodoc: 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 |