blocks 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,13 @@
1
+ 2.5.0 (October 14, 2013)
2
+ * Added skip method as a shorthand for defining or replacing a block's definition so that nothing is output when the block is rendered
3
+ * Added ability to wrap each block in a collection with an element or render a block with an element, using syntax like:
4
+ <%= blocks.render :brand,
5
+ :collection => @brands,
6
+ :wrap_each => {:tag => "div", :style => "border: 1px solid black", :class => Proc.new {cycle("even", "odd")}},
7
+ :wrap_with => {:tag => "div", :style => "color: red"} %>
8
+ <%= blocks.render :brand, :wrap_with => {:tag => "div", :class => Proc.new {cycle("even", "odd")}} %>
9
+ * Changed blocks.surrounding_tag_surrounds_before_and_after_blocks to Blocks.wrap_with_surrounds_before_and_after_blocks
10
+
1
11
  2.4.0 (September 4, 2013)
2
12
  * Moved queue and render_templates methods into the new with_template gem.
3
13
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.4.0
1
+ 2.5.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.4.0"
8
+ s.version = "2.5.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-05"
12
+ s.date = "2013-10-15"
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.rb CHANGED
@@ -14,8 +14,8 @@ module Blocks
14
14
  mattr_accessor :use_partials
15
15
  @@use_partials = false
16
16
 
17
- mattr_accessor :surrounding_tag_surrounds_before_and_after_blocks
18
- @@surrounding_tag_surrounds_before_and_after_blocks = false
17
+ mattr_accessor :wrap_with_surrounds_before_and_after_blocks
18
+ @@wrap_with_surrounds_before_and_after_blocks = false
19
19
 
20
20
  # Shortcut for using the templating feature / rendering templates
21
21
  def self.render_template(view, partial, options={}, &block)
data/lib/blocks/base.rb CHANGED
@@ -23,8 +23,8 @@ module Blocks
23
23
  # Boolean variable for whether Blocks should attempt to render blocks as partials if a defined block cannot be found
24
24
  attr_accessor :use_partials
25
25
 
26
- # Boolean variable for whether Blocks should render before and after blocks inside or outside of a collections' elements' surrounding tags
27
- attr_accessor :surrounding_tag_surrounds_before_and_after_blocks
26
+ # Boolean variable for whether Blocks should render before and after blocks inside or outside of a collections' elements' wrap_with tags
27
+ attr_accessor :wrap_with_surrounds_before_and_after_blocks
28
28
 
29
29
  # Checks if a particular block has been defined within the current block scope.
30
30
  # <%= blocks.defined? :some_block_name %>
@@ -84,6 +84,25 @@ module Blocks
84
84
  nil
85
85
  end
86
86
 
87
+ # Skip the rendering of a particular block when blocks.render is called for the a particular block name
88
+ # <%= blocks.define :some_block_name do %>
89
+ # My output
90
+ # <% end %>
91
+ #
92
+ # <%= blocks.skip :some_block_name %>
93
+ #
94
+ # <%= blocks.render :some_block_name %>
95
+ # <%# will not render anything %>
96
+ # Options:
97
+ # [+name+]
98
+ # The name of the block to skip rendering for
99
+ def skip(name)
100
+ blocks[name.to_sym] = nil
101
+ self.define_block_container(name) do
102
+ end
103
+ nil
104
+ end
105
+
87
106
  # Render a block, first rendering any "before" blocks, then rendering the block itself, then rendering
88
107
  # any "after" blocks. Additionally, a collection may also be passed in, and Blocks will render
89
108
  # an the block, along with corresponding before and after blocks for each element of the collection.
@@ -123,10 +142,10 @@ module Blocks
123
142
  # The collection of elements to render blocks for
124
143
  # [:as]
125
144
  # The variable name to assign the current element in the collection being rendered over
126
- # [:surrounding_tag]
145
+ # [:wrap_with]
127
146
  # The content tag to render around a block, which might be particularly useful when rendering a collection of blocks,
128
147
  # such as for a list or table
129
- # [:surrounding_tag_html]
148
+ # [:wrap_each]
130
149
  # The attributes to be applied to the HTML content tag, such as styling or special properties. Please note, any Procs passed
131
150
  # in will automatically be evaluated (For example: :class => lambda { cycle("even", "odd") })
132
151
  # [:use_partials]
@@ -141,30 +160,34 @@ module Blocks
141
160
 
142
161
  buffer = ActiveSupport::SafeBuffer.new
143
162
 
