view_component-contrib 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ade40aaa2e5db43b72e6ee1de31978639bd057aef496e2aa5885296b67d58ac4
4
- data.tar.gz: ab518d6378b41e51fe978a50b00e22bd7e6e03231a8e93c1d1f3d1ad707f0ea4
3
+ metadata.gz: cebcea7442b79ea935dd0039c361e813b597c8f63a15a9cd90407635a6a96856
4
+ data.tar.gz: 98e1aeecc1354a1e619bf75c765aa64f0051ac5ff4d7d0c81fb42425e7da7ae5
5
5
  SHA512:
6
- metadata.gz: 15b64d686c8dd5d871c1c5b3c07adbbfa9fd4f538130df0ab4bda67cdf88f4109db386f6f311e3392a005860ea0e06115f18e1112fa536bab034ee3264acf79e
7
- data.tar.gz: 32a4e8f9106866b0f49808a5b5250023d08a001ac3b1c66c66cb7971820046679570040b70e98ebdc56ad7113ce87945b59112156a3c5a941f1b022d1bc6c0bb
6
+ metadata.gz: 427691d12a38bfcab3c0861506aade1ada63422badb1b207e5fff49cd72bc66c8a6aadfd220b51385ef5abc2166061cd8333afbf14e915058e98bf2899438ad5
7
+ data.tar.gz: 5eb44287d55acb724d458d6eca667296787215cafced750c9071834ee2203b89327d93ff983b1b2e9879b961e13eb8b5d4f2af237c690a4c3f6bc6cbdaa6654c
data/CHANGELOG.md CHANGED
@@ -2,6 +2,53 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.1.6 (2023-11-07)
6
+
7
+ - Support preview classes named `<component|partial>_preview.rb`. ([@palkan][])
8
+
9
+ It's also possible to explicitly specify the component class name for the preview class:
10
+
11
+ ```ruby
12
+ class MyComponentPreview
13
+ self.component_class_name = "SomeComponent"
14
+
15
+ def default
16
+ render_component
17
+ end
18
+ end
19
+ ```
20
+
21
+ ## 0.1.5 (2023-11-02)
22
+
23
+ - Support content blocks in `#render_component` and `#render_with`. ([@palkan][])
24
+
25
+ ```ruby
26
+ class MyComponent::Preview
27
+ def default
28
+ # Now you can pass a block to render_component to render it inside the component:
29
+ render_component(kind: "info") do
30
+ "Welcome!"
31
+ end
32
+ end
33
+ end
34
+ ```
35
+
36
+ - Support implicit components in `#render_component` helper. ([@palkan][])
37
+
38
+ ```ruby
39
+ class MyComponent::Preview
40
+ def default
41
+ # Before
42
+ render_component(MyComponent::Component.new(foo: "bar"))
43
+ end
44
+
45
+ # After
46
+ def default
47
+ render_component(foo: "bar")
48
+ end
49
+ end
50
+ ```
51
+
5
52
  ## 0.1.4 (2023-04-30)
6
53
 
7
54
  - 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
- <%= render component %>
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 -%>
@@ -43,11 +43,29 @@ module ViewComponentContrib
43
43
  end
44
44
  end
45
45
 
46
+ # Infer component class name from preview class name:
47
+ # - Namespace::ButtonPreview => Namespace::Button::Component | Namespace::ButtonComponent | Namespace::Button
48
+ # - Button::Preview => Button::Component | ButtonComponent | Button
49
+ def component_class_name
50
+ @component_class_name ||= begin
51
+ component_name = name.sub(/(::Preview|Preview)$/, "")
52
+ [
53
+ "#{component_name}::Component",
54
+ "#{component_name}Component",
55
+ component_name
56
+ ].find do
57
+ _1.safe_constantize
58
+ end
59
+ end
60
+ end
61
+
62
+ attr_writer :component_class_name
63
+
46
64
  private
47
65
 
48
66
  def build_component_instance(locals)
49
67
  return locals unless locals[:component].nil?
50
- locals[:component] = name.sub(/Preview$/, "Component").safe_constantize&.new
68
+ locals[:component] = component_class_name.safe_constantize&.new
51
69
  rescue => e
52
70
  locals[:component] = nil
53
71
  locals[:error] = e.message
@@ -60,8 +78,14 @@ module ViewComponentContrib
60
78
  end
61
79
 
62
80
  # Shortcut for render_with_template(locals: {component: ...})
63
- def render_component(component)
64
- render_with(component: component)
81
+ def render_component(component_or_props = nil, &block)
82
+ component = if component_or_props.is_a?(::ViewComponent::Base)
83
+ component_or_props
84
+ else
85
+ self.class.component_class_name.constantize.new(**(component_or_props || {}))
86
+ end
87
+
88
+ render_with(component: component, content_block: block)
65
89
  end
66
90
  end
67
91
  end
@@ -3,7 +3,7 @@
3
3
  module ViewComponentContrib
4
4
  module Preview
5
5
  module Sidecarable
6
- PREVIEW_GLOB = "**/preview.rb"
6
+ PREVIEW_GLOB = "**/{preview.rb,*_preview.rb}"
7
7
 
8
8
  def self.extended(base)
9
9
  base.singleton_class.prepend(ClassMethods)
@@ -17,7 +17,7 @@ module ViewComponentContrib
17
17
  end
18
18
 
19
19
  def preview_name
20
- name.chomp("::Preview").underscore
20
+ name.sub(/(::Preview|Preview)$/, "").underscore
21
21
  end
22
22
  end
23
23
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ViewComponentContrib # :nodoc:all
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.6"
5
5
  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
4
+ version: 0.1.6
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-04-30 00:00:00.000000000 Z
11
+ date: 2023-11-07 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.8
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