blocks 2.5.1 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,7 +1,8 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem "rails", ">= 3.0.0"
4
- gem "call_with_params"
4
+ gem "call_with_params", "~> 0.0.2"
5
+ gem "hashie"
5
6
 
6
7
  group :development do
7
8
  gem "bundler", "~> 1.3.5"
@@ -1,9 +1,99 @@
1
1
  = Blocks
2
2
 
3
- I AM CURRENTLY IN THE PROCESS OF WRITING THIS DOCUMENTATION OVER AGAIN SINCE A LOT HAS CHANGED SINCE THE DOCUMENTATION WAS ORIGINALLY WRITTEN.
4
-
5
3
  How do you render your blocks of code?
6
4
 
7
- The idea of this gem is simple (yet difficult to see the practical application for): blocks of code, regardless of code, whether they are a ruby block or a rails partial, are, or should be, completely interchangeable.
5
+ With blocks, you can:
6
+ 1. Easily define named blocks of view-level code, capable of taking parameters as arguments
7
+ 2. Render a defined block of view-level code and pass in an arbitrary number of parameters
8
+ 3. Provide a default block of code to render if a named block is not found
9
+ 4. Specify view-level code to render before, after, or around a particular block of code
10
+ 5. Render a collection of elements using a defined block
11
+
12
+ == Author's Note
13
+ To fully appreciate how powerful Blocks can be, i highly recommend checking out the
14
+ project {TableFor}[https://github.com/hunterae/table-for] first.
15
+ This is a gem that was built using Blocks and {WithTemplate (an extension of Blocks)}[https://github.com/hunterae/with_template]
16
+ with very few lines of code, and illustrates the power of Blocks and WithTemplate
17
+ and how they can be can be used to build extremely useful reusable UI components.
18
+
19
+ == Installation
20
+
21
+ In <b>Rails 3</b> or <b>Rails 4</b>, add this to your Gemfile.
22
+
23
+ gem "blocks"
24
+
25
+ == Simple Case
26
+ <% blocks.define :sample do |a, b| %>
27
+ This is a snippet of code with parameters <%= a %> and <%= b %> passed in<br>
28
+ <% end %>
29
+
30
+ <% blocks.before :sample do %>
31
+ This is a snippet of code that will render before the block named :sample<br>
32
+ <% end %>
33
+
34
+ <% blocks.after :sample do %>
35
+ This is a snippet of code that will render after the block named :sample<br>
36
+ <% end %>
37
+
38
+ <%# This is when the code gets rendered %>
39
+ <%= blocks.render :sample, "1", 2 %>
40
+
41
+ <%# Provide a wrapper element to wrap around the rendered block
42
+ <%= blocks.render :sample, "1", 2, :wrap_with => {:tag => :div, :id => "sample" } %>
43
+
44
+ <%# Rendering a block that doesn't exist will simply render nothing %>
45
+ <%= blocks.render :sample2 %>
46
+
47
+ <%# Rendering a block that doesn't exist with a default definition will simply use the default definition %>
48
+ <%= blocks.render :sample3 do %>
49
+ This is a snippet that will get rendered if a block named :sample3 is not defined anywhere
50
+ <% end %>
51
+
52
+ == A More Complicated Case
53
+ In this example, @brands will represent a collection of brands fetched from the database
54
+ <% blocks.define :brand do |brand| %>
55
+ Brand <%= brand.name %>
56
+ <% end %>
57
+
58
+ <%= blocks.render :brand,
59
+ :collection => @brands,
60
+ :wrap_with => { :tag => :ul, :id => "brands", :style => "list-style-type:circle" },
61
+ :wrap_each => { :tag => :li,
62
+ :id => Proc.new {|brand| "brand#{brand.id}"},
63
+ :class => Proc.new { cycle("even", "odd") },
64
+ :style => "padding-left: 20px;" }%>
65
+
66
+ == A Practical example
67
+ Surround your javascript includes and css includes in your
68
+ template with a named block, and from the view, the included order can easily be changed.
69
+ Note: I use this approach in every single project I setup.
70
+
71
+ In layout, such as application.html.erb:
72
+ <!DOCTYPE html>
73
+ <head>
74
+ ...
75
+ <%= blocks.render :stylesheets do %>
76
+ <%= stylesheet_link_tag "application", :media => "all" %>
77
+ <% end %>
78
+ </head>
79
+ <body>
80
+ ...
81
+ <%= blocks.render :javascripts do %>
82
+ <%= javascript_include_tag "application" %>
83
+ <% end %>
84
+ </body>
85
+ </html>
8
86
 
9
- This gem seeks to set an order of precedence for how rails determines how to render a block of code.
87
+ In a view, stylesheets and javascript files can easily be prepended, appended, or replace the existing set of
88
+ includes:
89
+ <% blocks.before :stylesheets do %>
90
+ <%= stylesheet_link_tag "SOME_STYLESHEET_TO_INCLUDE_FIRST", :media => "all" %>
91
+ <% end %>
92
+ <% blocks.after :stylesheets do %>
93
+ <%= stylesheet_link_tag "SOME_STYLESHEET_TO_INCLUDE_LAST", :media => "all" %>
94
+ <% end %>
95
+
96
+ <%# Flat out replaces the javascript includes %>
97
+ <% blocks.define :javascripts do %>
98
+ <%= stylesheet_link_tag "MY_JAVASCRIPT_FILES_TO_INCLUDE", :media => "all" %>
99
+ <% end %>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.5.1
1
+ 2.6.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "blocks"
8
- s.version = "2.5.1"
8
+ s.version = "2.6.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-10-17"
12
+ s.date = "2013-11-04"
13
13
  s.description = "Blocks goes beyond blocks and partials"
14
14
  s.email = "hunterae@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -49,20 +49,23 @@ Gem::Specification.new do |s|
49
49
 
50
50
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
51
  s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
52
- s.add_runtime_dependency(%q<call_with_params>, [">= 0"])
52
+ s.add_runtime_dependency(%q<call_with_params>, ["~> 0.0.2"])
53
+ s.add_runtime_dependency(%q<hashie>, [">= 0"])
53
54
  s.add_development_dependency(%q<bundler>, ["~> 1.3.5"])
54
55
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
55
56
  s.add_development_dependency(%q<simplecov>, [">= 0"])
56
57
  else
57
58
  s.add_dependency(%q<rails>, [">= 3.0.0"])
58
- s.add_dependency(%q<call_with_params>, [">= 0"])
59
+ s.add_dependency(%q<call_with_params>, ["~> 0.0.2"])
60
+ s.add_dependency(%q<hashie>, [">= 0"])
59
61
  s.add_dependency(%q<bundler>, ["~> 1.3.5"])
60
62
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
61
63
  s.add_dependency(%q<simplecov>, [">= 0"])
62
64
  end
63
65
  else
64
66
  s.add_dependency(%q<rails>, [">= 3.0.0"])
65
- s.add_dependency(%q<call_with_params>, [">= 0"])
67
+ s.add_dependency(%q<call_with_params>, ["~> 0.0.2"])
68
+ s.add_dependency(%q<hashie>, [">= 0"])
66
69
  s.add_dependency(%q<bundler>, ["~> 1.3.5"])
67
70
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
68
71
  s.add_dependency(%q<simplecov>, [">= 0"])
@@ -1,6 +1,7 @@
1
1
  require "action_view"
2
2
  require "action_controller"
3
3
  require "call_with_params"
4
+ require "hashie"
4
5
 
5
6
  module Blocks
6
7
  autoload :Base, "blocks/base"
@@ -8,23 +9,15 @@ module Blocks
8
9
  autoload :ViewAdditions, "blocks/view_additions"
9
10
  autoload :ControllerAdditions, "blocks/controller_additions"
10
11
 
11
- mattr_accessor :template_folder
12
- @@template_folder = "blocks"
13
-
14
- mattr_accessor :use_partials
15
- @@use_partials = false
16
-
17
- mattr_accessor :wrap_before_and_after_blocks
18
- @@wrap_before_and_after_blocks = false
19
-
20
- # Shortcut for using the templating feature / rendering templates
21
- def self.render_template(view, partial, options={}, &block)
22
- Blocks::Base.new(view, options).render_template(partial, &block)
23
- end
12
+ mattr_accessor :config
13
+ @@config = Hashie::Mash.new
14
+ @@config.wrap_before_and_after_blocks = false
15
+ @@config.use_partials = false
16
+ @@config.partials_folder = "blocks"
24
17
 
25
18
  # Default way to setup Blocks
26
19
  def self.setup
27
- yield self
20
+ yield config
28
21
  end
29
22
  end
30
23
 
@@ -1,3 +1,5 @@
1
+ require 'active_support'
2
+
1
3
  module Blocks
2
4
  class Base
3
5
  include CallWithParams
@@ -14,18 +16,6 @@ module Blocks
14
16
  # These are the options that are passed into the initalize method
15
17
  attr_accessor :global_options
16
18
 
17
- # The default folder to look in for global partials
18
- attr_accessor :template_folder
19
-
20
- # The variable to use when rendering the partial for the templating feature (by default, "blocks")
21
- attr_accessor :variable
22
-
23
- # Boolean variable for whether Blocks should attempt to render blocks as partials if a defined block cannot be found
24
- attr_accessor :use_partials
25
-
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_before_and_after_blocks
28
-
29
19
  # Checks if a particular block has been defined within the current block scope.
30
20
  # <%= blocks.defined? :some_block_name %>
31
21
  # Options:
@@ -42,10 +32,12 @@ module Blocks
42
32
  #
43
33
  # Options:
44
34
  # [+name+]
45
- # The name of the block being defined (either a string or a symbol)
35
+ # The name of the block being defined (either a string or a symbol or a Proc)
46
36
  # [+options+]
47
37
  # The default options for the block definition. Any or all of these options may be overrideen by
48
- # whomever calls "blocks.render" on this block.
38
+ # whomever calls "blocks.render" on this block. If :collection => some_array,
39
+ # Blocks will assume that the first argument is a Proc and define a block for each object in the
40
+ # collection
49
41
  # [+block+]
50
42
  # The block that is to be rendered when "blocks.render" is called for this block.
51
43
  def define(name, options={}, &block)
@@ -184,7 +176,7 @@ module Blocks
184
176
  else
185
177
  args.push(options)
186
178
 
187
- if wrap_before_and_after_blocks
179
+ if global_options.merge(options)[:wrap_before_and_after_blocks]
188
180
  buffer << content_tag_with_block(wrap_with[:tag], wrap_with.except(:tag), *args) do
189
181
  temp_buffer = ActiveSupport::SafeBuffer.new
190
182
  temp_buffer << render_before_blocks(name_or_container, *args)
@@ -415,14 +407,10 @@ module Blocks
415
407
  protected
416
408
 
417
409
  def initialize(view, options={})
418
- self.template_folder = options[:template_folder] ? options.delete(:template_folder) : Blocks.template_folder
419
- self.variable = (options[:variable] ? options.delete(:variable) : :blocks).to_sym
420
410
  self.view = view
421
- self.global_options = options
411
+ self.global_options = Blocks.config.merge(options)
422
412
  self.blocks = {}
423
413
  self.anonymous_block_number = 0
424
- self.use_partials = options[:use_partials].nil? ? Blocks.use_partials : options.delete(:use_partials)
425
- self.wrap_before_and_after_blocks = options[:wrap_before_and_after_blocks].nil? ? Blocks.wrap_before_and_after_blocks : options.delete(:wrap_before_and_after_blocks)
426
414
  end
427
415
 
428
416
  # Return a unique name for an anonymously defined block (i.e. a block that has not been given a name)
@@ -450,28 +438,34 @@ module Blocks
450
438
 
451
439
  # Render a block, first trying to find a previously defined block with the same name
452
440
  def render_block(name_or_container, *args, &block)
453
- options = args.extract_options!
454
-
455
441
  buffer = ActiveSupport::SafeBuffer.new
456
442
 
457
- block_options = {}
458
443
  if (name_or_container.is_a?(Blocks::Container))
459
444
  name = name_or_container.name.to_sym
460
- block_options = name_or_container.options
445
+ block_render_options = name_or_container.options
461
446
  else
462
447
  name = name_or_container.to_sym
448
+ block_render_options = {}
449
+ end
450
+
451
+ block_definition_options = {}
452
+ if blocks[name]
453
+ block_container = blocks[name]
454
+ block_definition_options = block_container.options
463
455
  end
464
456
 
457
+ options = global_options.merge(block_definition_options).merge(block_render_options).merge(args.extract_options!)
458
+
465
459
  if blocks[name]
466
460
  block_container = blocks[name]
467
- args.push(global_options.merge(block_container.options).merge(block_options).merge(options))
461
+ args.push(options)
468
462
  buffer << view.capture(*(args[0, block_container.block.arity]), &block_container.block)
469
- elsif (use_partials || options[:use_partials]) && !options[:skip_partials]
463
+ elsif options[:use_partials] && !options[:skip_partials]
470
464
  begin
471
465
  begin
472
- buffer << view.render("#{name.to_s}", global_options.merge(block_options).merge(options))
466
+ buffer << view.render("#{name.to_s}", options)
473
467
  rescue ActionView::MissingTemplate
474
- buffer << view.render("#{self.template_folder}/#{name.to_s}", global_options.merge(block_options).merge(options))
468
+ buffer << view.render("#{options[:partials_folder]}/#{name.to_s}", options)
475
469
  end
476
470
  rescue ActionView::MissingTemplate
477
471
  args.push(global_options.merge(options))
@@ -561,9 +555,9 @@ module Blocks
561
555
 
562
556
  def content_tag_with_block(tag, tag_html, *args, &block)
563
557
  if tag
564
- view.content_tag(tag, block.call, call_each_hash_value_with_params(tag_html, *args))
558
+ view.content_tag(tag, view.capture(&block), call_each_hash_value_with_params(tag_html, *args))
565
559
  else
566
- block.call
560
+ view.capture(&block)
567
561
  end
568
562
  end
569
563
  end
@@ -7,18 +7,20 @@ describe Blocks::Base do
7
7
  end
8
8
 
9
9
  it "should be able change the default global partials directory" do
10
- Blocks.template_folder = "shared"
11
- Blocks.use_partials = true
10
+ Blocks.setup do |config|
11
+ config.partials_folder = "shared"
12
+ config.use_partials = true
13
+ end
12
14
  @builder = Blocks::Base.new(@view)
13
15
  @builder.expects(:render_before_blocks).at_least_once
14
16
  @builder.expects(:render_after_blocks).at_least_once
15
- @view.expects(:capture).with(:value1 => 1, :value2 => 2).never
16
- @view.expects(:render).with("some_block", :value1 => 1, :value2 => 2).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
17
- @view.expects(:render).with("shared/some_block", :value1 => 1, :value2 => 2).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, '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, 'value1' => 1, 'value2' => 2).once
18
20
  @builder.render :some_block, :value1 => 1, :value2 => 2
19
21
  end
20
22
 
21
- describe "defined? method" do
23
+ describe "#defined?" do
22
24
  it "should be able to determine if a block by a specific name is already defined" do
23
25
  @builder.defined?(:test_block).should be_false
24
26
  @builder.define :test_block do end
@@ -42,7 +44,7 @@ describe Blocks::Base do
42
44
  end
43
45
  end
44
46
 
45
- describe "define method" do
47
+ describe "#define" do
46
48
  it "should be able to define a new block" do
47
49
  block = Proc.new { |options| }
48
50
 
@@ -54,7 +56,7 @@ describe Blocks::Base do
54
56
  test_block.name.should eql(:test_block)
55
57
  test_block.block.should eql(block)
56
58
  end
57
-
59
+
58
60
  it "should not replace a defined block with another attempted definition" do
59
61
  block1 = Proc.new do |options| end
60
62
  @builder.define :test_block, :option1 => "value1", :option2 => "value2", &block1
@@ -70,9 +72,21 @@ describe Blocks::Base do
70
72
  test_block.name.should eql(:test_block)
71
73
  test_block.block.should eql(block1)
72
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
73
87
  end
74
88
 
75
- describe "replace method" do
89
+ describe "#replace" do
76
90
  it "should be able to replace a defined block" do
77
91
  block1 = Proc.new do |options| end
78
92
  @builder.define :test_block, :option1 => "value1", :option2 => "value2", &block1
@@ -90,14 +104,22 @@ describe Blocks::Base do
90
104
  end
91
105
  end
92
106
 
93
- describe "before method" do
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
94
116
  it "should be aliased with prepend" do
95
117
  block = Proc.new { |options| }
96
118
  @builder.prepend :some_block, &block
97
119
  @builder.blocks[:before_some_block].should be_present
98
120
  end
99
121
 
100
- it "should defined before blocks as the block name with the word 'before_' prepended to it" do
122
+ it "should define before blocks as the block name with the word 'before_' prepended to it" do
101
123
  block = Proc.new { |options| }
102
124
  @builder.before :some_block, &block
103
125
  @builder.blocks[:before_some_block].should be_present
@@ -143,7 +165,7 @@ describe Blocks::Base do
143
165
  end
144
166
  end
145
167
 
146
- describe "after method" do
168
+ describe "#after" do
147
169
  it "should be aliased with append and for" do
148
170
  block = Proc.new { |options| }
149
171
  @builder.append :some_block, &block
@@ -154,7 +176,7 @@ describe Blocks::Base do
154
176
  @builder.blocks[:after_some_block].should be_present
155
177
  end
156
178
 
157
- it "should defined after blocks as the block name with the word 'after_' prepended to it" do
179
+ it "should define after blocks as the block name with the word 'after_' prepended to it" do
158
180
  block = Proc.new { |options| }
159
181
  @builder.after :some_block, &block
160
182
  @builder.blocks[:after_some_block].should be_present
@@ -200,42 +222,103 @@ describe Blocks::Base do
200
222
  end
201
223
  end
202
224
 
203
- describe "render method" do
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
204
287
  it "should alias the render method as use" do
205
288
  block = Proc.new {"output"}
206
289
  @builder.define :some_block, &block
207
290
  @builder.use(:some_block).should eql "output"
208
291
  end
209
292
 
210
- it "should be able to use a defined block by its name" do
293
+ it "should be able to render a defined block by its name" do
211
294
  block = Proc.new {"output"}
212
295
  @builder.define :some_block, &block
213
296
  @builder.render(:some_block).should eql "output"
214
297
  end
215
298
 
216
- it "should automatically pass in an options hash to a defined block that takes one paramter when that block is used" do
217
- block = Proc.new {|options| "Options are #{options.inspect}"}
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) }
218
301
  @builder.define :some_block, &block