163
+ wrap_with = options.delete(:wrap_with) || {}
164
+
144
165
  if collection
145
166
  as = options.delete(:as)
146
-
147
- collection.each do |object|
148
- cloned_args = args.clone
149
- cloned_args.unshift(object)
150
- cloned_options = options.clone
151
- cloned_options = cloned_options.merge(object.options) if object.is_a?(Blocks::Container)
152
- cloned_args.push(cloned_options)
153
-
154
- block_name = call_with_params(name_or_container, *cloned_args)
155
- as_name = (as.presence || block_name).to_sym
156
- cloned_options[as_name] = object
157
-
158
- buffer << render(block_name, *cloned_args, &block)
167
+ wrap_each = options.delete(:wrap_each) || {}
168
+
169
+ buffer = content_tag_with_block(wrap_with[:tag], wrap_with.except(:tag), *args) do
170
+ collection.each do |object|
171
+ cloned_args = args.clone
172
+ cloned_args.unshift(object)
173
+ cloned_options = options.clone
174
+ cloned_options = cloned_options.merge(object.options) if object.is_a?(Blocks::Container)
175
+ cloned_args.push(cloned_options)
176
+
177
+ block_name = call_with_params(name_or_container, *cloned_args)
178
+ as_name = (as.presence || block_name).to_sym
179
+ cloned_options[as_name] = object
180
+ cloned_options[:wrap_with] = wrap_each
181
+
182
+ buffer << render(block_name, *cloned_args, &block)
183
+ end
184
+ buffer
159
185
  end
160
186
  else
161
- surrounding_tag = options.delete(:surrounding_tag)
162
- surrounding_tag_html = options.delete(:surrounding_tag_html)
163
-
164
187
  args.push(options)
165
188
 
166
- if surrounding_tag_surrounds_before_and_after_blocks
167
- buffer << content_tag_with_block(surrounding_tag, surrounding_tag_html, *args) do
189
+ if wrap_with_surrounds_before_and_after_blocks
190
+ buffer << content_tag_with_block(wrap_with[:tag], wrap_with.except(:tag), *args) do
168
191
  temp_buffer = ActiveSupport::SafeBuffer.new
169
192
  temp_buffer << render_before_blocks(name_or_container, *args)
170
193
  temp_buffer << render_block_with_around_blocks(name_or_container, *args, &block)
@@ -172,7 +195,7 @@ module Blocks
172
195
  end
173
196
  else
174
197
  buffer << render_before_blocks(name_or_container, *args)
175
- buffer << content_tag_with_block(surrounding_tag, surrounding_tag_html, *args) do
198
+ buffer << content_tag_with_block(wrap_with[:tag], wrap_with.except(:tag), *args) do
176
199
  render_block_with_around_blocks(name_or_container, *args, &block)
177
200
  end
178
201
  buffer << render_after_blocks(name_or_container, *args)
@@ -405,7 +428,7 @@ module Blocks
405
428
  self.blocks = {}
406
429
  self.anonymous_block_number = 0
407
430
  self.use_partials = options[:use_partials].nil? ? Blocks.use_partials : options.delete(:use_partials)
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)
431
+ self.wrap_with_surrounds_before_and_after_blocks = options[:wrap_with_surrounds_before_and_after_blocks].nil? ? Blocks.wrap_with_surrounds_before_and_after_blocks : options.delete(:wrap_with_surrounds_before_and_after_blocks)
409
432
  end
410
433
 
411
434
  # Return a unique name for an anonymously defined block (i.e. a block that has not been given a name)
@@ -331,6 +331,14 @@ describe Blocks::Base do
331
331
  buffer.should eql "rendered content"
332
332
  end
333
333
 
334
+ it "should be able to render an element surrounding the block" do
335
+ block = Proc.new {}
336
+ @view.expects(:capture).with(nil).returns("rendered content")
337
+ @builder.define :some_block, &block
338
+ buffer = @builder.render :some_block, :wrap_with => {:tag => "span", :id => "my-id"}
339
+ buffer.should eql "<span id=\"my-id\">rendered content</span>"
340
+ end
341
+
334
342
  describe "with a collection passed in" do
335
343
  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
336
344
  block = Proc.new {|item, options| "output#{options[:some_block]} "}
@@ -347,13 +355,13 @@ describe Blocks::Base do
347
355
  it "should render a block for each element of the collection with a surrounding element if that option is specified" do
348
356
  block = Proc.new {|item, options| "output#{options[:my_block_name]} "}
349
357
  @builder.define :some_block, &block
