blocks 2.3.1 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +3 -0
- data/Gemfile +1 -0
- data/Rakefile +1 -8
- data/VERSION +1 -1
- data/blocks.gemspec +2 -2
- data/lib/blocks/base.rb +8 -123
- data/spec/blocks/base_spec.rb +3 -122
- data/spec/spec_helper.rb +1 -0
- metadata +3 -3
data/CHANGELOG.rdoc
CHANGED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -27,11 +27,4 @@ Jeweler::Tasks.new do |gem|
|
|
27
27
|
gem.authors = ["Andrew Hunter"]
|
28
28
|
# dependencies defined in Gemfile
|
29
29
|
end
|
30
|
-
Jeweler::RubygemsDotOrgTasks.new
|
31
|
-
|
32
|
-
require 'rake/testtask'
|
33
|
-
Rake::TestTask.new(:test) do |test|
|
34
|
-
test.libs << 'lib' << 'test'
|
35
|
-
test.pattern = 'test/**/test_*.rb'
|
36
|
-
test.verbose = true
|
37
|
-
end
|
30
|
+
Jeweler::RubygemsDotOrgTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.4.0
|
data/blocks.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "blocks"
|
8
|
-
s.version = "2.
|
8
|
+
s.version = "2.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Andrew Hunter"]
|
12
|
-
s.date = "2013-09-
|
12
|
+
s.date = "2013-09-05"
|
13
13
|
s.description = "Blocks goes beyond blocks and partials"
|
14
14
|
s.email = "hunterae@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/blocks/base.rb
CHANGED
@@ -8,15 +8,9 @@ module Blocks
|
|
8
8
|
# Hash of block names to Blocks::Container objects
|
9
9
|
attr_accessor :blocks
|
10
10
|
|
11
|
-
# Array of Blocks::Container objects, storing the order of blocks as they were queued
|
12
|
-
attr_accessor :queued_blocks
|
13
|
-
|
14
11
|
# counter, used to give unnamed blocks a unique name
|
15
12
|
attr_accessor :anonymous_block_number
|
16
13
|
|
17
|
-
# A Hash of queued_blocks arrays; a new array is started when method_missing is invoked
|
18
|
-
attr_accessor :block_groups
|
19
|
-
|
20
14
|
# These are the options that are passed into the initalize method
|
21
15
|
attr_accessor :global_options
|
22
16
|
|
@@ -170,7 +164,7 @@ module Blocks
|
|
170
164
|
args.push(options)
|
171
165
|
|
172
166
|
if surrounding_tag_surrounds_before_and_after_blocks
|
173
|
-
buffer <<
|
167
|
+
buffer << content_tag_with_block(surrounding_tag, surrounding_tag_html, *args) do
|
174
168
|
temp_buffer = ActiveSupport::SafeBuffer.new
|
175
169
|
temp_buffer << render_before_blocks(name_or_container, *args)
|
176
170
|
temp_buffer << render_block_with_around_blocks(name_or_container, *args, &block)
|
@@ -178,7 +172,7 @@ module Blocks
|
|
178
172
|
end
|
179
173
|
else
|
180
174
|
buffer << render_before_blocks(name_or_container, *args)
|
181
|
-
buffer <<
|
175
|
+
buffer << content_tag_with_block(surrounding_tag, surrounding_tag_html, *args) do
|
182
176
|
render_block_with_around_blocks(name_or_container, *args, &block)
|
183
177
|
end
|
184
178
|
buffer << render_after_blocks(name_or_container, *args)
|
@@ -277,91 +271,6 @@ module Blocks
|
|
277
271
|
render(name_or_container, *args, &block)
|
278
272
|
end
|
279
273
|
|
280
|
-
# Queue a block for later rendering, such as within a template.
|
281
|
-
# <%= Blocks::Base.new(self).render_template("shared/wizard") do |blocks| %>
|
282
|
-
# <% blocks.queue :step1 %>
|
283
|
-
# <% blocks.queue :step2 do %>
|
284
|
-
# My overridden Step 2 |
|
285
|
-
# <% end %>
|
286
|
-
# <% blocks.queue :step3 %>
|
287
|
-
# <% blocks.queue do %>
|
288
|
-
# | Anonymous Step 4
|
289
|
-
# <% end %>
|
290
|
-
# <% end %>
|
291
|
-
#
|
292
|
-
# <!-- In /app/views/shared/wizard -->
|
293
|
-
# <% blocks.define :step1 do %>
|
294
|
-
# Step 1 |
|
295
|
-
# <% end %>
|
296
|
-
#
|
297
|
-
# <% blocks.define :step2 do %>
|
298
|
-
# Step 2 |
|
299
|
-
# <% end %>
|
300
|
-
#
|
301
|
-
# <% blocks.define :step3 do %>
|
302
|
-
# Step 3
|
303
|
-
# <% end %>
|
304
|
-
#
|
305
|
-
# <% blocks.queued_blocks.each do |block| %>
|
306
|
-
# <%= blocks.render block %>
|
307
|
-
# <% end %>
|
308
|
-
#
|
309
|
-
# <!-- Will render: Step 1 | My overridden Step 2 | Step 3 | Anonymous Step 4-->
|
310
|
-
# Options:
|
311
|
-
# [+*args+]
|
312
|
-
# The options to pass in when this block is rendered. These will override any options provided to the actual block
|
313
|
-
# definition. Any or all of these options may be overriden by whoever calls "blocks.render" on this block.
|
314
|
-
# Usually the first of these args will be the name of the block being queued (either a string or a symbol)
|
315
|
-
# [+block+]
|
316
|
-
# The optional block definition to render when the queued block is rendered
|
317
|
-
def queue(*args, &block)
|
318
|
-
self.queued_blocks << self.define_block_container(*args, &block)
|
319
|
-
nil
|
320
|
-
end
|
321
|
-
|
322
|
-
# Render a partial, treating it as a template, and any code in the block argument will impact how the template renders
|
323
|
-
# <%= Blocks::Base.new(self).render_template("shared/wizard") do |blocks| %>
|
324
|
-
# <% blocks.queue :step1 %>
|
325
|
-
# <% blocks.queue :step2 do %>
|
326
|
-
# My overridden Step 2 |
|
327
|
-
# <% end %>
|
328
|
-
# <% blocks.queue :step3 %>
|
329
|
-
# <% blocks.queue do %>
|
330
|
-
# | Anonymous Step 4
|
331
|
-
# <% end %>
|
332
|
-
# <% end %>
|
333
|
-
#
|
334
|
-
# <!-- In /app/views/shared/wizard -->
|
335
|
-
# <% blocks.define :step1 do %>
|
336
|
-
# Step 1 |
|
337
|
-
# <% end %>
|
338
|
-
#
|
339
|
-
# <% blocks.define :step2 do %>
|
340
|
-
# Step 2 |
|
341
|
-
# <% end %>
|
342
|
-
#
|
343
|
-
# <% blocks.define :step3 do %>
|
344
|
-
# Step 3
|
345
|
-
# <% end %>
|
346
|
-
#
|
347
|
-
# <% blocks.queued_blocks.each do |block| %>
|
348
|
-
# <%= blocks.render block %>
|
349
|
-
# <% end %>
|
350
|
-
#
|
351
|
-
# <!-- Will render: Step 1 | My overridden Step 2 | Step 3 | Anonymous Step 4-->
|
352
|
-
# Options:
|
353
|
-
# [+partial+]
|
354
|
-
# The partial to render as a template
|
355
|
-
# [+block+]
|
356
|
-
# An optional block with code that affects how the template renders
|
357
|
-
def render_template(partial, &block)
|
358
|
-
render_options = global_options.clone
|
359
|
-
render_options[self.variable] = self
|
360
|
-
render_options[:captured_block] = view.capture(self, &block) if block_given?
|
361
|
-
|
362
|
-
view.render partial, render_options
|
363
|
-
end
|
364
|
-
|
365
274
|
# Add a block to render before another block. This before block will be put into an array so that multiple
|
366
275
|
# before blocks may be queued. They will render in the order in which they are declared when the
|
367
276
|
# "blocks#render" method is called. Any options specified to the before block will override any options
|
@@ -396,7 +305,7 @@ module Blocks
|
|
396
305
|
# [+block+]
|
397
306
|
# The block of code to render before another block
|
398
307
|
def before(name, options={}, &block)
|
399
|
-
self.
|
308
|
+
self.add_block_container_to_list("before_#{name.to_s}", options, &block)
|
400
309
|
nil
|
401
310
|
end
|
402
311
|
alias prepend before
|
@@ -435,7 +344,7 @@ module Blocks
|
|
435
344
|
# [+block+]
|
436
345
|
# The block of code to render after another block
|
437
346
|
def after(name, options={}, &block)
|
438
|
-
self.
|
347
|
+
self.add_block_container_to_list("after_#{name.to_s}", options, &block)
|
439
348
|
nil
|
440
349
|
end
|
441
350
|
alias append after
|
@@ -482,43 +391,19 @@ module Blocks
|
|
482
391
|
# [+block+]
|
483
392
|
# The block of code to render after another block
|
484
393
|
def around(name, options={}, &block)
|
485
|
-
self.
|
394
|
+
self.add_block_container_to_list("around_#{name.to_s}", options, &block)
|
486
395
|
nil
|
487
396
|
end
|
488
397
|
|
489
398
|
protected
|
490
399
|
|
491
|
-
# If a method is missing, we'll assume the user is starting a new block group by that missing method name
|
492
|
-
def method_missing(m, *args, &block)
|
493
|
-
options = args.extract_options!
|
494
|
-
|
495
|
-
# If the specified block group has already been defined, it is simply returned here for iteration.
|
496
|
-
# It will consist of all the blocks used in this block group that have yet to be rendered,
|
497
|
-
# as the call for their use occurred before the template was rendered (where their definitions likely occurred)
|
498
|
-
return self.block_groups[m] unless self.block_groups[m].nil?
|
499
|
-
|
500
|
-
# Allows for nested block groups, store the current block positions array and start a new one
|
501
|
-
original_queued_blocks = self.queued_blocks
|
502
|
-
self.queued_blocks = []
|
503
|
-
self.block_groups[m] = self.queued_blocks
|
504
|
-
|
505
|
-
# Capture the contents of the block group (this will only capture block definitions and block renders; it will ignore anything else)
|
506
|
-
view.capture(global_options.merge(options), &block) if block_given?
|
507
|
-
|
508
|
-
# restore the original block positions array
|
509
|
-
self.queued_blocks = original_queued_blocks
|
510
|
-
nil
|
511
|
-
end
|
512
|
-
|
513
400
|
def initialize(view, options={})
|
514
401
|
self.template_folder = options[:template_folder] ? options.delete(:template_folder) : Blocks.template_folder
|
515
402
|
self.variable = (options[:variable] ? options.delete(:variable) : :blocks).to_sym
|
516
403
|
self.view = view
|
517
404
|
self.global_options = options
|
518
|
-
self.queued_blocks = []
|
519
405
|
self.blocks = {}
|
520
406
|
self.anonymous_block_number = 0
|
521
|
-
self.block_groups = {}
|
522
407
|
self.use_partials = options[:use_partials].nil? ? Blocks.use_partials : options.delete(:use_partials)
|
523
408
|
self.surrounding_tag_surrounds_before_and_after_blocks = options[:surrounding_tag_surrounds_before_and_after_blocks].nil? ? Blocks.surrounding_tag_surrounds_before_and_after_blocks : options.delete(:surrounding_tag_surrounds_before_and_after_blocks)
|
524
409
|
end
|
@@ -639,8 +524,8 @@ module Blocks
|
|
639
524
|
end
|
640
525
|
|
641
526
|
# Build a Blocks::Container object and add it to an array of containers matching it's block name
|
642
|
-
# (used only for queuing a collection of before and
|
643
|
-
def
|
527
|
+
# (used only for queuing a collection of before, after, and around blocks for a particular block name)
|
528
|
+
def add_block_container_to_list(*args, &block)
|
644
529
|
block_container = self.build_block_container(*args, &block)
|
645
530
|
if blocks[block_container.name].nil?
|
646
531
|
blocks[block_container.name] = [block_container]
|
@@ -657,7 +542,7 @@ module Blocks
|
|
657
542
|
block_container
|
658
543
|
end
|
659
544
|
|
660
|
-
def
|
545
|
+
def content_tag_with_block(tag, tag_html, *args, &block)
|
661
546
|
if tag
|
662
547
|
view.content_tag(tag, block.call, call_each_hash_value_with_params(tag_html, *args))
|
663
548
|
else
|
data/spec/blocks/base_spec.rb
CHANGED
@@ -90,98 +90,6 @@ describe Blocks::Base do
|
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
|
-
describe "queue method" do
|
94
|
-
it "should store all queued blocks in the queued_blocks array" do
|
95
|
-
@builder.queued_blocks.should be_empty
|
96
|
-
@builder.queue :test_block
|
97
|
-
@builder.queued_blocks.length.should eql 1
|
98
|
-
@builder.queued_blocks.map(&:name).first.should eql(:test_block)
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should convert a string block name to a symbol" do
|
102
|
-
@builder.queue "test_block"
|
103
|
-
@builder.queued_blocks.map(&:name).first.should eql(:test_block)
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should queue blocks as Blocks::Container objects" do
|
107
|
-
@builder.queue :test_block, :a => 1, :b => 2, :c => 3
|
108
|
-
container = @builder.queued_blocks.first
|
109
|
-
container.should be_a(Blocks::Container)
|
110
|
-
container.name.should eql(:test_block)
|
111
|
-
container.options.should eql(:a => 1, :b => 2, :c => 3)
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should not require a name for the block being queued" do
|
115
|
-
@builder.queue
|
116
|
-
@builder.queue
|
117
|
-
@builder.queued_blocks.length.should eql 2
|
118
|
-
@builder.queued_blocks.map(&:name).first.should eql(:block_1)
|
119
|
-
@builder.queued_blocks.map(&:name).second.should eql(:block_2)
|
120
|
-
end
|
121
|
-
|
122
|
-
it "should anonymously define the name of a block if not specified" do
|
123
|
-
@builder.queue
|
124
|
-
@builder.queue :my_block
|
125
|
-
@builder.queue
|
126
|
-
@builder.queued_blocks.map(&:name).first.should eql(:block_1)
|
127
|
-
@builder.queued_blocks.map(&:name).second.should eql(:my_block)
|
128
|
-
@builder.queued_blocks.map(&:name).third.should eql(:block_2)
|
129
|
-
end
|
130
|
-
|
131
|
-
it "should store queued blocks in the order in which they are queued" do
|
132
|
-
@builder.queue :block1
|
133
|
-
@builder.queue :block3
|
134
|
-
@builder.queue :block2
|
135
|
-
@builder.queued_blocks.map(&:name).first.should eql(:block1)
|
136
|
-
@builder.queued_blocks.map(&:name).second.should eql(:block3)
|
137
|
-
@builder.queued_blocks.map(&:name).third.should eql(:block2)
|
138
|
-
end
|
139
|
-
|
140
|
-
it "should allow a definition to be provided for a queued block" do
|
141
|
-
block = Proc.new do |options| end
|
142
|
-
@builder.queue :test_block, &block
|
143
|
-
container = @builder.queued_blocks.first
|
144
|
-
container.block.should eql block
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
describe "render_template method" do
|
149
|
-
it "should attempt to render a partial specified as the :template parameter" do
|
150
|
-
view = mock()
|
151
|
-
builder = Blocks::Base.new(view)
|
152
|
-
view.expects(:render).with{ |template, options| template == "my_template"}
|
153
|
-
builder.render_template("my_template")
|
154
|
-
end
|
155
|
-
|
156
|
-
it "should set all of the global options as local variables to the partial it renders" do
|
157
|
-
view = mock()
|
158
|
-
builder = Blocks::Base.new(view)
|
159
|
-
view.expects(:render).with { |template, options| template == 'some_template' && options[:blocks] == builder }
|
160
|
-
builder.render_template("some_template")
|
161
|
-
end
|
162
|
-
|
163
|
-
it "should capture the data of a block if a block has been specified" do
|
164
|
-
block = Proc.new { |options| "my captured block" }
|
165
|
-
builder = Blocks::Base.new(@view)
|
166
|
-
@view.expects(:render).with { |tempate, options| options[:captured_block] == "my captured block" }
|
167
|
-
builder.render_template("template", &block)
|
168
|
-
end
|
169
|
-
|
170
|
-
it "should by default add a variable to the partial called 'blocks' as a pointer to the Blocks::Base instance" do
|
171
|
-
view = mock()
|
172
|
-
builder = Blocks::Base.new(view)
|
173
|
-
view.expects(:render).with { |template, options| options[:blocks] == builder }
|
174
|
-
builder.render_template("some_template")
|
175
|
-
end
|
176
|
-
|
177
|
-
it "should allow the user to override the local variable passed to the partial as a pointer to the Blocks::Base instance" do
|
178
|
-
view = mock()
|
179
|
-
builder = Blocks::Base.new(view, :variable => "my_variable")
|
180
|
-
view.expects(:render).with { |template, options| options[:blocks].should be_nil }
|
181
|
-
builder.render_template("some_template")
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
93
|
describe "before method" do
|
186
94
|
it "should be aliased with prepend" do
|
187
95
|
block = Proc.new { |options| }
|
@@ -406,13 +314,12 @@ describe Blocks::Base do
|
|
406
314
|
@builder.render :some_block, :value1 => 1, :value2 => 2, &block
|
407
315
|
end
|
408
316
|
|
409
|
-
it "should override hash options for a block by merging the runtime options the define default options into the queue level options into the global options" do
|
317
|
+
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
|
410
318
|
block = Proc.new {|options|}
|
411
319
|
@builder.global_options.merge!(:param1 => "global level", :param2 => "global level", :param3 => "global level", :param4 => "global level")
|
412
|
-
@builder.
|
320
|
+
block_container = @builder.send(:define_block_container, :my_before_block, :param1 => "queue level", :param2 => "queue level")
|
413
321
|
@builder.define(:my_before_block, :param1 => "define level", :param2 => "define level", :param3 => "define level", &block)
|
414
|
-
|
415
|
-
@view.expects(:capture).with(:param4 => 'global level', :param1 => 'use level', :param2 => 'queue level', :param3 => 'define level')
|
322
|
+
@view.expects(:capture).with(:param1 => 'use level', :param2 => 'queue level', :param3 => 'define level', :param4 => 'global level')
|
416
323
|
@builder.render block_container, :param1 => "use level"
|
417
324
|
end
|
418
325
|
|
@@ -587,30 +494,4 @@ describe Blocks::Base do
|
|
587
494
|
@builder.render :my_after_block, :param1 => "top level"
|
588
495
|
end
|
589
496
|
end
|
590
|
-
|
591
|
-
describe "method_missing method" do
|
592
|
-
it "should start a new block group if a method is missing" do
|
593
|
-
@builder.some_method
|
594
|
-
queued_blocks = @builder.block_groups[:some_method]
|
595
|
-
queued_blocks.should eql []
|
596
|
-
end
|
597
|
-
|
598
|
-
it "should add items to a queue when a new block group is started" do
|
599
|
-
@builder.some_method do
|
600
|
-
@builder.queue :myblock1
|
601
|
-
@builder.queue :myblock2
|
602
|
-
end
|
603
|
-
@builder.some_method2 do
|
604
|
-
@builder.queue :myblock3
|
605
|
-
end
|
606
|
-
queued_blocks = @builder.block_groups[:some_method]
|
607
|
-
queued_blocks.length.should eql 2
|
608
|
-
queued_blocks.first.name.should eql :myblock1
|
609
|
-
queued_blocks.second.name.should eql :myblock2
|
610
|
-
queued_blocks = @builder.block_groups[:some_method2]
|
611
|
-
queued_blocks.length.should eql 1
|
612
|
-
queued_blocks.first.name.should eql :myblock3
|
613
|
-
@builder.queued_blocks.should eql []
|
614
|
-
end
|
615
|
-
end
|
616
497
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blocks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
12
|
+
date: 2013-09-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -134,7 +134,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
134
134
|
version: '0'
|
135
135
|
segments:
|
136
136
|
- 0
|
137
|
-
hash:
|
137
|
+
hash: 1012298278410896826
|
138
138
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
139
|
none: false
|
140
140
|
requirements:
|