219
- @builder.render(:some_block).should eql "Options are {}"
302
+ @builder.render(:some_block).should eql print_hash(:wrap_before_and_after_blocks => false, :use_partials => false, :partials_folder => "blocks", )
220
303
  end
221
304
 
222
- it "should be able to use a defined block by its name and pass in runtime arguments as a hash" do
305
+ it "should be able to render a defined block by its name and pass in runtime arguments as a hash" do
223
306
  block = Proc.new do |options|
224
307
  print_hash(options)
225
308
  end
226
309
  @builder.define :some_block, &block
227
- @builder.render(:some_block, :param1 => 1, :param2 => "value2").should eql print_hash(:param1 => 1, :param2 => "value2")
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", :param1 => 1, :param2 => "value2")
228
311
  end
229
312
 
230
- it "should be able to use a defined block by its name and pass in runtime arguments one by one" do
313
+ it "should be able to render a defined block by its name and pass in runtime arguments one by one" do
231
314
  block = Proc.new do |first_param, second_param, options|
232
315
  "first_param: #{first_param}, second_param: #{second_param}, #{print_hash options}"
233
316
  end
234
317
  @builder.define :some_block, &block
235
- @builder.render(:some_block, 3, 4, :param1 => 1, :param2 => "value2").should eql("first_param: 3, second_param: 4, #{print_hash(:param1 => 1, :param2 => "value2")}")
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", :param1 => 1, :param2 => "value2")}")
236
319
  end
