view_component-contrib 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +31 -0
- data/README.md +25 -0
- data/app/views/view_component_contrib/preview.html.erb +5 -1
- data/lib/view_component_contrib/preview/base.rb +8 -2
- data/lib/view_component_contrib/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3dfe8a56d9d79e6fbb11442811f68a9cdc7980c4dcbe00fdeba825a11d5124c7
|
4
|
+
data.tar.gz: 4ec650eab3f6c6b11ba79f598ecb79f34a3f900b44e1b6e53b55d08a9501f7aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a3f375f7e61f7b29ff6379caf4dec9d89a98226ca6c0c2bf55349e1ee3e28a6b6076840fe29a937b1700bc65546c6d946b575cc5337208a7ae49e555a75f3dc
|
7
|
+
data.tar.gz: e129b0af8c6f11b979bd202e9761f2acb5dc267e895ad010043eac3a028f9a5312a3c552bfc49cac4fa173abf48b07578fc017ea126162ee017739b8de1a3207
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,37 @@
|
|
2
2
|
|
3
3
|
## master
|
4
4
|
|
5
|
+
## 0.1.5 (2023-11-02)
|
6
|
+
|
7
|
+
- Support content blocks in `#render_component` and `#render_with`. ([@palkan][])
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class MyComponent::Preview
|
11
|
+
def default
|
12
|
+
# Now you can pass a block to render_component to render it inside the component:
|
13
|
+
render_component(kind: "info") do
|
14
|
+
"Welcome!"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
- Support implicit components in `#render_component` helper. ([@palkan][])
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class MyComponent::Preview
|
24
|
+
def default
|
25
|
+
# Before
|
26
|
+
render_component(MyComponent::Component.new(foo: "bar"))
|
27
|
+
end
|
28
|
+
|
29
|
+
# After
|
30
|
+
def default
|
31
|
+
render_component(foo: "bar")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
5
36
|
## 0.1.4 (2023-04-30)
|
6
37
|
|
7
38
|
- Fix compatibility with new errors classes in view_component.
|
data/README.md
CHANGED
@@ -47,6 +47,9 @@ bundle exec rails g view_component -h
|
|
47
47
|
**Why adding a custom generator to the project instead of bundling it into the gem?** The generator could only be useful if it fits
|
48
48
|
your project needs. The more control you have over the generator the better. Thus, the best way is to make the generator a part of a project.
|
49
49
|
|
50
|
+
> [!IMPORTANT]
|
51
|
+
> If your application has the `lib/` folder in the autoload paths, make sure you ignored the generated `lib/generators` folder. In Rails 7.1+, you can do this via adding `generators` the `config.autoload_lib` call's `ignore` option. Before, you can use `Rails.autoloaders.main.ignore(...)`.
|
52
|
+
|
50
53
|
## Organizing components, or sidecar pattern extended
|
51
54
|
|
52
55
|
ViewComponent provides different ways to organize your components: putting everyhing (Ruby files, templates, etc.) into `app/components` folder or using a _sidecar_ directory for everything but the `.rb` file itself. The first approach could easily result in a directory bloat; the second is better though there is a room for improvement: we can move `.rb` files into sidecar folders as well. Then, we can get rid of the _noisy_ `_component` suffixes. Finally, we can also put previews there (since storing them within the test folder is a little bit confusing):
|
@@ -156,6 +159,14 @@ class Banner::Preview < ApplicationViewComponentPreview
|
|
156
159
|
def default
|
157
160
|
# This will use `absolute w-full` for the container class
|
158
161
|
render_component Banner::Component.new(text: "Welcome!")
|
162
|
+
|
163
|
+
# or even shorter
|
164
|
+
render_component(text: "Welcome!")
|
165
|
+
|
166
|
+
# you can also pass a content block
|
167
|
+
render_component(kind: :notice) do
|
168
|
+
"Some content"
|
169
|
+
end
|
159
170
|
end
|
160
171
|
|
161
172
|
def mobile
|
@@ -434,6 +445,20 @@ class FlashAlert::Component < ApplicationViewComponent
|
|
434
445
|
end
|
435
446
|
```
|
436
447
|
|
448
|
+
## Supporting `.with_collection`
|
449
|
+
|
450
|
+
The `.with_collection` method from ViewComponent expects a component class to have the "Component" suffix to correctly infer the parameter name. Since we're using a different naming convention, we need to specify the collection parameter name explicitly. For example:
|
451
|
+
|
452
|
+
```ruby
|
453
|
+
class PostCard::Component < ApplicationViewComponent
|
454
|
+
with_collection_parameter :post
|
455
|
+
|
456
|
+
option :post
|
457
|
+
end
|
458
|
+
```
|
459
|
+
|
460
|
+
You can add this to following line to your component generator (unless it's already added): `with_collection_parameter :<%= singular_name %>` to always explicitly provide the collection parameter name.
|
461
|
+
|
437
462
|
## Wrapped components
|
438
463
|
|
439
464
|
Sometimes we need to wrap a component into a custom HTML container (for positioning or whatever). By default, such wrapping doesn't play well with the `#render?` method because if we don't need a component, we don't need a wrapper.
|
@@ -1,6 +1,10 @@
|
|
1
1
|
<div class="<%= container_class %>">
|
2
2
|
<%- if component -%>
|
3
|
-
|
3
|
+
<%- if local_assigns[:content_block] -%>
|
4
|
+
<%= render component, &content_block %>
|
5
|
+
<% else %>
|
6
|
+
<%= render component %>
|
7
|
+
<% end %>
|
4
8
|
<%- else -%>
|
5
9
|
Failed to infer a component from the preview: <%= error %>
|
6
10
|
<%- end -%>
|
@@ -60,8 +60,14 @@ module ViewComponentContrib
|
|
60
60
|
end
|
61
61
|
|
62
62
|
# Shortcut for render_with_template(locals: {component: ...})
|
63
|
-
def render_component(
|
64
|
-
|
63
|
+
def render_component(component_or_props = nil, &block)
|
64
|
+
component = if component_or_props.is_a?(::ViewComponent::Base)
|
65
|
+
component_or_props
|
66
|
+
else
|
67
|
+
self.class.name.sub(/Preview$/, "Component").constantize.new(**(component_or_props || {}))
|
68
|
+
end
|
69
|
+
|
70
|
+
render_with(component: component, content_block: block)
|
65
71
|
end
|
66
72
|
end
|
67
73
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: view_component-contrib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Dementyev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: view_component
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name: ruby-next
|
28
|
+
name: ruby-next-core
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -184,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
184
|
- !ruby/object:Gem::Version
|
185
185
|
version: '0'
|
186
186
|
requirements: []
|
187
|
-
rubygems_version: 3.4.
|
187
|
+
rubygems_version: 3.4.20
|
188
188
|
signing_key:
|
189
189
|
specification_version: 4
|
190
190
|
summary: A collection of extensions and developer tools for ViewComponent
|