350
- @builder.render(:some_block, :collection => [1,2,3], :surrounding_tag => "div").should eql "<div>output </div><div>output </div><div>output </div>"
358
+ @builder.render(:some_block, :collection => [1,2,3], :wrap_each => {:tag => "div"}).should eql "<div>output </div><div>output </div><div>output </div>"
351
359
  end
352
360
 
353
361
  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
354
362
  block = Proc.new {|item, options| "output#{options[:my_block_name]} "}
355
363
  @builder.define :some_block, &block
356
- @builder.render(:some_block, :collection => [1,2,3], :surrounding_tag => "div", :surrounding_tag_html => {: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>"
364
+ @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>"
357
365
  end
358
366
 
359
367
  it "should be able to render before blocks before each element of a collection" do
@@ -395,11 +403,11 @@ describe Blocks::Base do
395
403
 
396
404
  block = Proc.new {|item, options| "output#{options[:some_block]} "}
397
405
  @builder.define :some_block, &block
398
- @builder.render(:some_block, :collection => [1,2,3], :surrounding_tag => "div").should eql "<div>before1 output1 after1 </div><div>before2 output2 after2 </div><div>before3 output3 after3 </div>"
406
+ @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>"
399
407
  end
400
408
 
401
409
  it "should allow the global option to be set to render before and after blocks outside of surrounding elements" do
402
- Blocks.surrounding_tag_surrounds_before_and_after_blocks = false
410
+ Blocks.wrap_with_surrounds_before_and_after_blocks = false
403
411
  @builder = Blocks::Base.new(@view)
404
412
  before_block = Proc.new {|item, options| "before#{options[:some_block]} "}
405
413
  @builder.before :some_block, &before_block
@@ -409,11 +417,23 @@ describe Blocks::Base do
409
417
 
410
418
  block = Proc.new {|item, options| "output#{options[:some_block]} "}
411
419
  @builder.define :some_block, &block
412
- @builder.render(:some_block, :collection => [1,2,3], :surrounding_tag => "div").should eql "before1 <div>output1 </div>after1 before2 <div>output2 </div>after2 before3 <div>output3 </div>after3 "
420
+ @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 "
413
421
  end
414
422
 
415
423
  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
416
- @builder = Blocks::Base.new(@view, :surrounding_tag_surrounds_before_and_after_blocks => false)
424
+ @builder = Blocks::Base.new(@view, :wrap_with_surrounds_before_and_after_blocks => false)
425
+ before_block = Proc.new {|item, options| "before#{options[:some_block]} "}
426
+ @builder.before :some_block, &before_block
427
+
428
+ after_block = Proc.new {|item, options| "after#{options[:some_block]} "}
429
+ @builder.after :some_block, &after_block
430
+
431
+ block = Proc.new {|item, options| "output#{options[:some_block]} "}
432
+ @builder.define :some_block, &block
433
+ @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 "
434
+ end
435
+
436
+ it "should be able to render an element around everything" do
417
437
  before_block = Proc.new {|item, options| "before#{options[:some_block]} "}
418
438
  @builder.before :some_block, &before_block
419
439
 
@@ -422,7 +442,7 @@ describe Blocks::Base do
422
442
 
423
443
  block = Proc.new {|item, options| "output#{options[:some_block]} "}
424
444
  @builder.define :some_block, &block
425
- @builder.render(:some_block, :collection => [1,2,3], :surrounding_tag => "div").should eql "before1 <div>output1 </div>after1 before2 <div>output2 </div>after2 before3 <div>output3 </div>after3 "
445
+ @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\"><div>before1 output1 after1 </div><div>before2 output2 after2 </div><div>before3 output3 after3 </div></div>"
426
446
  end
427
447
  end
428
448
  end
data/spec/spec_helper.rb CHANGED
@@ -13,7 +13,7 @@ RSpec.configure do |config|
13
13
 
14
14
  config.before :each do
15
15
  Blocks.template_folder = "blocks"
16
- Blocks.surrounding_tag_surrounds_before_and_after_blocks = true
16
+ Blocks.wrap_with_surrounds_before_and_after_blocks = true
17
17
  Blocks.use_partials = false
18
18
  end
19
19
  end
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.0
4
+ version: 2.5.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-05 00:00:00.000000000 Z
12
+ date: 2013-10-15 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: 1012298278410896826
137
+ hash: -2216410614422126676
138
138
  required_rubygems_version: !ruby/object:Gem::Requirement
139
139
  none: false
140
140
  requirements: