view_component 2.6.0 → 2.7.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.
Potentially problematic release.
This version of view_component might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +43 -1
- data/lib/view_component/base.rb +16 -1
- data/lib/view_component/test_helpers.rb +5 -3
- data/lib/view_component/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c62fe72fd9c16275ff15c18684090a2678dfba28d585da3133b430431a672e7
|
4
|
+
data.tar.gz: b05a979440d506690105a9a8c0f501a914bb0db30d3c56303dd145b5d4aad208
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f47750eee5ae067e03b3945ae696eccf5c14bd5677a9a65854eb15ccd060ae3d5d08591424c922097c4596546b007958b2f04bf0c14360939c44f200153ff9dc
|
7
|
+
data.tar.gz: 4098da390547ba5d915d7eb887b22d9d932e860a079ab8a04f54f2fd8d437aa7830462e116f2ef0efdf078b73dc70f05e64380747acbbd736a99fc4117c462c6
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
# master
|
2
2
|
|
3
|
+
# 2.7.0
|
4
|
+
|
5
|
+
* Add `rendered_component` method to `ViewComponent::TestHelpers` which exposes the raw output of the rendered component.
|
6
|
+
|
7
|
+
*Richard Macklin*
|
8
|
+
|
9
|
+
* Support sidecar directories for views and other assets.
|
10
|
+
|
11
|
+
*Jon Palmer*
|
12
|
+
|
3
13
|
# 2.6.0
|
4
14
|
|
5
15
|
* Add `config.view_component.preview_route` to set the endpoint for component previews. By default `/rails/view_components` is used.
|
data/README.md
CHANGED
@@ -191,6 +191,38 @@ class InlineVariantComponent < ViewComponent::Base
|
|
191
191
|
end
|
192
192
|
```
|
193
193
|
|
194
|
+
### Sidecar Assets
|
195
|
+
|
196
|
+
ViewComponents supports two options for defining view files.
|
197
|
+
|
198
|
+
#### Sidecar view
|
199
|
+
|
200
|
+
The simplest option is to place the view next to the Ruby component:
|
201
|
+
|
202
|
+
```
|
203
|
+
app/components
|
204
|
+
├── ...
|
205
|
+
├── test_component.rb
|
206
|
+
├── test_component.html.erb
|
207
|
+
├── ...
|
208
|
+
```
|
209
|
+
|
210
|
+
#### Sidecar directory
|
211
|
+
|
212
|
+
As an alternative, views and other assets can be placed in a sidecar directory with the same name as the component, which can be useful for organizing views alongside other assets like Javascript and CSS.
|
213
|
+
|
214
|
+
```
|
215
|
+
app/components
|
216
|
+
├── ...
|
217
|
+
├── test_component.rb
|
218
|
+
├── test_component
|
219
|
+
| ├── test_component.css
|
220
|
+
| ├── test_component.html.erb
|
221
|
+
| └── test_component.js
|
222
|
+
├── ...
|
223
|
+
|
224
|
+
```
|
225
|
+
|
194
226
|
### Conditional Rendering
|
195
227
|
|
196
228
|
Components can implement a `#render?` method to be called after initialization to determine if the component should render.
|
@@ -387,7 +419,7 @@ class MyComponentTest < ViewComponent::TestCase
|
|
387
419
|
end
|
388
420
|
```
|
389
421
|
|
390
|
-
In the absence of `capybara`,
|
422
|
+
In the absence of `capybara`, assert against the return value of `render_inline`, which is an instance of `Nokogiri::HTML::DocumentFragment`:
|
391
423
|
|
392
424
|
```ruby
|
393
425
|
test "render component" do
|
@@ -397,6 +429,16 @@ test "render component" do
|
|
397
429
|
end
|
398
430
|
```
|
399
431
|
|
432
|
+
Alternatively, assert against the raw output of the component, which is exposed as `rendered_component`:
|
433
|
+
|
434
|
+
```ruby
|
435
|
+
test "render component" do
|
436
|
+
render_inline(TestComponent.new(title: "my title")) { "Hello, World!" }
|
437
|
+
|
438
|
+
assert_includes rendered_component, "Hello, World!"
|
439
|
+
end
|
440
|
+
```
|
441
|
+
|
400
442
|
#### Action Pack Variants
|
401
443
|
|
402
444
|
Use the `with_variant` helper to test specific variants:
|
data/lib/view_component/base.rb
CHANGED
@@ -341,7 +341,22 @@ module ViewComponent
|
|
341
341
|
|
342
342
|
def matching_views_in_source_location
|
343
343
|
return [] unless source_location
|
344
|
-
|
344
|
+
|
345
|
+
location_without_extension = source_location.chomp(File.extname(source_location))
|
346
|
+
|
347
|
+
extenstions = ActionView::Template.template_handler_extensions.join(",")
|
348
|
+
|
349
|
+
# view files in the same directory as te component
|
350
|
+
sidecar_files = Dir["#{location_without_extension}.*{#{extenstions}}"]
|
351
|
+
|
352
|
+
# view files in a directory named like the component
|
353
|
+
directory = File.dirname(source_location)
|
354
|
+
filename = File.basename(source_location, ".rb")
|
355
|
+
component_name = name.demodulize.underscore
|
356
|
+
|
357
|
+
sidecar_directory_files = Dir["#{directory}/#{component_name}/#{filename}.*{#{extenstions}}"]
|
358
|
+
|
359
|
+
(sidecar_files - [source_location] + sidecar_directory_files)
|
345
360
|
end
|
346
361
|
|
347
362
|
def templates
|
@@ -7,7 +7,7 @@ module ViewComponent
|
|
7
7
|
include Capybara::Minitest::Assertions
|
8
8
|
|
9
9
|
def page
|
10
|
-
Capybara::Node::Simple.new(@
|
10
|
+
Capybara::Node::Simple.new(@rendered_component)
|
11
11
|
end
|
12
12
|
|
13
13
|
def refute_component_rendered
|
@@ -17,10 +17,12 @@ module ViewComponent
|
|
17
17
|
warn "WARNING in `ViewComponent::TestHelpers`: You must add `capybara` to your Gemfile to use Capybara assertions."
|
18
18
|
end
|
19
19
|
|
20
|
+
attr_reader :rendered_component
|
21
|
+
|
20
22
|
def render_inline(component, **args, &block)
|
21
|
-
@
|
23
|
+
@rendered_component = controller.view_context.render(component, args, &block)
|
22
24
|
|
23
|
-
Nokogiri::HTML.fragment(@
|
25
|
+
Nokogiri::HTML.fragment(@rendered_component)
|
24
26
|
end
|
25
27
|
|
26
28
|
def controller
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: view_component
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GitHub Open Source
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|