public
Description: A Rails plugin that lets you embed actions in your views (with caching and response_to support)
Homepage: http://github.com/sd/embedded-actions/wikis/home
Clone URL: git://github.com/sd/embedded-actions.git
Patch from Jerret Taylor to fix caching issue with controller and action 
names

Original message:

I noticed a bug with the caching - if you caches_embedded an action, the 
way it registers that as being cachable will cause it to cache that action 
for any controller during the request - and seems to leak into other 
requests as well...An example, a controller calling two embedded actions 
on two seperate controllers, both with the :show action - if one is set to 
cache_embedded and the other is not, both will.

I wrote some tests and a patch to fix it - I also added a test 
'test_should_not_return_from_cache_if_params_are_different' just to make 
sure, which passed right away without any code changes, but i figured I'd 
leave it anyway, more tests is always a good thing!


git-svn-id: 
https://dev.notso.net/svn/rails/plugins/embedded_actions/trunk@149 
71bf3dff-0d17-0410-8985-d5c41cf5d41c
sd (author)
Thu Nov 29 12:08:14 -0800 2007
commit  eac5e49db155d44356434d98e9fc308bc835d267
tree    3bb6da358fd1ccd68fbc32d45a11a3df763f7849
parent  37d36381095555bec32f949ef4aeabd0e6def0d6
...
16
17
18
19
 
20
21
22
23
24
25
 
26
27
28
29
 
30
31
32
...
16
17
18
 
19
20
21
22
23
24
25
26
27
28
29
 
30
31
32
33
0
@@ -16,17 +16,18 @@ module ActionController
0
       def caches_embedded(*actions)
0
         return unless perform_caching
0
         actions.each do |action|
0
- self.cached_embedded[action.to_sym] = true
0
+ self.cached_embedded["#{controller_name}_#{action}".to_sym] = true
0
         end
0
       end
0
     end
0
     
0
     module InstanceMethods
0
       def cache_embedded?(options)
0
+ # require 'ruby-debug'; debugger
0
         cache_this_instance = options[:params] && options[:params].delete(:caching) # the rest of the request processing code doesn't have to know about this option
0
         return false unless self.perform_caching
0
     
0
- if embedded_class(options).cached_embedded[options[:action].to_sym]
0
+ if embedded_class(options).cached_embedded["#{options[:controller]}_#{options[:action]}".to_sym]
0
           return true unless cache_this_instance == false
0
         end
0
 
...
124
125
126
127
 
128
129
130
...
124
125
126
 
127
128
129
130
0
@@ -124,7 +124,7 @@ module ActionController #:nodoc:
0
 
0
           request.instance_variable_set(
0
             :@parameters,
0
- (options[:params] || {}).with_indifferent_access.update(
0
+ (options[:params].with_indifferent_access || {}).with_indifferent_access.update(
0
               "controller" => controller_name, "action" => options[:action], "id" => options[:id]
0
             )
0
           )
...
2
3
4
 
5
6
7
8
9
10
11
 
12
13
14
...
44
45
46
 
 
 
 
 
47
48
...
2
3
4
5
6
7
8
9
10
11
 
12
13
14
15
...
45
46
47
48
49
50
51
52
53
54
0
@@ -2,13 +2,14 @@ class TestController < ActionController::Base
0
   cattr_accessor :test_value
0
 
0
   caches_embedded :cached_action
0
+
0
   def cached_action
0
     @id = params[:id]
0
     @value = TestController.test_value || "N/A"
0
     
0
     render :template => "test/value", :layout => false
0
   end
0
-
0
+
0
   def regular_action
0
     @id = params[:id]
0
     @value = TestController.test_value || "N/A"
0
@@ -44,5 +45,10 @@ class TestController < ActionController::Base
0
   def inline_erb_action
0
     render :inline => params[:erb]
0
   end
0
+
0
+ def call_uncached_controller
0
+ render :text => embed_action_as_string(:controller => "test_no_caching",:action => "cached_action")
0
+ end
0
+
0
 end
0
 
...
30
31
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
34
35
...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
0
@@ -30,6 +30,25 @@ class CachesEmbeddedTest < Test::Unit::TestCase
0
     get :embedded_actions
0
     assert_equal "regular value is 2\ncached value is 2", @response.body
0
   end
0
+
0
+ def test_should_not_return_from_cache_if_params_are_different
0
+ TestController.test_value = "test"
0
+ get :cached_action, :id => 2
0
+ assert_equal "test (id=2)", @response.body
0
+
0
+ get :cached_action, :id => 3
0
+ assert_equal "test (id=3)", @response.body
0
+ end
0
+
0
+ def test_ensure_caching_only_is_enabled_where_it_should_be
0
+ TestNoCachingController.test_value = 1
0
+ get :call_uncached_controller
0
+ assert_equal "This should never cache. value: 1", @response.body
0
+
0
+ TestNoCachingController.test_value = 2
0
+ get :call_uncached_controller
0
+ assert_equal "This should never cache. value: 2", @response.body
0
+ end
0
 
0
   def test_embedded_caching_overrides
0
     # This page uses explicit overrides to reverse which embedded actions are cached

Comments

    No one has commented yet.