cells 2.3.0 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/CHANGES +3 -3
  2. data/MIT-LICENSE +22 -0
  3. data/README.rdoc +2 -2
  4. data/Rakefile +22 -25
  5. data/generators/cell/templates/cell.rb +1 -1
  6. data/generators/cells_install/USAGE +3 -0
  7. data/generators/cells_install/cells_install_generator.rb +12 -0
  8. data/generators/cells_install/templates/initializer.rb +9 -0
  9. data/lib/cell.rb +9 -0
  10. data/lib/cells.rb +68 -0
  11. data/lib/cells/cell.rb +15 -0
  12. data/lib/cells/cell/base.rb +461 -0
  13. data/lib/cells/cell/caching.rb +163 -0
  14. data/lib/cells/cell/view.rb +56 -0
  15. data/lib/cells/helpers.rb +7 -0
  16. data/lib/cells/helpers/capture_helper.rb +51 -0
  17. data/lib/cells/rails.rb +17 -0
  18. data/lib/cells/rails/action_controller.rb +37 -0
  19. data/lib/cells/rails/action_view.rb +37 -0
  20. data/lib/cells/version.rb +5 -0
  21. data/rails/init.rb +30 -0
  22. data/test/{cells → app/cells}/cells_test_one_cell.rb +2 -2
  23. data/test/{cells → app/cells}/cells_test_two_cell.rb +2 -0
  24. data/test/{cells → app/cells}/really_module/nested_cell.rb +1 -1
  25. data/test/app/cells/simple_cell.rb +7 -0
  26. data/test/{cells → app/cells}/test_cell.rb +3 -7
  27. data/test/app/controllers/cells_test_controller.rb +44 -0
  28. data/test/app/helpers/application_helper.rb +7 -0
  29. data/test/{helpers → app/helpers}/helper_using_cell_helper.rb +3 -1
  30. data/test/bugs_test.rb +10 -13
  31. data/test/caching_test.rb +169 -165
  32. data/test/capture_helper_test.rb +59 -0
  33. data/test/cells_test.rb +160 -158
  34. data/test/helper_test.rb +83 -104
  35. data/test/rails_test.rb +35 -0
  36. data/test/render_test.rb +163 -106
  37. data/test/support/assertions_helper.rb +60 -0
  38. data/test/test_helper.rb +67 -0
  39. metadata +35 -25
  40. data/README +0 -150
  41. data/VERSION +0 -1
  42. data/init.rb +0 -59
  43. data/lib/cell/base.rb +0 -454
  44. data/lib/cell/caching.rb +0 -151
  45. data/lib/cell/view.rb +0 -55
  46. data/lib/cells_helper.rb +0 -49
  47. data/lib/rails_extensions.rb +0 -75
  48. data/test/capture_test.rb +0 -56
  49. data/test/cell_view_test.rb +0 -9
  50. data/test/cells/simple_cell.rb +0 -5
  51. data/test/rails_extensions_test.rb +0 -25
  52. data/test/testing_helper.rb +0 -67
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), *%w[test_helper])
3
+ require File.join(File.dirname(__FILE__), *%w[app cells test_cell])
4
+
5
+ class CaptureTest < ActionController::TestCase
6
+ tests CellsTestController
7
+
8
+ def setup
9
+ super
10
+
11
+ CellsTestController.class_eval do
12
+ def test_capture
13
+ @cell_content = render_cell(:test, :state_invoking_capture)
14
+
15
+ # captured_block comes from the cell view:
16
+ render :inline => '<h3><%= @captured_block %></h3>' << @cell_content
17
+ end
18
+
19
+ def test_content_for
20
+ @cell_content = render_cell(:test, :state_invoking_content_for)
21
+
22
+ # :js comes from the cell views:
23
+ render :inline => '<pre><%= yield :js %></pre>' << @cell_content
24
+ end
25
+ end
26
+ end
27
+
28
+ def test_global_capture
29
+ TestCell.class_eval do
30
+ helper ::Cells::Helpers::CaptureHelper
31
+ def state_invoking_capture
32
+ render
33
+ 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
+ def test_global_content_for
44
+ TestCell.class_eval do
45
+ helper ::Cells::Helpers::CaptureHelper
46
+ def state_invoking_content_for
47
+ render
48
+ end
49
+ def state_invoking_content_for_twice
50
+ render
51
+ end
52
+ end
53
+
54
+ get :test_content_for
55
+
56
+ assert_select 'js', ''
57
+ assert_select 'pre', "\nfirst line\n\nsecond line\n\nthird line\n"
58
+ end
59
+ end
data/test/cells_test.rb CHANGED
@@ -1,82 +1,66 @@
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
-
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), 'test_helper')
3
+ require File.join(File.dirname(__FILE__), *%w[app cells cells_test_one_cell])
12
4
 
