blocks 2.8.0 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +4 -4
  3. data/.travis.yml +51 -0
  4. data/Gemfile +15 -2
  5. data/Guardfile +15 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +41 -0
  8. data/Rakefile +3 -7
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/blocks.gemspec +18 -11
  12. data/gemfiles/Gemfile.rails-3-0-stable +13 -0
  13. data/gemfiles/Gemfile.rails-3-1-stable +13 -0
  14. data/gemfiles/Gemfile.rails-3-2-stable +13 -0
  15. data/gemfiles/Gemfile.rails-4-0-stable +12 -0
  16. data/gemfiles/Gemfile.rails-4-1-stable +12 -0
  17. data/gemfiles/Gemfile.rails-4-2-stable +12 -0
  18. data/gemfiles/Gemfile.rails-5-0-stable +10 -0
  19. data/gemfiles/Gemfile.rails-5-1-stable +10 -0
  20. data/lib/blocks.rb +41 -20
  21. data/lib/blocks/action_view_extensions/view_extensions.rb +26 -0
  22. data/lib/blocks/builders/block_definition.rb +71 -0
  23. data/lib/blocks/builders/builder.rb +159 -0
  24. data/lib/blocks/builders/hook_definition.rb +25 -0
  25. data/lib/blocks/experimental/builder_permissions.rb +52 -0
  26. data/lib/blocks/experimental/invalid_permissions_handler.rb +27 -0
  27. data/lib/blocks/renderers/abstract_renderer.rb +69 -0
  28. data/lib/blocks/renderers/adjacent_blocks_renderer.rb +15 -0
  29. data/lib/blocks/renderers/block_placeholder.rb +13 -0
  30. data/lib/blocks/renderers/block_renderer.rb +13 -0
  31. data/lib/blocks/renderers/block_with_hooks_renderer.rb +35 -0
  32. data/lib/blocks/renderers/collection_renderer.rb +17 -0
  33. data/lib/blocks/renderers/nesting_blocks_renderer.rb +24 -0
  34. data/lib/blocks/renderers/partial_renderer.rb +18 -0
  35. data/lib/blocks/renderers/renderer.rb +43 -0
  36. data/lib/blocks/renderers/runtime_context.rb +193 -0
  37. data/lib/blocks/renderers/wrapper_renderer.rb +19 -0
  38. data/lib/blocks/utilities/configurator.rb +26 -0
  39. data/lib/blocks/utilities/dynamic_configuration.rb +71 -0
  40. data/lib/blocks/utilities/hash_with_caller.rb +73 -0
  41. data/lib/blocks/utilities/hash_with_render_strategy.rb +47 -0
  42. data/lib/blocks/utilities/options_set.rb +95 -0
  43. data/lib/blocks/version.rb +1 -1
  44. metadata +70 -80
  45. data/.ruby-gemset +0 -1
  46. data/.ruby-version +0 -1
  47. data/README.rdoc +0 -99
  48. data/blocks_render_order.graffle +0 -1397
  49. data/blocks_render_order.png +0 -0
  50. data/lib/blocks/base.rb +0 -580
  51. data/lib/blocks/container.rb +0 -5
  52. data/lib/blocks/controller_additions.rb +0 -9
  53. data/lib/blocks/view_additions.rb +0 -10
  54. data/rails/init.rb +0 -1
  55. data/spec/blocks/base_spec.rb +0 -641
  56. data/spec/blocks/blocks_spec.rb +0 -12
  57. data/spec/blocks/view_additions_spec.rb +0 -22
  58. data/spec/spec_helper.rb +0 -22