237
320
 
238
- it "should match up the number of arguments to a defined block with the parameters passed when a block is used" do
321
+ it "should match up the number of arguments to a defined block with the parameters passed when a block is rendered" do
239
322
  block = Proc.new {|first_param| "first_param = #{first_param}"}
240
323
  @builder.define :some_block, &block
241
324
  @builder.render(:some_block, 3, 4, :param1 => 1, :param2 => "value2").should eql "first_param = 3"
@@ -248,57 +331,68 @@ describe Blocks::Base do
248
331
  "first_param: #{first_param}, second_param: #{second_param}, #{print_hash options}"
249
332
  end
250
333
  @builder.replace :some_block, &block
251
- @builder.render(:some_block, 3, 4, :param1 => 1, :param2 => "value2").should eql("first_param: 3, second_param: 4, #{print_hash(:param1 => 1, :param2 => "value2")}")
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", :param1 => 1, :param2 => "value2")}")
252
335
  end
253
336
 
254
337
  it "should not render anything if using a block that has been defined" do
255
- @builder.use_partials = true
256
- @view.expects(:capture).never
257
- @view.expects(:render).with("some_block", {}).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
258
- @view.expects(:render).with("blocks/some_block", {}).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
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).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
343
+ @view.expects(:render).with("blocks/some_block", 'partials_folder' => 'blocks', 'wrap_before_and_after_blocks' => false, 'use_partials' => true).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
259
344
  @builder.render :some_block
