public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Allow caches_action to accept a layout option [#198 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
josevalim (author)
Tue Jun 03 12:02:51 -0700 2008
josh (committer)
Tue Jun 03 12:02:51 -0700 2008
commit  d54d90f2b590c763fe710482a9b993923fe03ec0
tree    99b293aa7c115d9d083f6f42280d8438faa5fe4a
parent  aa1771668877f20ca044e8f45a9736fbb7c8402e
...
 
 
1
2
3
...
1
2
3
4
5
0
@@ -1,3 +1,5 @@
0
+* Allow caches_action to accept a layout option [José Valim]
0
+
0
 * Added Rack processor [Ezra Zygmuntowicz, Josh Peek]
0
 
0
 
...
40
41
42
 
 
43
44
45
...
54
55
56
57
 
 
58
59
60
...
81
82
83
84
 
 
 
85
86
87
...
90
91
92
93
 
 
94
95
96
...
105
106
107
 
 
 
 
 
 
 
 
108
109
110
...
40
41
42
43
44
45
46
47
...
56
57
58
 
59
60
61
62
63
...
84
85
86
 
87
88
89
90
91
92
...
95
96
97
 
98
99
100
101
102
...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
0
@@ -40,6 +40,8 @@ module ActionController #:nodoc:
0
     #         controller.send(:list_url, c.params[:id]) }
0
     #   end
0
     #
0
+    # If you pass :layout => false, it will only cache your action content. It is useful when your layout has dynamic information.
0
+    #
0
     module Actions
0
       def self.included(base) #:nodoc:
0
         base.extend(ClassMethods)
0
@@ -54,7 +56,8 @@ module ActionController #:nodoc:
0
         def caches_action(*actions)
0
           return unless cache_configured?
0
           options = actions.extract_options!
0
-          around_filter(ActionCacheFilter.new(:cache_path => options.delete(:cache_path)), {:only => actions}.merge(options))
0
+          cache_filter = ActionCacheFilter.new(:layout => options.delete(:layout), :cache_path => options.delete(:cache_path))
0
+          around_filter(cache_filter, {:only => actions}.merge(options))
0
         end
0
       end
0
 
0
@@ -81,7 +84,9 @@ module ActionController #:nodoc:
0
           if cache = controller.read_fragment(cache_path.path)
0
             controller.rendered_action_cache = true
0
             set_content_type!(controller, cache_path.extension)
0
-            controller.send!(:render_for_text, cache)
0
+            options = { :text => cache }
0
+            options.merge!(:layout => true) if cache_layout?
0
+            controller.send!(:render, options)
0
             false
0
           else
0
             controller.action_cache_path = cache_path
0
@@ -90,7 +95,8 @@ module ActionController #:nodoc:
0
 
0
         def after(controller)
0
           return if controller.rendered_action_cache || !caching_allowed(controller)
0
-          controller.write_fragment(controller.action_cache_path.path, controller.response.body)
0
+          action_content = cache_layout? ? content_for_layout(controller) : controller.response.body
0
+          controller.write_fragment(controller.action_cache_path.path, action_content)
0
         end
0
 
0
         private
0
@@ -105,6 +111,14 @@ module ActionController #:nodoc:
0
           def caching_allowed(controller)
0
             controller.request.get? && controller.response.headers['Status'].to_i == 200
0
           end
0
+
0
+          def cache_layout?
0
+            @options[:layout] == false
0
+          end
0
+
0
+          def content_for_layout(controller)
0
+            controller.response.layout && controller.response.template.instance_variable_get('@content_for_layout')
0
+          end
0
       end
0
 
0
       class ActionCachePath
...
156
157
158
 
159
160
161
...
181
182
183
 
184
185
186
...
263
264
265
 
 
 
 
 
 
 
 
 
 
 
 
 
266
267
268
...
156
157
158
159
160
161
162
...
182
183
184
185
186
187
188
...
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
0
@@ -156,6 +156,7 @@ class ActionCachingTestController < ActionController::Base
0
   caches_action :show, :cache_path => 'http://test.host/custom/show'
0
   caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" }
0
   caches_action :with_layout
0
+  caches_action :layout_false, :layout => false
0
 
0
   layout 'talk_from_action.erb'
0
 
0
@@ -181,6 +182,7 @@ class ActionCachingTestController < ActionController::Base
0
   alias_method :show, :index
0
   alias_method :edit, :index
0
   alias_method :destroy, :index
0
+  alias_method :layout_false, :with_layout
0
 
0
   def expire
0
     expire_action :controller => 'action_caching_test', :action => 'index'
0
@@ -263,6 +265,19 @@ class ActionCacheTest < Test::Unit::TestCase
0
     assert_equal @response.body, read_fragment('hostname.com/action_caching_test/with_layout')
0
   end
0
 
0
+  def test_action_cache_with_layout_and_layout_cache_false
0
+    get :layout_false
0
+    cached_time = content_to_cache
0
+    assert_not_equal cached_time, @response.body
0
+    assert fragment_exist?('hostname.com/action_caching_test/layout_false')
0
+    reset!
0
+
0
+    get :layout_false
0
+    assert_not_equal cached_time, @response.body
0
+
0
+    assert_equal cached_time, read_fragment('hostname.com/action_caching_test/layout_false')
0
+  end
0
+
0
   def test_action_cache_conditional_options
0
     @request.env['HTTP_ACCEPT'] = 'application/json'
0
     get :index

Comments