view_component 2.43.0 → 2.46.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/README.md +2 -1
- data/docs/CHANGELOG.md +134 -16
- data/lib/view_component/base.rb +15 -23
- data/lib/view_component/compile_cache.rb +5 -1
- data/lib/view_component/compiler.rb +46 -38
- data/lib/view_component/engine.rb +20 -1
- data/lib/view_component/polymorphic_slots.rb +0 -1
- data/lib/view_component/preview.rb +1 -1
- data/lib/view_component/previewable.rb +8 -0
- data/lib/view_component/slot_v2.rb +2 -2
- data/lib/view_component/slotable.rb +4 -4
- data/lib/view_component/slotable_v2.rb +3 -3
- data/lib/view_component/test_helpers.rb +1 -1
- data/lib/view_component/version.rb +1 -1
- data/lib/view_component.rb +11 -1
- metadata +11 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9b1ca38d53b64e55ff434c43f11839cf122d82d17768c2f064e2622ef88dc88
|
4
|
+
data.tar.gz: 7bc10806de04abbe6c7458305592d09ea6f4dfe9d86aff5d0cf0b9b9cfa9a620
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c100fe77483f4b5f7227a47188f63428cbde65ebddf75cd0accf881d2140536150ea1d52f5c68b66126dd5087d9ac7fd7725f4c829a82d58c5cf86ef8abff2e
|
7
|
+
data.tar.gz: 19e3230c2b894651462dd6dbae44b4579267cc662c962e2bf9208e28cea299e01286427ae3c949d5622e8302cad4ba2b8f5e58a6aa6dabd3e872de367affb8bb
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
![ViewComponent logo](/docs/logo/readme-light.svg#gh-light-mode-only)
|
2
|
+
![ViewComponent logo](/docs/logo/readme-dark.svg#gh-dark-mode-only)
|
2
3
|
|
3
4
|
A framework for building reusable, testable & encapsulated view components in Ruby on Rails.
|
4
5
|
|
data/docs/CHANGELOG.md
CHANGED
@@ -7,6 +7,118 @@ title: Changelog
|
|
7
7
|
|
8
8
|
## main
|
9
9
|
|
10
|
+
## 2.46.0
|
11
|
+
|
12
|
+
* Add thread safety to the compiler.
|
13
|
+
|
14
|
+
*Horia Radu*
|
15
|
+
|
16
|
+
* Add theme-specific logo images to readme.
|
17
|
+
|
18
|
+
*Dylan Smith*
|
19
|
+
|
20
|
+
* Add Orbit to users list.
|
21
|
+
|
22
|
+
*Nicolas Goutay*
|
23
|
+
|
24
|
+
* Increase clarity around purpose and use of slots.
|
25
|
+
|
26
|
+
*Simon Fish*
|
27
|
+
|
28
|
+
## 2.45.0
|
29
|
+
|
30
|
+
* Remove internal APIs from API documentation, fix link to license.
|
31
|
+
|
32
|
+
*Joel Hawksley*
|
33
|
+
|
34
|
+
* Add @yhirano55 to triage team.
|
35
|
+
|
36
|
+
*Joel Hawksley*
|
37
|
+
|
38
|
+
* Correct a typo in the sample slots code.
|
39
|
+
|
40
|
+
*Simon Fish*
|
41
|
+
|
42
|
+
* Add note about `allowed_queries`.
|
43
|
+
|
44
|
+
*Joel Hawksley*
|
45
|
+
|
46
|
+
* Add `vale` content linter.
|
47
|
+
|
48
|
+
*Joel Hawksley*
|
49
|
+
|
50
|
+
* Remove `require "rails/generators/test_case"` in generator tests.
|
51
|
+
|
52
|
+
*Yoshiyuki Hirano*
|
53
|
+
|
54
|
+
* Suppress zeitwerk warning about circular require.
|
55
|
+
|
56
|
+
*Yoshiyuki Hirano*
|
57
|
+
|
58
|
+
* Move `test_unit_generator_test.rb` from `test/view_component/` to `test/generators/`.
|
59
|
+
|
60
|
+
*Yoshiyuki Hirano*
|
61
|
+
|
62
|
+
* Unify test code of `TestUnitGeneratorTest` with the other generators tests.
|
63
|
+
|
64
|
+
*Yoshiyuki Hirano*
|
65
|
+
|
66
|
+
* Deprecate engine loading manually.
|
67
|
+
|
68
|
+
*Yoshiyuki Hirano*
|
69
|
+
|
70
|
+
## 2.44.0
|
71
|
+
|
72
|
+
* Rename internal accessor to use private naming.
|
73
|
+
|
74
|
+
*Joel Hawksley*, *Blake Williams*, *Cameron Dutro*
|
75
|
+
|
76
|
+
* Add Github repo link to docs website header.
|
77
|
+
|
78
|
+
*Hans Lemuet*
|
79
|
+
|
80
|
+
* Change logo in README for dark theme readability.
|
81
|
+
|
82
|
+
*Dylan Smith*
|
83
|
+
|
84
|
+
* Add Litmus to users list.
|
85
|
+
|
86
|
+
*Dylan Smith*
|
87
|
+
|
88
|
+
* Add @dylanatsmith as codeowner of the ViewComponent logo and member of committers team.
|
89
|
+
|
90
|
+
*Joel Hawksley*
|
91
|
+
|
92
|
+
* Autoload `CompileCache`, which is optionally called in `engine.rb`.
|
93
|
+
|
94
|
+
*Gregory Igelmund*
|
95
|
+
|
96
|
+
* Move frequently asked questions to other pages, add History page.
|
97
|
+
|
98
|
+
*Joel Hawksley*
|
99
|
+
|
100
|
+
* Fix typo.
|
101
|
+
|
102
|
+
*James Hart*
|
103
|
+
|
104
|
+
* Add `require "method_source"` if it options.show_previews_source is enabled.
|
105
|
+
|
106
|
+
*Yoshiyuki Hirano*
|
107
|
+
|
108
|
+
* Move show_previews_source definition to Previewable.
|
109
|
+
|
110
|
+
*Yoshiyuki Hirano*
|
111
|
+
|
112
|
+
* Clear cache in MethodSource to apply the change odf preview code without app server restart.
|
113
|
+
|
114
|
+
*Yoshiyuki Hirano*
|
115
|
+
|
116
|
+
## 2.43.1
|
117
|
+
|
118
|
+
* Remove unnecessary call to `ruby2_keywords` for polymorphic slot getters.
|
119
|
+
|
120
|
+
*Cameron Dutro*
|
121
|
+
|
10
122
|
## 2.43.0
|
11
123
|
|
12
124
|
* Add note about tests and instance methods.
|
@@ -90,7 +202,7 @@ title: Changelog
|
|
90
202
|
|
91
203
|
*Matthew Rider*
|
92
204
|
|
93
|
-
* Fix bug where `with_collection_parameter`
|
205
|
+
* Fix bug where `with_collection_parameter` didn't inherit from parent component.
|
94
206
|
|
95
207
|
*Will Drexler*, *Christian Campoli*
|
96
208
|
|
@@ -294,7 +406,7 @@ title: Changelog
|
|
294
406
|
|
295
407
|
*Hans Lemuet*
|
296
408
|
|
297
|
-
* Fix bug where ViewComponents
|
409
|
+
* Fix bug where ViewComponents didn't work in ActionMailers.
|
298
410
|
|
299
411
|
*dark-panda*
|
300
412
|
|
@@ -342,7 +454,7 @@ title: Changelog
|
|
342
454
|
|
343
455
|
## 2.31.0
|
344
456
|
|
345
|
-
_Note: This release includes an underlying change to Slots that may affect incorrect usage of the API, where Slots were set on a line prefixed by `<%=`. The result of setting a Slot
|
457
|
+
_Note: This release includes an underlying change to Slots that may affect incorrect usage of the API, where Slots were set on a line prefixed by `<%=`. The result of setting a Slot shouldn't be returned. (`<%`)_
|
346
458
|
|
347
459
|
* Add `#with_content` to allow setting content without a block.
|
348
460
|
|
@@ -353,7 +465,7 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
353
465
|
*Mario Schüttel*
|
354
466
|
|
355
467
|
* Improve feature parity with Rails translations
|
356
|
-
* Don't create a translation
|
468
|
+
* Don't create a translation back end if the component has no translation file
|
357
469
|
* Mark translation keys ending with `html` as HTML-safe
|
358
470
|
* Always convert keys to String
|
359
471
|
* Support multiple keys
|
@@ -395,7 +507,7 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
395
507
|
|
396
508
|
*Alex Robbin, Blake Williams*
|
397
509
|
|
398
|
-
* Experimental: call `._sidecar_files` to fetch the sidecar files for a given list of extensions,
|
510
|
+
* Experimental: call `._sidecar_files` to fetch the sidecar files for a given list of extensions, for example passing `["yml", "yaml"]`.
|
399
511
|
|
400
512
|
*Elia Schito*
|
401
513
|
|
@@ -435,11 +547,11 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
435
547
|
|
436
548
|
## 2.26.0
|
437
549
|
|
438
|
-
*
|
550
|
+
* Delay evaluating component `content` in `render?`, preventing the `content` block from being evaluated when `render?` returns false.
|
439
551
|
|
440
552
|
*Blake Williams*
|
441
553
|
|
442
|
-
*
|
554
|
+
* Don't generate template when using `--inline` flag.
|
443
555
|
|
444
556
|
*Hans Lemuet*
|
445
557
|
|
@@ -500,7 +612,7 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
500
612
|
* `with_slot collection: true` becomes `renders_many`.
|
501
613
|
* Slot definitions now accept either a component class, component class name, or a lambda instead of a `class_name:` keyword argument.
|
502
614
|
* Slots now support positional arguments.
|
503
|
-
* Slots no longer use the `content` attribute to render content, instead relying on `to_s`.
|
615
|
+
* Slots no longer use the `content` attribute to render content, instead relying on `to_s`. for example `<%= my_slot %>`.
|
504
616
|
* Slot values are no longer set via the `slot` method, and instead use the name of the slot.
|
505
617
|
|
506
618
|
*Blake Williams*
|
@@ -563,7 +675,7 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
563
675
|
|
564
676
|
## 2.18.2
|
565
677
|
|
566
|
-
* Raise an error if controller or view context is accessed during initialize as they
|
678
|
+
* Raise an error if controller or view context is accessed during initialize as they're only available in render.
|
567
679
|
|
568
680
|
*Julian Nadeau*
|
569
681
|
|
@@ -579,7 +691,7 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
579
691
|
|
580
692
|
## 2.18.0
|
581
693
|
|
582
|
-
* Fix auto
|
694
|
+
* Fix auto loading of previews (changes no longer require a server restart)
|
583
695
|
|
584
696
|
*Matt Brictson*
|
585
697
|
|
@@ -672,7 +784,7 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
672
784
|
|
673
785
|
## 2.10.0
|
674
786
|
|
675
|
-
* Raise an `ArgumentError` with a helpful message when Ruby
|
787
|
+
* Raise an `ArgumentError` with a helpful message when Ruby can't parse a component class.
|
676
788
|
|
677
789
|
*Max Beizer*
|
678
790
|
|
@@ -764,7 +876,7 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
764
876
|
|
765
877
|
## v2.2.1
|
766
878
|
|
767
|
-
* Fix bug where template
|
879
|
+
* Fix bug where template couldn't be found if `inherited` was redefined.
|
768
880
|
|
769
881
|
*Joel Hawksley*
|
770
882
|
|
@@ -780,7 +892,7 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
780
892
|
|
781
893
|
## v2.1.0
|
782
894
|
|
783
|
-
* Support rendering collections (
|
895
|
+
* Support rendering collections (for example, `render(MyComponent.with_collection(@items))`).
|
784
896
|
|
785
897
|
*Tim Clem*
|
786
898
|
|
@@ -802,7 +914,7 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
802
914
|
|
803
915
|
*Andrew Mason*
|
804
916
|
|
805
|
-
* ViewComponent generators
|
917
|
+
* ViewComponent generators don't not prompt for content requirement.
|
806
918
|
|
807
919
|
*Joel Hawksley*
|
808
920
|
|
@@ -924,7 +1036,7 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
924
1036
|
|
925
1037
|
*Jon Palmer*
|
926
1038
|
|
927
|
-
* Add `#render?` hook to
|
1039
|
+
* Add `#render?` hook to allow components to be no-ops.
|
928
1040
|
|
929
1041
|
*Kyle Fox*
|
930
1042
|
|
@@ -992,7 +1104,9 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
992
1104
|
|
993
1105
|
* Fix edge case issue with extracting variants from less conventional source_locations.
|
994
1106
|
|
1107
|
+
<!-- vale proselint.GenderBias = NO -->
|
995
1108
|
*Ryan Workman*
|
1109
|
+
<!-- vale proselint.GenderBias = YES -->
|
996
1110
|
|
997
1111
|
## v1.6.0
|
998
1112
|
|
@@ -1028,7 +1142,9 @@ _Note: This release includes an underlying change to Slots that may affect incor
|
|
1028
1142
|
|
1029
1143
|
* Add support for RSpec to generators.
|
1030
1144
|
|
1145
|
+
<!-- vale proselint.GenderBias = NO -->
|
1031
1146
|
*Dylan Clark, Ryan Workman*
|
1147
|
+
<!-- vale proselint.GenderBias = YES -->
|
1032
1148
|
|
1033
1149
|
* Require controllers as part of setting autoload paths.
|
1034
1150
|
|
@@ -1052,7 +1168,9 @@ Note: `actionview-component` is now loaded by requiring `actionview/component`,
|
|
1052
1168
|
|
1053
1169
|
* Fix issue with generating component method signatures.
|
1054
1170
|
|
1171
|
+
<!-- vale proselint.GenderBias = NO -->
|
1055
1172
|
*Ryan Workman, Dylan Clark*
|
1173
|
+
<!-- vale proselint.GenderBias = YES -->
|
1056
1174
|
|
1057
1175
|
* Create component generator.
|
1058
1176
|
|
@@ -1124,7 +1242,7 @@ Note: `actionview-component` is now loaded by requiring `actionview/component`,
|
|
1124
1242
|
|
1125
1243
|
## v1.3.3
|
1126
1244
|
|
1127
|
-
*
|
1245
|
+
* Don't raise error when sidecar files that aren't templates exist.
|
1128
1246
|
|
1129
1247
|
*Joel Hawksley*
|
1130
1248
|
|
data/lib/view_component/base.rb
CHANGED
@@ -29,7 +29,7 @@ module ViewComponent
|
|
29
29
|
class_attribute :content_areas
|
30
30
|
self.content_areas = [] # class_attribute:default doesn't work until Rails 5.2
|
31
31
|
|
32
|
-
attr_accessor :
|
32
|
+
attr_accessor :__vc_original_view_context
|
33
33
|
|
34
34
|
# EXPERIMENTAL: This API is experimental and may be removed at any time.
|
35
35
|
# Hook for allowing components to do work as part of the compilation process.
|
@@ -52,7 +52,7 @@ module ViewComponent
|
|
52
52
|
self.class.compile(raise_errors: true)
|
53
53
|
|
54
54
|
@view_context = view_context
|
55
|
-
self.
|
55
|
+
self.__vc_original_view_context ||= view_context
|
56
56
|
|
57
57
|
@lookup_context ||= view_context.lookup_context
|
58
58
|
|
@@ -137,10 +137,10 @@ module ViewComponent
|
|
137
137
|
# @private
|
138
138
|
def render(options = {}, args = {}, &block)
|
139
139
|
if options.is_a? ViewComponent::Base
|
140
|
-
options.
|
140
|
+
options.__vc_original_view_context = __vc_original_view_context
|
141
141
|
super
|
142
142
|
else
|
143
|
-
|
143
|
+
__vc_original_view_context.render(options, args, &block)
|
144
144
|
end
|
145
145
|
end
|
146
146
|
|
@@ -152,7 +152,7 @@ module ViewComponent
|
|
152
152
|
if view_context.nil?
|
153
153
|
raise(
|
154
154
|
ViewContextCalledBeforeRenderError,
|
155
|
-
"`#controller`
|
155
|
+
"`#controller` can't be used during initialization, as it depends " \
|
156
156
|
"on the view context that only exists once a ViewComponent is passed to " \
|
157
157
|
"the Rails render pipeline.\n\n" \
|
158
158
|
"It's sometimes possible to fix this issue by moving code dependent on " \
|
@@ -171,7 +171,7 @@ module ViewComponent
|
|
171
171
|
if view_context.nil?
|
172
172
|
raise(
|
173
173
|
ViewContextCalledBeforeRenderError,
|
174
|
-
"`#helpers`
|
174
|
+
"`#helpers` can't be used during initialization, as it depends " \
|
175
175
|
"on the view context that only exists once a ViewComponent is passed to " \
|
176
176
|
"the Rails render pipeline.\n\n" \
|
177
177
|
"It's sometimes possible to fix this issue by moving code dependent on " \
|
@@ -186,7 +186,7 @@ module ViewComponent
|
|
186
186
|
#
|
187
187
|
# This allows ivars to remain persisted when using the same helper via
|
188
188
|
# `helpers` across multiple components and partials.
|
189
|
-
@__vc_helpers ||=
|
189
|
+
@__vc_helpers ||= __vc_original_view_context || controller.view_context
|
190
190
|
end
|
191
191
|
|
192
192
|
# Exposes .virtual_path as an instance method
|
@@ -206,7 +206,7 @@ module ViewComponent
|
|
206
206
|
#
|
207
207
|
# @private
|
208
208
|
def format
|
209
|
-
# Ruby 2.6 throws a warning without checking `defined?`, 2.7
|
209
|
+
# Ruby 2.6 throws a warning without checking `defined?`, 2.7 doesn't
|
210
210
|
if defined?(@__vc_variant)
|
211
211
|
@__vc_variant
|
212
212
|
end
|
@@ -271,14 +271,6 @@ module ViewComponent
|
|
271
271
|
#
|
272
272
|
mattr_accessor :render_monkey_patch_enabled, instance_writer: false, default: true
|
273
273
|
|
274
|
-
# Enable or disable source code previews in component previews:
|
275
|
-
#
|
276
|
-
# config.view_component.show_previews_source = true
|
277
|
-
#
|
278
|
-
# Defaults to `false`.
|
279
|
-
#
|
280
|
-
mattr_accessor :show_previews_source, instance_writer: false, default: false
|
281
|
-
|
282
274
|
# Always generate a Stimulus controller alongside the component:
|
283
275
|
#
|
284
276
|
# config.view_component.generate_stimulus_controller = true
|
@@ -291,7 +283,7 @@ module ViewComponent
|
|
291
283
|
#
|
292
284
|
# config.view_component.view_component_path = "app/my_components"
|
293
285
|
#
|
294
|
-
# Defaults to
|
286
|
+
# Defaults to `app/components`.
|
295
287
|
mattr_accessor :view_component_path, instance_writer: false, default: "app/components"
|
296
288
|
|
297
289
|
# Parent class for generated components
|
@@ -326,7 +318,7 @@ module ViewComponent
|
|
326
318
|
|
327
319
|
# Add support for nested components defined in the same file.
|
328
320
|
#
|
329
|
-
#
|
321
|
+
# for example
|
330
322
|
#
|
331
323
|
# class MyComponent < ViewComponent::Base
|
332
324
|
# class MyOtherComponent < ViewComponent::Base
|
@@ -439,7 +431,7 @@ module ViewComponent
|
|
439
431
|
end
|
440
432
|
|
441
433
|
# Ensure the component initializer accepts the
|
442
|
-
# collection parameter. By default, we
|
434
|
+
# collection parameter. By default, we don't
|
443
435
|
# validate that the default parameter name
|
444
436
|
# is accepted, as support for collection
|
445
437
|
# rendering is optional.
|
@@ -450,7 +442,7 @@ module ViewComponent
|
|
450
442
|
return unless parameter
|
451
443
|
return if initialize_parameter_names.include?(parameter)
|
452
444
|
|
453
|
-
# If Ruby
|
445
|
+
# If Ruby can't parse the component class, then the initalize
|
454
446
|
# parameters will be empty and ViewComponent will not be able to render
|
455
447
|
# the component.
|
456
448
|
if initialize_parameters.empty?
|
@@ -463,14 +455,14 @@ module ViewComponent
|
|
463
455
|
end
|
464
456
|
|
465
457
|
raise ArgumentError.new(
|
466
|
-
"The initializer for #{self}
|
458
|
+
"The initializer for #{self} doesn't accept the parameter `#{parameter}`, " \
|
467
459
|
"which is required in order to render it as a collection.\n\n" \
|
468
460
|
"To fix this issue, update the initializer to accept `#{parameter}`.\n\n" \
|
469
461
|
"See https://viewcomponent.org/guide/collections.html for more information on rendering collections."
|
470
462
|
)
|
471
463
|
end
|
472
464
|
|
473
|
-
# Ensure the component initializer
|
465
|
+
# Ensure the component initializer doesn't define
|
474
466
|
# invalid parameters that could override the framework's
|
475
467
|
# methods.
|
476
468
|
# @private TODO: add documentation
|
@@ -478,7 +470,7 @@ module ViewComponent
|
|
478
470
|
return unless initialize_parameter_names.include?(RESERVED_PARAMETER)
|
479
471
|
|
480
472
|
raise ViewComponent::ComponentError.new(
|
481
|
-
"#{self} initializer
|
473
|
+
"#{self} initializer can't accept the parameter `#{RESERVED_PARAMETER}`, as it will override a " \
|
482
474
|
"public ViewComponent method. To fix this issue, rename the parameter."
|
483
475
|
)
|
484
476
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module ViewComponent
|
4
4
|
# Keeps track of which templates have already been compiled
|
5
|
-
# This
|
5
|
+
# This isn't part of the public API
|
6
6
|
module CompileCache
|
7
7
|
mattr_accessor :cache, instance_reader: false, instance_accessor: false do
|
8
8
|
Set.new
|
@@ -18,6 +18,10 @@ module ViewComponent
|
|
18
18
|
cache.include? klass
|
19
19
|
end
|
20
20
|
|
21
|
+
def invalidate_class!(klass)
|
22
|
+
cache.delete(klass)
|
23
|
+
end
|
24
|
+
|
21
25
|
def invalidate!
|
22
26
|
cache.clear
|
23
27
|
end
|
@@ -2,8 +2,12 @@
|
|
2
2
|
|
3
3
|
module ViewComponent
|
4
4
|
class Compiler
|
5
|
+
# Lock required to be obtained before compiling the component
|
6
|
+
attr_reader :__vc_compiler_lock
|
7
|
+
|
5
8
|
def initialize(component_class)
|
6
9
|
@component_class = component_class
|
10
|
+
@__vc_compiler_lock = Monitor.new
|
7
11
|
end
|
8
12
|
|
9
13
|
def compiled?
|
@@ -13,55 +17,59 @@ module ViewComponent
|
|
13
17
|
def compile(raise_errors: false)
|
14
18
|
return if compiled?
|
15
19
|
|
16
|
-
|
20
|
+
__vc_compiler_lock.synchronize do
|
21
|
+
CompileCache.invalidate_class!(component_class)
|
17
22
|
|
18
|
-
|
19
|
-
raise ViewComponent::ComponentError.new(
|
20
|
-
"#{component_class} implements a reserved method, `#with_content`.\n\n" \
|
21
|
-
"To fix this issue, change the name of the method."
|
22
|
-
)
|
23
|
-
end
|
23
|
+
subclass_instance_methods = component_class.instance_methods(false)
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
if subclass_instance_methods.include?(:with_content) && raise_errors
|
26
|
+
raise ViewComponent::ComponentError.new(
|
27
|
+
"#{component_class} implements a reserved method, `#with_content`.\n\n" \
|
28
|
+
"To fix this issue, change the name of the method."
|
29
|
+
)
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
"`#before_render_check` will be removed in v3.0.0.\n\n" \
|
34
|
-
"To fix this issue, use `#before_render` instead."
|
35
|
-
)
|
36
|
-
end
|
32
|
+
if template_errors.present?
|
33
|
+
raise ViewComponent::TemplateError.new(template_errors) if raise_errors
|
37
34
|
|
38
|
-
|
39
|
-
|
40
|
-
component_class.validate_collection_parameter!
|
41
|
-
end
|
35
|
+
return false
|
36
|
+
end
|
42
37
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
38
|
+
if subclass_instance_methods.include?(:before_render_check)
|
39
|
+
ActiveSupport::Deprecation.warn(
|
40
|
+
"`#before_render_check` will be removed in v3.0.0.\n\n" \
|
41
|
+
"To fix this issue, use `#before_render` instead."
|
42
|
+
)
|
43
|
+
end
|
47
44
|
|
48
|
-
if
|
49
|
-
component_class.
|
45
|
+
if raise_errors
|
46
|
+
component_class.validate_initialization_parameters!
|
47
|
+
component_class.validate_collection_parameter!
|
50
48
|
end
|
51
49
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
50
|
+
templates.each do |template|
|
51
|
+
# Remove existing compiled template methods,
|
52
|
+
# as Ruby warns when redefining a method.
|
53
|
+
method_name = call_method_name(template[:variant])
|
54
|
+
|
55
|
+
if component_class.instance_methods.include?(method_name.to_sym)
|
56
|
+
component_class.send(:undef_method, method_name.to_sym)
|
56
57
|
end
|
57
|
-
RUBY
|
58
|
-
end
|
59
58
|
|
60
|
-
|
59
|
+
component_class.class_eval <<-RUBY, template[:path], -1
|
60
|
+
def #{method_name}
|
61
|
+
@output_buffer = ActionView::OutputBuffer.new
|
62
|
+
#{compiled_template(template[:path])}
|
63
|
+
end
|
64
|
+
RUBY
|
65
|
+
end
|
66
|
+
|
67
|
+
define_render_template_for
|
61
68
|
|
62
|
-
|
69
|
+
component_class._after_compile
|
63
70
|
|
64
|
-
|
71
|
+
CompileCache.register(component_class)
|
72
|
+
end
|
65
73
|
end
|
66
74
|
|
67
75
|
private
|
@@ -95,7 +103,7 @@ module ViewComponent
|
|
95
103
|
errors = []
|
96
104
|
|
97
105
|
if (templates + inline_calls).empty?
|
98
|
-
errors << "
|
106
|
+
errors << "Couldn't find a template file or inline render method for #{component_class}."
|
99
107
|
end
|
100
108
|
|
101
109
|
if templates.count { |template| template[:variant].nil? } > 1
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "rails"
|
4
|
-
require "view_component"
|
5
4
|
|
6
5
|
module ViewComponent
|
7
6
|
class Engine < Rails::Engine # :nodoc:
|
@@ -17,6 +16,7 @@ module ViewComponent
|
|
17
16
|
|
18
17
|
options.render_monkey_patch_enabled = true if options.render_monkey_patch_enabled.nil?
|
19
18
|
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
20
|
options.instrumentation_enabled = false if options.instrumentation_enabled.nil?
|
21
21
|
options.preview_route ||= ViewComponent::Base.preview_route
|
22
22
|
options.preview_controller ||= ViewComponent::Base.preview_controller
|
@@ -32,6 +32,14 @@ module ViewComponent
|
|
32
32
|
)
|
33
33
|
options.preview_paths << options.preview_path
|
34
34
|
end
|
35
|
+
|
36
|
+
if options.show_previews_source
|
37
|
+
require "method_source"
|
38
|
+
|
39
|
+
app.config.to_prepare do
|
40
|
+
MethodSource.instance_variable_set(:@lines_for_file, {})
|
41
|
+
end
|
42
|
+
end
|
35
43
|
end
|
36
44
|
|
37
45
|
ActiveSupport.on_load(:view_component) do
|
@@ -136,3 +144,14 @@ module ViewComponent
|
|
136
144
|
end
|
137
145
|
end
|
138
146
|
end
|
147
|
+
|
148
|
+
# :nocov:
|
149
|
+
unless defined?(ViewComponent::Base)
|
150
|
+
ActiveSupport::Deprecation.warn(
|
151
|
+
"This manually engine loading is deprecated and will be removed in v3.0.0. " \
|
152
|
+
"Remove `require \"view_component/engine\"`."
|
153
|
+
)
|
154
|
+
|
155
|
+
require "view_component"
|
156
|
+
end
|
157
|
+
# :nocov:
|
@@ -41,7 +41,6 @@ module ViewComponent
|
|
41
41
|
define_method(getter_name) do
|
42
42
|
get_slot(slot_name)
|
43
43
|
end
|
44
|
-
ruby2_keywords(getter_name.to_sym) if respond_to?(:ruby2_keywords, true)
|
45
44
|
|
46
45
|
define_method(setter_name) do |*args, &block|
|
47
46
|
set_polymorphic_slot(slot_name, poly_type, *args, &block)
|
@@ -80,7 +80,7 @@ module ViewComponent # :nodoc:
|
|
80
80
|
if preview_path.nil?
|
81
81
|
raise(
|
82
82
|
PreviewTemplateError,
|
83
|
-
"A preview template for example #{example}
|
83
|
+
"A preview template for example #{example} doesn't exist.\n\n" \
|
84
84
|
"To fix this issue, create a template for the example."
|
85
85
|
)
|
86
86
|
end
|
@@ -15,6 +15,14 @@ module ViewComponent
|
|
15
15
|
#
|
16
16
|
mattr_accessor :show_previews, instance_writer: false
|
17
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
|
+
|
18
26
|
# Set a custom default layout used for preview index and individual previews:
|
19
27
|
#
|
20
28
|
# config.view_component.default_preview_layout = "component_preview"
|
@@ -20,7 +20,7 @@ module ViewComponent
|
|
20
20
|
# component, or a function that returns a component, we render that
|
21
21
|
# component instance, returning the string.
|
22
22
|
#
|
23
|
-
# If the slot renderable is a function and returns a string, it
|
23
|
+
# If the slot renderable is a function and returns a string, it's
|
24
24
|
# set as `@__vc_content` and is returned directly.
|
25
25
|
#
|
26
26
|
# If there is no slot renderable, we evaluate the block passed to
|
@@ -69,7 +69,7 @@ module ViewComponent
|
|
69
69
|
|
70
70
|
# Allow access to public component methods via the wrapper
|
71
71
|
#
|
72
|
-
#
|
72
|
+
# for example
|
73
73
|
#
|
74
74
|
# calling `header.name` (where `header` is a slot) will call `name`
|
75
75
|
# on the `HeaderComponent` instance.
|
@@ -29,12 +29,12 @@ module ViewComponent
|
|
29
29
|
)
|
30
30
|
|
31
31
|
slot_names.each do |slot_name|
|
32
|
-
# Ensure slot_name
|
32
|
+
# Ensure slot_name isn't already declared
|
33
33
|
if self.slots.key?(slot_name)
|
34
34
|
raise ArgumentError.new("#{slot_name} slot declared multiple times")
|
35
35
|
end
|
36
36
|
|
37
|
-
# Ensure slot name
|
37
|
+
# Ensure slot name isn't :content
|
38
38
|
if slot_name == :content
|
39
39
|
raise ArgumentError.new ":content is a reserved slot name. Please use another name, such as ':body'"
|
40
40
|
end
|
@@ -105,7 +105,7 @@ module ViewComponent
|
|
105
105
|
# <% end %>
|
106
106
|
#
|
107
107
|
def slot(slot_name, **args, &block)
|
108
|
-
# Raise ArgumentError if `slot`
|
108
|
+
# Raise ArgumentError if `slot` doesn't exist
|
109
109
|
unless slots.keys.include?(slot_name)
|
110
110
|
raise ArgumentError.new "Unknown slot '#{slot_name}' - expected one of '#{slots.keys}'"
|
111
111
|
end
|
@@ -140,7 +140,7 @@ module ViewComponent
|
|
140
140
|
instance_variable_set(slot[:instance_variable_name], slot_instance)
|
141
141
|
end
|
142
142
|
|
143
|
-
# Return nil, as this method
|
143
|
+
# Return nil, as this method shouldn't output anything to the view itself.
|
144
144
|
nil
|
145
145
|
end
|
146
146
|
end
|
@@ -121,7 +121,7 @@ module ViewComponent
|
|
121
121
|
singular_name = ActiveSupport::Inflector.singularize(slot_name)
|
122
122
|
|
123
123
|
# Define setter for singular names
|
124
|
-
#
|
124
|
+
# for example `renders_many :items` allows fetching all tabs with
|
125
125
|
# `component.tabs` and setting a tab with `component.tab`
|
126
126
|
define_method singular_name do |*args, &block|
|
127
127
|
set_slot(slot_name, nil, *args, &block)
|
@@ -181,7 +181,7 @@ module ViewComponent
|
|
181
181
|
# If callable is a string, we assume it's referencing an internal class
|
182
182
|
slot[:renderable_class_name] = callable
|
183
183
|
elsif callable.respond_to?(:call)
|
184
|
-
# If slot
|
184
|
+
# If slot doesn't respond to `render_in`, we assume it's a proc,
|
185
185
|
# define a method, and save a reference to it to call when setting
|
186
186
|
method_name = :"_call_#{slot_name}"
|
187
187
|
define_method method_name, &callable
|
@@ -256,7 +256,7 @@ module ViewComponent
|
|
256
256
|
# 1. If this is a `content_area` style sub-component, we will render the
|
257
257
|
# block via the `slot`
|
258
258
|
#
|
259
|
-
# 2. Since we
|
259
|
+
# 2. Since we've to pass block content to components when calling
|
260
260
|
# `render`, evaluating the block here would require us to call
|
261
261
|
# `view_context.capture` twice, which is slower
|
262
262
|
slot.__vc_content_block = block if block_given?
|
@@ -103,7 +103,7 @@ module ViewComponent
|
|
103
103
|
@controller = old_controller
|
104
104
|
end
|
105
105
|
|
106
|
-
# Set the URL
|
106
|
+
# Set the URL of the current request (such as when using request-dependent path helpers):
|
107
107
|
#
|
108
108
|
# ```ruby
|
109
109
|
# with_request_url("/users/42") do
|
data/lib/view_component.rb
CHANGED
@@ -8,6 +8,7 @@ module ViewComponent
|
|
8
8
|
|
9
9
|
autoload :Base
|
10
10
|
autoload :Compiler
|
11
|
+
autoload :CompileCache
|
11
12
|
autoload :ComponentError
|
12
13
|
autoload :Instrumentation
|
13
14
|
autoload :Preview
|
@@ -18,4 +19,13 @@ module ViewComponent
|
|
18
19
|
autoload :Translatable
|
19
20
|
end
|
20
21
|
|
21
|
-
|
22
|
+
# :nocov:
|
23
|
+
if defined?(ViewComponent::Engine)
|
24
|
+
ActiveSupport::Deprecation.warn(
|
25
|
+
"This manually engine loading is deprecated and will be removed in v3.0.0. " \
|
26
|
+
"Remove `require \"view_component/engine\"`."
|
27
|
+
)
|
28
|
+
elsif defined?(Rails::Engine)
|
29
|
+
require "view_component/engine"
|
30
|
+
end
|
31
|
+
# :nocov:
|
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.46.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GitHub Open Source
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -76,16 +76,16 @@ dependencies:
|
|
76
76
|
name: bundler
|
77
77
|
requirement: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
|
-
- - "
|
79
|
+
- - ">="
|
80
80
|
- !ruby/object:Gem::Version
|
81
|
-
version:
|
81
|
+
version: 1.15.0
|
82
82
|
type: :development
|
83
83
|
prerelease: false
|
84
84
|
version_requirements: !ruby/object:Gem::Requirement
|
85
85
|
requirements:
|
86
|
-
- - "
|
86
|
+
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
|
-
version:
|
88
|
+
version: 1.15.0
|
89
89
|
- !ruby/object:Gem::Dependency
|
90
90
|
name: erb_lint
|
91
91
|
requirement: !ruby/object:Gem::Requirement
|
@@ -268,7 +268,7 @@ dependencies:
|
|
268
268
|
- - ">="
|
269
269
|
- !ruby/object:Gem::Version
|
270
270
|
version: '0'
|
271
|
-
description:
|
271
|
+
description:
|
272
272
|
email:
|
273
273
|
- opensource+view_component@github.com
|
274
274
|
executables: []
|
@@ -341,7 +341,7 @@ licenses:
|
|
341
341
|
- MIT
|
342
342
|
metadata:
|
343
343
|
allowed_push_host: https://rubygems.org
|
344
|
-
post_install_message:
|
344
|
+
post_install_message:
|
345
345
|
rdoc_options: []
|
346
346
|
require_paths:
|
347
347
|
- lib
|
@@ -356,8 +356,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
356
356
|
- !ruby/object:Gem::Version
|
357
357
|
version: '0'
|
358
358
|
requirements: []
|
359
|
-
rubygems_version: 3.1.
|
360
|
-
signing_key:
|
359
|
+
rubygems_version: 3.1.2
|
360
|
+
signing_key:
|
361
361
|
specification_version: 4
|
362
362
|
summary: View components for Rails
|
363
363
|
test_files: []
|