13
5
  module Some
14
- class Cell < Cell::Base
6
+ class Cell < ::Cell::Base
15
7
  end
16
8
  end
17
9
 
18
- class JustOneViewCell < Cell::Base
10
+ class JustOneViewCell < ::Cell::Base
19
11
  def some_state
20
12
  render
21
13
  end
22
14
  end
23
15
 
24
-
25
-
26
-
27
-
28
- class CellContainedInPlugin < Cell::Base
16
+ class CellContainedInPlugin < ::Cell::Base
29
17
  def some_view
30
- end
18
+ end
31
19
  end
32
20
 
33
-
34
- # fixture for various tests -----------------------------------
35
- # views are located in cells/test/cells/my_test/
36
- class MyTestCell < Cell::Base
21
+ class MyTestCell < ::Cell::Base
37
22
  def direct_output
38
23
  "<h9>this state method doesn't render a template but returns a string, which is great!</h9>"
39
-
40
24
  end
41
-
25
+
42
26
  def state_with_link_to
43
27
  render
44
28
  end
45
-
29
+
46
30
  def view_in_local_test_views_dir
47
31
  render
48
32
  end
49
-
33
+
50
34
  def view_with_explicit_english_translation
51
35
  render
52
36
  end
53
-
37
+
54
38
  def view_containing_partial
55
39
  render
56
40
  end
57
-
41
+
58
42
  def view_containing_partial_without_cell_name
59
43
  render
60
44
  end
61
-
45
+
62
46
  def view_containing_nonexistant_partial
63
47
  render
64
48
  end
65
-
49
+
66
50
  def view_containing_broken_partial
67
51
  render
68
52
  end
69
-
53
+
70
54
  def view_with_instance_var
71
- @instance_variable_one = "yeah"
72
- @instance_variable_two = "wow"
55
+ @instance_variable_one = 'yeah'
56
+ @instance_variable_two = 'wow'
73
57
  render
74
58
  end
75
-
59
+
76
60
  def missing_view
77
61
  render
78
62
  end
79
-
63
+
80
64
  def state_with_link_to_function
81
65
  render
82
66
  end
@@ -84,13 +68,13 @@ end
84
68
 
85
69
  # fixtures for view inheritance -------------------------------
86
70
  # views are located in cells/test/cells/my_mother_cell/
87
- class MyMotherCell < Cell::Base
71
+ class MyMotherCell < ::Cell::Base
88
72
  def hello
89
- @message = "hello, kid!"
73
+ @message = 'hello, kid!'
90
74
  render
91
75
  end
92
76
  def bye
93
- @message = "bye, you!"
77
+ @message = 'bye, you!'
94
78
  render
95
79
  end
96
80
  end
@@ -98,19 +82,18 @@ end
98
82
  # views are located in cells/test/cells/my_child_cell/
99
83
  class MyChildCell < MyMotherCell
100
84
  def hello
101
- @message = "hello, mom!"
85
+ @message = 'hello, mom!'
102
86
  render
103
87
  end
104
88
  # view is inherited and located in cells/test/cells/my_mother_cell/bye.html.erb
