view_component-contrib 0.1.5 → 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: 3dfe8a56d9d79e6fbb11442811f68a9cdc7980c4dcbe00fdeba825a11d5124c7
4
- data.tar.gz: 4ec650eab3f6c6b11ba79f598ecb79f34a3f900b44e1b6e53b55d08a9501f7aa
3
+ metadata.gz: cebcea7442b79ea935dd0039c361e813b597c8f63a15a9cd90407635a6a96856
4
+ data.tar.gz: 98e1aeecc1354a1e619bf75c765aa64f0051ac5ff4d7d0c81fb42425e7da7ae5
5
5
  SHA512:
6
- metadata.gz: 6a3f375f7e61f7b29ff6379caf4dec9d89a98226ca6c0c2bf55349e1ee3e28a6b6076840fe29a937b1700bc65546c6d946b575cc5337208a7ae49e555a75f3dc
7
- data.tar.gz: e129b0af8c6f11b979bd202e9761f2acb5dc267e895ad010043eac3a028f9a5312a3c552bfc49cac4fa173abf48b07578fc017ea126162ee017739b8de1a3207
6
+ metadata.gz: 427691d12a38bfcab3c0861506aade1ada63422badb1b207e5fff49cd72bc66c8a6aadfd220b51385ef5abc2166061cd8333afbf14e915058e98bf2899438ad5
7
+ data.tar.gz: 5eb44287d55acb724d458d6eca667296787215cafced750c9071834ee2203b89327d93ff983b1b2e9879b961e13eb8b5d4f2af237c690a4c3f6bc6cbdaa6654c
data/CHANGELOG.md CHANGED
@@ -2,6 +2,22 @@
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
+
5
21
  ## 0.1.5 (2023-11-02)
6
22
 
7
23
  - Support content blocks in `#render_component` and `#render_with`. ([@palkan][])
@@ -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
@@ -64,7 +82,7 @@ module ViewComponentContrib
64
82
  component = if component_or_props.is_a?(::ViewComponent::Base)
65
83
  component_or_props
66
84
  else
67
- self.class.name.sub(/Preview$/, "Component").constantize.new(**(component_or_props || {}))
85
+ self.class.component_class_name.constantize.new(**(component_or_props || {}))
68
86
  end
69
87
 
70
88
  render_with(component: component, content_block: block)
@@ -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.5"
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.5
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-11-03 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