blocks 3.0.0.rc5 → 3.0.0.rc6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/docs/_includes/configuration.md +60 -1
- data/docs/_includes/custom-builders.md +34 -1
- data/docs/_includes/hooks.md +57 -1
- data/docs/_includes/rendering.md +105 -1
- data/docs/index.md +3 -3
- data/lib/blocks/renderers/runtime_context.rb +1 -1
- data/lib/blocks/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '018a7c22841a5dd37edc183e18a0a105c52dfdbb'
|
4
|
+
data.tar.gz: b869d230657f1eb1740a135b3a46694cd7edc737
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 954c8fcaa1290b02b06ecc32b1fedcf1b3f23f89fd74d2578a83763b7b53492ee43cb3af80f759f38ad19505b9f10e20eac11e6e750817373768da0e6de297a5
|
7
|
+
data.tar.gz: 8897465d0814eec1ac500ad1ce24616217d2a38cfef6055f7f48466a19c66be2a9b50a2024764512f38a9b19399bc4dd80050f1284bcf22f4ff641e27a532824
|
@@ -1,3 +1,62 @@
|
|
1
1
|
# Configuration
|
2
2
|
|
3
|
-
|
3
|
+
Blocks is customized by adding an initializer to config/initializers directory called blocks.rb.
|
4
|
+
|
5
|
+
{% highlight erb %}
|
6
|
+
Blocks.configure do |config|
|
7
|
+
# Configuration code goes here
|
8
|
+
end
|
9
|
+
{% endhighlight %}
|
10
|
+
|
11
|
+
## Configuring Global Options
|
12
|
+
|
13
|
+
Global Options are merged into the set of options generated when rendering a block or hook or wrapper for a block. They are given the lowest precedence when merging options.
|
14
|
+
|
15
|
+
### Global Runtime Options
|
16
|
+
{% highlight erb %}
|
17
|
+
Blocks.configure do |config|
|
18
|
+
config.
|
19
|
+
global_options_set.
|
20
|
+
add_options runtime: {
|
21
|
+
a: 1, b: 2
|
22
|
+
}
|
23
|
+
end
|
24
|
+
{% endhighlight %}
|
25
|
+
|
26
|
+
### Global Standard Options
|
27
|
+
{% highlight erb %}
|
28
|
+
Blocks.configure do |config|
|
29
|
+
config.
|
30
|
+
global_options_set.
|
31
|
+
add_options a: 1, b: 2
|
32
|
+
end
|
33
|
+
{% endhighlight %}
|
34
|
+
|
35
|
+
### Global Default Options
|
36
|
+
{% highlight erb %}
|
37
|
+
Blocks.configure do |config|
|
38
|
+
config.
|
39
|
+
global_options_set.
|
40
|
+
add_options defaults: {
|
41
|
+
a: 1, b: 2
|
42
|
+
}
|
43
|
+
end
|
44
|
+
{% endhighlight %}
|
45
|
+
|
46
|
+
## Configuring Caller ID
|
47
|
+
{% highlight erb %}
|
48
|
+
Blocks.configure do |config|
|
49
|
+
config.lookup_caller_location = true
|
50
|
+
end
|
51
|
+
{% endhighlight %}
|
52
|
+
|
53
|
+
Caller ID is a debugging feature that is turned off by default, and should really only ever be turned on in Development mode and perhaps Test mode. It is enabled by running the configuration code to the right. Setting this flag to true will noticeably affect the execution speed of page rendering. This is because for every option that is added to a Block, a Proxy to a Block, or when rendering a block, the code will figure out what line of code triggered the setting of that option.
|
54
|
+
|
55
|
+
## Configuring the Builder Class
|
56
|
+
{% highlight erb %}
|
57
|
+
Blocks.configure do |config|
|
58
|
+
config.builder_class = LayoutBuilder
|
59
|
+
end
|
60
|
+
{% endhighlight %}
|
61
|
+
|
62
|
+
The Builder Class is Blocks::Builder by default, but can be changed to something else using this configuration option. Whatever it is changed to, the new class should be a subclass of Blocks::Builder. Configuring the Builder Class is useful when you want to add in block definitions or shared functionality for the entire application.
|
@@ -1,3 +1,36 @@
|
|
1
1
|
# Custom Builders
|
2
2
|
|
3
|
-
|
3
|
+
{% highlight ruby %}
|
4
|
+
class MyCustomBuilder < Blocks.builder_class
|
5
|
+
def initialize(view, options={})
|
6
|
+
super view, options
|
7
|
+
|
8
|
+
# Additional initialization /
|
9
|
+
# block definitions could happen here
|
10
|
+
end
|
11
|
+
end
|
12
|
+
{% endhighlight %}
|
13
|
+
|
14
|
+
{% highlight ruby %}
|
15
|
+
# From a controller action:
|
16
|
+
builder = MyCustomBuilder.new(view_context)
|
17
|
+
builder.define :some_block do
|
18
|
+
"Hello"
|
19
|
+
end
|
20
|
+
builder.render :some_block
|
21
|
+
{% endhighlight %}
|
22
|
+
|
23
|
+
> This will output "Hello"
|
24
|
+
|
25
|
+
Blocks::Builder is the main class for storing information about block definitions and their corresponding hooks and wrappers.
|
26
|
+
|
27
|
+
Wherever a Block is defined, or a hook is registered for a Block, or a block is skipped, or a Block is rendered, all actions will be executed on an instance of Blocks::Builder.
|
28
|
+
|
29
|
+
When the "blocks" keyword is called from the view for the first time, it will instantiate a new instance of a Blocks::Builder (or a subclass of Blocks::Builder if the Blocks.builder_class is configured to something else - [See Configuring the Builder Class](#configuring-the-builder-class)).
|
30
|
+
|
31
|
+
A custom builder is just a subclass of Blocks::Builder. Instead of extending the Blocks::Builder class directly though, it is usually better to extend Blocks.builder_class (which will be Blocks::Builder unless it has been configured to something else). The one general exception to this rule is on the class that is configured to be the Blocks.builder_class - this should extend Blocks::Builder directly.
|
32
|
+
|
33
|
+
If #initialize is overridden in the subclass, it must, at a minimum call super with a reference to the view or view_context, and optionally, the init options hash.
|
34
|
+
|
35
|
+
## Custom Builders with Helper Methods
|
36
|
+
|
data/docs/_includes/hooks.md
CHANGED
@@ -1111,7 +1111,63 @@ end
|
|
1111
1111
|
|
1112
1112
|
## With Options
|
1113
1113
|
|
1114
|
-
|
1114
|
+
```erb
|
1115
|
+
<% blocks.define :my_block,
|
1116
|
+
a: "Block def",
|
1117
|
+
b: "Block def" %>
|
1118
|
+
|
1119
|
+
<% blocks.around :my_block,
|
1120
|
+
a: "Hook def",
|
1121
|
+
c: "Hook def" do |block, options| %>
|
1122
|
+
Options are <%= options.inspect %>
|
1123
|
+
<%= block.call %>
|
1124
|
+
<% end %>
|
1125
|
+
|
1126
|
+
<%= blocks.render :my_block %>
|
1127
|
+
```
|
1128
|
+
|
1129
|
+
```haml
|
1130
|
+
- blocks.define :my_block,
|
1131
|
+
a: "Block def",
|
1132
|
+
b: "Block def"
|
1133
|
+
|
1134
|
+
- blocks.around :my_block,
|
1135
|
+
a: "Hook def",
|
1136
|
+
c: "Hook def" do |block, options|
|
1137
|
+
Options are
|
1138
|
+
= options.inspect
|
1139
|
+
= block.call
|
1140
|
+
|
1141
|
+
= blocks.render :my_block
|
1142
|
+
```
|
1143
|
+
|
1144
|
+
```ruby
|
1145
|
+
# where builder is an instance
|
1146
|
+
# of Blocks::Builder
|
1147
|
+
builder.define :my_block,
|
1148
|
+
a: "Block def",
|
1149
|
+
b: "Block def"
|
1150
|
+
|
1151
|
+
builder.around :my_block,
|
1152
|
+
a: "Hook def",
|
1153
|
+
c: "Hook def" do |block, options|
|
1154
|
+
|
1155
|
+
"Options are #{options.inspect} #{block.call}"
|
1156
|
+
end
|
1157
|
+
builder.render :my_block
|
1158
|
+
```
|
1159
|
+
|
1160
|
+
> When rendered, this will produce the following output:
|
1161
|
+
|
1162
|
+
```
|
1163
|
+
Options are {
|
1164
|
+
"a"=>"Hook def",
|
1165
|
+
"c"=>"Hook def",
|
1166
|
+
"b"=>"Block def"
|
1167
|
+
}
|
1168
|
+
```
|
1169
|
+
|
1170
|
+
Just as Blocks may be defined with options, so too may hooks be defined with options. When the hook is rendered, these options will take a higher merge precedence than the options that were defined on the block itself. They will however take a lower merge precedence than any render items that were specified when the render call was made for the block being hooked (however, any of the reserved-keywords that are sent to the render call will have already been stripped out).
|
1115
1171
|
|
1116
1172
|
## With a Partial
|
1117
1173
|
|
data/docs/_includes/rendering.md
CHANGED
@@ -320,7 +320,111 @@ When the block definition and the render options share a duplicate key with hash
|
|
320
320
|
|
321
321
|
## With Parameters
|
322
322
|
|
323
|
-
|
323
|
+
```erb
|
324
|
+
<% blocks.define :my_block do |param_1, param_2, param_3, param_4| %>
|
325
|
+
Param 1: <%= param_1.inspect %>
|
326
|
+
<br>
|
327
|
+
Param 2: <%= param_2.inspect %>
|
328
|
+
<br>
|
329
|
+
Param 3: <%= param_3.inspect %>
|
330
|
+
<br>
|
331
|
+
Param 4: <%= param_4.inspect %>
|
332
|
+
<br>
|
333
|
+
<% end %>
|
334
|
+
|
335
|
+
Without a Collection:
|
336
|
+
<br>
|
337
|
+
<%= blocks.render :my_block,
|
338
|
+
"foo", "bar", a: 1, b: 2 %>
|
339
|
+
<br>
|
340
|
+
With a Collection:
|
341
|
+
<br>
|
342
|
+
<%= blocks.render :my_block,
|
343
|
+
"foo", "bar", a: 1, b: 2,
|
344
|
+
collection: ["Item1", "Item2"] %>
|
345
|
+
```
|
346
|
+
|
347
|
+
```haml
|
348
|
+
- blocks.define :my_block do |param_1,
|
349
|
+
param_2, param_3, param_4|
|
350
|
+
Param 1:
|
351
|
+
= param_1.inspect
|
352
|
+
%br
|
353
|
+
Param 2:
|
354
|
+
= param_2.inspect
|
355
|
+
%br
|
356
|
+
Param 3:
|
357
|
+
= param_3.inspect
|
358
|
+
%br
|
359
|
+
Param 4:
|
360
|
+
= param_4.inspect
|
361
|
+
%br
|
362
|
+
|
363
|
+
Without a Collection:
|
364
|
+
%br
|
365
|
+
= blocks.render :my_block,
|
366
|
+
"foo", "bar", a: 1, b: 2
|
367
|
+
%br
|
368
|
+
With a Collection:
|
369
|
+
%br
|
370
|
+
= blocks.render :my_block,
|
371
|
+
"foo", "bar", a: 1, b: 2,
|
372
|
+
collection: ["Item1", "Item2"]
|
373
|
+
```
|
374
|
+
|
375
|
+
```ruby
|
376
|
+
# where builder is an instance
|
377
|
+
# of Blocks::Builder
|
378
|
+
builder.define :my_block do |param_1,
|
379
|
+
param_2, param_3, param_4|
|
380
|
+
"Param 1: #{param_1.inspect}<br>".html_safe +
|
381
|
+
"Param 2: #{param_2.inspect}<br>".html_safe +
|
382
|
+
"Param 3: #{param_3.inspect}<br>".html_safe +
|
383
|
+
"Param 4: #{param_4.inspect}".html_safe +
|
384
|
+
end
|
385
|
+
|
386
|
+
"Without a Collection:<br>".html_safe +
|
387
|
+
builder.render(:my_block,
|
388
|
+
"foo", "bar", a: 1, b: 2) +
|
389
|
+
"<br>With a Collection:<br>".html_safe +
|
390
|
+
builder.render(:my_block,
|
391
|
+
"foo", "bar", a: 1, b: 2,
|
392
|
+
collection: ["Item1", "Item2"])
|
393
|
+
```
|
394
|
+
|
395
|
+
> This will produce the following output
|
396
|
+
|
397
|
+
```
|
398
|
+
Without a Collection:
|
399
|
+
Param 1: "foo"
|
400
|
+
Param 2: "bar"
|
401
|
+
Param 3: {"a"=>1, "b"=>2}
|
402
|
+
Param 4: nil
|
403
|
+
|
404
|
+
With a Collection:
|
405
|
+
Param 1: "Item1"
|
406
|
+
Param 2: "foo"
|
407
|
+
Param 3: "bar"
|
408
|
+
Param 4: {"a"=>1, "b"=>2, "object"=>"Item1", "current_index"=>0}
|
409
|
+
Param 1: "Item2"
|
410
|
+
Param 2: "foo"
|
411
|
+
Param 3: "bar"
|
412
|
+
Param 4: {"a"=>1, "b"=>2, "object"=>"Item2", "current_index"=>1}
|
413
|
+
```
|
414
|
+
|
415
|
+
Blocks are also capable of taking additional parameters besides potentially the options and the item if a collection is being rendered.
|
416
|
+
|
417
|
+
Though this concept is still relatively limited in its potential uses at this time (and some additional use cases probably still need to be hammered out), it can be used to pass an arbitrary number of additional parameters to a defined block.
|
418
|
+
|
419
|
+
At this time, this will not do anything meaningful if the Block is defined to render a partial. However, if the Block is defined to be a Ruby block or a Proxy to a Block defined with a Ruby block, Blocks will attempt to match up the number of arguments the block expects with the number of arguments that could be sent to it from the render call.
|
420
|
+
|
421
|
+
Remember, options will always be the last argument, and if rendering a collection, item will be the first. Any additional params passed to the render call will then be sent as the second argument onward, followed by the options hash. However, if less arguments are expected by the block than are to be sent, Blocks will send the less number of arguments.
|
422
|
+
|
423
|
+
See the example to the right to make better sense out of what is being explained here.
|
424
|
+
|
425
|
+
<aside class="notice">
|
426
|
+
Take note that passing a collection into the render call changes the first parameter to the block to the item in the collection and shifts the rest of the arguments over.
|
427
|
+
</aside>
|
324
428
|
|
325
429
|
## With a Collection
|
326
430
|
|
data/docs/index.md
CHANGED
@@ -140,7 +140,7 @@ module Blocks
|
|
140
140
|
[
|
141
141
|
parent_runtime_context.render_options_set.clone,
|
142
142
|
block_options_set,
|
143
|
-
parent_runtime_context.block_options_set.clone,
|
143
|
+
parent_runtime_context.block_options_set.try(:clone),
|
144
144
|
# TODO: figure out how to deal with these - they don't technically belong here
|
145
145
|
parent_runtime_context.proxy_options_set.clone,
|
146
146
|
builder_options_set,
|
data/lib/blocks/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blocks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0.
|
4
|
+
version: 3.0.0.rc6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Hunter
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: call_with_params
|