cells 2.3.0 → 3.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +3 -3
- data/MIT-LICENSE +22 -0
- data/README.rdoc +2 -2
- data/Rakefile +22 -25
- data/generators/cell/templates/cell.rb +1 -1
- data/generators/cells_install/USAGE +3 -0
- data/generators/cells_install/cells_install_generator.rb +12 -0
- data/generators/cells_install/templates/initializer.rb +9 -0
- data/lib/cell.rb +9 -0
- data/lib/cells.rb +68 -0
- data/lib/cells/cell.rb +15 -0
- data/lib/cells/cell/base.rb +461 -0
- data/lib/cells/cell/caching.rb +163 -0
- data/lib/cells/cell/view.rb +56 -0
- data/lib/cells/helpers.rb +7 -0
- data/lib/cells/helpers/capture_helper.rb +51 -0
- data/lib/cells/rails.rb +17 -0
- data/lib/cells/rails/action_controller.rb +37 -0
- data/lib/cells/rails/action_view.rb +37 -0
- data/lib/cells/version.rb +5 -0
- data/rails/init.rb +30 -0
- data/test/{cells → app/cells}/cells_test_one_cell.rb +2 -2
- data/test/{cells → app/cells}/cells_test_two_cell.rb +2 -0
- data/test/{cells → app/cells}/really_module/nested_cell.rb +1 -1
- data/test/app/cells/simple_cell.rb +7 -0
- data/test/{cells → app/cells}/test_cell.rb +3 -7
- data/test/app/controllers/cells_test_controller.rb +44 -0
- data/test/app/helpers/application_helper.rb +7 -0
- data/test/{helpers → app/helpers}/helper_using_cell_helper.rb +3 -1
- data/test/bugs_test.rb +10 -13
- data/test/caching_test.rb +169 -165
- data/test/capture_helper_test.rb +59 -0
- data/test/cells_test.rb +160 -158
- data/test/helper_test.rb +83 -104
- data/test/rails_test.rb +35 -0
- data/test/render_test.rb +163 -106
- data/test/support/assertions_helper.rb +60 -0
- data/test/test_helper.rb +67 -0
- metadata +35 -25
- data/README +0 -150
- data/VERSION +0 -1
- data/init.rb +0 -59
- data/lib/cell/base.rb +0 -454
- data/lib/cell/caching.rb +0 -151
- data/lib/cell/view.rb +0 -55
- data/lib/cells_helper.rb +0 -49
- data/lib/rails_extensions.rb +0 -75
- data/test/capture_test.rb +0 -56
- data/test/cell_view_test.rb +0 -9
- data/test/cells/simple_cell.rb +0 -5
- data/test/rails_extensions_test.rb +0 -25
- data/test/testing_helper.rb +0 -67
@@ -1,7 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
# encoding: utf-8
|
4
2
|
|
3
|
+
class TestCell < ::Cell::Base
|
5
4
|
def needs_view
|
6
5
|
@instance_variable_one = "yeah"
|
7
6
|
render
|
@@ -21,13 +20,10 @@ class TestCell < Cell::Base
|
|
21
20
|
render
|
22
21
|
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
23
|
def state_with_not_included_helper_method
|
27
24
|
render
|
28
25
|
end
|
29
|
-
|
30
|
-
|
26
|
+
|
31
27
|
def state_using_params
|
32
28
|
params[:my_param].to_s
|
33
29
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class CellsTestController < ActionController::Base
|
4
|
+
def rescue_action(e)
|
5
|
+
raise e
|
6
|
+
end
|
7
|
+
|
8
|
+
def render_cell_state
|
9
|
+
cell = params[:cell]
|
10
|
+
state = params[:state]
|
11
|
+
|
12
|
+
render :text => render_cell_to_string(cell, state)
|
13
|
+
end
|
14
|
+
|
15
|
+
def call_render_cell_with_strings
|
16
|
+
static = render_cell_to_string('my_test', 'direct_output')
|
17
|
+
render :text => static
|
18
|
+
end
|
19
|
+
|
20
|
+
def call_render_cell_with_syms
|
21
|
+
static = render_cell_to_string(:my_test, :direct_output)
|
22
|
+
render :text => static
|
23
|
+
end
|
24
|
+
|
25
|
+
def call_render_cell_with_state_view
|
26
|
+
render :text => render_cell_to_string(:my_test, :view_with_instance_var)
|
27
|
+
return
|
28
|
+
end
|
29
|
+
|
30
|
+
def render_view_with_render_cell_invocation
|
31
|
+
render :file => File.join(File.dirname(__FILE__), *%w[.. views view_with_render_cell_invocation.html.erb])
|
32
|
+
return
|
33
|
+
end
|
34
|
+
|
35
|
+
def render_just_one_view_cell
|
36
|
+
static = render_cell_to_string('just_one_view', 'some_state')
|
37
|
+
render :text => static
|
38
|
+
end
|
39
|
+
|
40
|
+
def render_state_with_link_to
|
41
|
+
static = render_cell_to_string('my_test', 'state_with_link_to')
|
42
|
+
render :text => static
|
43
|
+
end
|
44
|
+
end
|
data/test/bugs_test.rb
CHANGED
@@ -1,26 +1,23 @@
|
|
1
|
-
|
2
|
-
require File.dirname(__FILE__)
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
3
3
|
|
4
|
-
|
5
|
-
class MyTestCell < Cell::Base
|
4
|
+
class MyTestCell < ::Cell::Base
|
6
5
|
def state_with_instance_var
|
7
|
-
@my_ivar =
|
6
|
+
@my_ivar = 'value from cell'
|
8
7
|
render
|
9
8
|
end
|
10
9
|
end
|
11
10
|
|
12
|
-
|
13
|
-
class CellsTest < ActionController::TestCase
|
14
|
-
include CellsTestMethods
|
15
|
-
|
11
|
+
class BugsTest < ActionController::TestCase
|
16
12
|
def test_controller_overriding_cell_ivars
|
17
13
|
@controller.class_eval do
|
18
14
|
attr_accessor :my_ivar
|
19
15
|
end
|
20
|
-
@controller.my_ivar =
|
21
|
-
|
16
|
+
@controller.my_ivar = 'value from controller'
|
17
|
+
|
22
18
|
cell = MyTestCell.new(@controller)
|
23
19
|
c = cell.render_state(:state_with_instance_var)
|
24
|
-
|
20
|
+
|
21
|
+
assert_equal 'value from cell', c
|
25
22
|
end
|
26
|
-
end
|
23
|
+
end
|
data/test/caching_test.rb
CHANGED
@@ -1,266 +1,270 @@
|
|
1
|
-
|
2
|
-
require File.dirname(__FILE__)
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), *%w[test_helper])
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
class CachingCell < ::Cell::Base
|
5
|
+
cache :cached_state
|
6
|
+
|
7
|
+
def cached_state
|
8
|
+
count = controller.session[:cache_count]
|
9
|
+
count ||= 0
|
10
|
+
count += 1
|
11
|
+
"#{count} should remain the same forever!"
|
12
|
+
end
|
13
|
+
|
14
|
+
def not_cached_state
|
15
|
+
"i'm really static"
|
16
|
+
end
|
17
|
+
|
18
|
+
cache :versioned_cached_state, Proc.new { |cell|
|
19
|
+
if (v = cell.session[:version]) > 0
|
20
|
+
{:version => v}
|
21
|
+
else
|
22
|
+
{:version => 0}
|
23
|
+
end
|
24
|
+
}
|
25
|
+
def versioned_cached_state
|
26
|
+
"#{session[:version].inspect} should change every third call!"
|
27
|
+
end
|
28
|
+
|
29
|
+
def my_version_proc
|
30
|
+
if (v = session[:version]) > 0
|
31
|
+
{:version => v}
|
32
|
+
else
|
33
|
+
{:version => 0}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# def cached_state_with_symbol_proc
|
38
|
+
# end
|
39
|
+
|
40
|
+
cache :cheers
|
41
|
+
def cheers
|
42
|
+
'cheers!'
|
43
|
+
end
|
44
|
+
|
45
|
+
cache :another_state
|
46
|
+
def another_state
|
47
|
+
@opts[:str]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class AnotherCachingCell < ::Cell::Base
|
52
|
+
cache :cheers
|
53
|
+
def cheers
|
54
|
+
'prost!'
|
55
|
+
end
|
6
56
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
57
|
+
def another_state
|
58
|
+
@opts[:str]
|
59
|
+
end
|
60
|
+
end
|
11
61
|
|
12
|
-
class
|
13
|
-
include CellsTestMethods
|
14
|
-
|
62
|
+
class CachingTest < ActiveSupport::TestCase
|
15
63
|
def setup
|
16
64
|
super
|
17
|
-
@controller.session= {}
|
65
|
+
@controller.session = {}
|
18
66
|
@cc = CachingCell.new(@controller)
|
19
67
|
@c2 = AnotherCachingCell.new(@controller)
|
20
|
-
|
68
|
+
|
69
|
+
@old_action_controller_cache_store = ::ActionController::Base.cache_store
|
70
|
+
@old_action_controller_perform_caching = ::ActionController::Base.perform_caching
|
71
|
+
::ActionController::Base.cache_store = :memory_store
|
72
|
+
::ActionController::Base.perform_caching = true
|
21
73
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
74
|
+
### FIXME: sorry for that, but we need to force caching. avoid #alias_method_chain.
|
75
|
+
Cell::Base.alias_method_chain :render_state, :caching unless Cell::Base.method_defined? :render_state_without_caching
|
76
|
+
end
|
77
|
+
|
78
|
+
def teardown
|
79
|
+
::ActionController::Base.cache_store = @old_action_controller_cache_store
|
80
|
+
::ActionController::Base.perform_caching = @old_action_controller_perform_caching
|
81
|
+
end
|
82
|
+
|
27
83
|
def test_state_cached?
|
28
84
|
assert @cc.state_cached?(:cached_state)
|
29
|
-
|
85
|
+
assert_not @cc.state_cached?(:not_cached_state)
|
30
86
|
end
|
31
|
-
|
87
|
+
|
32
88
|
def test_cache_without_options
|
33
89
|
# :cached_state is cached without any options:
|
34
|
-
assert_nil
|
35
|
-
assert_nil
|
36
|
-
|
90
|
+
assert_nil @cc.version_procs[:cached_state]
|
91
|
+
assert_nil @cc.version_procs[:not_cached_state]
|
92
|
+
|
37
93
|
# cache_options must at least return an empty hash for a cached state:
|
38
|
-
assert_equal(
|
39
|
-
assert_nil
|
94
|
+
assert_equal ({}), @cc.cache_options[:cached_state]
|
95
|
+
assert_nil @cc.cache_options[:not_cached_state]
|
40
96
|
end
|
41
|
-
|
42
|
-
|
97
|
+
|
43
98
|
def test_cache_with_proc_only
|
44
99
|
CachingCell.class_eval do
|
45
100
|
cache :my_state, Proc.new {}
|
46
101
|
end
|
47
|
-
|
48
|
-
assert_kind_of
|
49
|
-
assert_equal(
|
102
|
+
|
103
|
+
assert_kind_of Proc, @cc.version_procs[:my_state]
|
104
|
+
assert_equal ({}), @cc.cache_options[:my_state]
|
50
105
|
end
|
51
|
-
|
52
|
-
|
106
|
+
|
53
107
|
def test_cache_with_proc_and_cache_options
|
54
108
|
CachingCell.class_eval do
|
55
109
|
cache :my_state, Proc.new{}, {:expires_in => 10.seconds}
|
56
110
|
end
|
57
|
-
|
58
|
-
assert_kind_of
|
59
|
-
assert_equal(
|
111
|
+
|
112
|
+
assert_kind_of Proc, @cc.version_procs[:my_state]
|
113
|
+
assert_equal ({:expires_in => 10.seconds}), @cc.cache_options[:my_state]
|
60
114
|
end
|
61
|
-
|
62
|
-
|
115
|
+
|
63
116
|
def test_cache_with_cache_options_only
|
64
117
|
CachingCell.class_eval do
|
65
118
|
cache :my_state, :expires_in => 10.seconds
|
66
119
|
end
|
67
|
-
|
68
|
-
assert
|
69
|
-
assert_nil
|
70
|
-
assert_equal(
|
120
|
+
|
121
|
+
assert @cc.version_procs.has_key?(:my_state)
|
122
|
+
assert_nil @cc.version_procs[:my_state]
|
123
|
+
assert_equal ({:expires_in => 10.seconds}), @cc.cache_options[:my_state]
|
71
124
|
end
|
72
|
-
|
73
|
-
|
74
|
-
|
125
|
+
|
75
126
|
def test_if_caching_works
|
76
127
|
c = @cc.render_state(:cached_state)
|
77
|
-
assert_equal
|
78
|
-
|
128
|
+
assert_equal "1 should remain the same forever!", c
|
129
|
+
|
79
130
|
c = @cc.render_state(:cached_state)
|
80
|
-
assert_equal
|
131
|
+
assert_equal "1 should remain the same forever!", c, ":cached_state was invoked again"
|
81
132
|
end
|
82
|
-
|
133
|
+
|
83
134
|
def test_cache_key
|
84
135
|
assert_equal "cells/caching/some_state", @cc.cache_key(:some_state)
|
85
|
-
assert_equal @cc.cache_key(:some_state), Cell::Base.cache_key_for(:caching, :some_state)
|
136
|
+
assert_equal @cc.cache_key(:some_state), ::Cell::Base.cache_key_for(:caching, :some_state)
|
86
137
|
assert_equal "cells/caching/some_state/param=9", @cc.cache_key(:some_state, :param => 9)
|
87
138
|
assert_equal "cells/caching/some_state/a=1/b=2", @cc.cache_key(:some_state, :b => 2, :a => 1)
|
88
139
|
end
|
89
|
-
|
140
|
+
|
90
141
|
def test_render_state_without_caching
|
91
142
|
c = @cc.render_state(:not_cached_state)
|
92
|
-
assert_equal
|
143
|
+
assert_equal "i'm really static", c
|
144
|
+
|
93
145
|
c = @cc.render_state(:not_cached_state)
|
94
|
-
assert_equal
|
146
|
+
assert_equal "i'm really static", c
|
95
147
|
end
|
96
|
-
|
148
|
+
|
97
149
|
def test_caching_with_version_proc
|
98
150
|
@controller.session[:version] = 0
|
99
151
|
# render state, as it's not cached:
|
100
152
|
c = @cc.render_state(:versioned_cached_state)
|
101
|
-
assert_equal
|
102
|
-
|
153
|
+
assert_equal '0 should change every third call!', c
|
154
|
+
|
103
155
|
@controller.session[:version] = -1
|
104
156
|
c = @cc.render_state(:versioned_cached_state)
|
105
|
-
assert_equal
|
106
|
-
|
107
|
-
|
157
|
+
assert_equal '0 should change every third call!', c
|
158
|
+
|
108
159
|
@controller.session[:version] = 1
|
109
160
|
c = @cc.render_state(:versioned_cached_state)
|
110
|
-
assert_equal
|
111
|
-
|
112
|
-
|
161
|
+
assert_equal '1 should change every third call!', c
|
162
|
+
|
113
163
|
@controller.session[:version] = 2
|
114
164
|
c = @cc.render_state(:versioned_cached_state)
|
115
|
-
assert_equal
|
116
|
-
|
165
|
+
assert_equal '2 should change every third call!', c
|
166
|
+
|
117
167
|
@controller.session[:version] = 3
|
118
168
|
c = @cc.render_state(:versioned_cached_state)
|
119
|
-
assert_equal
|
169
|
+
assert_equal '3 should change every third call!', c
|
120
170
|
end
|
121
|
-
|
171
|
+
|
122
172
|
def test_caching_with_instance_version_proc
|
123
173
|
CachingCell.class_eval do
|
124
174
|
cache :versioned_cached_state, :my_version_proc
|
125
175
|
end
|
176
|
+
|
126
177
|
@controller.session[:version] = 0
|
127
178
|
c = @cc.render_state(:versioned_cached_state)
|
128
|
-
assert_equal
|
129
|
-
|
179
|
+
assert_equal '0 should change every third call!', c
|
180
|
+
|
130
181
|
@controller.session[:version] = 1
|
131
182
|
c = @cc.render_state(:versioned_cached_state)
|
132
|
-
assert_equal
|
183
|
+
assert_equal '1 should change every third call!', c
|
133
184
|
end
|
134
|
-
|
185
|
+
|
135
186
|
def test_caching_with_two_same_named_states
|
136
187
|
c = @cc.render_state(:cheers)
|
137
|
-
assert_equal
|
188
|
+
assert_equal 'cheers!', c
|
189
|
+
|
138
190
|
c = @c2.render_state(:cheers)
|
139
|
-
assert_equal
|
191
|
+
assert_equal 'prost!', c
|
192
|
+
|
140
193
|
c = @cc.render_state(:cheers)
|
141
|
-
assert_equal
|
194
|
+
assert_equal 'cheers!', c
|
195
|
+
|
142
196
|
c = @c2.render_state(:cheers)
|
143
|
-
assert_equal
|
197
|
+
assert_equal 'prost!', c
|
144
198
|
end
|
145
199
|
|
146
200
|
def test_caching_one_of_two_same_named_states
|
147
201
|
### DISCUSS with drogus: the problem was that CachingCell and AnotherCachingCell keep
|
148
202
|
### overwriting their version_procs, wasn't it? why don't we test that with different
|
149
203
|
### version_procs in each cell?
|
150
|
-
@cc = CachingCell.new(@controller, :str =>
|
204
|
+
@cc = CachingCell.new(@controller, :str => 'foo1')
|
151
205
|
c = @cc.render_state(:another_state)
|
152
|
-
assert_equal c
|
206
|
+
assert_equal 'foo1',c
|
153
207
|
|
154
|
-
@c2 = AnotherCachingCell.new(@controller, :str =>
|
208
|
+
@c2 = AnotherCachingCell.new(@controller, :str => 'foo2')
|
155
209
|
c = @c2.render_state(:another_state)
|
156
|
-
assert_equal
|
210
|
+
assert_equal 'foo2', c
|
157
211
|
|
158
|
-
@cc = CachingCell.new(@controller, :str =>
|
212
|
+
@cc = CachingCell.new(@controller, :str => 'bar1')
|
159
213
|
c = @cc.render_state(:another_state)
|
160
|
-
assert_equal
|
214
|
+
assert_equal 'foo1', c
|
161
215
|
|
162
|
-
@c2 = AnotherCachingCell.new(@controller, :str =>
|
216
|
+
@c2 = AnotherCachingCell.new(@controller, :str => 'bar2')
|
163
217
|
c = @c2.render_state(:another_state)
|
164
|
-
assert_equal
|
218
|
+
assert_equal 'bar2', c
|
165
219
|
end
|
166
|
-
|
220
|
+
|
167
221
|
def test_expire_cache_key
|
168
|
-
|
222
|
+
key = @cc.cache_key(:cached_state)
|
169
223
|
@cc.render_state(:cached_state)
|
170
|
-
assert Cell::Base.cache_store.read(
|
171
|
-
|
172
|
-
|
173
|
-
|
224
|
+
assert ::Cell::Base.cache_store.read(key)
|
225
|
+
|
226
|
+
::Cell::Base.expire_cache_key(key)
|
227
|
+
assert_not ::Cell::Base.cache_store.read(key)
|
228
|
+
|
174
229
|
# test via ActionController::expire_cell_state, which is called from Sweepers.
|
175
230
|
@cc.render_state(:cached_state)
|
176
|
-
assert Cell::Base.cache_store.read(
|
231
|
+
assert ::Cell::Base.cache_store.read(key)
|
232
|
+
|
177
233
|
@controller.expire_cell_state(:caching, :cached_state)
|
178
|
-
|
179
|
-
|
234
|
+
assert_not ::Cell::Base.cache_store.read(key)
|
235
|
+
|
180
236
|
# ..and additionally test if passing cache key args works:
|
181
|
-
|
182
|
-
assert Cell::Base.cache_store.write(
|
237
|
+
key = @cc.cache_key(:cached_state, :more => :yes)
|
238
|
+
assert ::Cell::Base.cache_store.write(key, 'test content')
|
239
|
+
|
183
240
|
@controller.expire_cell_state(:caching, :cached_state, :more => :yes)
|
184
|
-
|
241
|
+
assert_not ::Cell::Base.cache_store.read(key)
|
185
242
|
end
|
186
|
-
|
187
|
-
|
243
|
+
|
188
244
|
def test_find_family_view_for_state_with_caching
|
189
245
|
# test environment: --------------------------------------
|
190
|
-
assert_equal({}, ACell.state2view_cache
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
246
|
+
assert_equal ({}), ACell.state2view_cache
|
247
|
+
|
248
|
+
cell = ACell.new(@controller)
|
249
|
+
cell.class.instance_eval do
|
250
|
+
def cache_configured?
|
251
|
+
false
|
252
|
+
end
|
195
253
|
end
|
196
|
-
|
197
|
-
# in development/test environment, no view name caching should happen,
|
254
|
+
cell.render_state :existing_view
|
255
|
+
# in development/test environment, no view name caching should happen,
|
198
256
|
# if perform_caching is false.
|
199
|
-
assert_equal({}, ACell.state2view_cache
|
200
|
-
|
257
|
+
assert_equal ({}), ACell.state2view_cache
|
258
|
+
|
201
259
|
# production environment: --------------------------------
|
202
|
-
|
203
|
-
|
204
|
-
|
260
|
+
cell = ACell.new(@controller)
|
261
|
+
cell.class.instance_eval do
|
262
|
+
def cache_configured?
|
263
|
+
true
|
264
|
+
end
|
205
265
|
end
|
206
|
-
|
266
|
+
|
267
|
+
cell.render_state :existing_view
|
207
268
|
assert ACell.state2view_cache.has_key?("existing_view/html")
|
208
269
|
end
|
209
270
|
end
|
210
|
-
|
211
|
-
class CachingCell < Cell::Base
|
212
|
-
|
213
|
-
cache :cached_state
|
214
|
-
|
215
|
-
def cached_state
|
216
|
-
cnt = controller.session[:cache_count]
|
217
|
-
cnt ||= 0
|
218
|
-
cnt += 1
|
219
|
-
"#{cnt} should remain the same forever!"
|
220
|
-
end
|
221
|
-
|
222
|
-
def not_cached_state
|
223
|
-
"i'm really static"
|
224
|
-
end
|
225
|
-
|
226
|
-
cache :versioned_cached_state, Proc.new { |cell|
|
227
|
-
if (v = cell.session[:version]) > 0
|
228
|
-
{:version=>v}
|
229
|
-
else
|
230
|
-
{:version=>0}; end
|
231
|
-
}
|
232
|
-
def versioned_cached_state
|
233
|
-
"#{session[:version].inspect} should change every third call!"
|
234
|
-
end
|
235
|
-
|
236
|
-
|
237
|
-
def my_version_proc
|
238
|
-
if (v = session[:version]) > 0
|
239
|
-
{:version=>v}
|
240
|
-
else
|
241
|
-
{:version=>0}; end
|
242
|
-
end
|
243
|
-
#def cached_state_with_symbol_proc
|
244
|
-
#
|
245
|
-
#end
|
246
|
-
cache :cheers
|
247
|
-
def cheers
|
248
|
-
"cheers!"
|
249
|
-
end
|
250
|
-
|
251
|
-
cache :another_state
|
252
|
-
def another_state
|
253
|
-
@opts[:str]
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
class AnotherCachingCell < Cell::Base
|
258
|
-
cache :cheers
|
259
|
-
def cheers
|
260
|
-
"prost!"
|
261
|
-
end
|
262
|
-
|
263
|
-
def another_state
|
264
|
-
@opts[:str]
|
265
|
-
end
|
266
|
-
end
|