view_component 2.52.0 → 2.54.1
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/app/controllers/concerns/view_component/preview_actions.rb +97 -0
- data/app/controllers/view_components_controller.rb +1 -87
- data/docs/CHANGELOG.md +66 -1
- data/lib/view_component/base.rb +2 -1
- data/lib/view_component/compiler.rb +1 -0
- data/lib/view_component/polymorphic_slots.rb +6 -0
- data/lib/view_component/slotable_v2.rb +26 -6
- data/lib/view_component/translatable.rb +4 -4
- data/lib/view_component/version.rb +2 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9cab3c05c054f173b248b903912dfb432eeec09b2d3d42f6fd602a7565acd2d
|
4
|
+
data.tar.gz: 78b70e8ea1306fb635978dbe2a86cece2618247bb0079798e5a886f2210dfed1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 653d1c627f3fb52677e47516472fec508c562359e3beac3defa43d172d3e793359207df638f352f1c1cc6a876a0f3c3fdc4c7ff4eefdc1907c20a161e9359d83
|
7
|
+
data.tar.gz: 25bc7a21cde84c3311d9482cdb2ccccf821ce2c104492506a999e81f8cd9d9a5b3b3c19f3b417dc3f250787adf18fbe9f9d9ff5589e897fc199e26e50e4b0efa
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ViewComponent
|
4
|
+
module PreviewActions
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
prepend_view_path File.expand_path("../../../views", __dir__)
|
9
|
+
|
10
|
+
around_action :set_locale, only: :previews
|
11
|
+
before_action :find_preview, only: :previews
|
12
|
+
before_action :require_local!, unless: :show_previews?
|
13
|
+
|
14
|
+
if respond_to?(:content_security_policy)
|
15
|
+
content_security_policy(false)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def index
|
20
|
+
@previews = ViewComponent::Preview.all
|
21
|
+
@page_title = "Component Previews"
|
22
|
+
render "view_components/index", **determine_layout
|
23
|
+
end
|
24
|
+
|
25
|
+
def previews
|
26
|
+
if params[:path] == @preview.preview_name
|
27
|
+
@page_title = "Component Previews for #{@preview.preview_name}"
|
28
|
+
render "view_components/previews", **determine_layout
|
29
|
+
else
|
30
|
+
prepend_application_view_paths
|
31
|
+
prepend_preview_examples_view_path
|
32
|
+
@example_name = File.basename(params[:path])
|
33
|
+
@render_args = @preview.render_args(@example_name, params: params.permit!)
|
34
|
+
layout = determine_layout(@render_args[:layout], prepend_views: false)[:layout]
|
35
|
+
locals = @render_args[:locals]
|
36
|
+
opts = {}
|
37
|
+
opts[:layout] = layout if layout.present? || layout == false
|
38
|
+
opts[:locals] = locals if locals.present?
|
39
|
+
render "view_components/preview", opts # rubocop:disable GitHub/RailsControllerRenderLiteral
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def default_preview_layout # :doc:
|
46
|
+
ViewComponent::Base.default_preview_layout
|
47
|
+
end
|
48
|
+
|
49
|
+
def show_previews? # :doc:
|
50
|
+
ViewComponent::Base.show_previews
|
51
|
+
end
|
52
|
+
|
53
|
+
def find_preview # :doc:
|
54
|
+
candidates = []
|
55
|
+
params[:path].to_s.scan(%r{/|$}) { candidates << $` }
|
56
|
+
preview = candidates.detect { |candidate| ViewComponent::Preview.exists?(candidate) }
|
57
|
+
|
58
|
+
if preview
|
59
|
+
@preview = ViewComponent::Preview.find(preview)
|
60
|
+
else
|
61
|
+
raise AbstractController::ActionNotFound, "Component preview '#{params[:path]}' not found."
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def set_locale
|
66
|
+
I18n.with_locale(params[:locale] || I18n.default_locale) do
|
67
|
+
yield
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Returns either {} or {layout: value} depending on configuration
|
72
|
+
def determine_layout(layout_override = nil, prepend_views: true)
|
73
|
+
return {} unless defined?(Rails.root)
|
74
|
+
|
75
|
+
layout_declaration = {}
|
76
|
+
|
77
|
+
if !layout_override.nil?
|
78
|
+
# Allow component-level override, even if false (thus no layout rendered)
|
79
|
+
layout_declaration[:layout] = layout_override
|
80
|
+
elsif default_preview_layout.present?
|
81
|
+
layout_declaration[:layout] = default_preview_layout
|
82
|
+
end
|
83
|
+
|
84
|
+
prepend_application_view_paths if layout_declaration[:layout].present? && prepend_views
|
85
|
+
|
86
|
+
layout_declaration
|
87
|
+
end
|
88
|
+
|
89
|
+
def prepend_application_view_paths
|
90
|
+
prepend_view_path Rails.root.join("app/views") if defined?(Rails.root)
|
91
|
+
end
|
92
|
+
|
93
|
+
def prepend_preview_examples_view_path
|
94
|
+
prepend_view_path(ViewComponent::Base.preview_paths)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -3,91 +3,5 @@
|
|
3
3
|
require "rails/application_controller"
|
4
4
|
|
5
5
|
class ViewComponentsController < Rails::ApplicationController # :nodoc:
|
6
|
-
|
7
|
-
|
8
|
-
around_action :set_locale, only: :previews
|
9
|
-
before_action :find_preview, only: :previews
|
10
|
-
before_action :require_local!, unless: :show_previews?
|
11
|
-
|
12
|
-
if respond_to?(:content_security_policy)
|
13
|
-
content_security_policy(false)
|
14
|
-
end
|
15
|
-
|
16
|
-
def index
|
17
|
-
@previews = ViewComponent::Preview.all
|
18
|
-
@page_title = "Component Previews"
|
19
|
-
render "view_components/index", **determine_layout
|
20
|
-
end
|
21
|
-
|
22
|
-
def previews
|
23
|
-
if params[:path] == @preview.preview_name
|
24
|
-
@page_title = "Component Previews for #{@preview.preview_name}"
|
25
|
-
render "view_components/previews", **determine_layout
|
26
|
-
else
|
27
|
-
prepend_application_view_paths
|
28
|
-
prepend_preview_examples_view_path
|
29
|
-
@example_name = File.basename(params[:path])
|
30
|
-
@render_args = @preview.render_args(@example_name, params: params.permit!)
|
31
|
-
layout = determine_layout(@render_args[:layout], prepend_views: false)[:layout]
|
32
|
-
locals = @render_args[:locals]
|
33
|
-
opts = {}
|
34
|
-
opts[:layout] = layout if layout.present? || layout == false
|
35
|
-
opts[:locals] = locals if locals.present?
|
36
|
-
render "view_components/preview", opts # rubocop:disable GitHub/RailsControllerRenderLiteral
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
41
|
-
|
42
|
-
def default_preview_layout # :doc:
|
43
|
-
ViewComponent::Base.default_preview_layout
|
44
|
-
end
|
45
|
-
|
46
|
-
def show_previews? # :doc:
|
47
|
-
ViewComponent::Base.show_previews
|
48
|
-
end
|
49
|
-
|
50
|
-
def find_preview # :doc:
|
51
|
-
candidates = []
|
52
|
-
params[:path].to_s.scan(%r{/|$}) { candidates << $` }
|
53
|
-
preview = candidates.detect { |candidate| ViewComponent::Preview.exists?(candidate) }
|
54
|
-
|
55
|
-
if preview
|
56
|
-
@preview = ViewComponent::Preview.find(preview)
|
57
|
-
else
|
58
|
-
raise AbstractController::ActionNotFound, "Component preview '#{params[:path]}' not found."
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def set_locale
|
63
|
-
I18n.with_locale(params[:locale] || I18n.default_locale) do
|
64
|
-
yield
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
# Returns either {} or {layout: value} depending on configuration
|
69
|
-
def determine_layout(layout_override = nil, prepend_views: true)
|
70
|
-
return {} unless defined?(Rails.root)
|
71
|
-
|
72
|
-
layout_declaration = {}
|
73
|
-
|
74
|
-
if !layout_override.nil?
|
75
|
-
# Allow component-level override, even if false (thus no layout rendered)
|
76
|
-
layout_declaration[:layout] = layout_override
|
77
|
-
elsif default_preview_layout.present?
|
78
|
-
layout_declaration[:layout] = default_preview_layout
|
79
|
-
end
|
80
|
-
|
81
|
-
prepend_application_view_paths if layout_declaration[:layout].present? && prepend_views
|
82
|
-
|
83
|
-
layout_declaration
|
84
|
-
end
|
85
|
-
|
86
|
-
def prepend_application_view_paths
|
87
|
-
prepend_view_path Rails.root.join("app/views") if defined?(Rails.root)
|
88
|
-
end
|
89
|
-
|
90
|
-
def prepend_preview_examples_view_path
|
91
|
-
prepend_view_path(ViewComponent::Base.preview_paths)
|
92
|
-
end
|
6
|
+
include ViewComponent::PreviewActions
|
93
7
|
end
|
data/docs/CHANGELOG.md
CHANGED
@@ -3,10 +3,71 @@ layout: default
|
|
3
3
|
title: Changelog
|
4
4
|
---
|
5
5
|
|
6
|
+
<!-- Add unreleased changes under the "main" heading. -->
|
7
|
+
|
6
8
|
# Changelog
|
7
9
|
|
8
10
|
## main
|
9
11
|
|
12
|
+
## 2.54.1
|
13
|
+
|
14
|
+
* Update docs dependencies.
|
15
|
+
|
16
|
+
*Joel Hawksley*
|
17
|
+
|
18
|
+
* Resolve warning in slots API.
|
19
|
+
* Raise in the test environment when ViewComponent code emits a warning.
|
20
|
+
|
21
|
+
*Blake Williams*
|
22
|
+
|
23
|
+
## 2.54.0
|
24
|
+
|
25
|
+
* Add `with_*` slot API for defining slots. Note: we plan to deprecate the non `with_*` API for slots in an upcoming release.
|
26
|
+
|
27
|
+
*Blake Williams*
|
28
|
+
|
29
|
+
* Add QuickNode to list of companies that heavily rely on ViewComponent.
|
30
|
+
|
31
|
+
*Luc Castera*
|
32
|
+
|
33
|
+
* Include the `Translatable` module by default.
|
34
|
+
|
35
|
+
*Elia Schito*
|
36
|
+
|
37
|
+
* Update docs dependencies.
|
38
|
+
|
39
|
+
*Joel Hawksley*
|
40
|
+
|
41
|
+
## 2.53.0
|
42
|
+
|
43
|
+
* Add support for relative I18n scopes to translations.
|
44
|
+
|
45
|
+
*Elia Schito*
|
46
|
+
|
47
|
+
* Update CI configuration to use latest Rails 7.0.
|
48
|
+
|
49
|
+
*Hans Lemuet*
|
50
|
+
|
51
|
+
* Document how to use blocks with lambda slots.
|
52
|
+
|
53
|
+
*Sam Partington*
|
54
|
+
|
55
|
+
* Skip Rails 5.2 in local test environment if using incompatible Ruby version.
|
56
|
+
|
57
|
+
*Cameron Dutro*, *Blake Williams*, *Joel Hawksley*
|
58
|
+
|
59
|
+
* Improve landing page documentation.
|
60
|
+
|
61
|
+
*Jason Swett*
|
62
|
+
|
63
|
+
* Add Bearer to list of companies that heavily rely on ViewComponent.
|
64
|
+
|
65
|
+
*Yaroslav Shmarov*
|
66
|
+
|
67
|
+
* Add articles to resources page.
|
68
|
+
|
69
|
+
*Joel Hawksley*
|
70
|
+
|
10
71
|
## 2.52.0
|
11
72
|
|
12
73
|
* Add ADR for separate slot getter/setter API.
|
@@ -124,10 +185,14 @@ title: Changelog
|
|
124
185
|
|
125
186
|
*Joel Hawksley*
|
126
187
|
|
127
|
-
* Add Ruby 3.1 and Rails 7.0 to CI
|
188
|
+
* Add Ruby 3.1 and Rails 7.0 to CI.
|
128
189
|
|
129
190
|
*Peter Goldstein*
|
130
191
|
|
192
|
+
* Move preview logic to module for easier app integration.
|
193
|
+
|
194
|
+
*Sammy Henningsson*
|
195
|
+
|
131
196
|
## 2.48.0
|
132
197
|
|
133
198
|
* Correct path in example test command in Contributing docs.
|
data/lib/view_component/base.rb
CHANGED
@@ -17,6 +17,7 @@ module ViewComponent
|
|
17
17
|
include ViewComponent::ContentAreas
|
18
18
|
include ViewComponent::Previewable
|
19
19
|
include ViewComponent::SlotableV2
|
20
|
+
include ViewComponent::Translatable
|
20
21
|
include ViewComponent::WithContentHelper
|
21
22
|
|
22
23
|
ViewContextCalledBeforeRenderError = Class.new(StandardError)
|
@@ -69,7 +70,7 @@ module ViewComponent
|
|
69
70
|
@view_context = view_context
|
70
71
|
self.__vc_original_view_context ||= view_context
|
71
72
|
|
72
|
-
@output_buffer = ActionView::OutputBuffer.new unless @global_buffer_in_use
|
73
|
+
@output_buffer = ActionView::OutputBuffer.new unless defined?(@global_buffer_in_use) && @global_buffer_in_use
|
73
74
|
|
74
75
|
@lookup_context ||= view_context.lookup_context
|
75
76
|
|
@@ -45,10 +45,16 @@ module ViewComponent
|
|
45
45
|
"#{slot_name}_#{poly_type}"
|
46
46
|
end
|
47
47
|
|
48
|
+
# Deprecated: Will be removed in 3.0
|
48
49
|
define_method(setter_name) do |*args, &block|
|
49
50
|
set_polymorphic_slot(slot_name, poly_type, *args, &block)
|
50
51
|
end
|
51
52
|
ruby2_keywords(setter_name.to_sym) if respond_to?(:ruby2_keywords, true)
|
53
|
+
|
54
|
+
define_method("with_#{setter_name}") do |*args, &block|
|
55
|
+
set_polymorphic_slot(slot_name, poly_type, *args, &block)
|
56
|
+
end
|
57
|
+
ruby2_keywords(:"with_#{setter_name}") if respond_to?(:ruby2_keywords, true)
|
52
58
|
end
|
53
59
|
|
54
60
|
self.registered_slots[slot_name] = {
|
@@ -61,20 +61,26 @@ module ViewComponent
|
|
61
61
|
# = Setting sub-component content
|
62
62
|
#
|
63
63
|
# Consumers of the component can render a sub-component by calling a
|
64
|
-
# helper method with the same name as the slot
|
64
|
+
# helper method with the same name as the slot prefixed with `with_`.
|
65
65
|
#
|
66
66
|
# <%= render_inline(MyComponent.new) do |component| %>
|
67
|
-
# <% component.
|
67
|
+
# <% component.with_header(classes: "Foo") do %>
|
68
68
|
# <p>Bar</p>
|
69
69
|
# <% end %>
|
70
70
|
# <% end %>
|
71
71
|
def renders_one(slot_name, callable = nil)
|
72
72
|
validate_singular_slot_name(slot_name)
|
73
73
|
|
74
|
+
define_method :"with_#{slot_name}" do |*args, &block|
|
75
|
+
set_slot(slot_name, nil, *args, &block)
|
76
|
+
end
|
77
|
+
ruby2_keywords(:"with_#{slot_name}") if respond_to?(:ruby2_keywords, true)
|
78
|
+
|
74
79
|
define_method slot_name do |*args, &block|
|
75
80
|
if args.empty? && block.nil?
|
76
81
|
get_slot(slot_name)
|
77
82
|
else
|
83
|
+
# Deprecated: Will remove in 3.0
|
78
84
|
set_slot(slot_name, nil, *args, &block)
|
79
85
|
end
|
80
86
|
end
|
@@ -112,15 +118,15 @@ module ViewComponent
|
|
112
118
|
# = Setting sub-component content
|
113
119
|
#
|
114
120
|
# Consumers of the component can set the content of a slot by calling a
|
115
|
-
# helper method with the same name as the slot
|
116
|
-
# called multiple times to append to the slot.
|
121
|
+
# helper method with the same name as the slot prefixed with `with_`. The
|
122
|
+
# method can be called multiple times to append to the slot.
|
117
123
|
#
|
118
124
|
# <%= render_inline(MyComponent.new) do |component| %>
|
119
|
-
# <% component.
|
125
|
+
# <% component.with_item(name: "Foo") do %>
|
120
126
|
# <p>One</p>
|
121
127
|
# <% end %>
|
122
128
|
#
|
123
|
-
# <% component.
|
129
|
+
# <% component.with_item(name: "Bar") do %>
|
124
130
|
# <p>two</p>
|
125
131
|
# <% end %>
|
126
132
|
# <% end %>
|
@@ -132,17 +138,31 @@ module ViewComponent
|
|
132
138
|
# Define setter for singular names
|
133
139
|
# for example `renders_many :items` allows fetching all tabs with
|
134
140
|
# `component.tabs` and setting a tab with `component.tab`
|
141
|
+
#
|
142
|
+
# Deprecated: Will remove in 3.0
|
135
143
|
define_method singular_name do |*args, &block|
|
136
144
|
set_slot(slot_name, nil, *args, &block)
|
137
145
|
end
|
138
146
|
ruby2_keywords(singular_name.to_sym) if respond_to?(:ruby2_keywords, true)
|
139
147
|
|
148
|
+
define_method :"with_#{singular_name}" do |*args, &block|
|
149
|
+
set_slot(slot_name, nil, *args, &block)
|
150
|
+
end
|
151
|
+
ruby2_keywords(:"with_#{singular_name}") if respond_to?(:ruby2_keywords, true)
|
152
|
+
|
153
|
+
define_method :"with_#{slot_name}" do |collection_args = nil, &block|
|
154
|
+
collection_args.map do |args|
|
155
|
+
set_slot(slot_name, nil, **args, &block)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
140
159
|
# Instantiates and and adds multiple slots forwarding the first
|
141
160
|
# argument to each slot constructor
|
142
161
|
define_method slot_name do |collection_args = nil, &block|
|
143
162
|
if collection_args.nil? && block.nil?
|
144
163
|
get_slot(slot_name)
|
145
164
|
else
|
165
|
+
# Deprecated: Will remove in 3.0
|
146
166
|
collection_args.map do |args|
|
147
167
|
set_slot(slot_name, nil, **args, &block)
|
148
168
|
end
|
@@ -3,7 +3,6 @@
|
|
3
3
|
require "erb"
|
4
4
|
require "set"
|
5
5
|
require "i18n"
|
6
|
-
require "action_view/helpers/translation_helper"
|
7
6
|
require "active_support/concern"
|
8
7
|
|
9
8
|
module ViewComponent
|
@@ -21,9 +20,7 @@ module ViewComponent
|
|
21
20
|
@i18n_scope ||= virtual_path.sub(%r{^/}, "").gsub(%r{/_?}, ".")
|
22
21
|
end
|
23
22
|
|
24
|
-
def
|
25
|
-
super
|
26
|
-
|
23
|
+
def build_i18n_backend
|
27
24
|
return if CompileCache.compiled? self
|
28
25
|
|
29
26
|
if (translation_files = _sidecar_files(%w[yml yaml])).any?
|
@@ -68,7 +65,10 @@ module ViewComponent
|
|
68
65
|
return key.map { |k| translate(k, **options) } if key.is_a?(Array)
|
69
66
|
|
70
67
|
locale = options.delete(:locale) || ::I18n.locale
|
68
|
+
scope = options.delete(:scope)
|
69
|
+
scope = scope.join(".") if scope.is_a? Array
|
71
70
|
key = key&.to_s unless key.is_a?(String)
|
71
|
+
key = "#{scope}.#{key}" if scope
|
72
72
|
key = "#{i18n_scope}#{key}" if key.start_with?(".")
|
73
73
|
|
74
74
|
if HTML_SAFE_TRANSLATION_KEY.match?(key)
|
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.54.1
|
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: 2022-
|
11
|
+
date: 2022-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -293,6 +293,7 @@ files:
|
|
293
293
|
- README.md
|
294
294
|
- app/assets/vendor/prism.css
|
295
295
|
- app/assets/vendor/prism.min.js
|
296
|
+
- app/controllers/concerns/view_component/preview_actions.rb
|
296
297
|
- app/controllers/view_components_controller.rb
|
297
298
|
- app/helpers/preview_helper.rb
|
298
299
|
- app/views/test_mailer/test_email.html.erb
|