Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
view_component (3.24.0)
view_component (3.25.0)
activesupport (>= 5.2.0, < 8.2)
concurrent-ruby (~> 1)
method_source (~> 1.0)
Expand Down
10 changes: 9 additions & 1 deletion app/helpers/preview_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def prism_js_source_url
end

def find_template_data_for_preview_source(lookup_context:, template_identifier:)
template = lookup_context.find_template(template_identifier)
template = find_template_for_preview_source(lookup_context, template_identifier)

if Rails.version.to_f >= 6.1 || template.source.present?
{
Expand Down Expand Up @@ -61,6 +61,14 @@ def find_template_data_for_preview_source(lookup_context:, template_identifier:)

private

def find_template_for_preview_source(lookup_context, template_identifier)
if lookup_context.respond_to?(:find_template)
lookup_context.find_template(template_identifier)
else
lookup_context.find(template_identifier)
end
end

def prism_language_name_by_template(template:)
language = template.identifier.split(".").last

Expand Down
8 changes: 8 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ nav_order: 6

## main

* Fix rendering partials from components rendered as a collection when the original view context is explicitly stashed as `nil`.

*Justin Coyne, Joel Hawksley*

* Restore compatibility with Rails main after `ActionView::Template.template_handler_extensions` and `ActionView::LookupContext#find_template` were removed.

*Joel Hawksley*

## 3.25.0

* Support Rails `render_in` options signature. Rails [#50623](https://github.com/rails/rails/pull/50623) changed the `render_in` signature from `render_in(view_context, &block)` to `render_in(view_context, **options, &block)`. `ViewComponent::Base#render_in`, `ViewComponent::Collection#render_in`, and `ViewComponent::Instrumentation#render_in` now accept `**options`, restoring compatibility with Rails main and silencing the deprecation warning.
Expand Down
2 changes: 1 addition & 1 deletion lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def render_in(view_context, **options, &block)
@view_context = view_context
self.__vc_original_view_context =
if instance_variable_defined?(:@__vc_pending_original_view_context)
remove_instance_variable(:@__vc_pending_original_view_context)
remove_instance_variable(:@__vc_pending_original_view_context) || view_context
else
view_context
end
Expand Down
10 changes: 9 additions & 1 deletion lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def gather_templates
@templates ||=
begin
templates = @component.sidecar_files(
ActionView::Template.template_handler_extensions
template_handler_extensions
).map do |path|
# Extract format and variant from template filename
this_format, variant =
Expand Down Expand Up @@ -225,5 +225,13 @@ def gather_templates
templates
end
end

def template_handler_extensions
if ActionView::Template.respond_to?(:template_handler_extensions)
ActionView::Template.template_handler_extensions
else
ActionView::Template::Handlers.extensions.map(&:to_s)
end
end
end
end
24 changes: 24 additions & 0 deletions test/sandbox/test/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ def call
end
end

# Renders a plain partial, which routes through `ViewComponent::Base#render`
# and so depends on `__vc_original_view_context` being set.
class PartialRenderingComponent < ViewComponent::Base
with_collection_parameter :item

def initialize(item: nil)
@item = item
end

def call
render partial: "integration_examples/test_partial"
end
end

def setup
@products = [Product.new(name: "Radio clock"), Product.new(name: "Mints")]
@collection = ProductComponent.with_collection(@products, notice: "secondhand")
Expand All @@ -46,5 +60,15 @@ def test_supports_collection_with_spacer_component
render_inline(ProductComponent.with_collection(@products, spacer_component: SpacerComponent.new))
assert_selector("hr", count: 1)
end

# A collection rendered from a plain view (rather than from inside another
# component) never has `set_original_view_context` called on it, so its own
# `__vc_original_view_context` is nil. That nil must not be forwarded to the
# child components, or rendering a partial from one raises NoMethodError.
def test_supports_components_that_render_partials
render_inline(PartialRenderingComponent.with_collection([1, 2]))

assert_text("hello,partial world!", count: 2)
end
end
end
Loading