260
345
  end
261
346
 
262
347
  it "should first attempt to capture a block's contents when blocks.render is called" do
263
348
  block = Proc.new {|options|}
264
- @view.expects(:capture).with(:value1 => 1, :value2 => 2)
265
- @view.expects(:render).with("some_block", :value1 => 1, :value2 => 2).never
266
- @view.expects(:render).with("blocks/some_block", :value1 => 1, :value2 => 2).never
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
267
351
  @builder.define :some_block, &block
268
352
  @builder.render :some_block, :value1 => 1, :value2 => 2
269
353
  end
270
354
 
271
355
  it "should second attempt to render a local partial by the block's name when blocks.render is called" do
272
- @builder.use_partials = true
273
- @view.expects(:capture).with(:value1 => 1, :value2 => 2).never
274
- @view.expects(:render).with("some_block", :value1 => 1, :value2 => 2).once
275
- @view.expects(:render).with("blocks/some_block", :value1 => 1, :value2 => 2).never
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, '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, 'value1' => 1, 'value2' => 2).never
276
362
  @builder.render :some_block, :value1 => 1, :value2 => 2
277
363
  end
278
364
 
279
365
  it "should third attempt to render a global partial by the block's name when blocks.render is called" do
280
- @builder.use_partials = true
281
- @view.expects(:capture).with(:value1 => 1, :value2 => 2).never
282
- @view.expects(:render).with("some_block", :value1 => 1, :value2 => 2).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
283
- @view.expects(:render).with("blocks/some_block", :value1 => 1, :value2 => 2).once
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, '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, 'value1' => 1, 'value2' => 2).once
284
372
  @builder.render :some_block, :value1 => 1, :value2 => 2
