public
Description: Merb More: The Full Stack. Take what you need; leave what you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-more.git
Search Repo:
added conditionals to action caching.

* hooks into the filter conditioning in abstract_controller of merb-core.
* supports :if/:unless with symbols & procs.
benburkert (author)
Thu May 15 13:09:37 -0700 2008
commit  ce59df9edc1a6a6b546c2c61407c8aadbe1e9087
tree    82f3fc6457ed3fa3b8088abf9e1b7a08e1cb244a
parent  205717dbff1ecfde35bf3b8f7c08ca05b3c3c84f
...
16
17
18
19
20
 
 
21
22
23
...
28
29
30
31
32
33
34
35
 
 
 
 
 
 
 
 
36
37
38
...
16
17
18
 
 
19
20
21
22
23
...
28
29
30
 
 
 
 
 
31
32
33
34
35
36
37
38
39
40
41
0
@@ -16,8 +16,8 @@
0
   # ==== Examples
0
   # cache_action :mostly_static
0
   # cache_action :barely_dynamic, 10
0
- def cache_action(action, from_now = nil)
0
- cache_actions([action, from_now])
0
+ def cache_action(action, from_now = nil, opts = {})
0
+ cache_actions([action, from_now, opts])
0
   end
0
 
0
   # Register actions for action caching (before and after filters)
0
@@ -28,11 +28,14 @@
0
   # ==== Example
0
   # cache_actions :mostly_static, [:barely_dynamic, 10]
0
   def cache_actions(*actions)
0
- if actions.any? && !Merb::Cache.cached_actions.key?(controller_name)
0
- before(:cache_action_before)
0
- after(:cache_action_after)
0
- end
0
- actions.each do |action, from_now|
0
+ actions.each do |action, from_now, opts|
0
+ from_now, opts = nil, from_now if Hash === from_now
0
+
0
+ before("cache_#{action}_before", opts.merge(:only => action))
0
+ after("cache_#{action}_after", opts.merge(:only => action))
0
+ alias_method "cache_#{action}_before", :cache_action_before
0
+ alias_method "cache_#{action}_after", :cache_action_after
0
+
0
       _actions = Merb::Cache.cached_actions[controller_name] ||= {}
0
       _actions[action] = from_now
0
     end
...
10
11
12
 
 
 
 
 
13
14
15
...
50
51
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
54
55
...
10
11
12
13
14
15
16
17
18
19
20
...
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
0
@@ -10,6 +10,11 @@
0
   # or cache_pages :action5, [:action6, 0.05]
0
   cache_page :action7
0
 
0
+ cache_action :action8, 0.05, :if => proc {|controller| !controller.params[:id].empty?}
0
+ cache_action :action9, 0.05, :unless => proc {|controller| controller.params[:id].empty?}
0
+ cache_action :action10, :if => :non_empty_id?
0
+ cache_action :action11, :unless => :empty_id?
0
+
0
   def action1
0
     render
0
   end
0
@@ -50,6 +55,30 @@
0
     else
0
       raise "BAD FORMAT: #{params[:format].inspect}"
0
     end
0
+ end
0
+
0
+ def action8
0
+ "test action8"
0
+ end
0
+
0
+ def action9
0
+ "test action9"
0
+ end
0
+
0
+ def action10
0
+ "test action10"
0
+ end
0
+
0
+ def action11
0
+ "test action11"
0
+ end
0
+
0
+ def empty_id?
0
+ params[:id].empty?
0
+ end
0
+
0
+ def non_empty_id?
0
+ !empty_id?
0
   end
0
 
0
   def index
...
103
104
105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
0
@@ -103,5 +103,36 @@
0
     CACHE.cached_action?(:key => "/cache_controller/action4/id1/id2").should be_false
0
   end
0
 
0
+ it "should allow :if conditions with procs" do
0
+ c = get("/cache_controller/action8")
0
+ CACHE.cached_action?(:key => "/cache_controller/action8").should be_false
0
+
0
+ c = get("/cache_controller/action8/cache")
0
+ CACHE.cached_action?(:key => "/cache_controller/action8/cache").should be_true
0
+ end
0
+
0
+ it "should allow :unless conditions with procs" do
0
+ c = get("/cache_controller/action9")
0
+ CACHE.cached_action?(:key => "/cache_controller/action9").should be_false
0
+
0
+ c = get("/cache_controller/action9/cache")
0
+ CACHE.cached_action?(:key => "/cache_controller/action9/cache").should be_true
0
+ end
0
+
0
+ it "should allow :if conditions with symbols" do
0
+ c = get("/cache_controller/action10")
0
+ CACHE.cached_action?(:key => "/cache_controller/action10").should be_false
0
+
0
+ c = get("/cache_controller/action10/cache")
0
+ CACHE.cached_action?(:key => "/cache_controller/action10/cache").should be_true
0
+ end
0
+
0
+ it "should allow :unless conditions with symbols" do
0
+ c = get("/cache_controller/action11")
0
+ CACHE.cached_action?(:key => "/cache_controller/action11").should be_false
0
+
0
+ c = get("/cache_controller/action11/cache")
0
+ CACHE.cached_action?(:key => "/cache_controller/action11/cache").should be_true
0
+ end
0
 end

Comments

    No one has commented yet.