nice_partials 0.9.3 → 0.10.1
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 +62 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +273 -0
- data/README.md +35 -19
- data/lib/nice_partials/monkey_patch.rb +34 -24
- data/lib/nice_partials/partial/section.rb +35 -0
- data/lib/nice_partials/partial.rb +18 -2
- data/lib/nice_partials/version.rb +1 -1
- data/lib/nice_partials.rb +0 -5
- data/nice_partials.gemspec +1 -1
- metadata +5 -5
- data/lib/nice_partials/partial/stack.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e57a9c56f84e3d03132b0b2d6052dc05ee87bf981958c90c65c3ef6edb93588
|
4
|
+
data.tar.gz: 2638ca63ace50ab9ad6659edf1b48f14460f8072479bd19412331e958fe0bf68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c25e3ce3e8ec78ffe0c1d7626aa912d1613735df7e6c0a29e9c33996973f9ef5c6bf8f5d3bfeb2fae84ecde7b8891efde9b3d90765f249e63eefdbd6a3194eb1
|
7
|
+
data.tar.gz: cd5e70bf89695f043bc17036ca3b25d018fa6af7e3ca64f95f8d7d8cc571b9fc19c919d58f901bd35cd659a50fd7e5eba57e5546dc30b1ea883c92ee0622d67e
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,67 @@
|
|
1
1
|
## CHANGELOG
|
2
2
|
|
3
|
+
* Feature: partial's expose `local_assigns` + `locals` alias
|
4
|
+
|
5
|
+
```html+erb
|
6
|
+
<%# app/views/articles/show.html.erb %>
|
7
|
+
<%= render "section", id: "an_article" %>
|
8
|
+
|
9
|
+
<%# app/views/application/_section.html.erb %>
|
10
|
+
<%# We can access the passed `id:` like this: %>
|
11
|
+
<% partial.local_assigns[:id] %>
|
12
|
+
<% partial.locals[:id] %>
|
13
|
+
```
|
14
|
+
|
15
|
+
Note: this is equal to the default partial local variable of `local_assigns`, but it becomes more useful with the next feature below.
|
16
|
+
|
17
|
+
* Feature: partial helpers can access `partial`
|
18
|
+
|
19
|
+
```html+erb
|
20
|
+
<%# app/views/articles/show.html.erb %>
|
21
|
+
<% render "section", id: "an_article" do |section| %>
|
22
|
+
<%= tag.h1 "An Article", id: section.labelledby %>
|
23
|
+
<% end %>
|
24
|
+
|
25
|
+
<%# app/views/application/_section.html.erb %>
|
26
|
+
<%
|
27
|
+
partial.helpers do
|
28
|
+
def aria
|
29
|
+
partial.locals.fetch(:aria, {}).with_defaults(labelledby:)
|
30
|
+
end
|
31
|
+
|
32
|
+
def labelledby
|
33
|
+
id = partial.locals[:id] and "#{id}_label"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
%>
|
37
|
+
|
38
|
+
<%= tag.section partial.yield, id:, aria: partial.aria %>
|
39
|
+
```
|
40
|
+
|
41
|
+
### 0.9.4
|
42
|
+
|
43
|
+
* Feature: declare contents via `required` and `optional`
|
44
|
+
|
45
|
+
```html+erb
|
46
|
+
<% if partial.title? %>
|
47
|
+
<h1 class="text-xl">
|
48
|
+
<%= partial.title %>
|
49
|
+
</h1>
|
50
|
+
<% end %>
|
51
|
+
|
52
|
+
<div><%= partial.body %></div>
|
53
|
+
```
|
54
|
+
|
55
|
+
Can now become:
|
56
|
+
|
57
|
+
```html+erb
|
58
|
+
<%= partial.title.optional.h1 class: "text-xl" %><%# Will not output any HTML element if no content has been provided. %>
|
59
|
+
|
60
|
+
<div><%= partial.body.required %></div> <%# Raises when this line is hit if no content has been provided %>
|
61
|
+
```
|
62
|
+
|
63
|
+
See the README for more.
|
64
|
+
|
3
65
|
### 0.9.3
|
4
66
|
|
5
67
|
* Fixed: section predicates not respecting `local_assigns` content
|
data/Gemfile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
source "https://rubygems.org"
|
2
2
|
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
3
3
|
|
4
|
+
ruby "3.3.3"
|
5
|
+
|
4
6
|
gemspec
|
5
7
|
|
6
8
|
gem "minitest"
|
@@ -15,3 +17,7 @@ end
|
|
15
17
|
|
16
18
|
gem "view_component"
|
17
19
|
gem "capybara"
|
20
|
+
|
21
|
+
gem "debug"
|
22
|
+
|
23
|
+
gem "net-pop", github: "ruby/net-pop" # Declare gem explicitly to workaround bug in Ruby 3.3.3. Ref: https://stackoverflow.com/a/78620570
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,273 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/ruby/net-pop.git
|
3
|
+
revision: e8d0afe2773b9eb6a23c39e9e437f6fc0fc7c733
|
4
|
+
specs:
|
5
|
+
net-pop (0.1.2)
|
6
|
+
|
7
|
+
PATH
|
8
|
+
remote: .
|
9
|
+
specs:
|
10
|
+
nice_partials (0.10.1)
|
11
|
+
actionview (>= 4.2.6)
|
12
|
+
|
13
|
+
GEM
|
14
|
+
remote: https://rubygems.org/
|
15
|
+
specs:
|
16
|
+
actioncable (7.1.3.4)
|
17
|
+
actionpack (= 7.1.3.4)
|
18
|
+
activesupport (= 7.1.3.4)
|
19
|
+
nio4r (~> 2.0)
|
20
|
+
websocket-driver (>= 0.6.1)
|
21
|
+
zeitwerk (~> 2.6)
|
22
|
+
actionmailbox (7.1.3.4)
|
23
|
+
actionpack (= 7.1.3.4)
|
24
|
+
activejob (= 7.1.3.4)
|
25
|
+
activerecord (= 7.1.3.4)
|
26
|
+
activestorage (= 7.1.3.4)
|
27
|
+
activesupport (= 7.1.3.4)
|
28
|
+
mail (>= 2.7.1)
|
29
|
+
net-imap
|
30
|
+
net-pop
|
31
|
+
net-smtp
|
32
|
+
actionmailer (7.1.3.4)
|
33
|
+
actionpack (= 7.1.3.4)
|
34
|
+
actionview (= 7.1.3.4)
|
35
|
+
activejob (= 7.1.3.4)
|
36
|
+
activesupport (= 7.1.3.4)
|
37
|
+
mail (~> 2.5, >= 2.5.4)
|
38
|
+
net-imap
|
39
|
+
net-pop
|
40
|
+
net-smtp
|
41
|
+
rails-dom-testing (~> 2.2)
|
42
|
+
actionpack (7.1.3.4)
|
43
|
+
actionview (= 7.1.3.4)
|
44
|
+
activesupport (= 7.1.3.4)
|
45
|
+
nokogiri (>= 1.8.5)
|
46
|
+
racc
|
47
|
+
rack (>= 2.2.4)
|
48
|
+
rack-session (>= 1.0.1)
|
49
|
+
rack-test (>= 0.6.3)
|
50
|
+
rails-dom-testing (~> 2.2)
|
51
|
+
rails-html-sanitizer (~> 1.6)
|
52
|
+
actiontext (7.1.3.4)
|
53
|
+
actionpack (= 7.1.3.4)
|
54
|
+
activerecord (= 7.1.3.4)
|
55
|
+
activestorage (= 7.1.3.4)
|
56
|
+
activesupport (= 7.1.3.4)
|
57
|
+
globalid (>= 0.6.0)
|
58
|
+
nokogiri (>= 1.8.5)
|
59
|
+
actionview (7.1.3.4)
|
60
|
+
activesupport (= 7.1.3.4)
|
61
|
+
builder (~> 3.1)
|
62
|
+
erubi (~> 1.11)
|
63
|
+
rails-dom-testing (~> 2.2)
|
64
|
+
rails-html-sanitizer (~> 1.6)
|
65
|
+
activejob (7.1.3.4)
|
66
|
+
activesupport (= 7.1.3.4)
|
67
|
+
globalid (>= 0.3.6)
|
68
|
+
activemodel (7.1.3.4)
|
69
|
+
activesupport (= 7.1.3.4)
|
70
|
+
activerecord (7.1.3.4)
|
71
|
+
activemodel (= 7.1.3.4)
|
72
|
+
activesupport (= 7.1.3.4)
|
73
|
+
timeout (>= 0.4.0)
|
74
|
+
activestorage (7.1.3.4)
|
75
|
+
actionpack (= 7.1.3.4)
|
76
|
+
activejob (= 7.1.3.4)
|
77
|
+
activerecord (= 7.1.3.4)
|
78
|
+
activesupport (= 7.1.3.4)
|
79
|
+
marcel (~> 1.0)
|
80
|
+
activesupport (7.1.3.4)
|
81
|
+
base64
|
82
|
+
bigdecimal
|
83
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
84
|
+
connection_pool (>= 2.2.5)
|
85
|
+
drb
|
86
|
+
i18n (>= 1.6, < 2)
|
87
|
+
minitest (>= 5.1)
|
88
|
+
mutex_m
|
89
|
+
tzinfo (~> 2.0)
|
90
|
+
addressable (2.8.7)
|
91
|
+
public_suffix (>= 2.0.2, < 7.0)
|
92
|
+
ast (2.4.2)
|
93
|
+
base64 (0.2.0)
|
94
|
+
bigdecimal (3.1.8)
|
95
|
+
builder (3.3.0)
|
96
|
+
capybara (3.40.0)
|
97
|
+
addressable
|
98
|
+
matrix
|
99
|
+
mini_mime (>= 0.1.3)
|
100
|
+
nokogiri (~> 1.11)
|
101
|
+
rack (>= 1.6.0)
|
102
|
+
rack-test (>= 0.6.3)
|
103
|
+
regexp_parser (>= 1.5, < 3.0)
|
104
|
+
xpath (~> 3.2)
|
105
|
+
concurrent-ruby (1.3.3)
|
106
|
+
connection_pool (2.4.1)
|
107
|
+
crass (1.0.6)
|
108
|
+
date (3.3.4)
|
109
|
+
debug (1.9.2)
|
110
|
+
irb (~> 1.10)
|
111
|
+
reline (>= 0.3.8)
|
112
|
+
drb (2.2.1)
|
113
|
+
erubi (1.13.0)
|
114
|
+
globalid (1.2.1)
|
115
|
+
activesupport (>= 6.1)
|
116
|
+
i18n (1.14.5)
|
117
|
+
concurrent-ruby (~> 1.0)
|
118
|
+
io-console (0.7.2)
|
119
|
+
irb (1.14.0)
|
120
|
+
rdoc (>= 4.0.0)
|
121
|
+
reline (>= 0.4.2)
|
122
|
+
json (2.7.2)
|
123
|
+
language_server-protocol (3.17.0.3)
|
124
|
+
lint_roller (1.1.0)
|
125
|
+
loofah (2.22.0)
|
126
|
+
crass (~> 1.0.2)
|
127
|
+
nokogiri (>= 1.12.0)
|
128
|
+
mail (2.8.1)
|
129
|
+
mini_mime (>= 0.1.1)
|
130
|
+
net-imap
|
131
|
+
net-pop
|
132
|
+
net-smtp
|
133
|
+
marcel (1.0.4)
|
134
|
+
matrix (0.4.2)
|
135
|
+
method_source (1.1.0)
|
136
|
+
mini_mime (1.1.5)
|
137
|
+
minitest (5.24.1)
|
138
|
+
mutex_m (0.2.0)
|
139
|
+
net-imap (0.4.14)
|
140
|
+
date
|
141
|
+
net-protocol
|
142
|
+
net-protocol (0.2.2)
|
143
|
+
timeout
|
144
|
+
net-smtp (0.5.0)
|
145
|
+
net-protocol
|
146
|
+
nio4r (2.7.3)
|
147
|
+
nokogiri (1.16.7-arm64-darwin)
|
148
|
+
racc (~> 1.4)
|
149
|
+
nokogiri (1.16.7-x86_64-linux)
|
150
|
+
racc (~> 1.4)
|
151
|
+
parallel (1.25.1)
|
152
|
+
parser (3.3.4.0)
|
153
|
+
ast (~> 2.4.1)
|
154
|
+
racc
|
155
|
+
psych (5.1.2)
|
156
|
+
stringio
|
157
|
+
public_suffix (6.0.1)
|
158
|
+
racc (1.8.0)
|
159
|
+
rack (3.1.7)
|
160
|
+
rack-session (2.0.0)
|
161
|
+
rack (>= 3.0.0)
|
162
|
+
rack-test (2.1.0)
|
163
|
+
rack (>= 1.3)
|
164
|
+
rackup (2.1.0)
|
165
|
+
rack (>= 3)
|
166
|
+
webrick (~> 1.8)
|
167
|
+
rails (7.1.3.4)
|
168
|
+
actioncable (= 7.1.3.4)
|
169
|
+
actionmailbox (= 7.1.3.4)
|
170
|
+
actionmailer (= 7.1.3.4)
|
171
|
+
actionpack (= 7.1.3.4)
|
172
|
+
actiontext (= 7.1.3.4)
|
173
|
+
actionview (= 7.1.3.4)
|
174
|
+
activejob (= 7.1.3.4)
|
175
|
+
activemodel (= 7.1.3.4)
|
176
|
+
activerecord (= 7.1.3.4)
|
177
|
+
activestorage (= 7.1.3.4)
|
178
|
+
activesupport (= 7.1.3.4)
|
179
|
+
bundler (>= 1.15.0)
|
180
|
+
railties (= 7.1.3.4)
|
181
|
+
rails-dom-testing (2.2.0)
|
182
|
+
activesupport (>= 5.0.0)
|
183
|
+
minitest
|
184
|
+
nokogiri (>= 1.6)
|
185
|
+
rails-html-sanitizer (1.6.0)
|
186
|
+
loofah (~> 2.21)
|
187
|
+
nokogiri (~> 1.14)
|
188
|
+
railties (7.1.3.4)
|
189
|
+
actionpack (= 7.1.3.4)
|
190
|
+
activesupport (= 7.1.3.4)
|
191
|
+
irb
|
192
|
+
rackup (>= 1.0.0)
|
193
|
+
rake (>= 12.2)
|
194
|
+
thor (~> 1.0, >= 1.2.2)
|
195
|
+
zeitwerk (~> 2.6)
|
196
|
+
rainbow (3.1.1)
|
197
|
+
rake (13.2.1)
|
198
|
+
rdoc (6.7.0)
|
199
|
+
psych (>= 4.0.0)
|
200
|
+
regexp_parser (2.9.2)
|
201
|
+
reline (0.5.9)
|
202
|
+
io-console (~> 0.5)
|
203
|
+
rexml (3.3.2)
|
204
|
+
strscan
|
205
|
+
rubocop (1.64.1)
|
206
|
+
json (~> 2.3)
|
207
|
+
language_server-protocol (>= 3.17.0)
|
208
|
+
parallel (~> 1.10)
|
209
|
+
parser (>= 3.3.0.2)
|
210
|
+
rainbow (>= 2.2.2, < 4.0)
|
211
|
+
regexp_parser (>= 1.8, < 3.0)
|
212
|
+
rexml (>= 3.2.5, < 4.0)
|
213
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
214
|
+
ruby-progressbar (~> 1.7)
|
215
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
216
|
+
rubocop-ast (1.31.3)
|
217
|
+
parser (>= 3.3.1.0)
|
218
|
+
rubocop-performance (1.21.1)
|
219
|
+
rubocop (>= 1.48.1, < 2.0)
|
220
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
221
|
+
ruby-progressbar (1.13.0)
|
222
|
+
standard (1.39.2)
|
223
|
+
language_server-protocol (~> 3.17.0.2)
|
224
|
+
lint_roller (~> 1.0)
|
225
|
+
rubocop (~> 1.64.0)
|
226
|
+
standard-custom (~> 1.0.0)
|
227
|
+
standard-performance (~> 1.4)
|
228
|
+
standard-custom (1.0.2)
|
229
|
+
lint_roller (~> 1.0)
|
230
|
+
rubocop (~> 1.50)
|
231
|
+
standard-performance (1.4.0)
|
232
|
+
lint_roller (~> 1.1)
|
233
|
+
rubocop-performance (~> 1.21.0)
|
234
|
+
stringio (3.1.1)
|
235
|
+
strscan (3.1.0)
|
236
|
+
thor (1.3.1)
|
237
|
+
timeout (0.4.1)
|
238
|
+
tzinfo (2.0.6)
|
239
|
+
concurrent-ruby (~> 1.0)
|
240
|
+
unicode-display_width (2.5.0)
|
241
|
+
view_component (3.13.0)
|
242
|
+
activesupport (>= 5.2.0, < 8.0)
|
243
|
+
concurrent-ruby (~> 1.0)
|
244
|
+
method_source (~> 1.0)
|
245
|
+
webrick (1.8.1)
|
246
|
+
websocket-driver (0.7.6)
|
247
|
+
websocket-extensions (>= 0.1.0)
|
248
|
+
websocket-extensions (0.1.5)
|
249
|
+
xpath (3.2.0)
|
250
|
+
nokogiri (~> 1.8)
|
251
|
+
zeitwerk (2.6.16)
|
252
|
+
|
253
|
+
PLATFORMS
|
254
|
+
arm64-darwin
|
255
|
+
x86_64-linux
|
256
|
+
|
257
|
+
DEPENDENCIES
|
258
|
+
capybara
|
259
|
+
debug
|
260
|
+
irb
|
261
|
+
minitest
|
262
|
+
net-pop!
|
263
|
+
nice_partials!
|
264
|
+
rails
|
265
|
+
rake
|
266
|
+
standard
|
267
|
+
view_component
|
268
|
+
|
269
|
+
RUBY VERSION
|
270
|
+
ruby 3.3.3p89
|
271
|
+
|
272
|
+
BUNDLED WITH
|
273
|
+
2.5.16
|
data/README.md
CHANGED
@@ -24,7 +24,7 @@ See, here we're outputting the `image`, `title`, and `body` sections:
|
|
24
24
|
Then in `render` we populate them:
|
25
25
|
|
26
26
|
```html+erb
|
27
|
-
<%= render "components/card"
|
27
|
+
<%= render "components/card" do |partial| %>
|
28
28
|
<% partial.title t(".title") %> # Same as `partial.content_for :title, t(".title")`
|
29
29
|
|
30
30
|
<% partial.body do %>
|
@@ -116,6 +116,40 @@ You can also use `slice` to pass on content from an outer partial:
|
|
116
116
|
<%= render "components/card", partial.slice(:title, :byline) %>
|
117
117
|
```
|
118
118
|
|
119
|
+
### Declaring content as optional or required
|
120
|
+
|
121
|
+
In traditional Rails partials, you'll see lots of checks for whether or not we have content to then output an element.
|
122
|
+
|
123
|
+
With Nice Partials, it would look like this:
|
124
|
+
|
125
|
+
```html+erb
|
126
|
+
<% if partial.title? %>
|
127
|
+
<h1 class="text-xl"><%= partial.title %></h1>
|
128
|
+
<% end %>
|
129
|
+
```
|
130
|
+
|
131
|
+
However, we can remove the conditional using `optional`:
|
132
|
+
|
133
|
+
```html+erb
|
134
|
+
<%= partial.title.optional.then do |title| %>
|
135
|
+
<h1 class="text-xl"><%= title %></h1>
|
136
|
+
<% end %>
|
137
|
+
```
|
138
|
+
|
139
|
+
This will avoid outputting an empty tag, which could mess with your markup, in case there's no content provided for `title`.
|
140
|
+
|
141
|
+
Note: with Nice Partials tag helpers support, this example could also be shortened to `<%= partial.title.optional.h1 class: "text-xl" %>`.
|
142
|
+
|
143
|
+
#### Required
|
144
|
+
|
145
|
+
Alternatively, if `title` is a section that we require to be provided, we can do:
|
146
|
+
|
147
|
+
```html+erb
|
148
|
+
<h1 class="text-xl"><%= partial.title.required %></h1>
|
149
|
+
```
|
150
|
+
|
151
|
+
Here, `required` will raise in case there's been no `title` content provided by that point.
|
152
|
+
|
119
153
|
### Appending content from the view into a section
|
120
154
|
|
121
155
|
Nice Partials supports calling any method on `ActionView::Base`, like the helpers shown here, and then have them auto-append to the section.
|
@@ -182,24 +216,6 @@ But the partial can also prepare tag builders that the rendering block can then
|
|
182
216
|
<% partial.title.yield tag.with_options(class: "text-m4", data: { controller: "title" }) %> # => <h1 class="text-m4" data-controller="title">Title content</h1>
|
183
217
|
```
|
184
218
|
|
185
|
-
### Smoother conditional rendering
|
186
|
-
|
187
|
-
In regular Rails partials it's common to see `content_for?` used to conditionally rendering something. With Nice Partials we can do this:
|
188
|
-
|
189
|
-
```html+erb
|
190
|
-
<% if partial.title? %>
|
191
|
-
<% partial.title.h1 %>
|
192
|
-
<% end %>
|
193
|
-
```
|
194
|
-
|
195
|
-
But since sections respond to and leverage `present?`, we can shorten the above to:
|
196
|
-
|
197
|
-
```html+erb
|
198
|
-
<% partial.title.presence&.h1 %>
|
199
|
-
```
|
200
|
-
|
201
|
-
This way no empty h1 element is rendered.
|
202
|
-
|
203
219
|
### Accessing the content returned via `partial.yield`
|
204
220
|
|
205
221
|
To access the inner content lines in the block here, partials have to manually insert a `<%= yield %>` call.
|
@@ -1,28 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Monkey patch required to make `t` work as expected. Is this evil?
|
2
4
|
# TODO Do we need to monkey patch other types of renderers as well?
|
3
5
|
module NicePartials::RenderingWithLocalePrefix
|
4
|
-
|
6
|
+
module BaseIntegration
|
7
|
+
::ActionView::Base.prepend self
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
+
def t(key, options = {})
|
10
|
+
if (template = @_nice_partials_translate_template) && key&.start_with?(".")
|
11
|
+
key = "#{virtual_path_translate_key_prefix(template.virtual_path)}#{key}"
|
12
|
+
end
|
13
|
+
|
14
|
+
super(key, **options)
|
15
|
+
end
|
9
16
|
|
10
|
-
|
11
|
-
|
12
|
-
|
17
|
+
def with_nice_partials_t_prefix(block)
|
18
|
+
old_nice_partials_translate_template = @_nice_partials_translate_template
|
19
|
+
@_nice_partials_translate_template = block ? @current_template : nil
|
20
|
+
yield
|
21
|
+
ensure
|
22
|
+
@_nice_partials_translate_template = old_nice_partials_translate_template
|
13
23
|
end
|
14
24
|
|
15
|
-
|
25
|
+
private
|
26
|
+
|
27
|
+
def virtual_path_translate_key_prefix(virtual_path)
|
28
|
+
@_scope_key_by_partial_cache ||= {} # Reuses Rails' existing `t` cache.
|
29
|
+
@_scope_key_by_partial_cache[virtual_path] ||= virtual_path.gsub(%r{/_?}, ".")
|
30
|
+
end
|
16
31
|
end
|
17
32
|
|
18
|
-
|
33
|
+
module PartialRendererIntegration
|
34
|
+
ActionView::PartialRenderer.prepend self
|
19
35
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
yield
|
24
|
-
ensure
|
25
|
-
@_nice_partials_t_prefix = _nice_partials_t_prefix
|
36
|
+
def render(partial, view, block)
|
37
|
+
view.with_nice_partials_t_prefix(block) { super }
|
38
|
+
end
|
26
39
|
end
|
27
40
|
end
|
28
41
|
|
@@ -32,10 +45,7 @@ NicePartials::DEPRECATOR = ActiveSupport::Deprecation.new("1.0", "nice_partials"
|
|
32
45
|
module NicePartials::RenderingWithAutoContext
|
33
46
|
ActionView::Base.prepend self
|
34
47
|
|
35
|
-
|
36
|
-
@__partials ||= NicePartials::Partial::Stack.new
|
37
|
-
end
|
38
|
-
delegate :partial, to: :__partials
|
48
|
+
attr_reader :partial
|
39
49
|
|
40
50
|
def p(*args)
|
41
51
|
if args.empty?
|
@@ -50,10 +60,10 @@ module NicePartials::RenderingWithAutoContext
|
|
50
60
|
# on the stack, so rendering has a fresh `partial` to store content in.
|
51
61
|
def render(options = {}, locals = {}, &block)
|
52
62
|
partial_locals = options.is_a?(Hash) ? options[:locals] : locals
|
53
|
-
|
63
|
+
@partial = nice_partial_with(partial_locals)
|
54
64
|
super
|
55
65
|
ensure
|
56
|
-
|
66
|
+
@partial = partial.outer_partial
|
57
67
|
end
|
58
68
|
|
59
69
|
# Since Action View passes any `yield`s in partials through `_layout_for`, we
|
@@ -81,10 +91,10 @@ module NicePartials::RenderingWithAutoContext
|
|
81
91
|
# Note: this happens because the `@partial` instance variable is shared between all
|
82
92
|
# `render` calls since rendering happens in one `ActionView::Base` instance.
|
83
93
|
def capture_with_outer_partial_access(*arguments, &block)
|
84
|
-
|
85
|
-
|
94
|
+
inner_partial, @partial = partial, partial.outer_partial
|
95
|
+
inner_partial.capture(*arguments, &block)
|
86
96
|
ensure
|
87
|
-
|
97
|
+
@partial = inner_partial
|
88
98
|
end
|
89
99
|
end
|
90
100
|
|
@@ -1,4 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class NicePartials::Partial::Section < NicePartials::Partial::Content
|
4
|
+
class RequiredError < ArgumentError; end
|
5
|
+
|
6
|
+
class Empty < BasicObject
|
7
|
+
def initialize(section)
|
8
|
+
@section = section
|
9
|
+
end
|
10
|
+
delegate :blank?, :present?, :presence, to: :@section
|
11
|
+
|
12
|
+
def nil?
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(...)
|
17
|
+
present? ? super : ""
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns self if `present?`, or raises.
|
22
|
+
# Useful to declare content that you require to be supplied during a `render` call.
|
23
|
+
#
|
24
|
+
# <%= partial.title.required.div class: "text-xl" %>
|
25
|
+
def required
|
26
|
+
present? ? self : raise(RequiredError, "Section expected to have content, but wasn't supplied during render")
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns self if `present?`, or returns a Null object that won't output any content.
|
30
|
+
# Useful to declare optional content sections, that you also don't want to print any HTML elements for.
|
31
|
+
#
|
32
|
+
# <%= partial.title.optional.div class: "text-xl" %> # => "" # Won't output an empty `<div>` that can mess with HTML markups.
|
33
|
+
def optional
|
34
|
+
present? ? self : Empty.new(self)
|
35
|
+
end
|
36
|
+
|
2
37
|
def yield(*arguments)
|
3
38
|
chunks.each { append @view_context.capture(*arguments, &_1) }
|
4
39
|
self
|
@@ -4,8 +4,14 @@ module NicePartials
|
|
4
4
|
autoload :Section, "nice_partials/partial/section"
|
5
5
|
autoload :Stack, "nice_partials/partial/stack"
|
6
6
|
|
7
|
+
attr_reader :outer_partial
|
8
|
+
|
9
|
+
attr_reader :local_assigns
|
10
|
+
alias_method :locals, :local_assigns
|
11
|
+
|
7
12
|
def initialize(view_context, local_assigns = nil)
|
8
13
|
@view_context, @local_assigns = view_context, local_assigns
|
14
|
+
@outer_partial = view_context.partial # Capture the existing outer partial we're rendering within, if any.
|
9
15
|
end
|
10
16
|
|
11
17
|
def yield(*arguments, &block)
|
@@ -107,13 +113,23 @@ module NicePartials
|
|
107
113
|
end
|
108
114
|
|
109
115
|
def helpers_context
|
110
|
-
@helpers_context ||= Helpers.new(@view_context)
|
116
|
+
@helpers_context ||= Helpers.new(@view_context, self)
|
111
117
|
end
|
112
118
|
|
113
119
|
class Helpers < SimpleDelegator
|
114
120
|
def self.method_added(name)
|
115
121
|
super
|
116
|
-
|
122
|
+
|
123
|
+
unless name == :initialize || name == :partial
|
124
|
+
NicePartials::Partial.delegate name, to: :helpers_context
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
attr_reader :partial
|
129
|
+
|
130
|
+
def initialize(view_context, partial)
|
131
|
+
super(view_context)
|
132
|
+
@partial = partial
|
117
133
|
end
|
118
134
|
end
|
119
135
|
end
|
data/lib/nice_partials.rb
CHANGED
@@ -3,11 +3,6 @@
|
|
3
3
|
require_relative "nice_partials/version"
|
4
4
|
|
5
5
|
module NicePartials
|
6
|
-
def self.locale_prefix_from(lookup_context, block)
|
7
|
-
partial_location = block.source_location.first.dup
|
8
|
-
lookup_context.view_paths.each { partial_location.delete_prefix!(_1.path)&.delete_prefix!("/") }
|
9
|
-
partial_location.split('.').first.gsub('/_', '/').gsub('/', '.')
|
10
|
-
end
|
11
6
|
end
|
12
7
|
|
13
8
|
ActiveSupport.on_load :action_view do
|
data/nice_partials.gemspec
CHANGED
@@ -23,7 +23,7 @@ Gem::Specification.new do |gem|
|
|
23
23
|
gem.executables = gem.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
24
24
|
gem.require_paths = ["lib"]
|
25
25
|
|
26
|
-
gem.required_ruby_version = ">=
|
26
|
+
gem.required_ruby_version = ">= 3.0"
|
27
27
|
|
28
28
|
gem.add_dependency "actionview", '>= 4.2.6'
|
29
29
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nice_partials
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Culver
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2024-08-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionview
|
@@ -64,6 +64,7 @@ files:
|
|
64
64
|
- CHANGELOG.md
|
65
65
|
- CODE_OF_CONDUCT.md
|
66
66
|
- Gemfile
|
67
|
+
- Gemfile.lock
|
67
68
|
- MIT-LICENSE.txt
|
68
69
|
- README.md
|
69
70
|
- Rakefile
|
@@ -73,7 +74,6 @@ files:
|
|
73
74
|
- lib/nice_partials/partial.rb
|
74
75
|
- lib/nice_partials/partial/content.rb
|
75
76
|
- lib/nice_partials/partial/section.rb
|
76
|
-
- lib/nice_partials/partial/stack.rb
|
77
77
|
- lib/nice_partials/version.rb
|
78
78
|
- nice_partials.gemspec
|
79
79
|
homepage: https://github.com/bullet-train-co/nice_partials
|
@@ -88,14 +88,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
88
|
requirements:
|
89
89
|
- - ">="
|
90
90
|
- !ruby/object:Gem::Version
|
91
|
-
version: '
|
91
|
+
version: '3.0'
|
92
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
requirements: []
|
98
|
-
rubygems_version: 3.
|
98
|
+
rubygems_version: 3.5.16
|
99
99
|
signing_key:
|
100
100
|
specification_version: 4
|
101
101
|
summary: A little bit of magic to make partials perfect for components.
|
@@ -1,19 +0,0 @@
|
|
1
|
-
class NicePartials::Partial::Stack
|
2
|
-
def initialize
|
3
|
-
@partials = []
|
4
|
-
reset_locator
|
5
|
-
end
|
6
|
-
delegate :prepend, :shift, :first, to: :@partials
|
7
|
-
|
8
|
-
def partial
|
9
|
-
@partials.public_send @locator
|
10
|
-
end
|
11
|
-
|
12
|
-
def locate_previous
|
13
|
-
@locator = :second
|
14
|
-
end
|
15
|
-
|
16
|
-
def reset_locator
|
17
|
-
@locator = :first
|
18
|
-
end
|
19
|
-
end
|