view_component 2.53.0 → 2.54.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of view_component might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d505afaa4cb16810628e814c69bb2d00bc76498f6c681173d7811f44cd87c8e
4
- data.tar.gz: 2bc859d6034fed19e34739cf1844679bcf83332c7e227c487ddb7364e8166f4b
3
+ metadata.gz: b4b1f5b50f3ff33ba025588ac11f83362684484dae4aafea6b3be2a866944b3c
4
+ data.tar.gz: bdc4bf615413d7b3ab8e936c6df0ffd12b8915e03f44c5a8f6ad6989eb5fb5b5
5
5
  SHA512:
6
- metadata.gz: 75dcf204fd8e3dfad8a1da18de6d96d43fd779f1c9a73ca66b76f0db1ac7be04cf04447c9eaa901d32bc674d7eb7fa79fd5572bff08652a892a7e96f5186faaa
7
- data.tar.gz: 7be68adaa9de981a4fb12451ebcc19875f5754fa4798eef60f98702b1c7396bb598a1f6ae77b768bbe55e5b4f77c33f6b9db7156fa18f04c0a63075c54fdd7cc
6
+ metadata.gz: 98ce9d4ac6888dd6b6fed5987f2a6a95b6589c12930886bb35ec1009cdf385f507d3b199e63d27bd93f2125294cc5b8f7e085de27fd67ed6e45c613534bc41cb
7
+ data.tar.gz: '058bdd0c93a6af548eb77feaafb432fe5a6fa9ded2730b4576b7e87c713a403c81e6db5340aa70a78444b4ce8b9b95f729294391e98ff946ecc50f4c86837d48'
@@ -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
- prepend_view_path File.expand_path("../views", __dir__)
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
@@ -9,6 +9,24 @@ title: Changelog
9
9
 
10
10
  ## main
11
11
 
12
+ ## 2.54.0
13
+
14
+ * Add `with_*` slot API for defining slots. Note: we plan to deprecate the non `with_*` API for slots in an upcoming release.
15
+
16
+ *Blake Williams*
17
+
18
+ * Add QuickNode to list of companies that heavily rely on ViewComponent.
19
+
20
+ *Luc Castera*
21
+
22
+ * Include the `Translatable` module by default.
23
+
24
+ *Elia Schito*
25
+
26
+ * Update docs dependencies.
27
+
28
+ *Joel Hawksley*
29
+
12
30
  ## 2.53.0
13
31
 
14
32
  * Add support for relative I18n scopes to translations.
@@ -160,6 +178,10 @@ title: Changelog
160
178
 
161
179
  *Peter Goldstein*
162
180
 
181
+ * Move preview logic to module for easier app integration.
182
+
183
+ *Sammy Henningsson*
184
+
163
185
  ## 2.48.0
164
186
 
165
187
  * Correct path in example test command in Contributing docs.
@@ -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)
@@ -77,6 +77,7 @@ module ViewComponent
77
77
 
78
78
  define_render_template_for
79
79
 
80
+ component_class.build_i18n_backend
80
81
  component_class._after_compile
81
82
 
82
83
  CompileCache.register(component_class)
@@ -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.header(classes: "Foo") do %>
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. The method can be
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.item(name: "Foo") do %>
125
+ # <% component.with_item(name: "Foo") do %>
120
126
  # <p>One</p>
121
127
  # <% end %>
122
128
  #
123
- # <% component.item(name: "Bar") do %>
129
+ # <% component.with_item(name: "Bar") do %>
124
130
  # <p>two</p>
125
131
  # <% end %>
126
132
  # <% end %>
@@ -132,17 +138,32 @@ 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
+ ruby2_keywords(:"with_#{slot_name}") if respond_to?(:ruby2_keywords, true)
159
+
140
160
  # Instantiates and and adds multiple slots forwarding the first
141
161
  # argument to each slot constructor
142
162
  define_method slot_name do |collection_args = nil, &block|
143
163
  if collection_args.nil? && block.nil?
144
164
  get_slot(slot_name)
145
165
  else
166
+ # Deprecated: Will remove in 3.0
146
167
  collection_args.map do |args|
147
168
  set_slot(slot_name, nil, **args, &block)
148
169
  end
@@ -20,9 +20,7 @@ module ViewComponent
20
20
  @i18n_scope ||= virtual_path.sub(%r{^/}, "").gsub(%r{/_?}, ".")
21
21
  end
22
22
 
23
- def _after_compile
24
- super
25
-
23
+ def build_i18n_backend
26
24
  return if CompileCache.compiled? self
27
25
 
28
26
  if (translation_files = _sidecar_files(%w[yml yaml])).any?
@@ -3,7 +3,7 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 2
6
- MINOR = 53
6
+ MINOR = 54
7
7
  PATCH = 0
8
8
 
9
9
  STRING = [MAJOR, MINOR, PATCH].join(".")
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.53.0
4
+ version: 2.54.0
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-04-20 00:00:00.000000000 Z
11
+ date: 2022-05-10 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