285
373
  end
286
374
 
287
375
  it "should fourth attempt to render a default block when blocks.render is called" do
288
- block = Proc.new {|options|}
289
- @builder.use_partials = true
290
- @view.expects(:render).with("some_block", :value1 => 1, :value2 => 2).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
291
- @view.expects(:render).with("blocks/some_block", :value1 => 1, :value2 => 2).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
292
- @view.expects(:capture).with(:value1 => 1, :value2 => 2)
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, '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, 'value1' => 1, 'value2' => 2).raises(ActionView::MissingTemplate.new([],[],[],[],[]))
293
386
  @builder.render :some_block, :value1 => 1, :value2 => 2, &block
294
387
  end
295
388
 
296
389
  it "should not attempt to render a partial if use_partials is set to false" do
297
- @builder.use_partials = false
298
- block = Proc.new {|options|}
299
- @view.expects(:render).with("some_block", :value1 => 1, :value2 => 2).never
300
- @view.expects(:render).with("blocks/some_block", :value1 => 1, :value2 => 2).never
301
- @view.expects(:capture).with(:value1 => 1, :value2 => 2)
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, '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, 'value1' => 1, 'value2' => 2).never
302
396
  @builder.render :some_block, :value1 => 1, :value2 => 2, &block
303
397
  end