105
89
  def bye
106
- @message = "bye, mom!"
90
+ @message = 'bye, mom!'
107
91
  render
108
92
  end
109
93
  end
110
94
 
111
-
112
95
  module ReallyModule
113
- class NestedCell < Cell::Base
96
+ class NestedCell < ::Cell::Base
114
97
  # view: cells/test/cells/really_module/nested_cell/happy_state.html.erb
115
98
  def happy_state
116
99
  render
@@ -118,228 +101,247 @@ module ReallyModule
118
101
  end
119
102
  end
120
103
 
121
-
122
104
  class CellsTest < ActionController::TestCase
123
- include CellsTestMethods
124
-
105
+ tests CellsTestController
125
106
 
126
107
  ### FIXME:
127
- #Cell::View.warn_cache_misses = true
108
+ # Cell::View.warn_cache_misses = true
128
109
  def setup
129
110
  super
130
111
  MyTestCell.default_template_format = :html
131
112
  end
132
-
113
+
114
+ def test_class_aliasing
115
+ assert_equal Cell::Base, Cells::Cell::Base
116
+ end
117
+
118
+ def test_view_paths
119
+ assert_kind_of ActionView::PathSet, Cell::Base.view_paths, "must be a PathSet for proper template caching/reloading (see issue#2)"
120
+ end
121
+
122
+ def test_cells_view_paths=
123
+ swap( Cell::Base, :view_paths => ['you', 'are', 'here']) do
124
+ paths = Cell::Base.view_paths
125
+ assert_kind_of ActionView::PathSet, paths, "must not wipe out the PathSet"
126
+ assert_equal 3, Cell::Base.view_paths.size
127
+ assert_equal %w(you are here), Cell::Base.view_paths
128
+ end
129
+ end
130
+
131
+
133
132
  def test_controller_render_methods
134
133
  get :call_render_cell_with_strings # render_cell("test", "state")
135
134
  assert_response :success
136
- assert_tag :tag => "h9"
135
+ assert_tag :tag => 'h9'
137
136
 
138
137
  get :call_render_cell_with_syms
139
138
  assert_response :success
140
- assert_tag :tag => "h9"
139
+ assert_tag :tag => 'h9'
141
140
 
142
141
  get :call_render_cell_with_state_view
143
- assert_select "#view_with_instance_var"
142
+ assert_select '#view_with_instance_var'
144
143
  end
145
-
146
-
144
+
147
145
  # test simple rendering cycle -------------------------------------------------
148
-
146
+
149
147
  def test_render_state_which_returns_a_string
150
148
  cell = MyTestCell.new(@controller)
151
-
152
- c= cell.render_state(:direct_output)
149
+ c = cell.render_state(:direct_output)
150
+
153
151
  assert_kind_of String, c
154
- assert_selekt c, "h9"
155
-
152
+ assert_selekt c, 'h9'
153
+
156
154
  #assert_raises (NoMethodError) { cell.render_state("non_existing_state") }
157
155
  end
158
-
156
+
159
157
  def test_render_state_with_view_file
160
158
  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"
159
+ c = cell.render_state(:view_with_instance_var)
160
+
161
+ assert_selekt c, '#one', 'yeah'
162
+ assert_selekt c, '#two', 'wow'
165
163
  end
166
-
164
+
167
165
  def test_render_state_with_layout
168
-
169
166
  end
170
-
171
-
167
+
172
168
  def test_render_state_with_missing_view
173
169
  cell = MyTestCell.new(@controller)
174
170
  ### TODO: production <-> development/test context.
175
-
171
+
176
172
  assert_raises ActionView::MissingTemplate do
177
173
  c = cell.render_state(:missing_view)
178
174
  end
179
175
  end
180
-
181
-
176
+
182
177
  # test partial rendering ------------------------------------------------------
183
-
178
+
184
179
  # ok
185
180
  def test_not_existing_partial
