unmagic-component-partial 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +31 -2
- data/lib/unmagic/component_partial/partial.rb +13 -5
- data/lib/unmagic/component_partial/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 529eef7a5ff2d79dd21a4b688f5cece6e3a90ffc93dc1300cfba899d91c70868
|
|
4
|
+
data.tar.gz: 8d35fb97bf0bfeb954cabac8a8bb8008cc602c261858aecb78f8c0ef1c78c44d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4a467474e28eb41b2dd0aae1197aac4c68f63ea0cf1a71b6d2ab4c2990952cf35fe5a8073ee35ab41e902e677978c0107ce6837debfd77213674d3066db446dc
|
|
7
|
+
data.tar.gz: 24134491954e79fb9e9e325df051f6b752df5274fd4c3b2658e4008f81b81adb5b1579fd72a504c742332dcf95f419e24889bf79326670b5eb07431f171fbc8a
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.2.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- Props on slots: `content_for` accepts keyword arguments
|
|
14
|
+
(`content_for :footer, align: :right do … end`) and the partial reads them
|
|
15
|
+
back with `props_for(:footer)`. Props merge across successive writes and may
|
|
16
|
+
be passed on their own to attach data without content. Existing `content_for`
|
|
17
|
+
read/write behaviour is unchanged.
|
|
18
|
+
|
|
10
19
|
## [0.1.0]
|
|
11
20
|
|
|
12
21
|
### Added
|
data/README.md
CHANGED
|
@@ -59,13 +59,42 @@ Inside the partial, grab a handle with `component_partial`, render the body with
|
|
|
59
59
|
The body block and the slot blocks are the *same* block, so the slots see the
|
|
60
60
|
caller's locals and helpers exactly as the body does.
|
|
61
61
|
|
|
62
|
+
## Props
|
|
63
|
+
|
|
64
|
+
A slot can carry **props** — structured data the partial reads back to decide
|
|
65
|
+
*how* to render the slot. Pass them as keyword arguments when filling the slot:
|
|
66
|
+
|
|
67
|
+
```erb
|
|
68
|
+
<% card.content_for :footer, align: :right do %>
|
|
69
|
+
<%= form.submit "Save" %>
|
|
70
|
+
<% end %>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Read them back with `props_for`:
|
|
74
|
+
|
|
75
|
+
```erb
|
|
76
|
+
<% if card.content_for(:footer) %>
|
|
77
|
+
<footer class="card__footer card__footer--<%= card.props_for(:footer)[:align] %>">
|
|
78
|
+
<%= card.content_for(:footer) %>
|
|
79
|
+
</footer>
|
|
80
|
+
<% end %>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`props_for` returns an empty hash for a slot that has no props, so indexing into
|
|
84
|
+
it is always safe.
|
|
85
|
+
|
|
62
86
|
## API
|
|
63
87
|
|
|
64
88
|
- `component_partial` — returns a fresh `Partial` bound to the current view.
|
|
65
|
-
- `Partial#content_for(name, content = nil, &block)`:
|
|
89
|
+
- `Partial#content_for(name, content = nil, **props, &block)`:
|
|
66
90
|
- with a string or block, **writes** the slot (successive writes to the same
|
|
67
91
|
slot append), and returns `nil`;
|
|
68
|
-
-
|
|
92
|
+
- `props` attach data to the slot (successive writes merge, last key winning),
|
|
93
|
+
and may be passed on their own to write props without content;
|
|
94
|
+
- with no content, block, or props, **reads** the slot back, or `nil` if it was
|
|
95
|
+
never set.
|
|
96
|
+
- `Partial#props_for(name)` — reads the props attached to a slot, or `{}` if none
|
|
97
|
+
were set.
|
|
69
98
|
|
|
70
99
|
## License
|
|
71
100
|
|
|
@@ -7,24 +7,32 @@ module Unmagic
|
|
|
7
7
|
module ComponentPartial
|
|
8
8
|
# A handle yielded to the block of a partial rendered as a layout. The block
|
|
9
9
|
# fills named slots; the partial reads them back wherever it likes — for
|
|
10
|
-
# content that isn't the main body, e.g. a card footer.
|
|
10
|
+
# content that isn't the main body, e.g. a card footer. A slot can also
|
|
11
|
+
# carry props: structured data the partial reads back with #props_for.
|
|
11
12
|
class Partial
|
|
12
13
|
def initialize(view_context)
|
|
13
14
|
@view_context = view_context
|
|
14
15
|
@contents = Hash.new { |h, k| h[k] = ActiveSupport::SafeBuffer.new }
|
|
16
|
+
@props = {}
|
|
15
17
|
end
|
|
16
18
|
|
|
17
|
-
# Write a slot (string or block), or read it back
|
|
18
|
-
#
|
|
19
|
-
def content_for(name, content = nil, &block)
|
|
20
|
-
if content || block
|
|
19
|
+
# Write a slot (string or block, with optional props), or read it back
|
|
20
|
+
# when called with nothing. Reading an unset slot returns nil.
|
|
21
|
+
def content_for(name, content = nil, **props, &block)
|
|
22
|
+
if content || block || props.any?
|
|
21
23
|
content = @view_context.capture(&block) if block
|
|
22
24
|
@contents[name] << content.to_s
|
|
25
|
+
(@props[name] ||= {}).merge!(props) if props.any?
|
|
23
26
|
nil
|
|
24
27
|
else
|
|
25
28
|
@contents[name].presence
|
|
26
29
|
end
|
|
27
30
|
end
|
|
31
|
+
|
|
32
|
+
# Read the props attached to a slot, or an empty hash when none were set.
|
|
33
|
+
def props_for(name)
|
|
34
|
+
@props[name] || {}
|
|
35
|
+
end
|
|
28
36
|
end
|
|
29
37
|
end
|
|
30
38
|
end
|