304
398
 
@@ -307,19 +401,25 @@ describe Blocks::Base do
307
401
  @builder = Blocks::Base.new(@view, :use_partials => false)
308
402
  @builder.expects(:render_before_blocks).at_least_once
309
403
  @builder.expects(:render_after_blocks).at_least_once
310
- block = Proc.new {|options|}
311
- @view.expects(:render).with("some_block", :value1 => 1, :value2 => 2).never
312
- @view.expects(:render).with("blocks/some_block", :value1 => 1, :value2 => 2).never
313
- @view.expects(:capture).with(:value1 => 1, :value2 => 2)
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, '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, 'value1' => 1, 'value2' => 2).never
314
410
  @builder.render :some_block, :value1 => 1, :value2 => 2, &block
315
411
  end
316
412
 
317
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
318
- block = Proc.new {|options|}
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
319
420
  @builder.global_options.merge!(:param1 => "global level", :param2 => "global level", :param3 => "global level", :param4 => "global level")
320
421
  block_container = @builder.send(:define_block_container, :my_before_block, :param1 => "queue level", :param2 => "queue level")
321
422
  @builder.define(:my_before_block, :param1 => "define level", :param2 => "define level", :param3 => "define level", &block)
322
- @view.expects(:capture).with(:param1 => 'use level', :param2 => 'queue level', :param3 => 'define level', :param4 => 'global level')
323
423
  @builder.render block_container, :param1 => "use level"
