view_component 4.0.0.rc2 → 4.0.0.rc4
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/docs/CHANGELOG.md +20 -0
- data/lib/generators/view_component/abstract_generator.rb +56 -0
- data/lib/generators/view_component/component/component_generator.rb +80 -0
- data/lib/generators/view_component/component/templates/component.rb.tt +21 -0
- data/lib/generators/view_component/erb/erb_generator.rb +34 -0
- data/lib/generators/view_component/erb/templates/component.html.erb.tt +1 -0
- data/lib/generators/view_component/haml/haml_generator.rb +22 -0
- data/lib/generators/view_component/haml/templates/component.html.haml.tt +1 -0
- data/lib/generators/view_component/locale/locale_generator.rb +46 -0
- data/lib/generators/view_component/preview/preview_generator.rb +39 -0
- data/lib/generators/view_component/preview/templates/component_preview.rb.tt +9 -0
- data/lib/generators/view_component/rspec/rspec_generator.rb +31 -0
- data/lib/generators/view_component/rspec/templates/component_spec.rb.tt +15 -0
- data/lib/generators/view_component/slim/slim_generator.rb +22 -0
- data/lib/generators/view_component/slim/templates/component.html.slim.tt +1 -0
- data/lib/generators/view_component/stimulus/stimulus_generator.rb +44 -0
- data/lib/generators/view_component/stimulus/templates/component_controller.js.tt +7 -0
- data/lib/generators/view_component/stimulus/templates/component_controller.ts.tt +9 -0
- data/lib/generators/view_component/tailwindcss/tailwindcss_generator.rb +11 -0
- data/lib/generators/view_component/tailwindcss/templates/component.html.erb.tt +1 -0
- data/lib/generators/view_component/test_unit/templates/component_test.rb.tt +12 -0
- data/lib/generators/view_component/test_unit/test_unit_generator.rb +20 -0
- data/lib/view_component/base.rb +13 -3
- data/lib/view_component/slot.rb +6 -2
- data/lib/view_component/version.rb +1 -1
- metadata +22 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d836f60a33d5a66087281b9edbb578a118d706721160676173390a8eab5d361
|
4
|
+
data.tar.gz: 9c6129650fbc464da3e315244eba6a893d5722dc9e577673efb0f3b090279386
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 46da774d6dd963153254af7e11ce5d03891d69c302e48220b5e7f54795ebdb58ca93d09af9975c47076bb58a13f379df60fafff04b597b5236bd77c3e2d122c2
|
7
|
+
data.tar.gz: a4038c0360c90b2958ce31b31bd50260d6e10964e15733de9c2a7b57488ddaaa0773d583da2efdbe81c7aaae340ad1f8324f00283dac5bd3db69588befb1b76d
|
data/docs/CHANGELOG.md
CHANGED
@@ -10,6 +10,26 @@ nav_order: 6
|
|
10
10
|
|
11
11
|
## main
|
12
12
|
|
13
|
+
## 4.0.0.rc4
|
14
|
+
|
15
|
+
* Fix issue where generators were not included in published gem.
|
16
|
+
|
17
|
+
*Jean-Louis Giordano*
|
18
|
+
|
19
|
+
## 4.0.0.rc3
|
20
|
+
|
21
|
+
* Reformat the avatars section to arrange them in a grid.
|
22
|
+
|
23
|
+
*Josh Cohen*
|
24
|
+
|
25
|
+
* Fix bug where relative paths in `translate` didn't work in blocks passed to ViewComponents.
|
26
|
+
|
27
|
+
*Joel Hawksley*
|
28
|
+
|
29
|
+
* Add SerpApi to "Who uses ViewComponent" list.
|
30
|
+
|
31
|
+
*Andy from SerpApi*
|
32
|
+
|
13
33
|
## 4.0.0.rc2
|
14
34
|
|
15
35
|
* Add `around_render` lifecyle method for wrapping component rendering in custom instrumentation, etc.
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ViewComponent
|
4
|
+
module AbstractGenerator
|
5
|
+
def copy_view_file
|
6
|
+
template("component.html.#{engine_name}", destination) unless options["inline"] || options["call"]
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def destination
|
12
|
+
File.join(destination_directory, "#{destination_file_name}.html.#{engine_name}")
|
13
|
+
end
|
14
|
+
|
15
|
+
def destination_directory
|
16
|
+
if sidecar?
|
17
|
+
File.join(component_path, class_path, destination_file_name)
|
18
|
+
else
|
19
|
+
File.join(component_path, class_path)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def destination_file_name
|
24
|
+
"#{file_name}_component"
|
25
|
+
end
|
26
|
+
|
27
|
+
def file_name
|
28
|
+
@_file_name ||= super.sub(/_component\z/i, "")
|
29
|
+
end
|
30
|
+
|
31
|
+
def component_path
|
32
|
+
ViewComponent::Base.config.generate.path
|
33
|
+
end
|
34
|
+
|
35
|
+
def stimulus_controller
|
36
|
+
if stimulus?
|
37
|
+
File.join(destination_directory, destination_file_name)
|
38
|
+
.sub("#{component_path}/", "")
|
39
|
+
.tr("_", "-")
|
40
|
+
.gsub("/", "--")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def sidecar?
|
45
|
+
options["sidecar"] || ViewComponent::Base.config.generate.sidecar
|
46
|
+
end
|
47
|
+
|
48
|
+
def stimulus?
|
49
|
+
options["stimulus"] || ViewComponent::Base.config.generate.stimulus_controller
|
50
|
+
end
|
51
|
+
|
52
|
+
def typescript?
|
53
|
+
options["typescript"] || ViewComponent::Base.config.generate.typescript
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "generators/view_component/abstract_generator"
|
4
|
+
|
5
|
+
module ViewComponent
|
6
|
+
module Generators
|
7
|
+
class ComponentGenerator < Rails::Generators::NamedBase
|
8
|
+
include ViewComponent::AbstractGenerator
|
9
|
+
|
10
|
+
source_root File.expand_path("templates", __dir__)
|
11
|
+
|
12
|
+
argument :attributes, type: :array, default: [], banner: "attribute"
|
13
|
+
check_class_collision suffix: "Component"
|
14
|
+
|
15
|
+
class_option :call, type: :boolean, default: false
|
16
|
+
class_option :inline, type: :boolean, default: false
|
17
|
+
class_option :locale, type: :boolean, default: ViewComponent::Base.config.generate.locale
|
18
|
+
class_option :parent, type: :string, desc: "The parent class for the generated component"
|
19
|
+
class_option :preview, type: :boolean, default: ViewComponent::Base.config.generate.preview
|
20
|
+
class_option :sidecar, type: :boolean, default: false
|
21
|
+
class_option :stimulus, type: :boolean,
|
22
|
+
default: ViewComponent::Base.config.generate.stimulus_controller
|
23
|
+
class_option :skip_suffix, type: :boolean, default: false
|
24
|
+
|
25
|
+
def create_component_file
|
26
|
+
template "component.rb", File.join(component_path, class_path, "#{file_name}#{options[:skip_suffix] ? "" : "_component"}.rb")
|
27
|
+
end
|
28
|
+
|
29
|
+
hook_for :test_framework
|
30
|
+
|
31
|
+
hook_for :preview, type: :boolean
|
32
|
+
|
33
|
+
hook_for :stimulus, type: :boolean
|
34
|
+
|
35
|
+
hook_for :locale, type: :boolean
|
36
|
+
|
37
|
+
hook_for :template_engine do |instance, template_engine|
|
38
|
+
instance.invoke template_engine, [instance.name]
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def parent_class
|
44
|
+
return options[:parent] if options[:parent]
|
45
|
+
|
46
|
+
ViewComponent::Base.config.generate.parent_class || default_parent_class
|
47
|
+
end
|
48
|
+
|
49
|
+
def initialize_signature?
|
50
|
+
initialize_signature.present?
|
51
|
+
end
|
52
|
+
|
53
|
+
def initialize_signature
|
54
|
+
return if attributes.blank?
|
55
|
+
|
56
|
+
attributes.map { |attr| "#{attr.name}:" }.join(", ")
|
57
|
+
end
|
58
|
+
|
59
|
+
def initialize_body
|
60
|
+
attributes.map { |attr| "@#{attr.name} = #{attr.name}" }.join("\n ")
|
61
|
+
end
|
62
|
+
|
63
|
+
def initialize_call_method_for_inline?
|
64
|
+
options["call"]
|
65
|
+
end
|
66
|
+
|
67
|
+
def inline_template?
|
68
|
+
options["inline"]
|
69
|
+
end
|
70
|
+
|
71
|
+
def template_engine
|
72
|
+
options["template_engine"]
|
73
|
+
end
|
74
|
+
|
75
|
+
def default_parent_class
|
76
|
+
defined?(ApplicationComponent) ? ApplicationComponent : ViewComponent::Base
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
<% module_namespacing do -%>
|
4
|
+
class <%= class_name %><%= options[:skip_suffix] ? "" : "Component" %> < <%= parent_class %>
|
5
|
+
<%- if inline_template? -%>
|
6
|
+
<%= template_engine %>_template <<~<%= template_engine.upcase %>
|
7
|
+
<h1>Hello, World!</h1>
|
8
|
+
<%= template_engine.upcase %>
|
9
|
+
<%- end -%>
|
10
|
+
<%- if initialize_signature? -%>
|
11
|
+
def initialize(<%= initialize_signature %>)
|
12
|
+
<%= initialize_body %>
|
13
|
+
end
|
14
|
+
<%- end -%>
|
15
|
+
<%- if initialize_call_method_for_inline? -%>
|
16
|
+
def call
|
17
|
+
content_tag :h1, "Hello world!"<%= ", data: { controller: \"#{stimulus_controller}\" }" if options["stimulus"] %>
|
18
|
+
end
|
19
|
+
<%- end -%>
|
20
|
+
end
|
21
|
+
<% end -%>
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators/erb"
|
4
|
+
require "generators/view_component/abstract_generator"
|
5
|
+
|
6
|
+
module ViewComponent
|
7
|
+
module Generators
|
8
|
+
class ErbGenerator < Rails::Generators::NamedBase
|
9
|
+
include ViewComponent::AbstractGenerator
|
10
|
+
|
11
|
+
source_root File.expand_path("templates", __dir__)
|
12
|
+
class_option :sidecar, type: :boolean, default: false
|
13
|
+
class_option :inline, type: :boolean, default: false
|
14
|
+
class_option :call, type: :boolean, default: false
|
15
|
+
class_option :stimulus, type: :boolean, default: false
|
16
|
+
|
17
|
+
def engine_name
|
18
|
+
"erb"
|
19
|
+
end
|
20
|
+
|
21
|
+
def copy_view_file
|
22
|
+
super
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def data_attributes
|
28
|
+
if stimulus?
|
29
|
+
" data-controller=\"#{stimulus_controller}\""
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<div<%= data_attributes %>>Add <%= class_name %> template here</div>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "generators/view_component/erb/erb_generator"
|
4
|
+
|
5
|
+
module ViewComponent
|
6
|
+
module Generators
|
7
|
+
class HamlGenerator < ViewComponent::Generators::ErbGenerator
|
8
|
+
include ViewComponent::AbstractGenerator
|
9
|
+
|
10
|
+
source_root File.expand_path("templates", __dir__)
|
11
|
+
class_option :sidecar, type: :boolean, default: false
|
12
|
+
|
13
|
+
def engine_name
|
14
|
+
"haml"
|
15
|
+
end
|
16
|
+
|
17
|
+
def copy_view_file
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
%div Add <%= class_name %> template here
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "generators/view_component/abstract_generator"
|
4
|
+
|
5
|
+
module ViewComponent
|
6
|
+
module Generators
|
7
|
+
class LocaleGenerator < ::Rails::Generators::NamedBase
|
8
|
+
include ViewComponent::AbstractGenerator
|
9
|
+
|
10
|
+
source_root File.expand_path("templates", __dir__)
|
11
|
+
argument :attributes, type: :array, default: [], banner: "attribute"
|
12
|
+
class_option :sidecar, type: :boolean, default: false
|
13
|
+
|
14
|
+
def create_locale_file
|
15
|
+
if ViewComponent::Base.config.generate.distinct_locale_files
|
16
|
+
I18n.available_locales.each do |locale|
|
17
|
+
create_file destination(locale), translations_hash([locale]).to_yaml
|
18
|
+
end
|
19
|
+
else
|
20
|
+
create_file destination, translations_hash(I18n.available_locales).to_yaml
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def translations_hash(locales = [:en])
|
27
|
+
locales.map { |locale| [locale.to_s, translation_keys] }.to_h
|
28
|
+
end
|
29
|
+
|
30
|
+
def translation_keys
|
31
|
+
keys = attributes.map(&:name)
|
32
|
+
keys = %w[hello] if keys.empty?
|
33
|
+
keys.map { |name| [name, name.capitalize] }.to_h
|
34
|
+
end
|
35
|
+
|
36
|
+
def destination(locale = nil)
|
37
|
+
extension = ".#{locale}" if locale
|
38
|
+
if sidecar?
|
39
|
+
File.join(component_path, class_path, "#{file_name}_component", "#{file_name}_component#{extension}.yml")
|
40
|
+
else
|
41
|
+
File.join(component_path, class_path, "#{file_name}_component#{extension}.yml")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ViewComponent
|
4
|
+
module Generators
|
5
|
+
class PreviewGenerator < ::Rails::Generators::NamedBase
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
7
|
+
class_option :preview_path, type: :string, desc: "Path for previews, required when multiple preview paths are configured", default: ViewComponent::Base.config.generate.preview_path
|
8
|
+
|
9
|
+
argument :attributes, type: :array, default: [], banner: "attribute"
|
10
|
+
check_class_collision suffix: "ComponentPreview"
|
11
|
+
|
12
|
+
def create_preview_file
|
13
|
+
preview_paths = ViewComponent::Base.config.previews.paths
|
14
|
+
optional_path = options[:preview_path]
|
15
|
+
return if preview_paths.count > 1 && optional_path.blank?
|
16
|
+
|
17
|
+
path_prefix = if optional_path.present?
|
18
|
+
optional_path
|
19
|
+
else
|
20
|
+
preview_paths.one? ? preview_paths.first : "test/components/previews"
|
21
|
+
end
|
22
|
+
|
23
|
+
template "component_preview.rb", File.join(path_prefix, class_path, "#{file_name}_component_preview.rb")
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def file_name
|
29
|
+
@_file_name ||= super.sub(/_component\z/i, "")
|
30
|
+
end
|
31
|
+
|
32
|
+
def render_signature
|
33
|
+
return if attributes.blank?
|
34
|
+
|
35
|
+
attributes.map { |attr| %(#{attr.name}: "#{attr.name}") }.join(", ")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "generators/view_component/abstract_generator"
|
4
|
+
|
5
|
+
module ViewComponent
|
6
|
+
module Generators
|
7
|
+
class RspecGenerator < ::Rails::Generators::NamedBase
|
8
|
+
include ViewComponent::AbstractGenerator
|
9
|
+
|
10
|
+
source_root File.expand_path("templates", __dir__)
|
11
|
+
|
12
|
+
def create_test_file
|
13
|
+
template "component_spec.rb", File.join(spec_component_path, class_path, "#{file_name}_component_spec.rb")
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def spec_component_path
|
19
|
+
return "spec/components" unless ViewComponent::Base.config.generate.use_component_path_for_rspec_tests
|
20
|
+
|
21
|
+
configured_component_path = component_path
|
22
|
+
if configured_component_path.start_with?("app#{File::SEPARATOR}")
|
23
|
+
_app, *rest_of_path = Pathname.new(configured_component_path).each_filename.to_a
|
24
|
+
File.join("spec", *rest_of_path)
|
25
|
+
else
|
26
|
+
"spec/components"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails_helper"
|
4
|
+
|
5
|
+
RSpec.describe <%= namespaced? ? "#{namespace.name}::" : '' %><%= class_name %>Component, type: :component do
|
6
|
+
pending "add some examples to (or delete) #{__FILE__}"
|
7
|
+
|
8
|
+
# it "renders something useful" do
|
9
|
+
# expect(
|
10
|
+
# render_inline(described_class.new(attr: "value")) { "Hello, components!" }.css("p").to_html
|
11
|
+
# ).to include(
|
12
|
+
# "Hello, components!"
|
13
|
+
# )
|
14
|
+
# end
|
15
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "generators/view_component/erb/erb_generator"
|
4
|
+
|
5
|
+
module ViewComponent
|
6
|
+
module Generators
|
7
|
+
class SlimGenerator < ViewComponent::Generators::ErbGenerator
|
8
|
+
include ViewComponent::AbstractGenerator
|
9
|
+
|
10
|
+
source_root File.expand_path("templates", __dir__)
|
11
|
+
class_option :sidecar, type: :boolean, default: false
|
12
|
+
|
13
|
+
def engine_name
|
14
|
+
"slim"
|
15
|
+
end
|
16
|
+
|
17
|
+
def copy_view_file
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
div Add <%= class_name %> template here
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "generators/view_component/abstract_generator"
|
4
|
+
|
5
|
+
module ViewComponent
|
6
|
+
module Generators
|
7
|
+
class StimulusGenerator < ::Rails::Generators::NamedBase
|
8
|
+
include ViewComponent::AbstractGenerator
|
9
|
+
|
10
|
+
source_root File.expand_path("templates", __dir__)
|
11
|
+
class_option :sidecar, type: :boolean, default: false
|
12
|
+
class_option :typescript, type: :boolean, default: false
|
13
|
+
|
14
|
+
def create_stimulus_controller
|
15
|
+
template "component_controller.#{filetype}", destination
|
16
|
+
end
|
17
|
+
|
18
|
+
def stimulus_module
|
19
|
+
return "stimulus" if legacy_stimulus?
|
20
|
+
|
21
|
+
"@hotwired/stimulus"
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def filetype
|
27
|
+
typescript? ? "ts" : "js"
|
28
|
+
end
|
29
|
+
|
30
|
+
def destination
|
31
|
+
if sidecar?
|
32
|
+
File.join(component_path, class_path, "#{file_name}_component", "#{file_name}_component_controller.#{filetype}")
|
33
|
+
else
|
34
|
+
File.join(component_path, class_path, "#{file_name}_component_controller.#{filetype}")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def legacy_stimulus?
|
39
|
+
package_json_pathname = Rails.root.join("package.json")
|
40
|
+
package_json_pathname.exist? && JSON.parse(package_json_pathname.read).dig("dependencies", "stimulus").present?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "generators/view_component/erb/erb_generator"
|
4
|
+
|
5
|
+
module ViewComponent
|
6
|
+
module Generators
|
7
|
+
class TailwindcssGenerator < ViewComponent::Generators::ErbGenerator
|
8
|
+
source_root File.expand_path("templates", __dir__)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
<div<%= data_attributes %>>Add <%= class_name %> template here</div>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
|
5
|
+
class <%= namespaced? ? "#{namespace.name}::" : '' %><%= class_name %>ComponentTest < ViewComponent::TestCase
|
6
|
+
def test_component_renders_something_useful
|
7
|
+
# assert_equal(
|
8
|
+
# %(<span>Hello, components!</span>),
|
9
|
+
# render_inline(<%= class_name %>Component.new(message: "Hello, components!")).css("span").to_html
|
10
|
+
# )
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ViewComponent
|
4
|
+
module Generators
|
5
|
+
class TestUnitGenerator < ::Rails::Generators::NamedBase
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
7
|
+
check_class_collision suffix: "ComponentTest"
|
8
|
+
|
9
|
+
def create_test_file
|
10
|
+
template "component_test.rb", File.join("test/components", class_path, "#{file_name}_component_test.rb")
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def file_name
|
16
|
+
@_file_name ||= super.sub(/_component\z/i, "")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/view_component/base.rb
CHANGED
@@ -108,7 +108,7 @@ module ViewComponent
|
|
108
108
|
self.class.__vc_compile(raise_errors: true)
|
109
109
|
|
110
110
|
@view_context = view_context
|
111
|
-
old_virtual_path = view_context.instance_variable_get(:@virtual_path)
|
111
|
+
@old_virtual_path = view_context.instance_variable_get(:@virtual_path)
|
112
112
|
self.__vc_original_view_context ||= view_context
|
113
113
|
|
114
114
|
@output_buffer = view_context.output_buffer
|
@@ -166,7 +166,7 @@ module ViewComponent
|
|
166
166
|
""
|
167
167
|
end
|
168
168
|
ensure
|
169
|
-
view_context.instance_variable_set(:@virtual_path, old_virtual_path)
|
169
|
+
view_context.instance_variable_set(:@virtual_path, @old_virtual_path)
|
170
170
|
@current_template = old_current_template
|
171
171
|
end
|
172
172
|
|
@@ -341,7 +341,9 @@ module ViewComponent
|
|
341
341
|
|
342
342
|
@__vc_content =
|
343
343
|
if __vc_render_in_block_provided?
|
344
|
-
|
344
|
+
with_original_virtual_path do
|
345
|
+
view_context.capture(self, &@__vc_render_in_block)
|
346
|
+
end
|
345
347
|
elsif __vc_content_set_by_with_content_defined?
|
346
348
|
@__vc_content_set_by_with_content
|
347
349
|
end
|
@@ -358,6 +360,14 @@ module ViewComponent
|
|
358
360
|
self.class.__vc_response_format
|
359
361
|
end
|
360
362
|
|
363
|
+
# @private
|
364
|
+
def with_original_virtual_path
|
365
|
+
@view_context.instance_variable_set(:@virtual_path, @old_virtual_path)
|
366
|
+
yield
|
367
|
+
ensure
|
368
|
+
@view_context.instance_variable_set(:@virtual_path, virtual_path)
|
369
|
+
end
|
370
|
+
|
361
371
|
private
|
362
372
|
|
363
373
|
attr_reader :view_context
|
data/lib/view_component/slot.rb
CHANGED
@@ -58,7 +58,9 @@ module ViewComponent
|
|
58
58
|
if defined?(@__vc_content_block)
|
59
59
|
# render_in is faster than `parent.render`
|
60
60
|
@__vc_component_instance.render_in(view_context) do |*args|
|
61
|
-
@
|
61
|
+
@parent.with_original_virtual_path do
|
62
|
+
@__vc_content_block.call(*args)
|
63
|
+
end
|
62
64
|
end
|
63
65
|
else
|
64
66
|
@__vc_component_instance.render_in(view_context)
|
@@ -66,7 +68,9 @@ module ViewComponent
|
|
66
68
|
elsif defined?(@__vc_content)
|
67
69
|
@__vc_content
|
68
70
|
elsif defined?(@__vc_content_block)
|
69
|
-
|
71
|
+
@parent.with_original_virtual_path do
|
72
|
+
view_context.capture(&@__vc_content_block)
|
73
|
+
end
|
70
74
|
elsif defined?(@__vc_content_set_by_with_content)
|
71
75
|
@__vc_content_set_by_with_content
|
72
76
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: view_component
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.0.0.
|
4
|
+
version: 4.0.0.rc4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ViewComponent Team
|
@@ -59,6 +59,27 @@ files:
|
|
59
59
|
- app/views/view_components/preview.html.erb
|
60
60
|
- app/views/view_components/previews.html.erb
|
61
61
|
- docs/CHANGELOG.md
|
62
|
+
- lib/generators/view_component/abstract_generator.rb
|
63
|
+
- lib/generators/view_component/component/component_generator.rb
|
64
|
+
- lib/generators/view_component/component/templates/component.rb.tt
|
65
|
+
- lib/generators/view_component/erb/erb_generator.rb
|
66
|
+
- lib/generators/view_component/erb/templates/component.html.erb.tt
|
67
|
+
- lib/generators/view_component/haml/haml_generator.rb
|
68
|
+
- lib/generators/view_component/haml/templates/component.html.haml.tt
|
69
|
+
- lib/generators/view_component/locale/locale_generator.rb
|
70
|
+
- lib/generators/view_component/preview/preview_generator.rb
|
71
|
+
- lib/generators/view_component/preview/templates/component_preview.rb.tt
|
72
|
+
- lib/generators/view_component/rspec/rspec_generator.rb
|
73
|
+
- lib/generators/view_component/rspec/templates/component_spec.rb.tt
|
74
|
+
- lib/generators/view_component/slim/slim_generator.rb
|
75
|
+
- lib/generators/view_component/slim/templates/component.html.slim.tt
|
76
|
+
- lib/generators/view_component/stimulus/stimulus_generator.rb
|
77
|
+
- lib/generators/view_component/stimulus/templates/component_controller.js.tt
|
78
|
+
- lib/generators/view_component/stimulus/templates/component_controller.ts.tt
|
79
|
+
- lib/generators/view_component/tailwindcss/tailwindcss_generator.rb
|
80
|
+
- lib/generators/view_component/tailwindcss/templates/component.html.erb.tt
|
81
|
+
- lib/generators/view_component/test_unit/templates/component_test.rb.tt
|
82
|
+
- lib/generators/view_component/test_unit/test_unit_generator.rb
|
62
83
|
- lib/view_component.rb
|
63
84
|
- lib/view_component/base.rb
|
64
85
|
- lib/view_component/collection.rb
|