186
- t = MyTestCell.new(@controller)
181
+ cell = MyTestCell.new(@controller)
182
+
187
183
  assert_raises ActionView::TemplateError do
188
- t.render_state(:view_containing_nonexistant_partial)
184
+ cell.render_state(:view_containing_nonexistant_partial)
189
185
  end
190
186
  end
191
-
187
+
192
188
  # ok
193
189
  def test_broken_partial
194
- t = MyTestCell.new(@controller)
190
+ cell = MyTestCell.new(@controller)
191
+
195
192
  assert_raises ActionView::TemplateError do
196
- t.render_state(:view_containing_broken_partial)
193
+ cell.render_state(:view_containing_broken_partial)
197
194
  end
198
195
  end
199
-
196
+
200
197
  # ok
201
198
  def test_render_state_with_partial
202
199
  cell = MyTestCell.new(@controller)
203
200
  c = cell.render_state(:view_containing_partial)
204
- assert_selekt c, "#partialContained>#partial"
201
+
202
+ assert_selekt c, '#partialContained > #partial'
205
203
  end
206
-
204
+
207
205
  def test_render_state_with_partial_without_cell_name
208
206
  cell = MyTestCell.new(@controller)
209
207
  c = cell.render_state(:view_containing_partial_without_cell_name)
210
- assert_selekt c, "#partialContained>#partial"
208
+
209
+ assert_selekt c, '#partialContained > #partial'
211
210
  end
212
-
211
+
213
212
  # test advanced views (prototype_helper, ...) --------------------------------
214
- ### TODO: fix CellTestController to allow rendering views with #link_to_function-
213
+ ### TODO: fix CellsTestController to allow rendering views with #link_to_function-
215
214
  def dont_test_view_with_link_to_function
216
215
  cell = MyTestCell.new(@controller)
217
216
  c = cell.render_state(:state_with_link_to_function)
218
- assert_selekt c, "#partialContained>#partial"
217
+
218
+ assert_selekt c, '#partialContained > #partial'
219
219
  end
220
-
220
+
221
221
  # test view inheritance ------------------------------------------------------
222
-
222
+
223
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
224
+ cell = MyChildCell.new(@controller)
225
+ cell_paths = cell.possible_paths_for_state(:bye)
226
+
227
+ assert_equal 'my_child/bye', cell_paths.first
228
+ assert_equal 'my_mother/bye', cell_paths.last
228
229
  end
229
-
230
-
230
+
231
231
  def test_render_state_on_child_where_child_view_exists
232
232
  cell = MyChildCell.new(@controller)
233
233
  c = cell.render_state(:hello)
234
- assert_selekt c, "#childHello", "hello, mom!"
234
+
235
+ assert_selekt c, '#childHello', 'hello, mom!'
235
236
  end
236
-
237
+
237
238
  def test_render_state_on_child_where_view_is_inherited_from_mother
238
239
  cell = MyChildCell.new(@controller)
239
- puts " rendering cell!"
240
+ # puts ' rendering cell!'
240
241
  c = cell.render_state(:bye)
241
- assert_selekt c, "#motherBye", "bye, mom!"
242
+
243
+ assert_selekt c, '#motherBye', 'bye, mom!'
242
244
  end
243
-
244
-
245
+
245
246
  # test Cell::View -------------------------------------------------------------
246
-
247
+
247
248
  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
249
+ cell = MyChildCell.new(@controller)
250
+ cells_path = File.join(File.dirname(__FILE__), 'app', 'cells')
251
+ cell_template = cell.find_family_view_for_state(:bye, ::Cell::View.new([cells_path], {}, @controller))
252
+
253
+ assert_equal 'my_mother/bye.html.erb', cell_template.path
251
254
  end
252
-
253
255
 
254
256
  ### API test (unit) -----------------------------------------------------------
255
257
  def test_cell_name
256
- cell_one = CellsTestOneCell.new(@controller)
258
+ cell = CellsTestOneCell.new(@controller)
257
259
 