324
424
  end
325
425
 
@@ -394,7 +494,7 @@ describe Blocks::Base do
394
494
  @builder.render(:some_block, :collection => [1,2,3]).should eql "before1 output1 after1 before2 output2 after2 before3 output3 after3 "
395
495
  end
396
496
 
397
- it "should by default put surrounding elements around before and after blocks" do
497
+ it "should by default render surrounding elements outside before and after blocks" do
398
498
  before_block = Proc.new {|item, options| "before#{options[:some_block]} "}
399
499
  @builder.before :some_block, &before_block
400
500
 
@@ -403,11 +503,13 @@ describe Blocks::Base do
403
503
 
404
504
  block = Proc.new {|item, options| "output#{options[:some_block]} "}
405
505
  @builder.define :some_block, &block
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>"
506
+ @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 "
407
507
  end
408
508
 
409
- it "should allow the global option to be set to render before and after blocks outside of surrounding elements" do
410
- Blocks.wrap_before_and_after_blocks = false
509
+ it "should allow the global option to be set to render before and after blocks inside of surrounding elements" do
510
+ Blocks.setup do |config|
511
+ config.wrap_before_and_after_blocks = true
512
+ end
411
513
  @builder = Blocks::Base.new(@view)
412
514
  before_block = Proc.new {|item, options| "before#{options[:some_block]} "}
413
515
  @builder.before :some_block, &before_block
@@ -417,7 +519,7 @@ describe Blocks::Base do
417
519
 
418
520
  block = Proc.new {|item, options| "output#{options[:some_block]} "}
419
521
  @builder.define :some_block, &block
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 "
522
+ @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>"
421
523
  end
422
524
 
423
525
  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
@@ -442,7 +544,7 @@ describe Blocks::Base do
442
544
 
443
545
  block = Proc.new {|item, options| "output#{options[:some_block]} "}
444
546
  @builder.define :some_block, &block
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>"
547
+ @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>"
446
548
  end
447
549
  end
448
550
  end
@@ -454,18 +556,26 @@ describe Blocks::Base do
454
556
  end
455
557
 
456
558
  it "should render before blocks when using a block" do
457
- block = Proc.new {|value1, value2, options|}
559
+ block = Proc.new do |value1, value2, options|
560
+ value1.should eql 1
561
+ value2.should eql 2
562
+ options[:value3].should eql 3
563
+ options[:value4].should eql 4
564
+ end
458
565
  @builder.before("my_before_block", &block)
459
- @view.expects(:capture).with(1, 2, :value3 => 3, :value4 => 4)
460
566
  @builder.render :my_before_block, 1, 2, :value3 => 3, :value4 => 4
461
567
  end
462
568
 
463
569
  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
464
- block = Proc.new {|options|}
570
+ block = Proc.new do |options|
571
+ options[:param1].should eql "top level"
572
+ options[:param2].should eql "before block level"
573
+ options[:param3].should eql "block level"
574
+ options[:param4].should eql "global level"
575
+ end
465
576
  @builder.global_options.merge!(:param1 => "global level", :param2 => "global level", :param3 => "global level", :param4 => "global level")
466
577
  @builder.define(:my_before_block, :param1 => "block level", :param2 => "block level", :param3 => "block level", &block)
467
578
  @builder.before(:my_before_block, :param1 => "before block level", :param2 => "before block level", &block)
468
- @view.expects(:capture).with(:param4 => 'global level', :param1 => 'top level', :param2 => 'before block level', :param3 => 'block level')
469
579
  @builder.render :my_before_block, :param1 => "top level"
470
580
  end
471
581
  end
