Permalink
Browse files

Make action caching work with progressive rendering.

  • Loading branch information...
oggy committed Apr 19, 2011
1 parent 4ec5fe0 commit 3c9122174838ba346709ecbf26e9fdc6766d4981
Showing with 55 additions and 3 deletions.
  1. +2 −1 lib/template_streaming.rb
  2. +23 −2 lib/template_streaming/caching.rb
  3. +30 −0 spec/caching_spec.rb
@@ -144,6 +144,8 @@ def start_rendering_progressively?(render_stack_height, *render_args)
render_stack_height == 1 or
return false
+ return false if rendered_action_cache
+
(render_options = render_args.last).is_a?(Hash) or
render_options = {}
@@ -298,7 +300,6 @@ def with_render_proc_for_layout(options)
end
end
begin
- # TODO: what is @cached_content_for_layout in base.rb ?
yield
ensure
@_proc_for_layout = original_proc_for_layout
@@ -20,11 +20,11 @@ def initialize(app)
def call(env)
response = @app.call(env)
path = env[CACHER_KEY] and
- response[2] = PageCachingBodyProxy.new(response[2], env[CACHER_KEY])
+ response[2] = CachingBodyProxy.new(response[2], env[CACHER_KEY])
response
end
- class PageCachingBodyProxy
+ class CachingBodyProxy
def initialize(body, cacher)
@body = body
@cacher = cacher
@@ -41,7 +41,28 @@ def each
end
end
+ module ActionCacheFilter
+ def self.included(base)
+ base.alias_method_chain :after, :template_streaming_caching
+ end
+
+ def after_with_template_streaming_caching(controller)
+ if controller.render_progressively?
+ # This flag is ass-backwards to me. It really means *don't* cache the layout...
+ cache_layout? and
+ raise NotImplementedError, "sorry, using caches_action with :layout => false is not yet supported by Template Streaming"
+ controller.request.env[CACHER_KEY] = lambda do |content|
+ # This is what the standard method does.
+ controller.write_fragment(controller.action_cache_path.path, content, @options[:store_options])
+ end
+ else
+ after_without_template_streaming_caching(controller)
+ end
+ end
+ end
+
ActionController::Base.send :include, Controller
ActionController::Dispatcher.middleware.use Middleware
+ ActionController::Caching::Actions::ActionCacheFilter.send :include, ActionCacheFilter
end
end
View
@@ -19,4 +19,34 @@
File.read("#{controller.page_cache_directory}/index.html").should == 'ab'
end
end
+
+ describe "action caching" do
+ before do
+ controller.caches_action :action
+ push_attribute_value ActionController::Base, :cache_store, ActiveSupport::Cache::MemoryStore.new
+ end
+
+ after do
+ pop_attribute_value ActionController::Base, :cache_store
+ end
+
+ describe "when no layout is used" do
+ it "should render the page correctly" do
+ view "a<% flush %>b"
+ action { render :progressive => true, :layout => nil }
+ run
+ received.should == chunks('a', 'b', :end => true)
+ end
+
+ it "should use the cached copy if it exists" do
+ view "<% data.render_count += 1 %>a<% flush %>b"
+ action { render :progressive => true, :layout => nil }
+ data.render_count = 0
+ run
+ run
+ received.should == 'ab'
+ data.render_count.should == 1
+ end
+ end
+ end
end

0 comments on commit 3c91221

Please sign in to comment.