258
- assert_equal cell_one.cell_name, "cells_test_one"
259
- assert_equal CellsTestOneCell.cell_name, "cells_test_one"
260
+ assert_equal 'cells_test_one', cell.cell_name
261
+ assert_equal 'cells_test_one', CellsTestOneCell.cell_name
260
262
  end
261
-
262
263
 
263
264
  def test_class_from_cell_name
264
- assert_equal Cell::Base.class_from_cell_name("cells_test_one"), CellsTestOneCell
265
+ assert_equal CellsTestOneCell, ::Cell::Base.class_from_cell_name('cells_test_one')
265
266
  end
266
-
267
+
267
268
  def test_default_template_format
268
269
  # 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
-
270
+ cell = MyTestCell.new(@controller)
271
+
272
+ assert_equal :html, ::Cell::Base.default_template_format
273
+ assert_equal :html, cell.class.default_template_format
274
+
273
275
  # test setter
274
276
  MyTestCell.default_template_format = :js
275
- assert_equal :html, Cell::Base.default_template_format
276
- assert_equal :js, u.class.default_template_format
277
+
278
+ assert_equal :html, ::Cell::Base.default_template_format
279
+ assert_equal :js, cell.class.default_template_format
277
280
  end
278
-
281
+
279
282
  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))
283
+ cell = MyTestCell.new(@controller)
284
+
285
+ assert_equal ({:template_format => :html, :view => :do_it}),
286
+ cell.defaultize_render_options_for({}, :do_it)
287
+ assert_equal ({:template_format => :html, :view => :do_it}),
288
+ cell.defaultize_render_options_for({}, :do_it)
289
+ assert_equal ({:template_format => :js, :view => :do_it}),
290
+ cell.defaultize_render_options_for({:template_format => :js}, :do_it)
291
+ assert_equal ({:template_format => :html, :layout => :metal, :view => :do_it}),
292
+ cell.defaultize_render_options_for({:layout => :metal}, :do_it)
293
+ assert_equal ({:template_format => :js, :layout => :metal, :view => :do_it}),
294
+ cell.defaultize_render_options_for({:layout => :metal, :template_format => :js}, :do_it)
291
295
  end
292
296
 
293
297
  def test_new_directory_hierarchy
294
298
  cell = ReallyModule::NestedCell.new(@controller)
295
- view = cell.render_state(:happy_state)
296
- @response.body = view
299
+ c = cell.render_state(:happy_state)
300
+ @response.body = c
297
301
 
298
- assert_select "#happyStateView"
302
+ assert_select '#happyStateView'
299
303
  end
300
304
 
301
305
  # Thanks to Fran Pena who made us aware of this bug and contributed a patch.
302
306
  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"
307
+ swap I18n, :locale => :en do
308
+ cell = MyTestCell.new(@controller)
309
+ c = cell.render_state(:view_with_explicit_english_translation)
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
315
316
  end
316
-
317
-
317
+
318
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"
319
+ cell = MyTestCell.new(@controller)
320
+ c = cell.render_state(:view_in_local_test_views_dir)
321
+
322
+ assert_selekt c, '#localView'
322
323
  end
323
-
324
-
324
+
325
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"
326
+ @controller.params = {:my_param => 'value'}
327
+ cell = TestCell.new(@controller)
328
+ c = cell.render_state(:state_using_params)
329
+
330
+ assert_equal 'value', c
330
331
  end
331
332
 
332
-
333
-
334
-
335
-
333
+ def test_log
334
+ assert_nothing_raised do
335
+ TestCell.new(@controller).log("everything is perfect!")
336
+ end
337
+ end
338
+
336
339
  ### functional tests: ---------------------------------------------------------
337
340
 
338
341
  def test_link_to_in_view
339
342
  get :render_state_with_link_to
340
343
 
341
344
  assert_response :success
342
- assert_select "a", "bla"
345
+ assert_select 'a', 'bla'
343
346
  end
344
-
345
347
  end