view_component 4.0.2 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 05c8fa66735f8008894ba88d64885dc307bc71381e2b88391798fa4266da7279
4
- data.tar.gz: f217babfa1c1894f377fff73fc3b603755122422c8596e349ffd0d8689e86708
3
+ metadata.gz: 79d1337f1589a6e9ec7617c7d07917825c4f90b2e6093e6e925ef9fad3e06937
4
+ data.tar.gz: aaa0ddcd3a30c628409a4001efdd99c702ff5b97ff7873a3f3e092e05b54c3f6
5
5
  SHA512:
6
- metadata.gz: a10d8362a64c181f11dd0aff69725eb7045a4a4cb546c2e960183bf416083dc8b5bcee76c807be3341dd01919eba3fdc2636083de2794101d8751f1c405396a1
7
- data.tar.gz: 2ae8c40ff912472d16d27e3a92af772905d74b3ab010ced00578208ee281f53f59adc31bc0ef0b170cf1ea8c64cd0750db0c0804bb65deca11a33f430cc7012d
6
+ metadata.gz: 98381938a50a979a6ee135dff98c4e3b13a4a1b5ac8ee2f19c7e5f9b1bdc04470b355c55696d2724c733fab29a9379fe59c0a8e4273374e9db5210202be2dc06
7
+ data.tar.gz: 0b5276ff41b7b7ff493f935c75e8c3e8cc604decc42a950c97f1da3becccfba3446b40178c35b9b54578a47f3718bfa53ba2c6e91ec7b929a6adc7f26f41f782
data/docs/CHANGELOG.md CHANGED
@@ -10,6 +10,84 @@ nav_order: 6
10
10
 
11
11
  ## main
12
12
 
