Permalink
Browse files

Add Controller#render_progressively(:only => _, :except => _).

For setting the :progressive flag to #render on a per-action basis.
  • Loading branch information...
1 parent cbaea57 commit 88aad921dc4abf558648ace45779cb5de752298e @oggy committed Apr 3, 2011
Showing with 84 additions and 3 deletions.
  1. +27 −1 lib/template_streaming.rb
  2. +57 −2 spec/template_streaming_spec.rb
@@ -40,6 +40,7 @@ def configure(config)
module Controller
def self.included(base)
base.class_eval do
+ extend ClassMethods
alias_method_chain :render, :template_streaming
alias_method_chain :render_to_string, :template_streaming
helper_method :flush, :push
@@ -49,7 +50,27 @@ def self.included(base)
end
end
+ module ClassMethods
+ def render_progressively(options={})
+ before_filter :action_renders_progressively, options
+ end
+ end
+
+ def action_renders_progressively
+ @action_progressively_renders = true
+ end
+
+ def action_renders_progressively?
+ @action_progressively_renders
+ end
+
def render_with_template_streaming(*args, &block)
+ options = args.first { |a| a.is_a?(Hash) }
+ if options && options.size == 1 && options.key?(:progressive)
+ # Need to set the default values, since the standard #render won't.
+ options[:template] = default_template
+ options[:layout] = true
+ end
push_render_stack_frame do |stack_height|
if start_rendering_progressively?(stack_height, *args)
@render_progressively = true
@@ -152,7 +173,12 @@ def start_rendering_progressively?(render_stack_height, *render_args)
if !(UNSTREAMABLE_KEYS & render_options.keys).empty? || render_args.first == :update
false
else
- render_options[:progressive]
+ explicit_option = render_options[:progressive]
+ if explicit_option.nil?
+ action_renders_progressively?
+ else
+ explicit_option
+ end
end
end
@@ -151,6 +151,50 @@
end
end
+ describe ".render_progressively" do
+ before do
+ TestController.layout 'layout'
+ layout "[<% flush %><%= yield %>]"
+ view "a"
+ end
+
+ it "should render all actions progressively if no options are given" do
+ TestController.render_progressively
+ run
+ received.should == chunks('[', 'a]', :end => true)
+ end
+
+ it "should render the action progressively if it is included with :only" do
+ TestController.render_progressively :only => :action
+ run
+ received.should == chunks('[', 'a]', :end => true)
+ end
+
+ it "should not render the action progressively if it is excepted" do
+ TestController.render_progressively :except => :action
+ run
+ received.should == "[a]"
+ end
+
+ it "should be overridden to true by an explicit :progressive => true when rendering" do
+ TestController.render_progressively :except => :action
+ action do
+ render :progressive => true
+ end
+ run
+ received.should == chunks('[', 'a]', :end => true)
+ end
+
+ it "should be overridden to false by an explicit :progressive => false when rendering" do
+ TestController.render_progressively :only => :action
+ action do
+ render :progressive => false
+ end
+ run
+ received.should == "[a]"
+ end
+ end
+
describe "#render in the controller" do
describe "when rendering progressively" do
before do
@@ -388,6 +432,17 @@
received.should == 'ab'
end
end
+
+ it "should use the standard defaults when only a :progressive option is given" do
+ template 'layouts/controller_layout', "[<%= yield %>]"
+ TestController.layout 'controller_layout'
+ view 'a'
+ action do
+ render :progressive => false
+ end
+ run
+ received.should == '[a]'
+ end
end
describe "#render in the view" do
@@ -442,7 +497,7 @@
describe "#render_to_string in the controller" do
it "should not flush anything out to the client" do
- #TestController.render_progressively
+ TestController.render_progressively
action do
@string = render_to_string :partial => 'partial'
received.should == ''
@@ -458,7 +513,7 @@
describe "#render_to_string in the view" do
it "should not flush anything out to the client" do
- #TestController.render_progressively
+ TestController.render_progressively
TestController.helper_method :render_to_string
layout "<%= yield %>"
view <<-'EOS'.gsub(/^ *\|/, '')

0 comments on commit 88aad92

Please sign in to comment.