view_component 2.65.0 → 2.68.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 +4 -4
- data/LICENSE.txt +1 -1
- data/app/controllers/concerns/view_component/preview_actions.rb +6 -10
- data/app/helpers/preview_helper.rb +2 -2
- data/app/views/view_components/preview.html.erb +2 -2
- data/docs/CHANGELOG.md +34 -0
- data/lib/rails/generators/abstract_generator.rb +3 -5
- data/lib/rails/generators/component/component_generator.rb +5 -4
- data/lib/rails/generators/locale/component_generator.rb +1 -1
- data/lib/rails/generators/preview/component_generator.rb +1 -1
- data/lib/view_component/base.rb +21 -30
- data/lib/view_component/config.rb +159 -0
- data/lib/view_component/engine.rb +10 -25
- data/lib/view_component/test_helpers.rb +44 -4
- data/lib/view_component/version.rb +1 -1
- data/lib/view_component.rb +1 -0
- metadata +15 -16
- data/lib/view_component/previewable.rb +0 -62
- data/lib/view_component/render_preview_helper.rb +0 -50
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc70b125c7bf45480588c0161df7993d19ce3f3c67f9303c2e7c09d836bc3b5e
|
4
|
+
data.tar.gz: 7cbaec3b7b25d762cfc8068f932520135c4e989a549754cdb9733d6c356853f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5afca6dd4a94bf0ee395fa72d577a7ce0031f590b6d6cd4c1fd7625422d71c5ebe6cce52111f9350b66803d3421cf504c694f8b35229fc726e2bd1d32dd414db
|
7
|
+
data.tar.gz: 185329894deb2a89735087da12093040e08a2009ba8b9fe934b83731539f93c479d130e2cd3a7d112895ad489b268167f0c8c71712ef42582921975e729eb59d
|
data/LICENSE.txt
CHANGED
@@ -10,9 +10,7 @@ module ViewComponent
|
|
10
10
|
around_action :set_locale, only: :previews
|
11
11
|
before_action :require_local!, unless: :show_previews?
|
12
12
|
|
13
|
-
if respond_to?(:content_security_policy)
|
14
|
-
content_security_policy(false)
|
15
|
-
end
|
13
|
+
content_security_policy(false) if respond_to?(:content_security_policy)
|
16
14
|
end
|
17
15
|
|
18
16
|
def index
|
@@ -45,18 +43,18 @@ module ViewComponent
|
|
45
43
|
|
46
44
|
# :doc:
|
47
45
|
def default_preview_layout
|
48
|
-
ViewComponent::Base.default_preview_layout
|
46
|
+
ViewComponent::Base.config.default_preview_layout
|
49
47
|
end
|
50
48
|
|
51
49
|
# :doc:
|
52
50
|
def show_previews?
|
53
|
-
ViewComponent::Base.show_previews
|
51
|
+
ViewComponent::Base.config.show_previews
|
54
52
|
end
|
55
53
|
|
56
54
|
# :doc:
|
57
55
|
def find_preview
|
58
56
|
candidates = []
|
59
|
-
params[:path].to_s.scan(%r{/|$}) { candidates <<
|
57
|
+
params[:path].to_s.scan(%r{/|$}) { candidates << Regexp.last_match.pre_match }
|
60
58
|
preview = candidates.detect { |candidate| ViewComponent::Preview.exists?(candidate) }
|
61
59
|
|
62
60
|
if preview
|
@@ -66,10 +64,8 @@ module ViewComponent
|
|
66
64
|
end
|
67
65
|
end
|
68
66
|
|
69
|
-
def set_locale
|
70
|
-
I18n.with_locale(params[:locale] || I18n.default_locale)
|
71
|
-
yield
|
72
|
-
end
|
67
|
+
def set_locale(&block)
|
68
|
+
I18n.with_locale(params[:locale] || I18n.default_locale, &block)
|
73
69
|
end
|
74
70
|
|
75
71
|
# Returns either {} or {layout: value} depending on configuration
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module PreviewHelper
|
4
|
-
AVAILABLE_PRISM_LANGUAGES = [
|
4
|
+
AVAILABLE_PRISM_LANGUAGES = %w[ruby erb haml]
|
5
5
|
FALLBACK_LANGUAGE = "ruby"
|
6
6
|
|
7
7
|
def preview_source
|
@@ -22,7 +22,7 @@ module PreviewHelper
|
|
22
22
|
# Fetch template source via finding it through preview paths
|
23
23
|
# to accomodate source view when exclusively using templates
|
24
24
|
# for previews for Rails < 6.1.
|
25
|
-
all_template_paths = ViewComponent::Base.preview_paths.map do |preview_path|
|
25
|
+
all_template_paths = ViewComponent::Base.config.preview_paths.map do |preview_path|
|
26
26
|
Dir.glob("#{preview_path}/**/*")
|
27
27
|
end.flatten
|
28
28
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
<% if @render_args[:component] %>
|
2
|
-
<% if ViewComponent::Base.render_monkey_patch_enabled || Rails.version.to_f >= 6.1 %>
|
2
|
+
<% if ViewComponent::Base.config.render_monkey_patch_enabled || Rails.version.to_f >= 6.1 %>
|
3
3
|
<%= render(@render_args[:component], @render_args[:args], &@render_args[:block]) %>
|
4
4
|
<% else %>
|
5
5
|
<%= render_component(@render_args[:component], &@render_args[:block]) %>
|
@@ -8,6 +8,6 @@
|
|
8
8
|
<%= render template: @render_args[:template], locals: @render_args[:locals] || {} %>
|
9
9
|
<% end %>
|
10
10
|
|
11
|
-
<% if ViewComponent::Base.show_previews_source %>
|
11
|
+
<% if ViewComponent::Base.config.show_previews_source %>
|
12
12
|
<%= preview_source %>
|
13
13
|
<% end %>
|
data/docs/CHANGELOG.md
CHANGED
@@ -9,6 +9,40 @@ title: Changelog
|
|
9
9
|
|
10
10
|
## main
|
11
11
|
|
12
|
+
## 2.68.0
|
13
|
+
|
14
|
+
* Update `gemspec` author to be ViewComponent team.
|
15
|
+
|
16
|
+
*Joel Hawksley*
|
17
|
+
|
18
|
+
* Fix bug where `ViewComponent::Compiler` wasn't required.
|
19
|
+
|
20
|
+
*Joel Hawksley*
|
21
|
+
|
22
|
+
## 2.67.0
|
23
|
+
|
24
|
+
* Use ViewComponent::Base.config as the internal endpoint for config.
|
25
|
+
|
26
|
+
*Simon Fish*
|
27
|
+
|
28
|
+
* Fix bug where `#with_request_url`, when used with query string, set the incorrect `request.path` and `request.fullpath`.
|
29
|
+
|
30
|
+
*Franz Liedke*
|
31
|
+
|
32
|
+
* Add link to [ViewComponentAttributes](https://github.com/amba-Health/view_component_attributes) in Resources section of docs.
|
33
|
+
|
34
|
+
*Romaric Pascal*
|
35
|
+
|
36
|
+
* `render_preview` test helper is available by default. It is no longer necessary to include `ViewComponent::RenderPreviewHelper`.
|
37
|
+
|
38
|
+
*Joel Hawksley*
|
39
|
+
|
40
|
+
## 2.66.0
|
41
|
+
|
42
|
+
* Add missing `generate.sidecar`, `generate.stimulus_controller`, `generate.locale`, `generate.distinct_locale_files`, `generate.preview` config options to `config.view_component`.
|
43
|
+
|
44
|
+
*Simon Fish*
|
45
|
+
|
12
46
|
## 2.65.0
|
13
47
|
|
14
48
|
* Raise `ArgumentError` when conflicting Slots are defined.
|
@@ -3,9 +3,7 @@
|
|
3
3
|
module ViewComponent
|
4
4
|
module AbstractGenerator
|
5
5
|
def copy_view_file
|
6
|
-
unless options["inline"]
|
7
|
-
template "component.html.#{engine_name}", destination
|
8
|
-
end
|
6
|
+
template "component.html.#{engine_name}", destination unless options["inline"]
|
9
7
|
end
|
10
8
|
|
11
9
|
private
|
@@ -31,7 +29,7 @@ module ViewComponent
|
|
31
29
|
end
|
32
30
|
|
33
31
|
def component_path
|
34
|
-
ViewComponent::Base.view_component_path
|
32
|
+
ViewComponent::Base.config.view_component_path
|
35
33
|
end
|
36
34
|
|
37
35
|
def stimulus_controller
|
@@ -44,7 +42,7 @@ module ViewComponent
|
|
44
42
|
end
|
45
43
|
|
46
44
|
def sidecar?
|
47
|
-
options["sidecar"] || ViewComponent::Base.generate.sidecar
|
45
|
+
options["sidecar"] || ViewComponent::Base.config.generate.sidecar
|
48
46
|
end
|
49
47
|
end
|
50
48
|
end
|
@@ -13,11 +13,12 @@ module Rails
|
|
13
13
|
check_class_collision suffix: "Component"
|
14
14
|
|
15
15
|
class_option :inline, type: :boolean, default: false
|
16
|
-
class_option :locale, type: :boolean, default: ViewComponent::Base.generate.locale
|
16
|
+
class_option :locale, type: :boolean, default: ViewComponent::Base.config.generate.locale
|
17
17
|
class_option :parent, type: :string, desc: "The parent class for the generated component"
|
18
|
-
class_option :preview, type: :boolean, default: ViewComponent::Base.generate.preview
|
18
|
+
class_option :preview, type: :boolean, default: ViewComponent::Base.config.generate.preview
|
19
19
|
class_option :sidecar, type: :boolean, default: false
|
20
|
-
class_option :stimulus, type: :boolean,
|
20
|
+
class_option :stimulus, type: :boolean,
|
21
|
+
default: ViewComponent::Base.config.generate.stimulus_controller
|
21
22
|
|
22
23
|
def create_component_file
|
23
24
|
template "component.rb", File.join(component_path, class_path, "#{file_name}_component.rb")
|
@@ -40,7 +41,7 @@ module Rails
|
|
40
41
|
def parent_class
|
41
42
|
return options[:parent] if options[:parent]
|
42
43
|
|
43
|
-
ViewComponent::Base.component_parent_class || default_parent_class
|
44
|
+
ViewComponent::Base.config.component_parent_class || default_parent_class
|
44
45
|
end
|
45
46
|
|
46
47
|
def initialize_signature
|
@@ -12,7 +12,7 @@ module Locale
|
|
12
12
|
class_option :sidecar, type: :boolean, default: false
|
13
13
|
|
14
14
|
def create_locale_file
|
15
|
-
if ViewComponent::Base.generate.distinct_locale_files
|
15
|
+
if ViewComponent::Base.config.generate.distinct_locale_files
|
16
16
|
I18n.available_locales.each do |locale|
|
17
17
|
create_file destination(locale), translations_hash([locale]).to_yaml
|
18
18
|
end
|
@@ -9,7 +9,7 @@ module Preview
|
|
9
9
|
check_class_collision suffix: "ComponentPreview"
|
10
10
|
|
11
11
|
def create_preview_file
|
12
|
-
preview_paths =
|
12
|
+
preview_paths = ViewComponent::Base.config.preview_paths
|
13
13
|
return if preview_paths.count > 1
|
14
14
|
|
15
15
|
path_prefix = preview_paths.one? ? preview_paths.first : "test/components/previews"
|
data/lib/view_component/base.rb
CHANGED
@@ -4,9 +4,10 @@ require "action_view"
|
|
4
4
|
require "active_support/configurable"
|
5
5
|
require "view_component/collection"
|
6
6
|
require "view_component/compile_cache"
|
7
|
+
require "view_component/compiler"
|
8
|
+
require "view_component/config"
|
7
9
|
require "view_component/content_areas"
|
8
10
|
require "view_component/polymorphic_slots"
|
9
|
-
require "view_component/previewable"
|
10
11
|
require "view_component/slotable"
|
11
12
|
require "view_component/slotable_v2"
|
12
13
|
require "view_component/translatable"
|
@@ -14,10 +15,16 @@ require "view_component/with_content_helper"
|
|
14
15
|
|
15
16
|
module ViewComponent
|
16
17
|
class Base < ActionView::Base
|
17
|
-
|
18
|
+
class << self
|
19
|
+
delegate(*ViewComponent::Config.defaults.keys, to: :config)
|
20
|
+
|
21
|
+
def config
|
22
|
+
@config ||= ViewComponent::Config.defaults
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
18
26
|
include ViewComponent::ContentAreas
|
19
27
|
include ViewComponent::PolymorphicSlots
|
20
|
-
include ViewComponent::Previewable
|
21
28
|
include ViewComponent::SlotableV2
|
22
29
|
include ViewComponent::Translatable
|
23
30
|
include ViewComponent::WithContentHelper
|
@@ -113,11 +120,9 @@ module ViewComponent
|
|
113
120
|
@current_template = self
|
114
121
|
|
115
122
|
if block && defined?(@__vc_content_set_by_with_content)
|
116
|
-
raise ArgumentError.
|
117
|
-
"It looks like a block was provided after calling `with_content` on #{self.class.name}, " \
|
123
|
+
raise ArgumentError, "It looks like a block was provided after calling `with_content` on #{self.class.name}, " \
|
118
124
|
"which means that ViewComponent doesn't know which content to use.\n\n" \
|
119
125
|
"To fix this issue, use either `with_content` or a block."
|
120
|
-
)
|
121
126
|
end
|
122
127
|
|
123
128
|
@__vc_content_evaluated = false
|
@@ -262,9 +267,7 @@ module ViewComponent
|
|
262
267
|
# @private
|
263
268
|
def format
|
264
269
|
# Ruby 2.6 throws a warning without checking `defined?`, 2.7 doesn't
|
265
|
-
if defined?(@__vc_variant)
|
266
|
-
@__vc_variant
|
267
|
-
end
|
270
|
+
@__vc_variant if defined?(@__vc_variant)
|
268
271
|
end
|
269
272
|
|
270
273
|
# Use the provided variant instead of the one determined by the current request.
|
@@ -313,11 +316,9 @@ module ViewComponent
|
|
313
316
|
# config.view_component.test_controller = "MyTestController"
|
314
317
|
# ```
|
315
318
|
#
|
316
|
-
# Defaults to ApplicationController. Can also be
|
317
|
-
# basis using `with_controller_class`.
|
319
|
+
# Defaults to `nil`. If this is falsy, `"ApplicationController"` is used. Can also be
|
320
|
+
# configured on a per-test basis using `with_controller_class`.
|
318
321
|
#
|
319
|
-
mattr_accessor :test_controller
|
320
|
-
@@test_controller = "ApplicationController"
|
321
322
|
|
322
323
|
# Set if render monkey patches should be included or not in Rails <6.1:
|
323
324
|
#
|
@@ -325,7 +326,6 @@ module ViewComponent
|
|
325
326
|
# config.view_component.render_monkey_patch_enabled = false
|
326
327
|
# ```
|
327
328
|
#
|
328
|
-
mattr_accessor :render_monkey_patch_enabled, instance_writer: false, default: true
|
329
329
|
|
330
330
|
# Path for component files
|
331
331
|
#
|
@@ -333,9 +333,8 @@ module ViewComponent
|
|
333
333
|
# config.view_component.view_component_path = "app/my_components"
|
334
334
|
# ```
|
335
335
|
#
|
336
|
-
# Defaults to `app/components
|
336
|
+
# Defaults to `nil`. If this is falsy, `app/components` is used.
|
337
337
|
#
|
338
|
-
mattr_accessor :view_component_path, instance_writer: false, default: "app/components"
|
339
338
|
|
340
339
|
# Parent class for generated components
|
341
340
|
#
|
@@ -346,7 +345,6 @@ module ViewComponent
|
|
346
345
|
# Defaults to nil. If this is falsy, generators will use
|
347
346
|
# "ApplicationComponent" if defined, "ViewComponent::Base" otherwise.
|
348
347
|
#
|
349
|
-
mattr_accessor :component_parent_class, instance_writer: false
|
350
348
|
|
351
349
|
# Configuration for generators.
|
352
350
|
#
|
@@ -397,7 +395,6 @@ module ViewComponent
|
|
397
395
|
# ```
|
398
396
|
#
|
399
397
|
# Defaults to `false`.
|
400
|
-
mattr_accessor :generate, instance_writer: false, default: ActiveSupport::OrderedOptions.new(false)
|
401
398
|
|
402
399
|
class << self
|
403
400
|
# @private
|
@@ -489,8 +486,8 @@ module ViewComponent
|
|
489
486
|
|
490
487
|
# If Rails application is loaded, add application url_helpers to the component context
|
491
488
|
# we need to check this to use this gem as a dependency
|
492
|
-
if defined?(Rails) && Rails.application
|
493
|
-
child.include Rails.application.routes.url_helpers
|
489
|
+
if defined?(Rails) && Rails.application && !(child < Rails.application.routes.url_helpers)
|
490
|
+
child.include Rails.application.routes.url_helpers
|
494
491
|
end
|
495
492
|
|
496
493
|
# Derive the source location of the component Ruby file from the call stack.
|
@@ -500,7 +497,7 @@ module ViewComponent
|
|
500
497
|
|
501
498
|
# Removes the first part of the path and the extension.
|
502
499
|
child.virtual_path = child.source_location.gsub(
|
503
|
-
|
500
|
+
/(.*#{Regexp.quote(ViewComponent::Base.config.view_component_path)})|(\.rb)/, ""
|
504
501
|
)
|
505
502
|
|
506
503
|
# Set collection parameter to the extended component
|
@@ -591,20 +588,16 @@ module ViewComponent
|
|
591
588
|
# parameters will be empty and ViewComponent will not be able to render
|
592
589
|
# the component.
|
593
590
|
if initialize_parameters.empty?
|
594
|
-
raise ArgumentError.
|
595
|
-
"The #{self} initializer is empty or invalid." \
|
591
|
+
raise ArgumentError, "The #{self} initializer is empty or invalid." \
|
596
592
|
"It must accept the parameter `#{parameter}` to render it as a collection.\n\n" \
|
597
593
|
"To fix this issue, update the initializer to accept `#{parameter}`.\n\n" \
|
598
594
|
"See https://viewcomponent.org/guide/collections.html for more information on rendering collections."
|
599
|
-
)
|
600
595
|
end
|
601
596
|
|
602
|
-
raise ArgumentError
|
603
|
-
"The initializer for #{self} doesn't accept the parameter `#{parameter}`, " \
|
597
|
+
raise ArgumentError, "The initializer for #{self} doesn't accept the parameter `#{parameter}`, " \
|
604
598
|
"which is required in order to render it as a collection.\n\n" \
|
605
599
|
"To fix this issue, update the initializer to accept `#{parameter}`.\n\n" \
|
606
600
|
"See https://viewcomponent.org/guide/collections.html for more information on rendering collections."
|
607
|
-
)
|
608
601
|
end
|
609
602
|
|
610
603
|
# Ensure the component initializer doesn't define
|
@@ -614,10 +607,8 @@ module ViewComponent
|
|
614
607
|
def validate_initialization_parameters!
|
615
608
|
return unless initialize_parameter_names.include?(RESERVED_PARAMETER)
|
616
609
|
|
617
|
-
raise ViewComponent::ComponentError
|
618
|
-
"#{self} initializer can't accept the parameter `#{RESERVED_PARAMETER}`, as it will override a " \
|
610
|
+
raise ViewComponent::ComponentError, "#{self} initializer can't accept the parameter `#{RESERVED_PARAMETER}`, as it will override a " \
|
619
611
|
"public ViewComponent method. To fix this issue, rename the parameter."
|
620
|
-
)
|
621
612
|
end
|
622
613
|
|
623
614
|
# @private
|
@@ -0,0 +1,159 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "view_component/deprecation"
|
4
|
+
|
5
|
+
module ViewComponent
|
6
|
+
class Config
|
7
|
+
class << self
|
8
|
+
# `new` without any arguments initializes the default configuration, but
|
9
|
+
# it's important to differentiate in case that's no longer the case in
|
10
|
+
# future.
|
11
|
+
alias_method :default, :new
|
12
|
+
|
13
|
+
def defaults
|
14
|
+
ActiveSupport::OrderedOptions.new.merge!({
|
15
|
+
generate: ActiveSupport::OrderedOptions.new(false),
|
16
|
+
preview_controller: "ViewComponentsController",
|
17
|
+
preview_route: "/rails/view_components",
|
18
|
+
show_previews_source: false,
|
19
|
+
instrumentation_enabled: false,
|
20
|
+
render_monkey_patch_enabled: true,
|
21
|
+
view_component_path: "app/components",
|
22
|
+
component_parent_class: nil,
|
23
|
+
show_previews: Rails.env.development? || Rails.env.test?,
|
24
|
+
preview_paths: default_preview_paths,
|
25
|
+
test_controller: "ApplicationController",
|
26
|
+
default_preview_layout: nil
|
27
|
+
})
|
28
|
+
end
|
29
|
+
|
30
|
+
# @!attribute generate
|
31
|
+
# @return [ActiveSupport::OrderedOptions]
|
32
|
+
# The subset of configuration options relating to generators.
|
33
|
+
#
|
34
|
+
# All options under this namespace default to `false` unless otherwise
|
35
|
+
# stated.
|
36
|
+
#
|
37
|
+
# #### #sidecar
|
38
|
+
#
|
39
|
+
# Always generate a component with a sidecar directory:
|
40
|
+
#
|
41
|
+
# config.view_component.generate.sidecar = true
|
42
|
+
#
|
43
|
+
# #### #stimulus_controller
|
44
|
+
#
|
45
|
+
# Always generate a Stimulus controller alongside the component:
|
46
|
+
#
|
47
|
+
# config.view_component.generate.stimulus_controller = true
|
48
|
+
#
|
49
|
+
# #### #locale
|
50
|
+
#
|
51
|
+
# Always generate translations file alongside the component:
|
52
|
+
#
|
53
|
+
# config.view_component.generate.locale = true
|
54
|
+
#
|
55
|
+
# #### #distinct_locale_files
|
56
|
+
#
|
57
|
+
# Always generate as many translations files as available locales:
|
58
|
+
#
|
59
|
+
# config.view_component.generate.distinct_locale_files = true
|
60
|
+
#
|
61
|
+
# One file will be generated for each configured `I18n.available_locales`,
|
62
|
+
# falling back to `[:en]` when no `available_locales` is defined.
|
63
|
+
#
|
64
|
+
# #### #preview
|
65
|
+
#
|
66
|
+
# Always generate a preview alongside the component:
|
67
|
+
#
|
68
|
+
# config.view_component.generate.preview = true
|
69
|
+
|
70
|
+
# @!attribute preview_controller
|
71
|
+
# @return [String]
|
72
|
+
# The controller used for previewing components.
|
73
|
+
# Defaults to `ViewComponentsController`.
|
74
|
+
|
75
|
+
# @!attribute preview_route
|
76
|
+
# @return [String]
|
77
|
+
# The entry route for component previews.
|
78
|
+
# Defaults to `"/rails/view_components"`.
|
79
|
+
|
80
|
+
# @!attribute show_previews_source
|
81
|
+
# @return [Boolean]
|
82
|
+
# Whether to display source code previews in component previews.
|
83
|
+
# Defaults to `false`.
|
84
|
+
|
85
|
+
# @!attribute instrumentation_enabled
|
86
|
+
# @return [Boolean]
|
87
|
+
# Whether ActiveSupport notifications are enabled.
|
88
|
+
# Defaults to `false`.
|
89
|
+
|
90
|
+
# @!attribute render_monkey_patch_enabled
|
91
|
+
# @return [Boolean] Whether the #render method should be monkey patched.
|
92
|
+
# If this is disabled, use `#render_component` or
|
93
|
+
# `#render_component_to_string` instead.
|
94
|
+
# Defaults to `true`.
|
95
|
+
|
96
|
+
# @!attribute view_component_path
|
97
|
+
# @return [String]
|
98
|
+
# The path in which components, their templates, and their sidecars should
|
99
|
+
# be stored.
|
100
|
+
# Defaults to `"app/components"`.
|
101
|
+
|
102
|
+
# @!attribute component_parent_class
|
103
|
+
# @return [String]
|
104
|
+
# The parent class from which generated components will inherit.
|
105
|
+
# Defaults to `nil`. If this is falsy, generators will use
|
106
|
+
# `"ApplicationComponent"` if defined, `"ViewComponent::Base"` otherwise.
|
107
|
+
|
108
|
+
# @!attribute show_previews
|
109
|
+
# @return [Boolean]
|
110
|
+
# Whether component previews are enabled.
|
111
|
+
# Defaults to `true` in development and test environments.
|
112
|
+
|
113
|
+
# @!attribute preview_paths
|
114
|
+
# @return [Array<String>]
|
115
|
+
# The locations in which component previews will be looked up.
|
116
|
+
# Defaults to `['test/component/previews']` relative to your Rails root.
|
117
|
+
|
118
|
+
# @!attribute preview_path
|
119
|
+
# @deprecated Use #preview_paths instead. Will be removed in v3.0.0.
|
120
|
+
|
121
|
+
# @!attribute test_controller
|
122
|
+
# @return [String]
|
123
|
+
# The controller used for testing components.
|
124
|
+
# Can also be configured on a per-test basis using `#with_controller_class`.
|
125
|
+
# Defaults to `ApplicationController`.
|
126
|
+
|
127
|
+
# @!attribute default_preview_layout
|
128
|
+
# @return [String]
|
129
|
+
# A custom default layout used for the previews index page and individual
|
130
|
+
# previews.
|
131
|
+
# Defaults to `nil`. If this is falsy, `"component_preview"` is used.
|
132
|
+
|
133
|
+
def default_preview_paths
|
134
|
+
return [] unless defined?(Rails.root) && Dir.exist?("#{Rails.root}/test/components/previews")
|
135
|
+
|
136
|
+
["#{Rails.root}/test/components/previews"]
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def initialize
|
141
|
+
@config = self.class.defaults
|
142
|
+
end
|
143
|
+
|
144
|
+
def preview_path
|
145
|
+
preview_paths
|
146
|
+
end
|
147
|
+
|
148
|
+
def preview_path=(new_value)
|
149
|
+
ViewComponent::Deprecation.warn("`preview_path` will be removed in v3.0.0. Use `preview_paths` instead.")
|
150
|
+
self.preview_paths = Array.wrap(new_value)
|
151
|
+
end
|
152
|
+
|
153
|
+
delegate_missing_to :config
|
154
|
+
|
155
|
+
private
|
156
|
+
|
157
|
+
attr_reader :config
|
158
|
+
end
|
159
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "rails"
|
4
|
+
require "view_component/base"
|
4
5
|
|
5
6
|
module ViewComponent
|
6
7
|
class Engine < Rails::Engine # :nodoc:
|
7
|
-
config.view_component =
|
8
|
-
config.view_component.preview_paths ||= []
|
8
|
+
config.view_component = ViewComponent::Base.config
|
9
9
|
|
10
10
|
rake_tasks do
|
11
11
|
load "view_component/rails/tasks/view_component.rake"
|
@@ -14,25 +14,20 @@ module ViewComponent
|
|
14
14
|
initializer "view_component.set_configs" do |app|
|
15
15
|
options = app.config.view_component
|
16
16
|
|
17
|
+
%i[generate preview_controller preview_route show_previews_source].each do |config_option|
|
18
|
+
options[config_option] ||= ViewComponent::Base.public_send(config_option)
|
19
|
+
end
|
20
|
+
options.instrumentation_enabled = false if options.instrumentation_enabled.nil?
|
17
21
|
options.render_monkey_patch_enabled = true if options.render_monkey_patch_enabled.nil?
|
18
22
|
options.show_previews = Rails.env.development? || Rails.env.test? if options.show_previews.nil?
|
19
|
-
options.show_previews_source ||= ViewComponent::Base.show_previews_source
|
20
23
|
options.instrumentation_enabled = false if options.instrumentation_enabled.nil?
|
21
|
-
options.preview_route ||= ViewComponent::Base.preview_route
|
22
|
-
options.preview_controller ||= ViewComponent::Base.preview_controller
|
23
24
|
|
24
25
|
if options.show_previews
|
26
|
+
# This is still necessary because when `config.view_component` is declared, `Rails.root` is unspecified.
|
25
27
|
options.preview_paths << "#{Rails.root}/test/components/previews" if defined?(Rails.root) && Dir.exist?(
|
26
28
|
"#{Rails.root}/test/components/previews"
|
27
29
|
)
|
28
30
|
|
29
|
-
if options.preview_path.present?
|
30
|
-
ViewComponent::Deprecation.warn(
|
31
|
-
"`preview_path` will be removed in v3.0.0. Use `preview_paths` instead."
|
32
|
-
)
|
33
|
-
options.preview_paths << options.preview_path
|
34
|
-
end
|
35
|
-
|
36
31
|
if options.show_previews_source
|
37
32
|
require "method_source"
|
38
33
|
|
@@ -41,10 +36,6 @@ module ViewComponent
|
|
41
36
|
end
|
42
37
|
end
|
43
38
|
end
|
44
|
-
|
45
|
-
ActiveSupport.on_load(:view_component) do
|
46
|
-
options.each { |k, v| send("#{k}=", v) if respond_to?("#{k}=") }
|
47
|
-
end
|
48
39
|
end
|
49
40
|
|
50
41
|
initializer "view_component.enable_instrumentation" do |app|
|
@@ -72,12 +63,6 @@ module ViewComponent
|
|
72
63
|
end
|
73
64
|
end
|
74
65
|
|
75
|
-
initializer "view_component.compile_config_methods" do
|
76
|
-
ActiveSupport.on_load(:view_component) do
|
77
|
-
config.compile_methods! if config.respond_to?(:compile_methods!)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
66
|
initializer "view_component.monkey_patch_render" do |app|
|
82
67
|
next if Rails.version.to_f >= 6.1 || !app.config.view_component.render_monkey_patch_enabled
|
83
68
|
|
@@ -94,7 +79,7 @@ module ViewComponent
|
|
94
79
|
end
|
95
80
|
end
|
96
81
|
|
97
|
-
initializer "view_component.include_render_component" do |
|
82
|
+
initializer "view_component.include_render_component" do |_app|
|
98
83
|
next if Rails.version.to_f >= 6.1
|
99
84
|
|
100
85
|
ActiveSupport.on_load(:action_view) do
|
@@ -116,7 +101,7 @@ module ViewComponent
|
|
116
101
|
end
|
117
102
|
end
|
118
103
|
|
119
|
-
initializer "compiler mode" do |
|
104
|
+
initializer "compiler mode" do |_app|
|
120
105
|
ViewComponent::Compiler.mode = if Rails.env.development? || Rails.env.test?
|
121
106
|
ViewComponent::Compiler::DEVELOPMENT_MODE
|
122
107
|
else
|
@@ -160,7 +145,7 @@ unless defined?(ViewComponent::Base)
|
|
160
145
|
|
161
146
|
ViewComponent::Deprecation.warn(
|
162
147
|
"This manually engine loading is deprecated and will be removed in v3.0.0. " \
|
163
|
-
|
148
|
+
'Remove `require "view_component/engine"`.'
|
164
149
|
)
|
165
150
|
|
166
151
|
require "view_component"
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "view_component/render_preview_helper"
|
4
|
-
|
5
3
|
module ViewComponent
|
6
4
|
module TestHelpers
|
7
5
|
begin
|
@@ -67,6 +65,47 @@ module ViewComponent
|
|
67
65
|
Nokogiri::HTML.fragment(@rendered_content)
|
68
66
|
end
|
69
67
|
|
68
|
+
# Render a preview inline. Internally sets `page` to be a `Capybara::Node::Simple`,
|
69
|
+
# allowing for Capybara assertions to be used:
|
70
|
+
#
|
71
|
+
# ```ruby
|
72
|
+
# render_preview(:default)
|
73
|
+
# assert_text("Hello, World!")
|
74
|
+
# ```
|
75
|
+
#
|
76
|
+
# Note: `#rendered_preview` expects a preview to be defined with the same class
|
77
|
+
# name as the calling test, but with `Test` replaced with `Preview`:
|
78
|
+
#
|
79
|
+
# MyComponentTest -> MyComponentPreview etc.
|
80
|
+
#
|
81
|
+
# In RSpec, `Preview` is appended to `described_class`.
|
82
|
+
#
|
83
|
+
# @param preview [String] The name of the preview to be rendered.
|
84
|
+
# @return [Nokogiri::HTML]
|
85
|
+
def render_preview(name)
|
86
|
+
begin
|
87
|
+
preview_klass = if respond_to?(:described_class)
|
88
|
+
raise "`render_preview` expected a described_class, but it is nil." if described_class.nil?
|
89
|
+
|
90
|
+
"#{described_class}Preview"
|
91
|
+
else
|
92
|
+
self.class.name.gsub("Test", "Preview")
|
93
|
+
end
|
94
|
+
preview_klass = preview_klass.constantize
|
95
|
+
rescue NameError
|
96
|
+
raise NameError, "`render_preview` expected to find #{preview_klass}, but it does not exist."
|
97
|
+
end
|
98
|
+
|
99
|
+
previews_controller = build_controller(Rails.application.config.view_component.preview_controller.constantize)
|
100
|
+
previews_controller.request.params[:path] = "#{preview_klass.preview_name}/#{name}"
|
101
|
+
previews_controller.response = ActionDispatch::Response.new
|
102
|
+
result = previews_controller.previews
|
103
|
+
|
104
|
+
@rendered_content = result
|
105
|
+
|
106
|
+
Nokogiri::HTML.fragment(@rendered_content)
|
107
|
+
end
|
108
|
+
|
70
109
|
# Execute the given block in the view context. Internally sets `page` to be a
|
71
110
|
# `Capybara::Node::Simple`, allowing for Capybara assertions to be used:
|
72
111
|
#
|
@@ -151,10 +190,11 @@ module ViewComponent
|
|
151
190
|
old_request_query_string = request.query_string
|
152
191
|
old_controller = defined?(@controller) && @controller
|
153
192
|
|
193
|
+
path, query = path.split("?", 2)
|
154
194
|
request.path_info = path
|
155
195
|
request.path_parameters = Rails.application.routes.recognize_path(path)
|
156
|
-
request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_nested_query(
|
157
|
-
request.set_header(Rack::QUERY_STRING,
|
196
|
+
request.set_header("action_dispatch.request.query_parameters", Rack::Utils.parse_nested_query(query))
|
197
|
+
request.set_header(Rack::QUERY_STRING, query)
|
158
198
|
yield
|
159
199
|
ensure
|
160
200
|
request.path_info = old_request_path_info
|
data/lib/view_component.rb
CHANGED
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.68.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- ViewComponent Team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-08-
|
11
|
+
date: 2022-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -104,16 +104,16 @@ dependencies:
|
|
104
104
|
name: bundler
|
105
105
|
requirement: !ruby/object:Gem::Requirement
|
106
106
|
requirements:
|
107
|
-
- - "
|
107
|
+
- - "~>"
|
108
108
|
- !ruby/object:Gem::Version
|
109
|
-
version:
|
109
|
+
version: '2'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
112
|
version_requirements: !ruby/object:Gem::Requirement
|
113
113
|
requirements:
|
114
|
-
- - "
|
114
|
+
- - "~>"
|
115
115
|
- !ruby/object:Gem::Version
|
116
|
-
version:
|
116
|
+
version: '2'
|
117
117
|
- !ruby/object:Gem::Dependency
|
118
118
|
name: erb_lint
|
119
119
|
requirement: !ruby/object:Gem::Requirement
|
@@ -300,19 +300,18 @@ dependencies:
|
|
300
300
|
name: yard-activesupport-concern
|
301
301
|
requirement: !ruby/object:Gem::Requirement
|
302
302
|
requirements:
|
303
|
-
- - "
|
303
|
+
- - "~>"
|
304
304
|
- !ruby/object:Gem::Version
|
305
|
-
version:
|
305
|
+
version: 0.0.1
|
306
306
|
type: :development
|
307
307
|
prerelease: false
|
308
308
|
version_requirements: !ruby/object:Gem::Requirement
|
309
309
|
requirements:
|
310
|
-
- - "
|
310
|
+
- - "~>"
|
311
311
|
- !ruby/object:Gem::Version
|
312
|
-
version:
|
312
|
+
version: 0.0.1
|
313
313
|
description:
|
314
314
|
email:
|
315
|
-
- opensource+view_component@github.com
|
316
315
|
executables: []
|
317
316
|
extensions: []
|
318
317
|
extra_rdoc_files: []
|
@@ -357,6 +356,7 @@ files:
|
|
357
356
|
- lib/view_component/compile_cache.rb
|
358
357
|
- lib/view_component/compiler.rb
|
359
358
|
- lib/view_component/component_error.rb
|
359
|
+
- lib/view_component/config.rb
|
360
360
|
- lib/view_component/content_areas.rb
|
361
361
|
- lib/view_component/deprecation.rb
|
362
362
|
- lib/view_component/docs_builder_component.html.erb
|
@@ -366,12 +366,10 @@ files:
|
|
366
366
|
- lib/view_component/polymorphic_slots.rb
|
367
367
|
- lib/view_component/preview.rb
|
368
368
|
- lib/view_component/preview_template_error.rb
|
369
|
-
- lib/view_component/previewable.rb
|
370
369
|
- lib/view_component/rails/tasks/view_component.rake
|
371
370
|
- lib/view_component/render_component_helper.rb
|
372
371
|
- lib/view_component/render_component_to_string_helper.rb
|
373
372
|
- lib/view_component/render_monkey_patch.rb
|
374
|
-
- lib/view_component/render_preview_helper.rb
|
375
373
|
- lib/view_component/render_to_string_monkey_patch.rb
|
376
374
|
- lib/view_component/rendering_component_helper.rb
|
377
375
|
- lib/view_component/rendering_monkey_patch.rb
|
@@ -386,7 +384,7 @@ files:
|
|
386
384
|
- lib/view_component/version.rb
|
387
385
|
- lib/view_component/with_content_helper.rb
|
388
386
|
- lib/yard/mattr_accessor_handler.rb
|
389
|
-
homepage: https://
|
387
|
+
homepage: https://viewcomponent.org
|
390
388
|
licenses:
|
391
389
|
- MIT
|
392
390
|
metadata:
|
@@ -409,5 +407,6 @@ requirements: []
|
|
409
407
|
rubygems_version: 3.2.32
|
410
408
|
signing_key:
|
411
409
|
specification_version: 4
|
412
|
-
summary:
|
410
|
+
summary: A framework for building reusable, testable & encapsulated view components
|
411
|
+
in Ruby on Rails.
|
413
412
|
test_files: []
|
@@ -1,62 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "active_support/concern"
|
4
|
-
|
5
|
-
module ViewComponent
|
6
|
-
module Previewable
|
7
|
-
extend ActiveSupport::Concern
|
8
|
-
|
9
|
-
included do
|
10
|
-
# Enable or disable component previews:
|
11
|
-
#
|
12
|
-
# config.view_component.show_previews = true
|
13
|
-
#
|
14
|
-
# Defaults to `true` in development.
|
15
|
-
#
|
16
|
-
mattr_accessor :show_previews, instance_writer: false
|
17
|
-
|
18
|
-
# Enable or disable source code previews in component previews:
|
19
|
-
#
|
20
|
-
# config.view_component.show_previews_source = true
|
21
|
-
#
|
22
|
-
# Defaults to `false`.
|
23
|
-
#
|
24
|
-
mattr_accessor :show_previews_source, instance_writer: false, default: false
|
25
|
-
|
26
|
-
# Set a custom default layout used for preview index and individual previews:
|
27
|
-
#
|
28
|
-
# config.view_component.default_preview_layout = "component_preview"
|
29
|
-
#
|
30
|
-
mattr_accessor :default_preview_layout, instance_writer: false
|
31
|
-
|
32
|
-
# Set the location of component previews:
|
33
|
-
#
|
34
|
-
# config.view_component.preview_paths << "#{Rails.root}/lib/component_previews"
|
35
|
-
#
|
36
|
-
mattr_accessor :preview_paths, instance_writer: false
|
37
|
-
|
38
|
-
# @deprecated Use `preview_paths` instead. Will be removed in v3.0.0.
|
39
|
-
mattr_accessor :preview_path, instance_writer: false
|
40
|
-
|
41
|
-
# Set the entry route for component previews:
|
42
|
-
#
|
43
|
-
# config.view_component.preview_route = "/previews"
|
44
|
-
#
|
45
|
-
# Defaults to `/rails/view_components` when `show_previews` is enabled.
|
46
|
-
#
|
47
|
-
mattr_accessor :preview_route, instance_writer: false do
|
48
|
-
"/rails/view_components"
|
49
|
-
end
|
50
|
-
|
51
|
-
# Set the controller used for previewing components:
|
52
|
-
#
|
53
|
-
# config.view_component.preview_controller = "MyPreviewController"
|
54
|
-
#
|
55
|
-
# Defaults to `ViewComponentsController`.
|
56
|
-
#
|
57
|
-
mattr_accessor :preview_controller, instance_writer: false do
|
58
|
-
"ViewComponentsController"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ViewComponent
|
4
|
-
module RenderPreviewHelper
|
5
|
-
# Render a preview inline. Internally sets `page` to be a `Capybara::Node::Simple`,
|
6
|
-
# allowing for Capybara assertions to be used:
|
7
|
-
#
|
8
|
-
# ```ruby
|
9
|
-
# render_preview(:default)
|
10
|
-
# assert_text("Hello, World!")
|
11
|
-
# ```
|
12
|
-
#
|
13
|
-
# Note: `#rendered_preview` expects a preview to be defined with the same class
|
14
|
-
# name as the calling test, but with `Test` replaced with `Preview`:
|
15
|
-
#
|
16
|
-
# MyComponentTest -> MyComponentPreview etc.
|
17
|
-
#
|
18
|
-
# In RSpec, `Preview` is appended to `described_class`.
|
19
|
-
#
|
20
|
-
# @param preview [String] The name of the preview to be rendered.
|
21
|
-
# @return [Nokogiri::HTML]
|
22
|
-
def render_preview(name)
|
23
|
-
begin
|
24
|
-
preview_klass = if respond_to?(:described_class)
|
25
|
-
if described_class.nil?
|
26
|
-
raise "`render_preview` expected a described_class, but it is nil."
|
27
|
-
end
|
28
|
-
|
29
|
-
"#{described_class}Preview"
|
30
|
-
else
|
31
|
-
self.class.name.gsub("Test", "Preview")
|
32
|
-
end
|
33
|
-
preview_klass = preview_klass.constantize
|
34
|
-
rescue NameError
|
35
|
-
raise NameError.new(
|
36
|
-
"`render_preview` expected to find #{preview_klass}, but it does not exist."
|
37
|
-
)
|
38
|
-
end
|
39
|
-
|
40
|
-
previews_controller = build_controller(ViewComponent::Base.preview_controller.constantize)
|
41
|
-
previews_controller.request.params[:path] = "#{preview_klass.preview_name}/#{name}"
|
42
|
-
previews_controller.response = ActionDispatch::Response.new
|
43
|
-
result = previews_controller.previews
|
44
|
-
|
45
|
-
@rendered_content = result
|
46
|
-
|
47
|
-
Nokogiri::HTML.fragment(@rendered_content)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|