@@ -499,18 +609,26 @@ describe Blocks::Base do
499
609
  end
500
610
 
501
611
  it "should render after blocks when using a block" do
502
- block = Proc.new {|value1, value2, options|}
612
+ block = Proc.new do |value1, value2, options|
613
+ value1.should eql 1
614
+ value2.should eql 2
615
+ options[:value3].should eql 3
616
+ options[:value4].should eql 4
617
+ end
503
618
  @builder.after("my_after_block", &block)
504
- @view.expects(:capture).with(1, 2, :value3 => 3, :value4 => 4)
505
619
  @builder.render :my_after_block, 1, 2, :value3 => 3, :value4 => 4
506
620
  end
507
621
 
508
622
  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
509
- block = Proc.new {|options|}
623
+ block = Proc.new do |options|
624
+ options[:param1].should eql "top level"
625
+ options[:param2].should eql "after block level"
626
+ options[:param3].should eql "block level"
627
+ options[:param4].should eql "global level"
628
+ end
510
629
  @builder.global_options.merge!(:param1 => "global level", :param2 => "global level", :param3 => "global level", :param4 => "global level")
511
630
  @builder.define(:my_after_block, :param1 => "block level", :param2 => "block level", :param3 => "block level", &block)
512
631
  @builder.after(:my_after_block, :param1 => "after block level", :param2 => "after block level", &block)
513
- @view.expects(:capture).with(:param4 => 'global level', :param1 => 'top level', :param2 => 'after block level', :param3 => 'block level')
514
632
  @builder.render :my_after_block, :param1 => "top level"
515
633
  end
516
634
  end
@@ -1,24 +1,12 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Blocks do
4
- it "should provide a util method to render a template" do
5
- view = stub
6
- options = stub
7
- partial = stub
8
- block = Proc.new { |options| }
9
- base_mock = mock
10
- base_mock.expects(:render_template).with(partial)
11
- Blocks::Base.expects(:new).with(view, options).returns(base_mock)
12
-
13
- Blocks.render_template(view, partial, options, &block)
14
- end
15
-
16
4
  it "should provide a setup method that can be called from an initializer" do
17
- Blocks.template_folder.should eql("blocks")
5
+ Blocks.config.partials_folder.should eql("blocks")
18
6
  Blocks.setup do |config|
19
- config.should eql(Blocks)
20
- config.template_folder = "shared"
7
+ config.should be_a(Hashie::Mash)
8
+ config.partials_folder = "shared"
21
9
  end
22
- Blocks.template_folder.should eql("shared")
10
+ Blocks.config.partials_folder.should eql("shared")
23
11
  end
24
12
  end
@@ -7,7 +7,7 @@ describe Blocks::ViewAdditions do
7
7
  @view_class.send(:include, Blocks::ViewAdditions::ClassMethods)
8
8
  end
9
9
 
10
- describe "blocks method" do
10
+ describe "#blocks" do
11
11
  it "should pass the view as the only parameter to Blocks::Base initialization" do
12
12
  Blocks::Base.expects(:new).with {|view| view == @view}
13
13
  @view.blocks
@@ -12,9 +12,11 @@ RSpec.configure do |config|
12
12
  config.mock_with :mocha
13
13
 
14
14
  config.before :each do
15
- Blocks.template_folder = "blocks"
16
- Blocks.wrap_before_and_after_blocks = true
17
- Blocks.use_partials = false
15
+ Blocks.setup do |config|
16
+ config.partials_folder = "blocks"
17
+ config.wrap_before_and_after_blocks = false
18
+ config.use_partials = false
19
+ end
18
20
  end
19
21
  end
20
22
 
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.5.1
4
+ version: 2.6.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-10-17 00:00:00.000000000 Z
12
+ date: 2013-11-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -29,6 +29,22 @@ dependencies:
29
29
  version: 3.0.0
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: call_with_params
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.0.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.0.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: hashie
32
48
  requirement: !ruby/object:Gem::Requirement
33
49
  none: false
34
50
  requirements:
@@ -134,7 +150,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
150
  version: '0'
135
151
  segments:
136
152
  - 0
137
- hash: -1089807617590558203
153
+ hash: -4078634663971351405
138
154
  required_rubygems_version: !ruby/object:Gem::Requirement
139
155
  none: false
140
156
  requirements: