cells 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +30 -0
- data/README +150 -0
- data/README.rdoc +150 -0
- data/Rakefile +77 -0
- data/VERSION +1 -0
- data/generators/cell/USAGE +26 -0
- data/generators/cell/cell_generator.rb +48 -0
- data/generators/cell/templates/cell.rb +9 -0
- data/generators/cell/templates/view.html.erb +2 -0
- data/generators/cell/templates/view.html.haml +4 -0
- data/init.rb +59 -0
- data/lib/cell/base.rb +454 -0
- data/lib/cell/caching.rb +151 -0
- data/lib/cell/view.rb +55 -0
- data/lib/cells_helper.rb +49 -0
- data/lib/rails_extensions.rb +75 -0
- data/test/bugs_test.rb +26 -0
- data/test/caching_test.rb +266 -0
- data/test/capture_test.rb +56 -0
- data/test/cell_view_test.rb +9 -0
- data/test/cells/cells_test_one_cell.rb +20 -0
- data/test/cells/cells_test_two_cell.rb +2 -0
- data/test/cells/really_module/nested_cell.rb +11 -0
- data/test/cells/simple_cell.rb +5 -0
- data/test/cells/test_cell.rb +34 -0
- data/test/cells_test.rb +345 -0
- data/test/helper_test.rb +159 -0
- data/test/helpers/helper_using_cell_helper.rb +5 -0
- data/test/rails_extensions_test.rb +25 -0
- data/test/render_test.rb +229 -0
- data/test/testing_helper.rb +67 -0
- metadata +85 -0
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../../../test/test_helper'
|
2
|
+
require File.dirname(__FILE__) + '/testing_helper'
|
3
|
+
|
4
|
+
require File.dirname(__FILE__) + '/cells/test_cell'
|
5
|
+
|
6
|
+
class CaptureTest < ActionController::TestCase
|
7
|
+
include CellsTestMethods
|
8
|
+
|
9
|
+
def setup
|
10
|
+
super
|
11
|
+
|
12
|
+
CellTestController.class_eval do
|
13
|
+
def test_capture
|
14
|
+
@cell_content = render_cell(:test, :state_invoking_capture)
|
15
|
+
|
16
|
+
# captured_block comes from the cell view:
|
17
|
+
render :inline => '<h3><%= @captured_block %></h3>'+@cell_content
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_content_for
|
21
|
+
@cell_content = render_cell(:test, :state_invoking_content_for)
|
22
|
+
|
23
|
+
# :js comes from the cell views:
|
24
|
+
render :inline => '<pre><%= yield :js %></pre>'+@cell_content
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def test_global_capture
|
31
|
+
TestCell.class_eval do
|
32
|
+
helper CellsHelper
|
33
|
+
def state_invoking_capture; render; end
|
34
|
+
end
|
35
|
+
|
36
|
+
get :test_capture
|
37
|
+
|
38
|
+
assert_select "h1", ""
|
39
|
+
assert_select "h2", "captured!"
|
40
|
+
assert_select "h3", "captured!", "captured block not visible in controller"
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def test_global_content_for
|
45
|
+
TestCell.class_eval do
|
46
|
+
helper CellsHelper
|
47
|
+
def state_invoking_content_for; render; end
|
48
|
+
def state_invoking_content_for_twice; render; end
|
49
|
+
end
|
50
|
+
#puts @controller.public_methods
|
51
|
+
get :test_content_for
|
52
|
+
|
53
|
+
assert_select "js", ""
|
54
|
+
assert_select "pre", "\nfirst line\n\nsecond line\n\nthird line\n"
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class CellsTestOneCell < Cell::Base
|
2
|
+
|
3
|
+
def super_state
|
4
|
+
@my_class = self.class.to_s
|
5
|
+
return
|
6
|
+
end
|
7
|
+
|
8
|
+
def instance_view
|
9
|
+
end
|
10
|
+
|
11
|
+
def view_for_state(state)
|
12
|
+
if state.to_s == 'instance_view'
|
13
|
+
return 'cells_test_one/renamed_instance_view'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def state_with_no_view
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
class TestCell < Cell::Base
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
def needs_view
|
6
|
+
@instance_variable_one = "yeah"
|
7
|
+
render
|
8
|
+
end
|
9
|
+
|
10
|
+
def another_rendering_state
|
11
|
+
@instance_variable_one = "go"
|
12
|
+
render
|
13
|
+
end
|
14
|
+
|
15
|
+
def setting_state
|
16
|
+
@reset_me = '<p id="ho">ho</p>'
|
17
|
+
render
|
18
|
+
end
|
19
|
+
|
20
|
+
def reset_state
|
21
|
+
render
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
def state_with_not_included_helper_method
|
27
|
+
render
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def state_using_params
|
32
|
+
params[:my_param].to_s
|
33
|
+
end
|
34
|
+
end
|
data/test/cells_test.rb
ADDED
@@ -0,0 +1,345 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../../../test/test_helper'
|
2
|
+
require File.dirname(__FILE__) + '/testing_helper'
|
3
|
+
|
4
|
+
|
5
|
+
# this would usually happen by rails' autoloading -
|
6
|
+
# anyway, we don't test loading but rendering in this file.
|
7
|
+
require File.dirname(__FILE__) + '/cells/cells_test_one_cell'
|
8
|
+
require File.dirname(__FILE__) + '/cells/cells_test_two_cell'
|
9
|
+
require File.dirname(__FILE__) + '/cells/simple_cell'
|
10
|
+
require File.dirname(__FILE__) + '/cells/test_cell'
|
11
|
+
|
12
|
+
|
13
|
+
module Some
|
14
|
+
class Cell < Cell::Base
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class JustOneViewCell < Cell::Base
|
19
|
+
def some_state
|
20
|
+
render
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
class CellContainedInPlugin < Cell::Base
|
29
|
+
def some_view
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
# fixture for various tests -----------------------------------
|
35
|
+
# views are located in cells/test/cells/my_test/
|
36
|
+
class MyTestCell < Cell::Base
|
37
|
+
def direct_output
|
38
|
+
"<h9>this state method doesn't render a template but returns a string, which is great!</h9>"
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
def state_with_link_to
|
43
|
+
render
|
44
|
+
end
|
45
|
+
|
46
|
+
def view_in_local_test_views_dir
|
47
|
+
render
|
48
|
+
end
|
49
|
+
|
50
|
+
def view_with_explicit_english_translation
|
51
|
+
render
|
52
|
+
end
|
53
|
+
|
54
|
+
def view_containing_partial
|
55
|
+
render
|
56
|
+
end
|
57
|
+
|
58
|
+
def view_containing_partial_without_cell_name
|
59
|
+
render
|
60
|
+
end
|
61
|
+
|
62
|
+
def view_containing_nonexistant_partial
|
63
|
+
render
|
64
|
+
end
|
65
|
+
|
66
|
+
def view_containing_broken_partial
|
67
|
+
render
|
68
|
+
end
|
69
|
+
|
70
|
+
def view_with_instance_var
|
71
|
+
@instance_variable_one = "yeah"
|
72
|
+
@instance_variable_two = "wow"
|
73
|
+
render
|
74
|
+
end
|
75
|
+
|
76
|
+
def missing_view
|
77
|
+
render
|
78
|
+
end
|
79
|
+
|
80
|
+
def state_with_link_to_function
|
81
|
+
render
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# fixtures for view inheritance -------------------------------
|
86
|
+
# views are located in cells/test/cells/my_mother_cell/
|
87
|
+
class MyMotherCell < Cell::Base
|
88
|
+
def hello
|
89
|
+
@message = "hello, kid!"
|
90
|
+
render
|
91
|
+
end
|
92
|
+
def bye
|
93
|
+
@message = "bye, you!"
|
94
|
+
render
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# views are located in cells/test/cells/my_child_cell/
|
99
|
+
class MyChildCell < MyMotherCell
|
100
|
+
def hello
|
101
|
+
@message = "hello, mom!"
|
102
|
+
render
|
103
|
+
end
|
104
|
+
# view is inherited and located in cells/test/cells/my_mother_cell/bye.html.erb
|
105
|
+
def bye
|
106
|
+
@message = "bye, mom!"
|
107
|
+
render
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
|
112
|
+
module ReallyModule
|
113
|
+
class NestedCell < Cell::Base
|
114
|
+
# view: cells/test/cells/really_module/nested_cell/happy_state.html.erb
|
115
|
+
def happy_state
|
116
|
+
render
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
class CellsTest < ActionController::TestCase
|
123
|
+
include CellsTestMethods
|
124
|
+
|
125
|
+
|
126
|
+
### FIXME:
|
127
|
+
#Cell::View.warn_cache_misses = true
|
128
|
+
def setup
|
129
|
+
super
|
130
|
+
MyTestCell.default_template_format = :html
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_controller_render_methods
|
134
|
+
get :call_render_cell_with_strings # render_cell("test", "state")
|
135
|
+
assert_response :success
|
136
|
+
assert_tag :tag => "h9"
|
137
|
+
|
138
|
+
get :call_render_cell_with_syms
|
139
|
+
assert_response :success
|
140
|
+
assert_tag :tag => "h9"
|
141
|
+
|
142
|
+
get :call_render_cell_with_state_view
|
143
|
+
assert_select "#view_with_instance_var"
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
# test simple rendering cycle -------------------------------------------------
|
148
|
+
|
149
|
+
def test_render_state_which_returns_a_string
|
150
|
+
cell = MyTestCell.new(@controller)
|
151
|
+
|
152
|
+
c= cell.render_state(:direct_output)
|
153
|
+
assert_kind_of String, c
|
154
|
+
assert_selekt c, "h9"
|
155
|
+
|
156
|
+
#assert_raises (NoMethodError) { cell.render_state("non_existing_state") }
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_render_state_with_view_file
|
160
|
+
cell = MyTestCell.new(@controller)
|
161
|
+
|
162
|
+
c= cell.render_state(:view_with_instance_var)
|
163
|
+
assert_selekt c, "#one", "yeah"
|
164
|
+
assert_selekt c, "#two", "wow"
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_render_state_with_layout
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
def test_render_state_with_missing_view
|
173
|
+
cell = MyTestCell.new(@controller)
|
174
|
+
### TODO: production <-> development/test context.
|
175
|
+
|
176
|
+
assert_raises ActionView::MissingTemplate do
|
177
|
+
c = cell.render_state(:missing_view)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
|
182
|
+
# test partial rendering ------------------------------------------------------
|
183
|
+
|
184
|
+
# ok
|
185
|
+
def test_not_existing_partial
|
186
|
+
t = MyTestCell.new(@controller)
|
187
|
+
assert_raises ActionView::TemplateError do
|
188
|
+
t.render_state(:view_containing_nonexistant_partial)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
# ok
|
193
|
+
def test_broken_partial
|
194
|
+
t = MyTestCell.new(@controller)
|
195
|
+
assert_raises ActionView::TemplateError do
|
196
|
+
t.render_state(:view_containing_broken_partial)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
# ok
|
201
|
+
def test_render_state_with_partial
|
202
|
+
cell = MyTestCell.new(@controller)
|
203
|
+
c = cell.render_state(:view_containing_partial)
|
204
|
+
assert_selekt c, "#partialContained>#partial"
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_render_state_with_partial_without_cell_name
|
208
|
+
cell = MyTestCell.new(@controller)
|
209
|
+
c = cell.render_state(:view_containing_partial_without_cell_name)
|
210
|
+
assert_selekt c, "#partialContained>#partial"
|
211
|
+
end
|
212
|
+
|
213
|
+
# test advanced views (prototype_helper, ...) --------------------------------
|
214
|
+
### TODO: fix CellTestController to allow rendering views with #link_to_function-
|
215
|
+
def dont_test_view_with_link_to_function
|
216
|
+
cell = MyTestCell.new(@controller)
|
217
|
+
c = cell.render_state(:state_with_link_to_function)
|
218
|
+
assert_selekt c, "#partialContained>#partial"
|
219
|
+
end
|
220
|
+
|
221
|
+
# test view inheritance ------------------------------------------------------
|
222
|
+
|
223
|
+
def test_possible_paths_for_state
|
224
|
+
t = MyChildCell.new(@controller)
|
225
|
+
p = t.possible_paths_for_state(:bye)
|
226
|
+
assert_equal "my_child/bye", p.first
|
227
|
+
assert_equal "my_mother/bye", p.last
|
228
|
+
end
|
229
|
+
|
230
|
+
|
231
|
+
def test_render_state_on_child_where_child_view_exists
|
232
|
+
cell = MyChildCell.new(@controller)
|
233
|
+
c = cell.render_state(:hello)
|
234
|
+
assert_selekt c, "#childHello", "hello, mom!"
|
235
|
+
end
|
236
|
+
|
237
|
+
def test_render_state_on_child_where_view_is_inherited_from_mother
|
238
|
+
cell = MyChildCell.new(@controller)
|
239
|
+
puts " rendering cell!"
|
240
|
+
c = cell.render_state(:bye)
|
241
|
+
assert_selekt c, "#motherBye", "bye, mom!"
|
242
|
+
end
|
243
|
+
|
244
|
+
|
245
|
+
# test Cell::View -------------------------------------------------------------
|
246
|
+
|
247
|
+
def test_find_family_view_for_state
|
248
|
+
t = MyChildCell.new(@controller)
|
249
|
+
tpl = t.find_family_view_for_state(:bye, Cell::View.new(["#{RAILS_ROOT}/vendor/plugins/cells/test/cells"], {}, @controller))
|
250
|
+
assert_equal "my_mother/bye.html.erb", tpl.path
|
251
|
+
end
|
252
|
+
|
253
|
+
|
254
|
+
### API test (unit) -----------------------------------------------------------
|
255
|
+
def test_cell_name
|
256
|
+
cell_one = CellsTestOneCell.new(@controller)
|
257
|
+
|
258
|
+
assert_equal cell_one.cell_name, "cells_test_one"
|
259
|
+
assert_equal CellsTestOneCell.cell_name, "cells_test_one"
|
260
|
+
end
|
261
|
+
|
262
|
+
|
263
|
+
def test_class_from_cell_name
|
264
|
+
assert_equal Cell::Base.class_from_cell_name("cells_test_one"), CellsTestOneCell
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_default_template_format
|
268
|
+
# test getter
|
269
|
+
u = MyTestCell.new(@controller)
|
270
|
+
assert_equal :html, Cell::Base.default_template_format
|
271
|
+
assert_equal :html, u.class.default_template_format
|
272
|
+
|
273
|
+
# test setter
|
274
|
+
MyTestCell.default_template_format = :js
|
275
|
+
assert_equal :html, Cell::Base.default_template_format
|
276
|
+
assert_equal :js, u.class.default_template_format
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_defaultize_render_options_for
|
280
|
+
u = MyTestCell.new(@controller)
|
281
|
+
assert_equal( {:template_format => :html, :view => :do_it},
|
282
|
+
u.defaultize_render_options_for({}, :do_it))
|
283
|
+
assert_equal( {:template_format => :html, :view => :do_it},
|
284
|
+
u.defaultize_render_options_for({}, :do_it))
|
285
|
+
assert_equal( {:template_format => :js, :view => :do_it},
|
286
|
+
u.defaultize_render_options_for({:template_format => :js}, :do_it))
|
287
|
+
assert_equal( {:template_format => :html, :layout => :metal, :view => :do_it},
|
288
|
+
u.defaultize_render_options_for({:layout => :metal}, :do_it))
|
289
|
+
assert_equal( {:template_format => :js, :layout => :metal, :view => :do_it},
|
290
|
+
u.defaultize_render_options_for({:layout => :metal, :template_format => :js}, :do_it))
|
291
|
+
end
|
292
|
+
|
293
|
+
def test_new_directory_hierarchy
|
294
|
+
cell = ReallyModule::NestedCell.new(@controller)
|
295
|
+
view = cell.render_state(:happy_state)
|
296
|
+
@response.body = view
|
297
|
+
|
298
|
+
assert_select "#happyStateView"
|
299
|
+
end
|
300
|
+
|
301
|
+
# Thanks to Fran Pena who made us aware of this bug and contributed a patch.
|
302
|
+
def test_i18n_support
|
303
|
+
orig_locale = I18n.locale
|
304
|
+
I18n.locale = :en
|
305
|
+
|
306
|
+
t = MyTestCell.new(@controller)
|
307
|
+
c = t.render_state(:view_with_explicit_english_translation)
|
308
|
+
|
309
|
+
I18n.locale = orig_locale # cleanup before we mess up!
|
310
|
+
|
311
|
+
# the view "view_with_explicit_english_translation.en" exists, check if
|
312
|
+
# rails' i18n found it:
|
313
|
+
assert_selekt c, "#defaultTranslation", 0
|
314
|
+
assert_selekt c, "#explicitEnglishTranslation"
|
315
|
+
end
|
316
|
+
|
317
|
+
|
318
|
+
def test_modified_view_finding_for_testing
|
319
|
+
t = MyTestCell.new(@controller)
|
320
|
+
c = t.render_state(:view_in_local_test_views_dir)
|
321
|
+
assert_selekt c, "#localView"
|
322
|
+
end
|
323
|
+
|
324
|
+
|
325
|
+
def test_params_in_a_cell_state
|
326
|
+
@controller.params = {:my_param => "value"}
|
327
|
+
t = TestCell.new(@controller)
|
328
|
+
c = t.render_state(:state_using_params)
|
329
|
+
assert_equal c, "value"
|
330
|
+
end
|
331
|
+
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
|
336
|
+
### functional tests: ---------------------------------------------------------
|
337
|
+
|
338
|
+
def test_link_to_in_view
|
339
|
+
get :render_state_with_link_to
|
340
|
+
|
341
|
+
assert_response :success
|
342
|
+
assert_select "a", "bla"
|
343
|
+
end
|
344
|
+
|
345
|
+
end
|