@@ -1,5 +0,0 @@
1
- module Blocks
2
- class Container
3
- attr_accessor :name, :options, :block, :anonymous
4
- end
5
- end
@@ -1,9 +0,0 @@
1
- module Blocks
2
- module ControllerAdditions
3
- module ClassMethods
4
- def blocks
5
- @controller_blocks ||= Blocks::Base.new(view_context)
6
- end
7
- end
8
- end
9
- end
@@ -1,10 +0,0 @@
1
- module Blocks
2
- module ViewAdditions
3
- def blocks
4
- return @blocks if @blocks
5
- @blocks = Blocks::Base.new(self)
6
- @blocks.blocks.merge! @controller_blocks.blocks if @controller_blocks
7
- @blocks
8
- end
9
- end
10
- end
data/rails/init.rb DELETED
@@ -1 +0,0 @@
1
- require 'blocks'
@@ -1,641 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Blocks::Base do
4
- before :each do
5
- @view = ActionView::Base.new
6
- @builder = Blocks::Base.new(@view)
7
- end
8
-
9
- it "should be able change the default global partials directory" do
10
- Blocks.setup do |config|
11
- config.partials_folder = "shared"
12
- config.use_partials = true
13
- end
14
- @builder = Blocks::Base.new(@view)
15
- @builder.expects(:render_before_blocks).at_least_once
16
- @builder.expects(:render_after_blocks).at_least_once
17
- # @view.expects(:capture).with(:value1 => 1, :value2 => 2).never
18
- @view.expects(:render).with("some_block", 'partials_folder' => 'shared', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
19
- @view.expects(:render).with("shared/some_block", 'partials_folder' => 'shared', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).once
20
- @builder.render :some_block, :value1 => 1, :value2 => 2
21
- end
22
-
23
- describe "#defined?" do
24
- it "should be able to determine if a block by a specific name is already defined" do
25
- @builder.defined?(:test_block).should be false
26
- @builder.define :test_block do end
27
- @builder.defined?(:test_block).should be true
28
- end
29
-
30
- it "should not care whether the block name was defined with a string or a symbol" do
31
- @builder.defined?(:test_block).should be false
32
- @builder.define "test_block" do end
33
- @builder.defined?(:test_block).should be true
34
-
35
- @builder.defined?(:test_block2).should be false
36
- @builder.define :test_block2 do end
37
- @builder.defined?(:test_block2).should be true
38
- end
39
-
40
- it "should not care whether the defined? method is passed a string or a symbol" do
41
- @builder.defined?("test_block").should be false
42
- @builder.define :test_block do end
43
- @builder.defined?("test_block").should be true
44
- end
45
- end
46
-
47
- describe "#define" do
48
- it "should be able to define a new block" do
49
- block = Proc.new { |options| }
50
-
51
- @builder.define :test_block, :option1 => "value1", :option2 => "value2", &block
52
-
53
- test_block = @builder.blocks[:test_block]
54
- test_block.options[:option1].should eql("value1")
55
- test_block.options[:option2].should eql("value2")
56
- test_block.name.should eql(:test_block)
57
- test_block.block.should eql(block)
58
- end
59
-
60
- it "should not replace a defined block with another attempted definition" do
61
- block1 = Proc.new do |options| end
62
- @builder.define :test_block, :option1 => "value1", :option2 => "value2", &block1
63
-
64
- block2 = Proc.new do |options| end
65
- @builder.define :test_block, :option3 => "value3", :option4 => "value4", &block2
66
-
67
- test_block = @builder.blocks[:test_block]
68
- test_block.options[:option1].should eql("value1")
69
- test_block.options[:option2].should eql("value2")
70
- test_block.options[:option3].should be_nil
71
- test_block.options[:option4].should be_nil
72
- test_block.name.should eql(:test_block)
73
- test_block.block.should eql(block1)
74
- end
75
-
76
- it "should be able to define a collection of blocks" do
77
- block = Proc.new {}
78
- collection = ["block1", "block2", "block3"]
79
- @builder.define Proc.new {|block_name| block_name }, :collection => collection, &block
80
- @builder.blocks[:block1].should be_present
81
- @builder.blocks[:block1].block.should eql block
82
- @builder.blocks[:block2].should be_present
83
- @builder.blocks[:block2].block.should eql block
84
- @builder.blocks[:block3].should be_present
85
- @builder.blocks[:block3].block.should eql block
86
- end
87
- end
88
-
89
- describe "#replace" do
90
- it "should be able to replace a defined block" do
91
- block1 = Proc.new do |options| end
92
- @builder.define :test_block, :option1 => "value1", :option2 => "value2", &block1
93
-
94
- block2 = Proc.new do |options| end
95
- @builder.replace :test_block, :option3 => "value3", :option4 => "value4", &block2
96
-
97
- test_block = @builder.blocks[:test_block]
98
- test_block.options[:option1].should be_nil
99
- test_block.options[:option2].should be_nil
100
- test_block.options[:option3].should eql("value3")
101
- test_block.options[:option4].should eql("value4")
102
- test_block.name.should eql(:test_block)
103
- test_block.block.should eql(block2)
104
- end
105
- end
106
-
107
- describe "#skip" do
108
- it "should define an empty block" do
109
- @builder.skip(:test_block)
110
- @builder.blocks[:test_block].should be_present
111
- @builder.blocks[:test_block].block.call.should be_nil
112
- end
113
- end
114
-
115
- describe "#before" do
116
- it "should be aliased with prepend" do
117
- block = Proc.new { |options| }
118
- @builder.prepend :some_block, &block
119
- @builder.blocks[:before_some_block].should be_present
120
- end
121
-
122
- it "should define before blocks as the block name with the word 'before_' prepended to it" do
123
- block = Proc.new { |options| }
124
- @builder.before :some_block, &block
125
- @builder.blocks[:before_some_block].should be_present
126
- end
127
-
128
- it "should store a before block in an array" do
129
- block = Proc.new { |options| }
130
- @builder.before :some_block, &block
131
- before_blocks = @builder.blocks[:before_some_block]
132
- before_blocks.should be_a(Array)
133
- end
134
-
135
- it "should store a before block as a Blocks::Container" do
136
- block = Proc.new { |options| }
137
- @builder.before :some_block, :option1 => "some option", &block
138
- before_blocks = @builder.blocks[:before_some_block]
139
- block_container = before_blocks.first
140
- block_container.should be_a(Blocks::Container)
141
- block_container.options.should eql :option1 => "some option"
142
- block_container.block.should eql block
143
- end
144
-
145
- it "should queue before blocks if there are multiple defined" do
146
- block = Proc.new { |options| }
147
- block2 = Proc.new { |options| }
148
- @builder.before :some_block, &block
149
- @builder.before :some_block, &block2
150
- before_blocks = @builder.blocks[:before_some_block]
151
- before_blocks.length.should eql 2
152
- end
153
-
154
- it "should store before blocks in the order in which they are defined" do
155
- block = Proc.new { |options| }
156
- block2 = Proc.new { |options| }
157
- block3 = Proc.new { |options| }
158
- @builder.before :some_block, &block
159
- @builder.before :some_block, &block2
160
- @builder.before :some_block, &block3
161
- before_blocks = @builder.blocks[:before_some_block]
162
- before_blocks.first.block.should eql block
163
- before_blocks.second.block.should eql block2
164
- before_blocks.third.block.should eql block3
165
- end
166
- end
167
-
168
- describe "#after" do
169
- it "should be aliased with append and for" do
170
- block = Proc.new { |options| }
171
- @builder.append :some_block, &block
172
- @builder.blocks[:after_some_block].should be_present
173
-
174
- block = Proc.new { |options| }
175
- @builder.for :some_block, &block
176
- @builder.blocks[:after_some_block].should be_present
177
- end
178
-
179
- it "should define after blocks as the block name with the word 'after_' prepended to it" do
180
- block = Proc.new { |options| }
181
- @builder.after :some_block, &block
182
- @builder.blocks[:after_some_block].should be_present
183
- end
184
-
185
- it "should store a after block in an array" do
186
- block = Proc.new { |options| }
187
- @builder.after :some_block, &block
188
- after_blocks = @builder.blocks[:after_some_block]
189
- after_blocks.should be_a(Array)
190
- end
191
-
192
- it "should store a after block as a Blocks::Container" do
193
- block = Proc.new { |options| }
194
- @builder.after :some_block, :option1 => "some option", &block
195
- after_blocks = @builder.blocks[:after_some_block]
196
- block_container = after_blocks.first
197
- block_container.should be_a(Blocks::Container)
198
- block_container.options.should eql :option1 => "some option"
199
- block_container.block.should eql block
200
- end
201
-
202
- it "should queue after blocks if there are multiple defined" do
203
- block = Proc.new { |options| }
204
- block2 = Proc.new { |options| }
205
- @builder.after :some_block, &block
206
- @builder.after :some_block, &block2
207
- after_blocks = @builder.blocks[:after_some_block]
208
- after_blocks.length.should eql 2
209
- end
210
-
211
- it "should store after blocks in the order in which they are defined" do
212
- block = Proc.new { |options| }
213
- block2 = Proc.new { |options| }
214
- block3 = Proc.new { |options| }
215
- @builder.after :some_block, &block
216
- @builder.after :some_block, &block2
217
- @builder.after :some_block, &block3
218
- after_blocks = @builder.blocks[:after_some_block]
219
- after_blocks.first.block.should eql block
220
- after_blocks.second.block.should eql block2
221
- after_blocks.third.block.should eql block3
222
- end
223
- end
224
-
225
- describe "#around" do
226
- it "should define around blocks as the block name with the word 'around_' prepended to it" do
227
- block = Proc.new { |options| }
228
- @builder.around :some_block, &block
229
- @builder.blocks[:around_some_block].should be_present
230
- end
231
-
232
- it "should store a around block in an array" do
233
- block = Proc.new { |options| }
234
- @builder.around :some_block, &block
235
- around_blocks = @builder.blocks[:around_some_block]
236
- around_blocks.should be_a(Array)
237
- end
238
-
239
- it "should store a around block as a Blocks::Container" do
240
- block = Proc.new { |options| }
241
- @builder.around :some_block, :option1 => "some option", &block
242
- around_blocks = @builder.blocks[:around_some_block]
243
- block_container = around_blocks.first
244
- block_container.should be_a(Blocks::Container)
245
- block_container.options.should eql :option1 => "some option"
246
- block_container.block.should eql block
247
- end
248
-
249
- it "should queue around blocks if there are multiple defined" do
250
- block = Proc.new { |options| }
251
- block2 = Proc.new { |options| }
252
- @builder.around :some_block, &block
253
- @builder.around :some_block, &block2
254
- around_blocks = @builder.blocks[:around_some_block]
255
- around_blocks.length.should eql 2
256
- end
257
-
258
- it "should store after blocks in the order in which they are defined" do
259
- block = Proc.new { |options| }
260
- block2 = Proc.new { |options| }
261
- block3 = Proc.new { |options| }
262
- @builder.around :some_block, &block
263
- @builder.around :some_block, &block2
264
- @builder.around :some_block, &block3
265
- around_blocks = @builder.blocks[:around_some_block]
266
- around_blocks.first.block.should eql block
267
- around_blocks.second.block.should eql block2
268
- around_blocks.third.block.should eql block3
269
- end
270
- end
271
-
272
- describe "#render_with_partials" do
273
- it "should trigger the call to #render with :use_partials set to true" do
274
- @builder.expects(:render).with(:some_block, :use_partials => true)
275
- @builder.render_with_partials(:some_block)
276
- end
277
- end
278
-
279
- describe "#render_without_partials" do
280
- it "should trigger the call to #render with :skip_partials set to true" do
281
- @builder.expects(:render).with(:some_block, :skip_partials => true)
282
- @builder.render_without_partials(:some_block)
283
- end
284
- end
285
-
286
- describe "#render" do
287
- it "should alias the render method as use" do
288
- block = Proc.new {"output"}
289
- @builder.define :some_block, &block
290
- @builder.use(:some_block).should eql "output"
291
- end
292
-
293
- it "should be able to render a defined block by its name" do
294
- block = Proc.new {"output"}
295
- @builder.define :some_block, &block
296
- @builder.render(:some_block).should eql "output"
297
- end
298
-
299
- it "should automatically pass in an options hash to a defined block that takes one paramter when that block is rendered" do
300
- block = Proc.new {|options| print_hash(options) }
301
- @builder.define :some_block, &block
302
- @builder.render(:some_block).should eql print_hash(:wrap_before_and_after_blocks => false, :use_partials => false, :partials_folder => "blocks", :skip_applies_to_surrounding_blocks => false)
303
- end
304
-
305
- it "should be able to render a defined block by its name and pass in runtime arguments as a hash" do
306
- block = Proc.new do |options|
307
- print_hash(options)
308
- end
309
- @builder.define :some_block, &block
310
- @builder.render(:some_block, :param1 => 1, :param2 => "value2").should eql print_hash(:wrap_before_and_after_blocks => false, :use_partials => false, :partials_folder => "blocks", :skip_applies_to_surrounding_blocks => false, :param1 => 1, :param2 => "value2")
311
- end
312
-
313
- it "should be able to render a defined block by its name and pass in runtime arguments one by one" do
314
- block = Proc.new do |first_param, second_param, options|
315
- "first_param: #{first_param}, second_param: #{second_param}, #{print_hash options}"
316
- end
317
- @builder.define :some_block, &block
318
- @builder.render(:some_block, 3, 4, :param1 => 1, :param2 => "value2").should eql("first_param: 3, second_param: 4, #{print_hash(:wrap_before_and_after_blocks => false, :use_partials => false, :partials_folder => "blocks", :skip_applies_to_surrounding_blocks => false, :param1 => 1, :param2 => "value2")}")
319
- end
320
-
321
- it "should match up the number of arguments to a defined block with the parameters passed when a block is rendered" do
322
- block = Proc.new {|first_param| "first_param = #{first_param}"}
323
- @builder.define :some_block, &block
324
- @builder.render(:some_block, 3, 4, :param1 => 1, :param2 => "value2").should eql "first_param = 3"
325
-
326
- block = Proc.new {|first_param, second_param| "first_param = #{first_param}, second_param = #{second_param}"}
327
- @builder.replace :some_block, &block
328
- @builder.render(:some_block, 3, 4, :param1 => 1, :param2 => "value2").should eql "first_param = 3, second_param = 4"
329
-
330
- block = Proc.new do |first_param, second_param, options|
331
- "first_param: #{first_param}, second_param: #{second_param}, #{print_hash options}"
332
- end
333
- @builder.replace :some_block, &block
334
- @builder.render(:some_block, 3, 4, :param1 => 1, :param2 => "value2").should eql("first_param: 3, second_param: 4, #{print_hash(:wrap_before_and_after_blocks => false, :use_partials => false, :partials_folder => "blocks", :skip_applies_to_surrounding_blocks => false, :param1 => 1, :param2 => "value2")}")
335
- end
336
-
337
- it "should not render anything if using a block that has been defined" do
338
- Blocks.setup do |config|
339
- config.use_partials = true
340
- end
341
- @builder = Blocks::Base.new(@view)
342
- @view.expects(:render).with("some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'skip_applies_to_surrounding_blocks' => false).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
343
- @view.expects(:render).with("blocks/some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'skip_applies_to_surrounding_blocks' => false).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
344
- @builder.render :some_block
345
- end
346
-
347
- it "should first attempt to capture a block's contents when blocks.render is called" do
348
- block = Proc.new {|options|}
349
- @view.expects(:render).with("some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'value1' => 1, 'value2' => 2).never
350
- @view.expects(:render).with("blocks/some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'value1' => 1, 'value2' => 2).never
351
- @builder.define :some_block, &block
352
- @builder.render :some_block, :value1 => 1, :value2 => 2
353
- end
354
-
355
- it "should second attempt to render a local partial by the block's name when blocks.render is called" do
356
- Blocks.setup do |config|
357
- config.use_partials = true
358
- end
359
- @builder = Blocks::Base.new(@view)
360
- @view.expects(:render).with("some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).once
361
- @view.expects(:render).with("blocks/some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).never
362
- @builder.render :some_block, :value1 => 1, :value2 => 2
363
- end
364
-
365
- it "should third attempt to render a global partial by the block's name when blocks.render is called" do
366
- Blocks.setup do |config|
367
- config.use_partials = true
368
- end
369
- @builder = Blocks::Base.new(@view)
370
- @view.expects(:render).with("some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
371
- @view.expects(:render).with("blocks/some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).once
372
- @builder.render :some_block, :value1 => 1, :value2 => 2
373
- end
374
-
375
- it "should fourth attempt to render a default block when blocks.render is called" do
376
- block = Proc.new do |options|
377
- options[:value1].should eql 1
378
- options[:value2].should eql 2
379
- end
380
- Blocks.setup do |config|
381
- config.use_partials = true
382
- end
383
- @builder = Blocks::Base.new(@view)
384
- @view.expects(:render).with("some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
385
- @view.expects(:render).with("blocks/some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => true, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
386
- @builder.render :some_block, :value1 => 1, :value2 => 2, &block
387
- end
388
-
389
- it "should not attempt to render a partial if use_partials is set to false" do
390
- block = Proc.new do |options|
391
- options[:value1].should eql 1
392
- options[:value2].should eql 2
393
- end
394
- @view.expects(:render).with("some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => false, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).never
395
- @view.expects(:render).with("blocks/some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => false, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).never
396
- @builder.render :some_block, :value1 => 1, :value2 => 2, &block
397
- end
398
-
399
- it "should not attempt to render a partial if use_partials is passed in as false as an option to Blocks::Base.new" do
400
- mocha_teardown
401
- @builder = Blocks::Base.new(@view, :use_partials => false)
402
- @builder.expects(:render_before_blocks).at_least_once
403
- @builder.expects(:render_after_blocks).at_least_once
404
- block = Proc.new do |options|
405
- options[:value1].should eql 1
406
- options[:value2].should eql 2
407
- end
408
- @view.expects(:render).with("some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => false, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).never
409
- @view.expects(:render).with("blocks/some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => false, 'skip_applies_to_surrounding_blocks' => false, 'value1' => 1, 'value2' => 2).never
410
- @builder.render :some_block, :value1 => 1, :value2 => 2, &block
411
- end
412
-
413
- it "should override hash options for a block by merging the runtime options into the define default options into the queue level options into the global options" do
414
- block = Proc.new do |options|
415
- options[:param1].should eql "use level"
416
- options[:param2].should eql "queue level"
417
- options[:param3].should eql "define level"
418
- options[:param4].should eql "global level"
419
- end
420
- @builder.global_options.merge!(:param1 => "global level", :param2 => "global level", :param3 => "global level", :param4 => "global level")
421
- block_container = @builder.send(:define_block_container, :my_before_block, :param1 => "queue level", :param2 => "queue level")
422
- @builder.define(:my_before_block, :param1 => "define level", :param2 => "define level", :param3 => "define level", &block)
423
- @builder.render block_container, :param1 => "use level"
424
- end
425
-
426
- it "should render the contents of a defined block when that block is used" do
427
- block = Proc.new {}
428
- @view.expects(:capture).with(nil).returns("rendered content")
429
- @builder.define :some_block, &block
430
- buffer = @builder.render :some_block
431
- buffer.should eql "rendered content"
432
- end
433
-
434
- it "should be able to render an element surrounding the block" do
435
- block = Proc.new {}
436
- @view.expects(:capture).with(nil).returns("rendered content")
437
- @builder.define :some_block, &block
438
- buffer = @builder.render :some_block, :wrap_with => {:tag => "span", :id => "my-id"}
439
- buffer.should eql "<span id=\"my-id\">rendered content</span>"
440
- end
441
-
442
- describe "with a collection passed in" do
443
- it "should render a block for each element of the collection with the name of the block used as the name of the element passed into the block" do
444
- block = Proc.new {|item, options| "output#{options[:some_block]} "}
445
- @builder.define :some_block, &block
446
- @builder.render(:some_block, :collection => [1,2,3]).should eql "output1 output2 output3 "
447
- end
448
-
449
- it "should track the current index and pass as an option to the block" do
450
- block = Proc.new {|item, options| "Item #{options[:current_index] + 1}: #{item} "}
451
- @builder.define :some_block, &block
452
- @builder.render(:some_block, :collection => ["a", "b", "c"]).should eql "Item 1: a Item 2: b Item 3: c "
453
- end
454
-
455
- it "should render a block for each element of the collection with the 'as' option specifying the name of the element passed into the block" do
456
- block = Proc.new {|item, options| "output#{options[:my_block_name]} "}
457
- @builder.define :some_block, &block
458
- @builder.render(:some_block, :collection => [1,2,3], :as => "my_block_name").should eql "output1 output2 output3 "
459
- end
460
-
461
- it "should render a block for each element of the collection with a surrounding element if that option is specified" do
462
- block = Proc.new {|item, options| "output#{options[:my_block_name]} "}
463
- @builder.define :some_block, &block
464
- @builder.render(:some_block, :collection => [1,2,3], :wrap_each => {:tag => "div"}).should eql "<div>output </div><div>output </div><div>output </div>"
465
- end
466
-
467
- it "should render a block for each element of the collection with a surrounding element and specified html options if those options are specified" do
468
- block = Proc.new {|item, options| "output#{options[:my_block_name]} "}
469
- @builder.define :some_block, &block
470
- @builder.render(:some_block, :collection => [1,2,3], :wrap_each => {:tag => "div", :class => lambda { @view.cycle("even", "odd")}, :style => "color:red"}).should eql "<div class=\"even\" style=\"color:red\">output </div><div class=\"odd\" style=\"color:red\">output </div><div class=\"even\" style=\"color:red\">output </div>"
471
- end
472
-
473
- it "should be able to render before blocks before each element of a collection" do
474
- before_block = Proc.new {|item, options| "before#{options[:some_block]} "}
475
- @builder.before :some_block, &before_block
476
-
477
- block = Proc.new {|item, options| "output#{options[:some_block]} "}
478
- @builder.define :some_block, &block
479
- @builder.render(:some_block, :collection => [1,2,3]).should eql "before1 output1 before2 output2 before3 output3 "
480
- end
481
-
482
- it "should be able to render after blocks before each element of a collection" do
483
- after_block = Proc.new {|item, options| "after#{options[:some_block]} "}
484
- @builder.after :some_block, &after_block
485
-
486
- block = Proc.new {|item, options| "output#{options[:some_block]} "}
487
- @builder.define :some_block, &block
488
- @builder.render(:some_block, :collection => [1,2,3]).should eql "output1 after1 output2 after2 output3 after3 "
489
- end
490
-
491
- it "should be able to render before and after blocks before each element of a collection" do
492
- before_block = Proc.new {|item, options| "before#{options[:some_block]} "}
493
- @builder.before :some_block, &before_block
494
-
495
- after_block = Proc.new {|item, options| "after#{options[:some_block]} "}
496
- @builder.after :some_block, &after_block
497
-
498
- block = Proc.new {|item, options| "output#{options[:some_block]} "}
499
- @builder.define :some_block, &block
500
- @builder.render(:some_block, :collection => [1,2,3]).should eql "before1 output1 after1 before2 output2 after2 before3 output3 after3 "
501
- end
502
-
503
- it "should by default render surrounding elements outside before and after blocks" do
504
- before_block = Proc.new {|item, options| "before#{options[:some_block]} "}
505
- @builder.before :some_block, &before_block
506
-
507
- after_block = Proc.new {|item, options| "after#{options[:some_block]} "}
508
- @builder.after :some_block, &after_block
509
-
510
- block = Proc.new {|item, options| "output#{options[:some_block]} "}
511
- @builder.define :some_block, &block
512
- @builder.render(:some_block, :collection => [1,2,3], :wrap_each => {:tag => "div"}).should eql "before1 <div>output1 </div>after1 before2 <div>output2 </div>after2 before3 <div>output3 </div>after3 "
513
- end
514
-
515
- it "should allow the global option to be set to render before and after blocks inside of surrounding elements" do
516
- Blocks.setup do |config|
517
- config.wrap_before_and_after_blocks = true
518
- end
519
- @builder = Blocks::Base.new(@view)
520
- before_block = Proc.new {|item, options| "before#{options[:some_block]} "}
521
- @builder.before :some_block, &before_block
522
-
523
- after_block = Proc.new {|item, options| "after#{options[:some_block]} "}
524
- @builder.after :some_block, &after_block
525
-
526
- block = Proc.new {|item, options| "output#{options[:some_block]} "}
527
- @builder.define :some_block, &block
528
- @builder.render(:some_block, :collection => [1,2,3], :wrap_each => {:tag => "div"}).should eql "<div>before1 output1 after1 </div><div>before2 output2 after2 </div><div>before3 output3 after3 </div>"
529
- end
530
-
531
- it "should allow the option to be set to render before and after blocks outside of surrounding elements to be specified when Blocks is initialized" do
532
- @builder = Blocks::Base.new(@view, :wrap_before_and_after_blocks => false)
533
- before_block = Proc.new {|item, options| "before#{options[:some_block]} "}
534
- @builder.before :some_block, &before_block
535
-
536
- after_block = Proc.new {|item, options| "after#{options[:some_block]} "}
537
- @builder.after :some_block, &after_block
538
-
539
- block = Proc.new {|item, options| "output#{options[:some_block]} "}
540
- @builder.define :some_block, &block
541
- @builder.render(:some_block, :collection => [1,2,3], :wrap_each => {:tag => "div"}).should eql "before1 <div>output1 </div>after1 before2 <div>output2 </div>after2 before3 <div>output3 </div>after3 "
542
- end
543
-
544
- it "should be able to render an element around everything" do
545
- before_block = Proc.new {|item, options| "before#{options[:some_block]} "}
546
- @builder.before :some_block, &before_block
547
-
548
- after_block = Proc.new {|item, options| "after#{options[:some_block]} "}
549
- @builder.after :some_block, &after_block
550
-
551
- block = Proc.new {|item, options| "output#{options[:some_block]} "}
552
- @builder.define :some_block, &block
553
- @builder.render(:some_block, :collection => [1,2,3], :wrap_each => {:tag => "div"}, :wrap_with => {:tag => "div", :style => "color:red"}).should eql "<div style=\"color:red\">before1 <div>output1 </div>after1 before2 <div>output2 </div>after2 before3 <div>output3 </div>after3 </div>"
554
- end
555
- end
556
- end
557
-
558
- describe "render method - before blocks" do
559
- before :each do
560
- @builder.expects(:render_block).at_least_once
561
- @builder.expects(:render_after_blocks).at_least_once
562
- end
563
-
564
- it "should render before blocks when using a block" do
565
- block = Proc.new do |value1, value2, options|
566
- value1.should eql 1
567
- value2.should eql 2
568
- options[:value3].should eql 3
569
- options[:value4].should eql 4
570
- end
571
- @builder.before("my_before_block", &block)
572
- @builder.render :my_before_block, 1, 2, :value3 => 3, :value4 => 4
573
- end
574
-
575
- it "should override hash options for before blocks by merging the runtime options into the before block options into the block options into the global options" do
576
- block = Proc.new do |options|
577
- options[:param1].should eql "top level"
578
- options[:param2].should eql "before block level"
579
- options[:param3].should eql "block level"
580
- options[:param4].should eql "global level"
581
- end
582
- @builder.global_options.merge!(:param1 => "global level", :param2 => "global level", :param3 => "global level", :param4 => "global level")
583
- @builder.define(:my_before_block, :param1 => "block level", :param2 => "block level", :param3 => "block level", &block)
584
- @builder.before(:my_before_block, :param1 => "before block level", :param2 => "before block level", &block)
585
- @builder.render :my_before_block, :param1 => "top level"
586
- end
587
- end
588
-
589
- describe "render method - around blocks" do
590
- it "should be able to render code around another block" do
591
- my_block = Proc.new { "test" }
592
- around_block = Proc.new { |content_block| "<span>#{content_block.call}</span>" }
593
- @builder.define(:my_block, &my_block)
594
- @builder.around(:my_block, &around_block)
595
- @builder.render(:my_block).should eql("&lt;span&gt;test&lt;/span&gt;")
596
- end
597
-
598
- it "should be able to nest multiple around blocks with the last defined around block on the outside" do
599
- my_block = Proc.new { "test" }
600
- around_block1 = Proc.new { |content_block| @view.content_tag :h1, content_block.call }
601
- around_block2 = Proc.new { |content_block| @view.content_tag :span, content_block.call, :style => "font-size: 100px" }
602
- around_block3 = Proc.new { |content_block| @view.content_tag :span, content_block.call, :style => "style='color:red" }
603
- @builder.define(:my_block, &my_block)
604
- @builder.around(:my_block, &around_block1)
605
- @builder.around(:my_block, &around_block2)
606
- @builder.around(:my_block, &around_block3)
607
- @builder.render(:my_block).should eql(%%<h1><span style="font-size: 100px"><span style="style=&#39;color:red">test</span></span></h1>%)
608
- end
609
- end
610
-
611
- describe "render method - after blocks" do
612
- before :each do
613
- @builder.expects(:render_block).at_least_once
614
- @builder.expects(:render_before_blocks).at_least_once
615
- end
616
-
617
- it "should render after blocks when using a block" do
618
- block = Proc.new do |value1, value2, options|
619
- value1.should eql 1
620
- value2.should eql 2
621
- options[:value3].should eql 3
622
- options[:value4].should eql 4
623
- end
624
- @builder.after("my_after_block", &block)
625
- @builder.render :my_after_block, 1, 2, :value3 => 3, :value4 => 4
626
- end
627
-
628
- it "should override hash options for after blocks by merging the runtime options into the after block options into the block options into the global options" do
629
- block = Proc.new do |options|
630
- options[:param1].should eql "top level"
631
- options[:param2].should eql "after block level"
632
- options[:param3].should eql "block level"
633
- options[:param4].should eql "global level"
634
- end
635
- @builder.global_options.merge!(:param1 => "global level", :param2 => "global level", :param3 => "global level", :param4 => "global level")
636
- @builder.define(:my_after_block, :param1 => "block level", :param2 => "block level", :param3 => "block level", &block)
637
- @builder.after(:my_after_block, :param1 => "after block level", :param2 => "after block level", &block)
638
- @builder.render :my_after_block, :param1 => "top level"
639
- end
640
- end
641
- end