blocks 3.0.2 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.ruby-version +1 -1
- data/.travis.yml +61 -17
- data/CHANGELOG.rdoc +27 -0
- data/Gemfile +11 -10
- data/Guardfile +1 -0
- data/README.md +2 -0
- data/bin/deploy_docs +1 -1
- data/blocks.gemspec +3 -1
- data/docs/_includes/configuration.md +3 -6
- data/docs/_includes/defining.md +7 -7
- data/docs/_includes/{introduction.md → features.md} +2 -2
- data/docs/_includes/helper-blocks.md +5 -0
- data/docs/_includes/helper-blocks/content_tag.md +44 -0
- data/docs/_includes/installation.md +16 -2
- data/docs/_includes/overview.md +21 -0
- data/docs/_includes/templating.md +1 -1
- data/docs/_includes/templating/bootstrap_4_cards.md +9 -9
- data/docs/_layouts/slate.html +11 -9
- data/docs/index.md +3 -1
- data/gemfiles/Gemfile.rails-3-0-stable +1 -0
- data/gemfiles/Gemfile.rails-3-1-stable +1 -0
- data/gemfiles/Gemfile.rails-3-2-stable +1 -0
- data/gemfiles/Gemfile.rails-4-0-stable +2 -1
- data/gemfiles/Gemfile.rails-4-1-stable +2 -1
- data/gemfiles/Gemfile.rails-4-2-stable +2 -1
- data/gemfiles/Gemfile.rails-5-0-stable +4 -3
- data/gemfiles/Gemfile.rails-5-1-stable +4 -3
- data/gemfiles/Gemfile.rails-5-2-stable +13 -0
- data/lib/blocks.rb +37 -29
- data/lib/blocks/builders/block_definition.rb +45 -43
- data/lib/blocks/builders/builder.rb +96 -60
- data/lib/blocks/builders/hook_definition.rb +19 -4
- data/lib/blocks/engine.rb +14 -0
- data/lib/blocks/helpers/controller_extensions.rb +13 -0
- data/lib/blocks/helpers/haml_capture.rb +44 -0
- data/lib/blocks/{action_view_extensions → helpers}/view_extensions.rb +10 -4
- data/lib/blocks/renderers/adjacent_blocks_renderer.rb +9 -7
- data/lib/blocks/renderers/block_placeholder.rb +2 -0
- data/lib/blocks/renderers/block_renderer.rb +26 -5
- data/lib/blocks/renderers/block_with_hooks_renderer.rb +29 -19
- data/lib/blocks/renderers/collection_renderer.rb +18 -6
- data/lib/blocks/renderers/nesting_blocks_renderer.rb +9 -11
- data/lib/blocks/renderers/partial_renderer.rb +16 -14
- data/lib/blocks/renderers/renderer.rb +9 -24
- data/lib/blocks/renderers/runtime_context.rb +175 -147
- data/lib/blocks/renderers/wrapper_renderer.rb +21 -10
- data/lib/blocks/utilities/configurator.rb +30 -6
- data/lib/blocks/utilities/hash_with_caller.rb +36 -32
- data/lib/blocks/utilities/hash_with_render_strategy.rb +67 -19
- data/lib/blocks/utilities/options_set.rb +38 -63
- data/lib/blocks/version.rb +3 -1
- metadata +23 -22
- data/docs/_includes/wip.md +0 -34
- data/lib/blocks/experimental/builder_permissions.rb +0 -52
- data/lib/blocks/experimental/invalid_permissions_handler.rb +0 -27
- data/lib/blocks/renderers/abstract_renderer.rb +0 -69
- data/lib/blocks/utilities/dynamic_configuration.rb +0 -71
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4d09904f4e9227a5c2087bfe8479106f5b82ba9c04f3fa44f679e30eae649e0c
|
4
|
+
data.tar.gz: 985af7cb5d86cc4a742afe5779043fb32598258fdc1d3ea2c8d9f7958005f689
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14dd1a0f2352f151d2b7076ab9909e546c1cbfb0522894f79344bfbd8cd20b4a8b0902bf3f67097b2b82a7ab35ce793c7a69a1de2dbcdde0d46f9797e0898d5f
|
7
|
+
data.tar.gz: a8d7ecfaf7aded7699f59508a4f0112d2e863861a3b28369c00ba9e97cd55355cf158064e50bcaef87d950781aa8f2816066cb0721c048ba79d150959254bc77
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.7.0
|
data/.travis.yml
CHANGED
@@ -2,15 +2,26 @@ language: ruby
|
|
2
2
|
cache: bundler
|
3
3
|
|
4
4
|
before_install:
|
5
|
-
-
|
6
|
-
|
5
|
+
- function version { echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'; };
|
6
|
+
if [ $(version $TRAVIS_RUBY_VERSION) -ge $(version '2.7.0') ];
|
7
|
+
then
|
8
|
+
gem install "rubygems-update" --no-document;
|
9
|
+
update_rubygems;
|
10
|
+
gem install bundler -v 1.17.3;
|
11
|
+
else
|
12
|
+
gem install "rubygems-update:<3.0.0" --no-document;
|
13
|
+
update_rubygems;
|
14
|
+
gem install bundler -v 1.17.3;
|
15
|
+
fi
|
7
16
|
rvm:
|
8
|
-
- 1.9.3
|
9
17
|
- 2.0.0
|
10
18
|
- 2.1.10
|
11
|
-
- 2.2.
|
12
|
-
- 2.3.
|
13
|
-
- 2.4.
|
19
|
+
- 2.2.10
|
20
|
+
- 2.3.8
|
21
|
+
- 2.4.9
|
22
|
+
- 2.5.6
|
23
|
+
- 2.6.5
|
24
|
+
- 2.7.0
|
14
25
|
|
15
26
|
gemfile:
|
16
27
|
- gemfiles/Gemfile.rails-3-0-stable
|
@@ -21,36 +32,69 @@ gemfile:
|
|
21
32
|
- gemfiles/Gemfile.rails-4-2-stable
|
22
33
|
- gemfiles/Gemfile.rails-5-0-stable
|
23
34
|
- gemfiles/Gemfile.rails-5-1-stable
|
35
|
+
- gemfiles/Gemfile.rails-5-2-stable
|
24
36
|
- Gemfile
|
25
37
|
matrix:
|
26
|
-
allow_failures:
|
27
|
-
- rvm: ruby-head
|
28
38
|
exclude:
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
gemfile: gemfiles/Gemfile.rails-5-0-stable
|
33
|
-
- rvm: 1.9.3
|
34
|
-
gemfile: Gemfile
|
39
|
+
# Exclude Rails 5 from Ruby < 2.3
|
40
|
+
- rvm: 2.0.0
|
41
|
+
gemfile: gemfiles/Gemfile.rails-5-2-stable
|
35
42
|
- rvm: 2.0.0
|
36
43
|
gemfile: gemfiles/Gemfile.rails-5-1-stable
|
37
44
|
- rvm: 2.0.0
|
38
45
|
gemfile: gemfiles/Gemfile.rails-5-0-stable
|
39
|
-
- rvm: 2.
|
40
|
-
gemfile: Gemfile
|
46
|
+
- rvm: 2.1.10
|
47
|
+
gemfile: gemfiles/Gemfile.rails-5-2-stable
|
41
48
|
- rvm: 2.1.10
|
42
49
|
gemfile: gemfiles/Gemfile.rails-5-1-stable
|
43
50
|
- rvm: 2.1.10
|
44
51
|
gemfile: gemfiles/Gemfile.rails-5-0-stable
|
52
|
+
- rvm: 2.2.10
|
53
|
+
gemfile: gemfiles/Gemfile.rails-5-2-stable
|
54
|
+
- rvm: 2.2.10
|
55
|
+
gemfile: gemfiles/Gemfile.rails-5-1-stable
|
56
|
+
- rvm: 2.2.10
|
57
|
+
gemfile: gemfiles/Gemfile.rails-5-0-stable
|
58
|
+
# Exclude Rails 6 from Ruby < 2.5
|
59
|
+
- rvm: 2.0.0
|
60
|
+
gemfile: Gemfile
|
45
61
|
- rvm: 2.1.10
|
46
62
|
gemfile: Gemfile
|
63
|
+
- rvm: 2.2.10
|
64
|
+
gemfile: Gemfile
|
65
|
+
- rvm: 2.3.8
|
66
|
+
gemfile: Gemfile
|
67
|
+
- rvm: 2.4.9
|
68
|
+
gemfile: Gemfile
|
69
|
+
# Exclude Rails 3.0 from Ruby => 2.5
|
70
|
+
- rvm: 2.5.6
|
71
|
+
gemfile: gemfiles/Gemfile.rails-3-0-stable
|
72
|
+
- rvm: 2.6.5
|
73
|
+
gemfile: gemfiles/Gemfile.rails-3-0-stable
|
74
|
+
# Exclude Rails 3 and 4 from Ruby 2.7, as they require
|
75
|
+
# Bunder < 2, and Ruby 2.7 ships with Bundler 2.1.2, and
|
76
|
+
# I can't seem to force it to use an earlier version
|
77
|
+
- rvm: 2.7.0
|
78
|
+
gemfile: gemfiles/Gemfile.rails-3-0-stable
|
79
|
+
- rvm: 2.7.0
|
80
|
+
gemfile: gemfiles/Gemfile.rails-3-1-stable
|
81
|
+
- rvm: 2.7.0
|
82
|
+
gemfile: gemfiles/Gemfile.rails-3-2-stable
|
83
|
+
- rvm: 2.7.0
|
84
|
+
gemfile: gemfiles/Gemfile.rails-4-0-stable
|
85
|
+
- rvm: 2.7.0
|
86
|
+
gemfile: gemfiles/Gemfile.rails-4-1-stable
|
87
|
+
- rvm: 2.7.0
|
88
|
+
gemfile: gemfiles/Gemfile.rails-4-2-stable
|
89
|
+
|
47
90
|
script: 'bundle exec rake'
|
48
91
|
|
49
92
|
jobs:
|
50
93
|
include:
|
94
|
+
# ./bin/deploy_docs will exit if the current branch is not master
|
51
95
|
- stage: documentation
|
52
96
|
script: './bin/deploy_docs'
|
53
|
-
rvm: 2.
|
97
|
+
rvm: 2.6.5
|
54
98
|
gemfile: Gemfile
|
55
99
|
|
56
100
|
notifications:
|
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,30 @@
|
|
1
|
+
3.1.1
|
2
|
+
* Injecting blocks method into controller to be able to define blocks from the controller that the view will use.
|
3
|
+
|
4
|
+
3.1.0
|
5
|
+
* Added content_tag utility block, wrapping around Rails' content_tag method, which
|
6
|
+
may be used directly or indirectly (such as via a wrapper or hook)
|
7
|
+
* Removed dead / outdated experimental code
|
8
|
+
* Removed dynamic configurator (which was really just an almost copy of Rails' class_attribute)
|
9
|
+
of Blocks in favor of mattr_accessor
|
10
|
+
* Removed Ruby 1.9.3 support, now requires >= 2.0
|
11
|
+
* Added Extensive Test coverage
|
12
|
+
* Deprecated Blocks::Builder#render_with_overrides and made it an alias for Blocks::Builder#render
|
13
|
+
* Added GTM to documentation
|
14
|
+
* Added a Rails Engine for autoconfiguration of eager_loading options
|
15
|
+
* Moved and deprecated content_tag_wrapper utility block into Blocks::LegacyBuilders
|
16
|
+
* Hooks and Wrappers now may render with their own sets of Hooks and Wrappers
|
17
|
+
* Cleaned and fixed logic around generation / merging of options for hooks and
|
18
|
+
and wrappers of a block
|
19
|
+
* Moved Haml Extensions into a separate module that only executes if Haml is defined
|
20
|
+
* Deprecated with_template method injected into ActionView::Base
|
21
|
+
* Moved Blocks::HashWithCaller into a module and now conditionally inject based
|
22
|
+
on global configuration
|
23
|
+
* Switched Blocks::HashWithRenderStrategy to use symbols for hash keys instead of string,
|
24
|
+
allowing it to be extracted as hash keyword arguments to methods
|
25
|
+
* Removed blocks.define :collection functionality
|
26
|
+
|
27
|
+
|
1
28
|
3.0.0 (September 27, 2017)
|
2
29
|
* Complete rewrite of the blocks gem
|
3
30
|
* Extensive test coverage added
|
data/Gemfile
CHANGED
@@ -2,24 +2,25 @@ source "https://rubygems.org"
|
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
-
|
5
|
+
# Requires min Ruby version 2.3
|
6
|
+
|
7
|
+
gem "nokogiri", "1.10.7"
|
6
8
|
gem "capybara"
|
7
9
|
gem "rspec", "~> 3.6.0"
|
8
|
-
gem "
|
9
|
-
gem "rails", github: "rails/rails"
|
10
|
+
gem "rspec-its", "~> 1.2.0"
|
11
|
+
gem "rails", github: "rails/rails", branch: "6-0-stable"
|
10
12
|
gem "rack"
|
11
|
-
gem "shoulda-matchers", "
|
12
|
-
gem "haml"
|
13
|
+
gem "shoulda-matchers", "3.1.3"
|
14
|
+
gem "haml", "~> 5.0.0"
|
13
15
|
|
14
16
|
# Extra development utilities
|
15
|
-
gem "ruby_dep"
|
16
|
-
gem "guard-rspec", "~> 4.7"
|
17
|
-
gem "terminal-notifier-guard"
|
18
|
-
gem "byebug"
|
19
17
|
gem "codeclimate-test-reporter", "~> 1.0.8"
|
20
18
|
|
21
19
|
group :development, :test do
|
20
|
+
gem "terminal-notifier-guard"
|
21
|
+
gem "guard-rspec", "~> 4.7"
|
22
|
+
gem "byebug"
|
22
23
|
gem "jekyll"
|
23
24
|
gem "jekyll-feed", "~> 0.6"
|
24
25
|
gem "jekyll-coffeescript"
|
25
|
-
end
|
26
|
+
end
|
data/Guardfile
CHANGED
@@ -9,6 +9,7 @@ rspec_options = {
|
|
9
9
|
|
10
10
|
guard :rspec, cmd: "bundle exec rspec" do
|
11
11
|
watch(%r{^spec/.+_spec\.rb$})
|
12
|
+
watch(%r{^spec/support/.+\.rb$}) { "spec" }
|
12
13
|
watch(%r{^lib/blocks/(.+)\.rb$}) { |m| ["spec/features", "spec/unit/#{m[1]}_spec.rb"] }
|
13
14
|
watch(%r{^lib/blocks.rb$}) { "spec" }
|
14
15
|
watch('spec/spec_helper.rb') { "spec" }
|
data/README.md
CHANGED
data/bin/deploy_docs
CHANGED
data/blocks.gemspec
CHANGED
@@ -27,8 +27,10 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib"]
|
29
29
|
|
30
|
-
spec.add_dependency "call_with_params"
|
31
30
|
spec.add_dependency "rails", ">= 3.0.0"
|
32
31
|
|
33
32
|
spec.add_development_dependency "simplecov"
|
33
|
+
if RUBY_VERSION >= "2.1"
|
34
|
+
spec.add_development_dependency "memory_profiler"
|
35
|
+
end
|
34
36
|
end
|
@@ -16,8 +16,7 @@ Global Options are merged into the set of options generated when rendering a blo
|
|
16
16
|
{% highlight erb %}
|
17
17
|
Blocks.configure do |config|
|
18
18
|
config.
|
19
|
-
|
20
|
-
add_options runtime: {
|
19
|
+
set_global_options runtime: {
|
21
20
|
a: 1, b: 2
|
22
21
|
}
|
23
22
|
end
|
@@ -27,8 +26,7 @@ end
|
|
27
26
|
{% highlight erb %}
|
28
27
|
Blocks.configure do |config|
|
29
28
|
config.
|
30
|
-
|
31
|
-
add_options a: 1, b: 2
|
29
|
+
set_global_options a: 1, b: 2
|
32
30
|
end
|
33
31
|
{% endhighlight %}
|
34
32
|
|
@@ -36,8 +34,7 @@ end
|
|
36
34
|
{% highlight erb %}
|
37
35
|
Blocks.configure do |config|
|
38
36
|
config.
|
39
|
-
|
40
|
-
add_options defaults: {
|
37
|
+
set_global_options defaults: {
|
41
38
|
a: 1, b: 2
|
42
39
|
}
|
43
40
|
end
|
data/docs/_includes/defining.md
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
# Defining Blocks
|
2
2
|
|
3
|
+
With Blocks, you can define a block of code for later rendering using Ruby blocks, Rails partials, and proxies to other blocks or methods.
|
4
|
+
|
5
|
+
A block consists of a name, an optional hash of options, and a rendering strategy (also called its definition).
|
6
|
+
|
3
7
|
```erb
|
4
8
|
<% blocks.define :my_block %>
|
5
|
-
<!--
|
9
|
+
<!-- SAME AS -->
|
6
10
|
<% blocks.define "my_block" %>
|
7
11
|
```
|
8
12
|
|
9
13
|
```haml
|
10
14
|
- blocks.define :my_block
|
11
|
-
#-
|
15
|
+
#- SAME AS
|
12
16
|
- blocks.define "my_block"
|
13
17
|
```
|
14
18
|
|
@@ -16,14 +20,10 @@
|
|
16
20
|
# where builder is an instance
|
17
21
|
# of Blocks::Builder
|
18
22
|
builder.define :my_block
|
19
|
-
#
|
23
|
+
# SAME AS
|
20
24
|
builder.define "my_block"
|
21
25
|
```
|
22
26
|
|
23
|
-
With Blocks, you can define a block of code for later rendering using Ruby blocks, Rails partials, and proxies to other blocks.
|
24
|
-
|
25
|
-
A block consists of a name, a hash of options, and a rendering strategy (also called its definition).
|
26
|
-
|
27
27
|
A block's name can be a symbol or a string. The underlying system treats symbols and strings the same. Therefore, any block that is defined with a String name can be accessed with its corresponding symbol name and vice-versa.
|
28
28
|
|
29
29
|
## With a Ruby Block
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Features
|
2
2
|
|
3
3
|
**VERSION {% gem_version %}**
|
4
4
|
|
@@ -7,7 +7,7 @@ The [blocks gem](http://github.com/hunterae/blocks) is many things.
|
|
7
7
|
It acts as:
|
8
8
|
|
9
9
|
* a container for reusable blocks of code and options
|
10
|
-
* a common interface for rendering code, whether the code was defined previously in Ruby blocks, Rails partials, or proxies to other blocks of code
|
10
|
+
* a common interface for rendering code, whether the code was defined previously in Ruby blocks, Ruby Methods, Rails partials, or proxies to other blocks of code
|
11
11
|
* a series of hooks and wrappers that can be utilized to render code before, after, and around other blocks of code, as well as before each, after each, and around each item in a collection
|
12
12
|
* a templating utility for easily building reusable and highly customizable UI components
|
13
13
|
* a means for DRYing up oft-repeated code in your layouts and views
|
@@ -0,0 +1,44 @@
|
|
1
|
+
## The :content_tag Block
|
2
|
+
|
3
|
+
> According to Bootstrap's documentation, a standard card has the following markup:
|
4
|
+
|
5
|
+
The :content_tag block is more or less a direct wrapper around [ActionView's content_tag method](https://apidock.com/rails/ActionView/Helpers/TagHelper/content_tag).
|
6
|
+
However, by defining it as a block, it may be used in several useful ways.
|
7
|
+
|
8
|
+
### As the definition for a block
|
9
|
+
|
10
|
+
```erb
|
11
|
+
<% blocks.define :title, with: :content_tag, tag: :h1, html: { style: "color: red" } %>
|
12
|
+
<%= blocks.render :title do %>
|
13
|
+
My Title
|
14
|
+
<% end %>
|
15
|
+
```
|
16
|
+
|
17
|
+
```haml
|
18
|
+
- blocks.define :title, with: :content_tag, tag: :h1, html: { style: "color: red" }
|
19
|
+
= blocks.render :title do
|
20
|
+
My Title
|
21
|
+
```
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
builder = Blocks::Builder.new(view_context)
|
25
|
+
builder.define :title, with: :content_tag, tag: :h1, html: { style: "color: red" }
|
26
|
+
# Since no overrides block is provided, this
|
27
|
+
# call is synonymous with:
|
28
|
+
builder.render :title do
|
29
|
+
"My Title"
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
> The output from running the code will be:
|
34
|
+
{% highlight html %}
|
35
|
+
<h1 style="color: red">
|
36
|
+
My Title
|
37
|
+
</h1>
|
38
|
+
{% endhighlight %}
|
39
|
+
|
40
|
+
When used as the definition for a block (using the with keyword), you may choose to specify the tag to use or any html options to be applied to the tag.
|
41
|
+
Then, you can either provide the definition a block of its own or supply as a render option when calling render.
|
42
|
+
|
43
|
+
### As a wrapper or a surrounding hook for a block
|
44
|
+
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# Installation & Prerequisites
|
2
2
|
|
3
|
-
Blocks requires Rails 3.0 or greater.
|
3
|
+
Blocks requires Rails 3.0 or greater and Ruby 2.0 or greater.
|
4
4
|
|
5
|
-
It has been tested with Ruby
|
5
|
+
It has been tested with Ruby 2.0.0 through 2.5.1 (as well as ruby-head), and Rails 3.0 - 5.2 (as well as Edge Rails)
|
6
6
|
|
7
7
|
```
|
8
8
|
gem 'blocks'
|
@@ -22,4 +22,18 @@ Then run this from your project directory command line:
|
|
22
22
|
|
23
23
|
<aside class="success">
|
24
24
|
In most cases, this will be all you need to do. The "blocks" helper method is now available within your Rails views
|
25
|
+
</aside>
|
26
|
+
|
27
|
+
{% highlight ruby %}
|
28
|
+
if !Object.respond_to?(:yaml_as)
|
29
|
+
class Object
|
30
|
+
def self.yaml_as(*args)
|
31
|
+
yaml_tag(*args)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
{% endhighlight %}
|
36
|
+
|
37
|
+
<aside class="warning">
|
38
|
+
To get Blocks working for Rails 3.0 with a Ruby version of 2.5 or greater, use the following patch:
|
25
39
|
</aside>
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# Overview
|
2
|
+
|
3
|
+
There are many ways to render content to the screen in web applications. Ruby on Rails offers a couple of default strategies right off the bat through its combination of ActionController, ActionView, and ERB templating language:
|
4
|
+
|
5
|
+
* Rendering content within an ActionView template (the controller action) and layout
|
6
|
+
* Pulling in additional content through rendering partials
|
7
|
+
* Capturing content from a block and outputting that content at some later point
|
8
|
+
* Calling a different method from the controller action which determines what and how to render
|
9
|
+
|
10
|
+
There are obviously additional means to render output, such as the redirect methods in a controller and rendering inline code, but generally speaking, most generated output come about through one of those above methods.
|
11
|
+
|
12
|
+
The approaches listed above can essentially be generalized as follows
|
13
|
+
|
14
|
+
* Rendering using a method
|
15
|
+
* Rendering fragments (using a Rails' partial)
|
16
|
+
* Storing content for later rendering (using a Ruby Block)
|
17
|
+
* Proxying to another source that knows what to / how to render
|
18
|
+
|
19
|
+
The Blocks gem attempts to take these four concepts and provide one common interface.
|
20
|
+
|
21
|
+
The reasoning behind this is simple.
|
@@ -55,7 +55,7 @@ Templating is best demonstrated through example. In the following set of iterati
|
|
55
55
|
|
56
56
|
```ruby
|
57
57
|
builder = Blocks::Builder.new(view_context)
|
58
|
-
builder.
|
58
|
+
builder.render partial: "shared/card"
|
59
59
|
# Since no overrides block is provided, this
|
60
60
|
# call is synonymous with:
|
61
61
|
builder.render partial: "shared/card"
|
@@ -215,7 +215,7 @@ Because all of :block_wrapper's options are default options, they are easily ove
|
|
215
215
|
|
216
216
|
:card_block, :card_title, and :card_text each follow the same paradigm. Notice that :card_text overrides the :wrapper_tag to :p and :card_title overrides it to :h4.
|
217
217
|
|
218
|
-
<aside class="notice">This paradigm of using a block wrapper that wraps a block within an HTML element is so common, that Blocks provides a similar block by default called :
|
218
|
+
<aside class="notice">This paradigm of using a block wrapper that wraps a block within an HTML element is so common, that Blocks provides a similar block by default called :content_tag (purposely named to match the ActionView method). If this automatically defined block is used instead, the code to the right would only need to change by removing the :block_wrapper definition and changing all references of :block_wrapper to :content_tag.</aside>
|
219
219
|
|
220
220
|
<aside class="notice">The only two blocks that are not using the :block_wrapper are :card_image and :card_action. While both could utilize the :block_wrapper wrapper, it makes more sense not to do so. This will enable us to utilize Rails' link_to and image_tag helper methods instead, since links and images need a bit more fine-tuning than regular HTML elements.</aside>
|
221
221
|
|
@@ -348,7 +348,7 @@ builder = Blocks::Builder.new(view_context,
|
|
348
348
|
card_text: "My Text",
|
349
349
|
card_action_path: 'http://mobilecause.com',
|
350
350
|
card_image: "my-image.png")
|
351
|
-
builder.
|
351
|
+
builder.render partial: "shared/card"
|
352
352
|
```
|
353
353
|
|
354
354
|
> The above code will output the following:
|
@@ -439,7 +439,7 @@ Now that the template is defined, we can start rendering it with actual override
|
|
439
439
|
|
440
440
|
```ruby
|
441
441
|
builder = Blocks::Builder.new(view_context)
|
442
|
-
text = builder.
|
442
|
+
text = builder.render partial:
|
443
443
|
"shared/card" do |builder|
|
444
444
|
builder.define :card do
|
445
445
|
"I am a complete replacement for the card"
|
@@ -447,7 +447,7 @@ text = builder.render_with_overrides partial:
|
|
447
447
|
end
|
448
448
|
|
449
449
|
builder = Blocks::Builder.new(view_context)
|
450
|
-
text2 = builder.
|
450
|
+
text2 = builder.render partial:
|
451
451
|
"shared/card" do |builder|
|
452
452
|
# Change card_title's tag to h2
|
453
453
|
builder.define :card_title,
|
@@ -607,7 +607,7 @@ Finding the option overrides aren't enough? There's always block overrides. Any
|
|
607
607
|
|
608
608
|
```ruby
|
609
609
|
builder = Blocks::Builder.new(view_context)
|
610
|
-
builder.
|
610
|
+
builder.render partial:
|
611
611
|
"shared/card" do |builder|
|
612
612
|
builder.skip :card_image
|
613
613
|
builder.after :card_title do
|
@@ -662,7 +662,7 @@ We also have access to the full arsenal of hooks, wrapper with relation to the v
|
|
662
662
|
> Assume the following partial exists in /app/views/shared/\_team_card.html.erb:
|
663
663
|
|
664
664
|
{% highlight erb %}
|
665
|
-
<%= builder.
|
665
|
+
<%= builder.render partial:
|
666
666
|
"shared/card" do |builder| %>
|
667
667
|
<% builder.define :card_title,
|
668
668
|
card_title: team.name %>
|
@@ -708,7 +708,7 @@ team = OpenStruct.new(
|
|
708
708
|
description: "Donate money"
|
709
709
|
)
|
710
710
|
builder = Blocks::Builder.new(view_context)
|
711
|
-
builder.
|
711
|
+
builder.render partial:
|
712
712
|
"shared/team_card",
|
713
713
|
team: team do |builder|
|
714
714
|
builder.after :card_title do
|
@@ -749,5 +749,5 @@ An example might be create new templates that render different versions of a car
|
|
749
749
|
In the example to the right, a team-specific version of card is created as a template. Code then renders this new template with some overrides of if its own. The overrides have been kept very basic in order to clearly demonstrate how to setup a template that extends another template.
|
750
750
|
|
751
751
|
<aside class="warning">
|
752
|
-
Take note that with the team_card template, builder.
|
752
|
+
Take note that with the team_card template, builder.render is used instead of render_with_overrides. This is forcing the two templates to render using the same Blocks::Builder instance, i.e. to share a Blocks namespace. This is what allows the overrides block for the team_card template to affect the defaults in card template.
|
753
753
|
</aside>
|