13
+ ## 4.2.0
14
+
15
+ * Fix translation scope resolution in deeply nested component blocks (3+ levels). Translations called inside deeply nested slot blocks using `renders_many`/`renders_one` were incorrectly resolving to an intermediate component's scope instead of the partial's scope where the block was defined. The fix captures the virtual path at block definition time and restores it during block execution, ensuring translations always resolve relative to where the block was created regardless of nesting depth.
16
+
17
+ *Nathaniel Watts*
18
+
19
+ * Allow `render_inline` with Nokogiri::HTML5 to parse more arbitrary content including bare table content otherwise illegal fragments like `<td>`.
20
+
21
+ *Jonathan Rochkind*
22
+
23
+ * Remove known issue from docs as ActiveScaffold is [now compatible](https://github.com/activescaffold/active_scaffold/pull/743) with ViewComponent.
24
+
25
+ *David Löwenfels*
26
+
27
+ * Add test to document the current behavior for resolving relative translation keys within partial blocks. When rendering a partial, relative translation keys are resolved relative to the partial's own path rather than the caller’s path. This test ensures that this behavior remains consistent.
28
+
29
+ *Oussama Hilal*
30
+
31
+ * Allow I18n calls in `render?`.
32
+
33
+ *23tux*
34
+
35
+ * ViewComponent now works without `rails` and `railties` gems loaded, enabling compatibility with Bridgetown 2.0.
36
+
37
+ *Tom Lord*
38
+
39
+ * Capture partial block in the component's context, allowing access to the component instance inside the block.
40
+
41
+ *23tux*
42
+
43
+ * Add `after_compile` class method hook to enable extensions to run logic after component compilation.
44
+
45
+ *Jose Solás*
46
+
47
+ * Fix outdated reference to preview layout configuration in docs.
48
+
49
+ *Lucas Geron*
50
+
51
+ * Allow ruby-head CI job to fail without failing workflow.
52
+
53
+ *Hakan Ensari*
54
+
55
+ * Fix bug where error line numbers were incorrect in Rails 8.1.
56
+
57
+ *Joel Hawksley*
58
+
59
+ * Remove `< 8.2` upper bound for `activesupport` and `actionview` dependencies.
60
+
61
+ *Hans Lemuet*
62
+
63
+ * Test compatibility with Herb/ReActionView.
64
+
65
+ *Joel Hawksley*
66
+
67
+ * Remove Who Uses ViewComponent section from docs.
68
+
69
+ *Joel Hawksley*
70
+
71
+ ## 4.1.1
72
+
73
+ * Resolve deprecation warning for `ActiveSupport::Configurable`.
74
+
75
+ *Simon Fish*
76
+
77
+ * Make `ViewComponent::VERSION` accessible to other gems by default.
78
+
79
+ *Hans Lemuet*
80
+
81
+ ## 4.1.0
82
+
83
+ * Add Rails 8.1 support.
84
+
85
+ *Hans Lemuet*
86
+
87
+ * Declare `actionview` as a `view_component` gem dependency.
88
+
89
+ *Michal Cichra*
90
+
13
91
  ## 4.0.2
14
92
 
15
93
  * Share the view context in tests to prevent out-of-order rendering issues for certain advanced use-cases, eg. testing instances of Rails' `FormBuilder`.
@@ -488,10 +566,6 @@ This release makes the following breaking changes:
488
566
 
489
567
  *Martin Meyerhoff*, *Joel Hawksley*
490
568
 
491
- * Add Content Harmony & Learn To Be to list of companies using ViewComponent.
492
-
493
- *Kane Jamison*
494
-
495
569
  * Clarify error message about render-dependent logic.
496
570
 
497
571
  Error messages about render-dependent logic were sometimes inaccurate, saying `during initialization` despite also being raised after a component had been initialized but before it was rendered.
@@ -510,10 +584,6 @@ This release makes the following breaking changes:
510
584
 
511
585
  *Reegan Viljoen*
512
586
 
513
- * Add HomeStyler AI to list of companies using ViewComponent.
514
-
515
- *JP Balarini*
516
-
517
587
  ## 3.21.0
518
588
 
519
589
  * Updates testing docs to include an example of how to use with RSpec.
@@ -524,10 +594,6 @@ This release makes the following breaking changes:
524
594
 
525
595
  *KAWAKAMI Moeki*
526
596
 
527
- * Add FreeATS to list of companies using ViewComponent.
528
-
529
- *Ilia Liamshin*
530
-
531
597
  * Ensure HTML output safety wrapper is used for all inline templates.
532
598
 
533
599
  *Joel Hawksley*
@@ -578,10 +644,6 @@ This release makes the following breaking changes:
578
644
 
579
645
  *Blake Williams*
580
646
 
581
- * Add [Niva]([niva.co](https://www.niva.co/)) to companies who use `ViewComponent`.
582
-
583
- *Daniel Vu Dao*
584
-
585
647
  * Fix `preview_paths` in docs.
586
648
 
587
649
  *Javier Aranda*
@@ -646,10 +708,6 @@ This release makes the following breaking changes:
646
708
 
647
709
  *Joel Hawksley*
648
710
 
649
- * Add Kicksite to list of companies using ViewComponent.
650
-
651
- *Adil Lari*
652
-
653
711
  * Allow overridden slot methods to use `super`.
654
712
 
655
713
  *Andrew Schwartz*
@@ -889,10 +947,6 @@ This release makes the following breaking changes:
889
947
 
890
948
  *Simon Fish*
891
949
 
892
- * Add Simundia to list of companies using ViewComponent.
893
-
894
- *Alexandre Ignjatovic*
895
-
896
950
  * Reduce UnboundMethod objects by memoizing initialize_parameters.
897
951
 
898
952
  *Rainer Borene*
@@ -943,10 +997,6 @@ This release makes the following breaking changes:
943
997
 
944
998
  *milk1000cc*
945
999
 
946
- * Add PeopleForce to list of companies using ViewComponent.
947
-
948
- *Volodymyr Khandiuk*
949
-
950
1000
  ## 3.5.0
951
1001
 
952
1002
  * Add Skroutz to users list.
@@ -1021,14 +1071,6 @@ This release makes the following breaking changes:
1021
1071
 
1022
1072
  *Joel Hawksley*
1023
1073
 
1024
- * Add Ophelos to list of companies using ViewComponent.
1025
-
1026
- *Graham Rogers*
1027
-
1028
- * Add FlightLogger to list of companies using ViewComponent.
1029
-
1030
- *Joseph Carpenter*
1031
-
1032
1074
  * Fix coverage reports overwriting each other when running locally.
1033
1075
 
1034
1076
  *Jonathan del Strother*
@@ -1268,14 +1310,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1268
1310
 
1269
1311
  *Graham Rogers*
1270
1312
 
1271
- * Add Krystal to list of companies using ViewComponent.
1272
-
1273
- *Matt Bearman*
1274
-
1275
- * Add Mon Ami to list of companies using ViewComponent.
1276
-
1277
- *Ethan Lee-Tyson*
1278
-
1279
1313
  ## 3.0.0.rc1
1280
1314
 
1281
1315
  1,000+ days and 100+ releases later, the 200+ contributors to ViewComponent are proud to ship v3.0.0!
@@ -1424,10 +1458,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1424
1458
 
1425
1459
  *Hans Lemuet*
1426
1460
 
1427
- * Add Startup Jobs to list of companies using ViewComponent.
1428
-
1429
- *Marc Köhlbrugge*
1430
-
1431
1461
  * Run PVC's accessibility tests in a single process to avoid resource contention in CI.
1432
1462
 
1433
1463
  *Cameron Dutro*
@@ -1468,10 +1498,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1468
1498
 
1469
1499
  ## 2.74.0
1470
1500
 
1471
- * Add Avo to list of companies using ViewComponent.
1472
-
1473
- *Adrian Marin*
1474
-
1475
1501
  * Promote experimental `_output_postamble` method to public API as `output_postamble`.
1476
1502
 
1477
1503
  *Joel Hawksley*
@@ -1498,10 +1524,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1498
1524
 
1499
1525
  *Erinna Chen*
1500
1526
 
1501
- * Add PrintReleaf to list of companies using ViewComponent.
1502
-
1503
- *Ry Kulp*
1504
-
1505
1527
  * Simplify CI configuration to a single build per Ruby/Rails version.
1506
1528
 
1507
1529
  *Joel Hawksley*
@@ -1510,10 +1532,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1510
1532
 
1511
1533
  *Ruben Smit*
1512
1534
 
1513
- * Add Yobbers to list of companies using ViewComponent.
1514
-
1515
- *Anton Prins*
1516
-
1517
1535
  ## 2.72.0
1518
1536
 
1519
1537
  * Deprecate support for Ruby < 2.7 for removal in v3.0.0.
@@ -1528,10 +1546,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1528
1546
 
1529
1547
  *Joel Hawksley.
1530
1548
 
1531
- * Add Aluuno to list of companies using ViewComponent.
1532
-
1533
- *Daniel Naves de Carvalho*
1534
-
1535
1549
  * Add `source_code_uri` to gemspec.
1536
1550
 
1537
1551
  *Yoshiyuki Hirano*
@@ -1566,22 +1580,10 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1566
1580
 
1567
1581
  *Joel Hawksley*
1568
1582
 
1569
- * Add Arrows to list of companies using ViewComponent.
1570
-
1571
- *Matt Swanson*
1572
-
1573
- * Add WIP to list of companies using ViewComponent.
1574
-
1575
- *Marc Köhlbrugge*
1576
-
1577
1583
  * Update slots documentation to include how to reference slots.
1578
1584
 
1579
1585
  *Brittany Ellich*
1580
1586
 
1581
- * Add Clio to list of companies using ViewComponent.
1582
-
1583
- *Mike Buckley*
1584
-
1585
1587
  ## 2.69.0
1586
1588
 
1587
1589
  * Add missing `require` to fix `pvc` build.
@@ -1699,10 +1701,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1699
1701
 
1700
1702
  *Vikram Dighe*
1701
1703
 
1702
- * Add HappyCo to list of companies using ViewComponent.
1703
-
1704
- *Josh Clayton*
1705
-
1706
1704
  * Add predicate method support to polymorphic slots.
1707
1705
 
1708
1706
  *Graham Rogers*
@@ -1813,10 +1811,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1813
1811
 
1814
1812
  *Thomas Hutterer*
1815
1813
 
1816
- * Add FreeAgent to list of companies using ViewComponent.
1817
-
1818
- *Simon Fish*
1819
-
1820
1814
  * Include polymorphic slots in `ViewComponent::Base` by default.
1821
1815
 
1822
1816
  *Cameron Dutro*
@@ -1872,18 +1866,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1872
1866
 
1873
1867
  *Joel Hawksley*
1874
1868
 
1875
- * Add G2 to list of companies that use ViewComponent.
1876
-
1877
- *Jack Shuff*
1878
-
1879
- * Add Within3 to list of companies that use ViewComponent.
1880
-
1881
- *Drew Bragg*
1882
-
1883
- * Add Mission Met to list of companies that use ViewComponent.
1884
-
1885
- *Nick Smith*
1886
-
1887
1869
  * Fix `#with_request_url` test helper not parsing nested query parameters into nested hashes.
1888
1870
 
1889
1871
  *Richard Marbach*
@@ -1923,10 +1905,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1923
1905
 
1924
1906
  *Blake Williams*
1925
1907
 
1926
- * Add QuickNode to list of companies that use ViewComponent.
1927
-
1928
- *Luc Castera*
1929
-
1930
1908
  * Include the `Translatable` module by default.
1931
1909
 
1932
1910
  *Elia Schito*
@@ -1957,10 +1935,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1957
1935
 
1958
1936
  *Jason Swett*
1959
1937
 
1960
- * Add Bearer to list of companies that use ViewComponent.
1961
-
1962
- *Yaroslav Shmarov*
1963
-
1964
1938
  * Add articles to resources page.
1965
1939
 
1966
1940
  *Joel Hawksley*
@@ -1990,10 +1964,6 @@ Run into an issue with this release? [Let us know](https://github.com/ViewCompon
1990
1964
 
1991
1965
  *Hans Lemuet*
1992
1966
 
1993
- * Alphabetize companies using ViewComponent and add Brightline to the list.
1994
-
1995
- *Jack Schuss*
1996
-
1997
1967
  * Add CMYK value for ViewComponent Red color on logo page.
1998
1968
 
1999
1969
  *Dylan Smith*
@@ -23,7 +23,7 @@ module ViewComponent
23
23
  class_option :skip_suffix, type: :boolean, default: false
24
24
 
25
25
  def create_component_file
26
- template "component.rb", File.join(component_path, class_path, "#{file_name}#{options[:skip_suffix] ? "" : "_component"}.rb")
26
+ template "component.rb", File.join(component_path, class_path, "#{file_name}#{"_component" unless options[:skip_suffix]}.rb")
27
27
  end
28
28
 
29
29
  hook_for :test_framework
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "action_view"
4
- require "active_support/configurable"
5
4
  require "view_component/collection"
6
5
  require "view_component/compile_cache"
7
6
  require "view_component/compiler"
@@ -47,7 +46,7 @@ module ViewComponent
47
46
  end
48
47
 
49
48
  include ActionView::Helpers
50
- include Rails.application.routes.url_helpers if defined?(Rails) && Rails.application
49
+ include Rails.application.routes.url_helpers if defined?(Rails.application.routes)
51
50
  include ERB::Escape
52
51
  include ActiveSupport::CoreExt::ERBUtil
53
52
 
@@ -132,6 +131,7 @@ module ViewComponent
132
131
 
133
132
  @__vc_content_evaluated = false
134
133
  @__vc_render_in_block = block
134
+ @view_context.instance_variable_set(:@virtual_path, virtual_path)
135
135
 
136
136
  before_render
137
137
 
@@ -139,8 +139,6 @@ module ViewComponent
139
139
  value = nil
140
140
 
141
141
  @output_buffer.with_buffer do
142
- @view_context.instance_variable_set(:@virtual_path, virtual_path)
143
-
144
142
  rendered_template =
145
143
  around_render do
146
144
  render_template_for(@__vc_requested_details).to_s
@@ -261,6 +259,9 @@ module ViewComponent
261
259
  @view_context.render(options, args, &block)
262
260
  elsif block
263
261
  __vc_original_view_context.render(options, args) do
262
+ # capture the block output in the view context of the component
263
+ output = capture(&block)
264
+
264
265
  # Partials are rendered to their own buffer and do not append to the
265
266
  # original @output_buffer we retain a reference to in #render_in. This
266
267
  # is a problem since the block passed to us here in the #render method
@@ -268,7 +269,7 @@ module ViewComponent
268
269
  # appends to the original @output_buffer. To avoid this, we evaluate the
269
270
  # block in the view context instead, which will append to the output buffer
270
271
  # created for the partial.
271
- __vc_original_view_context.instance_exec(&block)
272
+ __vc_original_view_context.capture { output }
272
273
  end
273
274
  else
274
275
  __vc_original_view_context.render(options, args)
@@ -302,7 +303,7 @@ module ViewComponent
302
303
  @__vc_helpers ||= __vc_original_view_context || controller.view_context
303
304
  end
304
305
 
305
- if ::Rails.env.development? || ::Rails.env.test?
306
+ if defined?(Rails.env) && (::Rails.env.development? || ::Rails.env.test?)
306
307
  # @private
307
308
  def method_missing(method_name, *args) # rubocop:disable Style/MissingRespondToMissing
308
309
  super
@@ -331,7 +332,7 @@ module ViewComponent
331
332
  []
332
333
  end
333
334
 
334
- if Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
335
+ if defined?(Rails::VERSION) && Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
335
336
  # Rails expects us to define `format` on all renderables,
336
337
  # but we do not know the `format` of a ViewComponent until runtime.
337
338
  def format
@@ -362,7 +363,7 @@ module ViewComponent
362
363
 
363
364
  @__vc_content =
364
365
  if __vc_render_in_block_provided?
365
- with_original_virtual_path do
366
+ with_captured_virtual_path(@old_virtual_path) do
366
367
  view_context.capture(self, &@__vc_render_in_block)
367
368
  end
368
369
  elsif __vc_content_set_by_with_content_defined?
@@ -378,11 +379,14 @@ module ViewComponent
378
379
  end
379
380
 
380
381
  # @private
381
- def with_original_virtual_path
382
- @view_context.instance_variable_set(:@virtual_path, @old_virtual_path)
382
+ # Temporarily sets the virtual path to the captured value, then restores it.
383
+ # This ensures translations and other path-dependent code execute with the correct scope.
384
+ def with_captured_virtual_path(captured_path)
385
+ old_virtual_path = @view_context.instance_variable_get(:@virtual_path)
386
+ @view_context.instance_variable_set(:@virtual_path, captured_path)
383
387
  yield
384
388
  ensure
385
- @view_context.instance_variable_set(:@virtual_path, virtual_path)
389
+ @view_context.instance_variable_set(:@virtual_path, old_virtual_path)
386
390
  end
387
391
 
388
392
  private
@@ -611,6 +615,16 @@ module ViewComponent
611
615
  __vc_compiler.compiled?
612
616
  end
613
617
 
618
+ # Hook called by the compiler after a component is compiled.
619
+ #
620
+ # Extensions can override this class method to run logic after
621
+ # compilation (e.g., generate helpers, register metadata, etc.).
622
+ #
623
+ # By default, this is a no-op.
624
+ def after_compile
625
+ # no-op by default
626
+ end
627
+
614
628
  # @private
615
629
  def __vc_ensure_compiled
616
630
  __vc_compile unless __vc_compiled?
@@ -5,6 +5,7 @@ require "action_view/renderer/collection_renderer"
5
5
  module ViewComponent
6
6
  class Collection
7
7
  include Enumerable
8
+
8
9
  attr_reader :component
9
10
 
10
11
  delegate :size, to: :@collection
@@ -19,7 +20,7 @@ module ViewComponent
19
20
  components.each(&block)
20
21
  end
21
22
 
22
- if Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
23
+ if defined?(Rails::VERSION) && Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
23
24
  # Rails expects us to define `format` on all renderables,
24
25
  # but we do not know the `format` of a ViewComponent until runtime.
25
26
  def format
@@ -52,6 +52,8 @@ module ViewComponent
52
52
  @component.__vc_build_i18n_backend
53
53
 
54
54
  CompileCache.register(@component)
55
+
56
+ @component.after_compile
55
57
  end
56
58
  end
57
59
 
@@ -163,7 +163,7 @@ module ViewComponent
163
163
  options = ActiveSupport::OrderedOptions.new
164
164
  options.controller = "ViewComponentsController"
165
165
  options.route = "/rails/view_components"
166
- options.enabled = Rails.env.development? || Rails.env.test?
166
+ options.enabled = defined?(Rails.env) && (Rails.env.development? || Rails.env.test?)
167
167
  options.default_layout = nil
168
168
  options.paths = default_preview_paths
169
169
  options
@@ -4,14 +4,30 @@ module ViewComponent
4
4
  module Configurable
5
5
  extend ActiveSupport::Concern
6
6
 
7
+ class_methods do
8
+ def config
9
+ @_config ||= if respond_to?(:superclass) && superclass.respond_to?(:config)
10
+ superclass.config.inheritable_copy
11
+ else
12
+ ActiveSupport::OrderedOptions.new
13
+ end
14
+ end
15
+
16
+ def configure
17
+ yield config
18
+ end
19
+ end
20
+
7
21
  included do
8
22
  next if respond_to?(:config) && config.respond_to?(:view_component) && config.respond_to_missing?(:instrumentation_enabled)
9
23
 
10
- include ActiveSupport::Configurable
11
-
12
24
  configure do |config|
13
25
  config.view_component ||= ActiveSupport::InheritableOptions.new
14
26
  end
27
+
28
+ def config
29
+ self.class.config
30
+ end
15
31
  end
16
32
  end
17
33
  end
@@ -3,6 +3,7 @@
3
3
  module ViewComponent # :nodoc:
4
4
  module InlineTemplate
5
5
  extend ActiveSupport::Concern
6
+
6
7
  Template = Struct.new(:source, :language, :path, :lineno)
7
8
 
8
9
  class_methods do
@@ -6,7 +6,7 @@ module ViewComponent
6
6
  class Slot
7
7
  include ViewComponent::WithContentHelper
8
8
 
9
- attr_writer :__vc_component_instance, :__vc_content_block, :__vc_content
9
+ attr_writer :__vc_component_instance, :__vc_content_block, :__vc_content, :__vc_content_block_virtual_path
10
10
 
11
11
  def initialize(parent)
12
12
  @parent = parent
@@ -58,7 +58,7 @@ module ViewComponent
58
58
  if defined?(@__vc_content_block)
59
59
  # render_in is faster than `parent.render`
60
60
  @__vc_component_instance.render_in(view_context) do |*args|
61
- @parent.with_original_virtual_path do
61
+ @parent.with_captured_virtual_path(@__vc_content_block_virtual_path) do
62
62
  @__vc_content_block.call(*args)
63
63
  end
64
64
  end
@@ -68,7 +68,7 @@ module ViewComponent
68
68
  elsif defined?(@__vc_content)
69
69
  @__vc_content
70
70
  elsif defined?(@__vc_content_block)
71
- @parent.with_original_virtual_path do
71
+ @parent.with_captured_virtual_path(@__vc_content_block_virtual_path) do
72
72
  view_context.capture(&@__vc_content_block)
73
73
  end
74
74
  elsif defined?(@__vc_content_set_by_with_content)
@@ -390,7 +390,12 @@ module ViewComponent
390
390
  # 2. Since we have to pass block content to components when calling
391
391
  # `render`, evaluating the block here would require us to call
392
392
  # `view_context.capture` twice, which is slower
393
- slot.__vc_content_block = block if block
393
+ if block
394
+ slot.__vc_content_block = block
395
+ # Capture the virtual path at the time the block is defined, so that
396
+ # translations resolve relative to where the block was created, not where it's rendered
397
+ slot.__vc_content_block_virtual_path = view_context.instance_variable_get(:@virtual_path)
398
+ end
394
399
 
395
400
  # If class
396
401
  if slot_definition[:renderable]
@@ -408,7 +413,9 @@ module ViewComponent
408
413
  renderable_value =
409
414
  if block
410
415
  renderable_function.call(*args, **kwargs) do |*rargs|
411
- view_context.capture(*rargs, &block)
416
+ with_captured_virtual_path(@old_virtual_path) do
417
+ view_context.capture(*rargs, &block)
418
+ end
412
419
  end
413
420
  else
414
421
  renderable_function.call(*args, **kwargs)
@@ -21,11 +21,20 @@ module ViewComponent
21
21
 
22
22
  class File < Template
23
23
  def initialize(component:, details:, path:)
24
+ # Rails 8.1 added a newline to the compiled ERB output in
25
+ # https://github.com/rails/rails/pull/53731
26
+ lineno =
27
+ if Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb
28
+ - 1
29
+ else
30
+ 0
31
+ end
32
+
24
33
  super(
25
34
  component: component,
26
35
  details: details,
27
36
  path: path,
28
- lineno: 0
37
+ lineno: lineno
29
38
  )
30
39
  end
31
40
 
@@ -45,11 +54,20 @@ module ViewComponent
45
54
  def initialize(component:, inline_template:)
46
55
  details = ActionView::TemplateDetails.new(nil, inline_template.language.to_sym, nil, nil)
47
56
 
57
+ # Rails 8.1 added a newline to the compiled ERB output in
58
+ # https://github.com/rails/rails/pull/53731
59
+ lineno =
60
+ if Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb
61
+ inline_template.lineno - 1
62
+ else
63
+ inline_template.lineno
64
+ end
65
+
48
66
  super(
49
67
  component: component,
50
68
  details: details,
51
69
  path: inline_template.path,
52
- lineno: inline_template.lineno,
70
+ lineno: lineno,
53
71
  )
54
72
 
55
73
  @source = inline_template.source.dup
@@ -40,7 +40,7 @@ module ViewComponent
40
40
  @page = nil
41
41
  @rendered_content = vc_test_view_context.render(component, args, &block)
42
42
 
43
- fragment = Nokogiri::HTML5.fragment(@rendered_content)
43
+ fragment = Nokogiri::HTML5.fragment(@rendered_content, context: "template")
44
44
  @vc_test_view_context = nil
45
45
  fragment
46
46
  end
@@ -3,8 +3,8 @@
3
3
  module ViewComponent
4
4
  module VERSION
5
5
  MAJOR = 4
6
- MINOR = 0
7
- PATCH = 2
6
+ MINOR = 2
7
+ PATCH = 0
8
8
  PRE = nil
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "action_view"
4
4
  require "active_support/dependencies/autoload"
5
+ require "view_component/version"
5
6
 
6
7
  module ViewComponent
7
8
  extend ActiveSupport::Autoload
@@ -16,7 +17,7 @@ module ViewComponent
16
17
  autoload :Preview
17
18
  autoload :Translatable
18
19
 
19
- if Rails.env.test?
20
+ if defined?(Rails.env) && Rails.env.test?
20
21
  autoload :TestHelpers
21
22
  autoload :SystemSpecHelpers
22
23
  autoload :SystemTestHelpers
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: view_component
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.2
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ViewComponent Team
@@ -16,9 +16,6 @@ dependencies:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
18
  version: 7.1.0
19
- - - "<"
20
- - !ruby/object:Gem::Version
21
- version: '8.1'
22
19
  type: :runtime
23
20
  prerelease: false
24
21
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,9 +23,20 @@ dependencies:
26
23
  - - ">="
27
24
  - !ruby/object:Gem::Version
28
25
  version: 7.1.0
29
- - - "<"
26
+ - !ruby/object:Gem::Dependency
27
+ name: actionview
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
30
31
  - !ruby/object:Gem::Version
31
- version: '8.1'
32
+ version: 7.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 7.1.0
32
40
  - !ruby/object:Gem::Dependency
33
41
  name: concurrent-ruby
34
42
  requirement: !ruby/object:Gem::Requirement