actionview-component 1.5.3 → 1.8.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/.github/ISSUE_TEMPLATE +25 -0
- data/.github/PULL_REQUEST_TEMPLATE +17 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +4 -0
- data/CHANGELOG.md +106 -0
- data/CONTRIBUTING.md +4 -13
- data/Gemfile.lock +13 -2
- data/README.md +282 -9
- data/actionview-component.gemspec +1 -0
- data/lib/action_view/component.rb +20 -2
- data/lib/action_view/component/base.rb +95 -39
- data/lib/action_view/component/conversion.rb +11 -0
- data/lib/action_view/component/preview.rb +8 -35
- data/lib/action_view/component/previewable.rb +27 -0
- data/lib/action_view/component/railtie.rb +15 -1
- data/lib/action_view/component/render_monkey_patch.rb +29 -0
- data/lib/action_view/component/template_error.rb +11 -0
- data/lib/action_view/component/test_case.rb +11 -0
- data/lib/action_view/component/test_helpers.rb +2 -2
- data/lib/action_view/component/version.rb +2 -2
- data/lib/rails/generators/component/component_generator.rb +2 -2
- data/lib/rails/generators/rspec/component_generator.rb +1 -1
- data/lib/rails/generators/test_unit/component_generator.rb +1 -1
- data/lib/rails/generators/test_unit/templates/component_test.rb.tt +2 -4
- data/lib/railties/lib/rails.rb +0 -1
- data/lib/railties/lib/rails/components_controller.rb +5 -1
- data/lib/railties/lib/rails/templates/rails/components/preview.html.erb +1 -1
- metadata +23 -5
- data/lib/action_view/component/monkey_patch.rb +0 -27
- data/lib/railties/lib/rails/component_examples_controller.rb +0 -9
- data/lib/railties/lib/rails/templates/rails/examples/show.html.erb +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab99ff30cf5a1d5fad5d3f670936b813d8923b9e4e5a3db566c0c08fa8cb7550
|
4
|
+
data.tar.gz: 7432ee39d40af863647a1cdf220b7775592bfef12c84bbc8a6175d741b88c2ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53a8dc19f3caa79baf1c2fc754ee0b715885b83eaae13a7a3b6f2f25fb574dd6a0b109cd61328444abaf93ef55ded12413d59a5343fc707c544baba766c14ae7
|
7
|
+
data.tar.gz: a73449076c9d073052c97ad8cc0d58a88d40f8973a576221a41d7b9e7ed15aeecd0d3704694e4f78bf918fbe6f4d875df0f389f71381b81ad0fed7f8fb4720d3
|
@@ -0,0 +1,25 @@
|
|
1
|
+
<!-- **** Filing a Feature Request? Include these sections. **** -->
|
2
|
+
|
3
|
+
### Feature request
|
4
|
+
<!-- Provide a summary of the behavior. -->
|
5
|
+
|
6
|
+
### Motivation
|
7
|
+
|
8
|
+
<!-- What would you like to do with this feature? Can you provide
|
9
|
+
context or references to similar behavior in other libraries? -->
|
10
|
+
|
11
|
+
<!-- **** Filing a Bug Report? Include these sections: **** -->
|
12
|
+
|
13
|
+
### Steps to reproduce
|
14
|
+
<!-- Provide a series of steps or, better yet, a link to a repo to demonstrate the bug you've identified. -->
|
15
|
+
|
16
|
+
### Expected behavior
|
17
|
+
<!-- Tell us what should happen -->
|
18
|
+
|
19
|
+
### Actual behavior
|
20
|
+
<!-- Tell us what happens instead -->
|
21
|
+
|
22
|
+
### System configuration
|
23
|
+
**Rails version**:
|
24
|
+
|
25
|
+
**Ruby version**:
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<!-- See https://github.com/github/actionview-component/blob/master/CONTRIBUTING.md#submitting-a-pull-request -->
|
2
|
+
|
3
|
+
### Summary
|
4
|
+
|
5
|
+
<!-- Provide a general description of the code changes in your pull
|
6
|
+
request... were there any bugs you had fixed? If so, mention them. If
|
7
|
+
these bugs have open GitHub issues, be sure to tag them here as well,
|
8
|
+
to keep the conversation linked together. -->
|
9
|
+
|
10
|
+
### Other Information
|
11
|
+
|
12
|
+
<!-- If there's anything else that's important and relevant to your pull
|
13
|
+
request, mention that information here. This could include
|
14
|
+
benchmarks, or other information.
|
15
|
+
|
16
|
+
If you are updating any of the CHANGELOG files or are asked to update the
|
17
|
+
CHANGELOG files by reviewers, please add the CHANGELOG entry at the top of the file. -->
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,109 @@
|
|
1
|
+
# v1.8.0
|
2
|
+
|
3
|
+
* Remove the unneeded ComponentExamplesController and simplify preview rendering.
|
4
|
+
|
5
|
+
*Jon Palmer*
|
6
|
+
|
7
|
+
* Add `#render?` hook to easily allow components to be no-ops.
|
8
|
+
|
9
|
+
*Kyle Fox*
|
10
|
+
|
11
|
+
* Don't assume ApplicationController exists.
|
12
|
+
|
13
|
+
*Jon Palmer*
|
14
|
+
|
15
|
+
* Allow some additional checks to overrided render?
|
16
|
+
|
17
|
+
*Sergey Malykh*
|
18
|
+
|
19
|
+
* Fix generator placing namespaced components in the root directory.
|
20
|
+
|
21
|
+
*Asger Behncke Jacobsen*
|
22
|
+
|
23
|
+
* Fix cache test.
|
24
|
+
|
25
|
+
*Sergey Malykh*
|
26
|
+
|
27
|
+
# v1.7.0
|
28
|
+
|
29
|
+
* Simplify validation of templates and compilation.
|
30
|
+
|
31
|
+
*Jon Palmer*
|
32
|
+
|
33
|
+
* Add support for multiple content areas.
|
34
|
+
|
35
|
+
*Jon Palmer*
|
36
|
+
|
37
|
+
# v1.6.2
|
38
|
+
|
39
|
+
* Fix Uninitialized Constant error.
|
40
|
+
|
41
|
+
*Jon Palmer*
|
42
|
+
|
43
|
+
* Add basic github issue and PR templates.
|
44
|
+
|
45
|
+
*Dylan Clark*
|
46
|
+
|
47
|
+
* Update readme phrasing around previews.
|
48
|
+
|
49
|
+
*Justin Coyne*
|
50
|
+
|
51
|
+
# v1.6.1
|
52
|
+
|
53
|
+
* Allow Previews to have no layout.
|
54
|
+
|
55
|
+
*Jon Palmer*
|
56
|
+
|
57
|
+
* Bump rack from 2.0.7 to 2.0.8.
|
58
|
+
|
59
|
+
*Dependabot*
|
60
|
+
|
61
|
+
* Compile components on application boot when eager loading is enabled.
|
62
|
+
|
63
|
+
*Joel Hawksley*
|
64
|
+
|
65
|
+
* Previews support content blocks.
|
66
|
+
|
67
|
+
*Cesario Uy*
|
68
|
+
|
69
|
+
* Follow Rails conventions. (refactor)
|
70
|
+
|
71
|
+
*Rainer Borene*
|
72
|
+
|
73
|
+
* Fix edge case issue with extracting variants from less conventional source_locations.
|
74
|
+
|
75
|
+
*Ryan Workman*
|
76
|
+
|
77
|
+
# v1.6.0
|
78
|
+
|
79
|
+
* Avoid dropping elements in the render_inline test helper.
|
80
|
+
|
81
|
+
*@dark-panda*
|
82
|
+
|
83
|
+
* Add test for helpers.asset_url.
|
84
|
+
|
85
|
+
*Christopher Coleman*
|
86
|
+
|
87
|
+
* Add rudimentary compatibility with better_html.
|
88
|
+
|
89
|
+
*Joel Hawksley*
|
90
|
+
|
91
|
+
* Template-less variants fall back to default template.
|
92
|
+
|
93
|
+
*Asger Behncke Jacobsen*, *Cesario Uy*
|
94
|
+
|
95
|
+
* Generated tests use new naming convention.
|
96
|
+
|
97
|
+
*Simon Træls Ravn*
|
98
|
+
|
99
|
+
* Eliminate sqlite dependency.
|
100
|
+
|
101
|
+
*Simon Dawson*
|
102
|
+
|
103
|
+
* Add support for rendering components via #to_component_class
|
104
|
+
|
105
|
+
*Vinicius Stock*
|
106
|
+
|
1
107
|
# v1.5.3
|
2
108
|
|
3
109
|
* Add support for RSpec to generators.
|
data/CONTRIBUTING.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# Contributing
|
2
2
|
|
3
3
|
[fork]: https://github.com/github/actionview-component/fork
|
4
4
|
[pr]: https://github.com/github/actionview-component/compare
|
@@ -15,9 +15,11 @@ Please note that this project is released with a [Contributor Code of Conduct][c
|
|
15
15
|
|
16
16
|
0. [Fork][fork] and clone the repository
|
17
17
|
0. Configure and install the dependencies: `bundle`
|
18
|
-
0. Make sure the tests pass on your machine: `rake`
|
18
|
+
0. Make sure the tests pass on your machine: `bundle exec rake`
|
19
19
|
0. Create a new branch: `git checkout -b my-branch-name`
|
20
20
|
0. Make your change, add tests, and make sure the tests still pass
|
21
|
+
0. Add an entry to the top of `CHANGELOG.md` for your changes
|
22
|
+
0. If it's your first time contributing, add yourself to the contributors at the bottom of `README.md`
|
21
23
|
0. Push to your fork and [submit a pull request][pr]
|
22
24
|
0. Pat your self on the back and wait for your pull request to be reviewed and merged.
|
23
25
|
|
@@ -36,20 +38,9 @@ If you are the current maintainer of this gem:
|
|
36
38
|
1. Add version heading/entries to `CHANGELOG.md`.
|
37
39
|
1. Make sure your local dependencies are up to date: `bundle`
|
38
40
|
1. Ensure that tests are green: `bundle exec rake`
|
39
|
-
1. Build a test gem `GEM_VERSION=$(git describe --tags 2>/dev/null | sed 's/-/./g' | sed 's/v//') gem build actionview-component.gemspec`
|
40
|
-
1. Test the test gem:
|
41
|
-
1. Bump the Gemfile and Gemfile.lock versions for an app which relies on this gem
|
42
|
-
1. Install the new gem locally
|
43
|
-
1. Test behavior locally, branch deploy, whatever needs to happen
|
44
41
|
1. Make a PR to github/actionview-component.
|
45
42
|
1. Build a local gem: `gem build actionview-component.gemspec`
|
46
43
|
1. Merge github/actionview-component PR
|
47
44
|
1. Tag and push: `git tag vx.xx.xx; git push --tags`
|
48
45
|
1. Create a GitHub release with the pushed tag (https://github.com/github/actionview-component/releases/new) and populate it with a list of the commits from `git log --pretty=format:"- %s" --reverse refs/tags/[OLD TAG]...refs/tags/[NEW TAG]`
|
49
46
|
1. Push to rubygems.org -- `gem push actionview-component-VERSION.gem`
|
50
|
-
|
51
|
-
## Resources
|
52
|
-
|
53
|
-
- [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
|
54
|
-
- [Using Pull Requests](https://help.github.com/articles/about-pull-requests/)
|
55
|
-
- [GitHub Help](https://help.github.com)
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
actionview-component (1.
|
4
|
+
actionview-component (1.8.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -62,6 +62,14 @@ GEM
|
|
62
62
|
tzinfo (~> 1.1)
|
63
63
|
zeitwerk (~> 2.1, >= 2.1.8)
|
64
64
|
ast (2.4.0)
|
65
|
+
better_html (1.0.14)
|
66
|
+
actionview (>= 4.0)
|
67
|
+
activesupport (>= 4.0)
|
68
|
+
ast (~> 2.0)
|
69
|
+
erubi (~> 1.4)
|
70
|
+
html_tokenizer (~> 0.0.6)
|
71
|
+
parser (>= 2.4)
|
72
|
+
smart_properties
|
65
73
|
builder (3.2.3)
|
66
74
|
concurrent-ruby (1.1.5)
|
67
75
|
crass (1.0.5)
|
@@ -71,6 +79,7 @@ GEM
|
|
71
79
|
haml (5.1.2)
|
72
80
|
temple (>= 0.8.0)
|
73
81
|
tilt
|
82
|
+
html_tokenizer (0.0.7)
|
74
83
|
i18n (1.6.0)
|
75
84
|
concurrent-ruby (~> 1.0)
|
76
85
|
jaro_winkler (1.5.3)
|
@@ -92,7 +101,7 @@ GEM
|
|
92
101
|
parallel (1.17.0)
|
93
102
|
parser (2.6.3.0)
|
94
103
|
ast (~> 2.4.0)
|
95
|
-
rack (2.0.
|
104
|
+
rack (2.0.8)
|
96
105
|
rack-test (1.1.0)
|
97
106
|
rack (>= 1.0, < 3)
|
98
107
|
rails (6.0.0)
|
@@ -139,6 +148,7 @@ GEM
|
|
139
148
|
slim (4.0.1)
|
140
149
|
temple (>= 0.7.6, < 0.9)
|
141
150
|
tilt (>= 2.0.6, < 2.1)
|
151
|
+
smart_properties (1.15.0)
|
142
152
|
sprockets (3.7.2)
|
143
153
|
concurrent-ruby (~> 1.0)
|
144
154
|
rack (> 1, < 3)
|
@@ -163,6 +173,7 @@ PLATFORMS
|
|
163
173
|
|
164
174
|
DEPENDENCIES
|
165
175
|
actionview-component!
|
176
|
+
better_html (~> 1)
|
166
177
|
bundler (>= 1.14)
|
167
178
|
haml (~> 5)
|
168
179
|
minitest (= 5.1.0)
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
|
8
8
|
This gem is meant to serve as a precursor to upstreaming the `ActionView::Component` class into Rails. It also serves to enable the usage of `ActionView::Component` in older versions of Rails.
|
9
9
|
|
10
|
-
Preliminary support for rendering components was merged into Rails `6.1.0.alpha` in https://github.com/rails/rails/pull/36388. Assuming `ActionView::Component` makes it into Rails
|
10
|
+
Preliminary support for rendering components was merged into Rails `6.1.0.alpha` in https://github.com/rails/rails/pull/36388. Assuming `ActionView::Component` makes it into Rails, this gem will then exist to serve as a backport.
|
11
11
|
|
12
12
|
## Design philosophy
|
13
13
|
|
@@ -32,7 +32,7 @@ $ bundle
|
|
32
32
|
In `config/application.rb`, add:
|
33
33
|
|
34
34
|
```bash
|
35
|
-
require "action_view/component"
|
35
|
+
require "action_view/component/railtie"
|
36
36
|
```
|
37
37
|
|
38
38
|
## Guide
|
@@ -163,6 +163,24 @@ Components can be rendered via:
|
|
163
163
|
|
164
164
|
`render(component: TestComponent, locals: { foo: :bar })`
|
165
165
|
|
166
|
+
**Rendering components through models**
|
167
|
+
|
168
|
+
Passing model instances will cause `render` to look for its respective component class.
|
169
|
+
|
170
|
+
The component is instantiated with the rendered model instance.
|
171
|
+
|
172
|
+
Example for a `Post` model:
|
173
|
+
|
174
|
+
`render(@post)`
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
class PostComponent < ActionView::Component::Base
|
178
|
+
def initialize(post)
|
179
|
+
@post = post
|
180
|
+
end
|
181
|
+
end
|
182
|
+
```
|
183
|
+
|
166
184
|
The following syntax has been deprecated and will be removed in v2.0.0:
|
167
185
|
|
168
186
|
`render(TestComponent.new(foo: :bar))`
|
@@ -181,17 +199,233 @@ An error will be raised:
|
|
181
199
|
|
182
200
|
`ActiveModel::ValidationError: Validation failed: Title can't be blank`
|
183
201
|
|
202
|
+
#### Content Areas
|
203
|
+
|
204
|
+
A component can declare additional content areas to be rendered in the component. For example:
|
205
|
+
|
206
|
+
`app/components/modal_component.rb`:
|
207
|
+
```ruby
|
208
|
+
class ModalComponent < ActionView::Component::Base
|
209
|
+
validates :user, :header, :body, presence: true
|
210
|
+
|
211
|
+
with_content_areas :header, :body
|
212
|
+
|
213
|
+
def initialize(user:)
|
214
|
+
@user = user
|
215
|
+
end
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
`app/components/modal_component.html.erb`:
|
220
|
+
```erb
|
221
|
+
<div class="modal">
|
222
|
+
<div class="header"><%= header %></div>
|
223
|
+
<div class="body"><%= body %>"></div>
|
224
|
+
</div>
|
225
|
+
```
|
226
|
+
|
227
|
+
We can render it in a view as:
|
228
|
+
|
229
|
+
```erb
|
230
|
+
<%= render(ModalComponent, user: {name: 'Jane'}) do |component| %>
|
231
|
+
<% component.with(:header) do %>
|
232
|
+
Hello <%= user[:name] %>
|
233
|
+
<% end %>
|
234
|
+
<% component.with(:body) do %>
|
235
|
+
<p>Have a great day.</p>
|
236
|
+
<% end %>
|
237
|
+
<% end %>
|
238
|
+
```
|
239
|
+
|
240
|
+
Which returns:
|
241
|
+
|
242
|
+
```html
|
243
|
+
<div class="modal">
|
244
|
+
<div class="header">Hello Jane</div>
|
245
|
+
<div class="body"><p>Have a great day.</p></div>
|
246
|
+
</div>
|
247
|
+
```
|
248
|
+
|
249
|
+
Content for content areas can be passed as arguments to the render method or as named blocks passed to the `with` method.
|
250
|
+
This allows a few different combinations of ways to render the component:
|
251
|
+
|
252
|
+
##### Required render argument optionally overridden or wrapped by a named block
|
253
|
+
|
254
|
+
`app/components/modal_component.rb`:
|
255
|
+
```ruby
|
256
|
+
class ModalComponent < ActionView::Component::Base
|
257
|
+
validates :header, :body, presence: true
|
258
|
+
|
259
|
+
with_content_areas :header, :body
|
260
|
+
|
261
|
+
def initialize(header:)
|
262
|
+
@header = header
|
263
|
+
end
|
264
|
+
end
|
265
|
+
```
|
266
|
+
|
267
|
+
```erb
|
268
|
+
<%= render(ModalComponent, header: "Hi!") do |component| %>
|
269
|
+
<% help_enabled? && component.with(:header) do %>
|
270
|
+
<span class="help_icon"><%= component.header %></span>
|
271
|
+
<% end %>
|
272
|
+
<% component.with(:body) do %>
|
273
|
+
<p>Have a great day.</p>
|
274
|
+
<% end %>
|
275
|
+
<% end %>
|
276
|
+
```
|
277
|
+
|
278
|
+
##### Required argument passed by render argument or by named block
|
279
|
+
|
280
|
+
`app/components/modal_component.rb`:
|
281
|
+
```ruby
|
282
|
+
class ModalComponent < ActionView::Component::Base
|
283
|
+
validates :header, :body, presence: true
|
284
|
+
|
285
|
+
with_content_areas :header, :body
|
286
|
+
|
287
|
+
def initialize(header: nil)
|
288
|
+
@header = header
|
289
|
+
end
|
290
|
+
end
|
291
|
+
```
|
292
|
+
|
293
|
+
`app/views/render_arg.html.erb`:
|
294
|
+
```erb
|
295
|
+
<%= render(ModalComponent, header: "Hi!") do |component| %>
|
296
|
+
<% component.with(:body) do %>
|
297
|
+
<p>Have a great day.</p>
|
298
|
+
<% end %>
|
299
|
+
<% end %>
|
300
|
+
```
|
301
|
+
|
302
|
+
`app/views/with_block.html.erb`:
|
303
|
+
```erb
|
304
|
+
<%= render(ModalComponent) do |component| %>
|
305
|
+
<% component.with(:header) do %>
|
306
|
+
<span class="help_icon">Hello</span>
|
307
|
+
<% end %>
|
308
|
+
<% component.with(:body) do %>
|
309
|
+
<p>Have a great day.</p>
|
310
|
+
<% end %>
|
311
|
+
<% end %>
|
312
|
+
```
|
313
|
+
|
314
|
+
##### Optional argument passed by render argument, by named block, or neither
|
315
|
+
|
316
|
+
`app/components/modal_component.rb`:
|
317
|
+
```ruby
|
318
|
+
class ModalComponent < ActionView::Component::Base
|
319
|
+
validates :body, presence: true
|
320
|
+
|
321
|
+
with_content_areas :header, :body
|
322
|
+
|
323
|
+
def initialize(header: nil)
|
324
|
+
@header = header
|
325
|
+
end
|
326
|
+
end
|
327
|
+
```
|
328
|
+
|
329
|
+
`app/components/modal_component.html.erb`:
|
330
|
+
```erb
|
331
|
+
<div class="modal">
|
332
|
+
<% if header %>
|
333
|
+
<div class="header"><%= header %></div>
|
334
|
+
<% end %>
|
335
|
+
<div class="body"><%= body %>"></div>
|
336
|
+
</div>
|
337
|
+
```
|
338
|
+
|
339
|
+
`app/views/render_arg.html.erb`:
|
340
|
+
```erb
|
341
|
+
<%= render(ModalComponent, header: "Hi!") do |component| %>
|
342
|
+
<% component.with(:body) do %>
|
343
|
+
<p>Have a great day.</p>
|
344
|
+
<% end %>
|
345
|
+
<% end %>
|
346
|
+
```
|
347
|
+
|
348
|
+
`app/views/with_block.html.erb`:
|
349
|
+
```erb
|
350
|
+
<%= render(ModalComponent) do |component| %>
|
351
|
+
<% component.with(:header) do %>
|
352
|
+
<span class="help_icon">Hello</span>
|
353
|
+
<% end %>
|
354
|
+
<% component.with(:body) do %>
|
355
|
+
<p>Have a great day.</p>
|
356
|
+
<% end %>
|
357
|
+
<% end %>
|
358
|
+
```
|
359
|
+
|
360
|
+
`app/views/no_header.html.erb`:
|
361
|
+
```erb
|
362
|
+
<%= render(ModalComponent) do |component| %>
|
363
|
+
<% component.with(:body) do %>
|
364
|
+
<p>Have a great day.</p>
|
365
|
+
<% end %>
|
366
|
+
<% end %>
|
367
|
+
```
|
368
|
+
|
369
|
+
### Conditional Rendering
|
370
|
+
|
371
|
+
Components can implement a `#render?` method which indicates if they should be rendered, or not at all.
|
372
|
+
|
373
|
+
For example, you might have a component that displays a "Please confirm your email address" banner to users who haven't confirmed their email address. The logic for rendering the banner would need to go in either the component template:
|
374
|
+
|
375
|
+
```
|
376
|
+
<!-- app/components/confirm_email_component.html.erb -->
|
377
|
+
<% if user.requires_confirmation? %>
|
378
|
+
<div class="alert">
|
379
|
+
Please confirm your email address.
|
380
|
+
</div>
|
381
|
+
<% end %>
|
382
|
+
```
|
383
|
+
|
384
|
+
or the view that renders the component:
|
385
|
+
|
386
|
+
```erb
|
387
|
+
<!-- app/views/_banners.html.erb -->
|
388
|
+
<% if current_user.requires_confirmation? %>
|
389
|
+
<%= render(ConfirmEmailComponent, user: current_user) %>
|
390
|
+
<% end %>
|
391
|
+
```
|
392
|
+
|
393
|
+
The `#render?` hook allows you to move this logic into the Ruby class, leaving your views more readable and declarative in style:
|
394
|
+
|
395
|
+
```ruby
|
396
|
+
# app/components/confirm_email_component.rb
|
397
|
+
class ConfirmEmailComponent < ApplicationComponent
|
398
|
+
def initialize(user:)
|
399
|
+
@user = user
|
400
|
+
end
|
401
|
+
|
402
|
+
def render?
|
403
|
+
@user.requires_confirmation?
|
404
|
+
end
|
405
|
+
end
|
406
|
+
```
|
407
|
+
|
408
|
+
```
|
409
|
+
<!-- app/components/confirm_email_component.html.erb -->
|
410
|
+
<div class="banner">
|
411
|
+
Please confirm your email address.
|
412
|
+
</div>
|
413
|
+
```
|
414
|
+
|
415
|
+
```erb
|
416
|
+
<!-- app/views/_banners.html.erb -->
|
417
|
+
<%= render(ConfirmEmailComponent, user: current_user) %>
|
418
|
+
```
|
419
|
+
|
184
420
|
### Testing
|
185
421
|
|
186
422
|
Components are unit tested directly. The `render_inline` test helper wraps the result in `Nokogiri.HTML`, allowing us to test the component above as:
|
187
423
|
|
188
424
|
```ruby
|
189
|
-
require "action_view/component/
|
425
|
+
require "action_view/component/test_case"
|
190
426
|
|
191
|
-
class MyComponentTest <
|
192
|
-
|
193
|
-
|
194
|
-
def test_render_component
|
427
|
+
class MyComponentTest < ActionView::Component::TestCase
|
428
|
+
test "render component" do
|
195
429
|
assert_equal(
|
196
430
|
%(<span title="my title">Hello, World!</span>),
|
197
431
|
render_inline(TestComponent, title: "my title") { "Hello, World!" }.to_html
|
@@ -207,7 +441,7 @@ In general, we’ve found it makes the most sense to test components based on th
|
|
207
441
|
To test a specific variant you can wrap your test with the `with_variant` helper method as:
|
208
442
|
|
209
443
|
```ruby
|
210
|
-
|
444
|
+
test "render component for tablet" do
|
211
445
|
with_variant :tablet do
|
212
446
|
assert_equal(
|
213
447
|
%(<span title="my title">Hello, tablets!</span>),
|
@@ -218,7 +452,7 @@ end
|
|
218
452
|
```
|
219
453
|
|
220
454
|
### Previewing Components
|
221
|
-
`ActionView::Component::Preview`
|
455
|
+
`ActionView::Component::Preview` provides a way to see how components look by visiting a special URL that renders them.
|
222
456
|
In the previous example, the preview class for `TestComponent` would be called `TestComponentPreview` and located in `test/components/previews/test_component_preview.rb`.
|
223
457
|
To see the preview of the component with a given title, implement a method that renders the component.
|
224
458
|
You can define as many examples as you want:
|
@@ -260,6 +494,16 @@ For example, if you want to use `lib/component_previews`, set the following in `
|
|
260
494
|
config.action_view_component.preview_path = "#{Rails.root}/lib/component_previews"
|
261
495
|
```
|
262
496
|
|
497
|
+
#### Configuring TestController
|
498
|
+
|
499
|
+
By default components tests and previews expect your Rails project to contain an `ApplicationController` class from which Controller classes inherit.
|
500
|
+
This can be configured using the `test_controller` option.
|
501
|
+
For example, if your controllers inherit from `BaseController`, set the following in `config/application.rb`:
|
502
|
+
|
503
|
+
```ruby
|
504
|
+
config.action_view_component.test_controller = "BaseController"
|
505
|
+
```
|
506
|
+
|
263
507
|
### Setting up RSpec
|
264
508
|
|
265
509
|
If you're using RSpec, you can configure component specs to have access to test helpers. Add the following to
|
@@ -315,6 +559,35 @@ Inline templates have been removed (for now) due to concerns raised by [@soutaro
|
|
315
559
|
|
316
560
|
Bug reports and pull requests are welcome on GitHub at https://github.com/github/actionview-component. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. We recommend reading the [contributing guide](./CONTRIBUTING.md) as well.
|
317
561
|
|
562
|
+
## Contributors
|
563
|
+
|
564
|
+
`actionview-component` is built by:
|
565
|
+
|
566
|
+
|<img src="https://avatars.githubusercontent.com/joelhawksley?s=256" alt="joelhawksley" width="128" />|<img src="https://avatars.githubusercontent.com/tenderlove?s=256" alt="tenderlove" width="128" />|<img src="https://avatars.githubusercontent.com/jonspalmer?s=256" alt="jonspalmer" width="128" />|<img src="https://avatars.githubusercontent.com/juanmanuelramallo?s=256" alt="juanmanuelramallo" width="128" />|<img src="https://avatars.githubusercontent.com/vinistock?s=256" alt="vinistock" width="128" />|
|
567
|
+
|:---:|:---:|:---:|:---:|:---:|
|
568
|
+
|@joelhawksley|@tenderlove|@jonspalmer|@juanmanuelramallo|@vinistock|
|
569
|
+
|Denver|Seattle|Boston||Toronto|
|
570
|
+
|
571
|
+
|<img src="https://avatars.githubusercontent.com/metade?s=256" alt="metade" width="128" />|<img src="https://avatars.githubusercontent.com/asgerb?s=256" alt="asgerb" width="128" />|<img src="https://avatars.githubusercontent.com/xronos-i-am?s=256" alt="xronos-i-am" width="128" />|<img src="https://avatars.githubusercontent.com/dylnclrk?s=256" alt="dylnclrk" width="128" />|<img src="https://avatars.githubusercontent.com/kaspermeyer?s=256" alt="kaspermeyer" width="128" />|
|
572
|
+
|:---:|:---:|:---:|:---:|:---:|
|
573
|
+
|@metade|@asgerb|@xronos-i-am|@dylnclrk|@kaspermeyer|
|
574
|
+
|London|Copenhagen|Russia, Kirov|Berkeley, CA|Denmark|
|
575
|
+
|
576
|
+
|<img src="https://avatars.githubusercontent.com/rdavid1099?s=256" alt="rdavid1099" width="128" />|<img src="https://avatars.githubusercontent.com/kylefox?s=256" alt="kylefox" width="128" />|<img src="https://avatars.githubusercontent.com/traels?s=256" alt="traels" width="128" />|<img src="https://avatars.githubusercontent.com/rainerborene?s=256" alt="rainerborene" width="128" />|<img src="https://avatars.githubusercontent.com/jcoyne?s=256" alt="jcoyne" width="128" />|
|
577
|
+
|:---:|:---:|:---:|:---:|:---:|
|
578
|
+
|@rdavid1099|@kylefox|@traels|@rainerborene|@jcoyne|
|
579
|
+
|Los Angeles|Edmonton|Odense, Denmark|Brazil|Minneapolis|
|
580
|
+
|
581
|
+
|<img src="https://avatars.githubusercontent.com/elia?s=256" alt="elia" width="128" />|<img src="https://avatars.githubusercontent.com/cesariouy?s=256" alt="cesariouy" width="128" />|<img src="https://avatars.githubusercontent.com/spdawson?s=256" alt="spdawson" width="128" />|<img src="https://avatars.githubusercontent.com/rmacklin?s=256" alt="rmacklin" width="128" />|<img src="https://avatars.githubusercontent.com/michaelem?s=256" alt="michaelem" width="128" />|
|
582
|
+
|:---:|:---:|:---:|:---:|:---:|
|
583
|
+
|@elia|@cesariouy|@spdawson|@rmacklin|@michaelem|
|
584
|
+
|Milan||United Kingdom||Berlin|
|
585
|
+
|
586
|
+
|<img src="https://avatars.githubusercontent.com/mellowfish?s=256" alt="mellowfish" width="128" />|<img src="https://avatars.githubusercontent.com/horacio?s=256" alt="horacio" width="128" />|<img src="https://avatars.githubusercontent.com/dukex?s=256" alt="dukex" width="128" />|<img src="https://avatars.githubusercontent.com/dark-panda?s=256" alt="dark-panda" width="128" />|
|
587
|
+
|:---:|:---:|:---:|:---:|
|
588
|
+
|@mellowfish|@horacio|@dukex|@dark-panda|
|
589
|
+
|Spring Hill, TN|Buenos Aires|São Paulo||
|
590
|
+
|
318
591
|
## License
|
319
592
|
|
320
593
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|