Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default preview controller should not crash with config.action_controller.include_all_helpers = false #2045

Merged
Show file tree
Hide file tree
Changes from 5 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
9 changes: 8 additions & 1 deletion app/controllers/concerns/view_component/preview_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ module PreviewActions

# Including helpers here ensures that we're loading the
# latest version of helpers if code-reloading is enabled
helper :all if include_all_helpers
if include_all_helpers
helper :all
else
# :nocov:
# Always provide the #view_source helper
helper PreviewHelper
# :nocov:
end
end

def index
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/preview_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def prism_js_source_url
serve_static_preview_assets? ? asset_path("prism.min.js", skip_pipeline: true) : "https://cdn.jsdelivr.net/npm/[email protected]/prism.min.js"
end

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

if Rails.version.to_f >= 6.1 || template.source.present?
Expand Down
2 changes: 1 addition & 1 deletion app/views/view_components/_preview_source.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<%= h @preview.preview_source(@example_name) %>
</code>
<% else %>
<% template_data = find_template_data(lookup_context: @view_renderer.lookup_context, template_identifier: @render_args[:template]) %>
<% template_data = find_template_data_for_preview_source(lookup_context: @view_renderer.lookup_context, template_identifier: @render_args[:template]) %>
<code class="language-<%= template_data[:prism_language_name] %>">
<%= h template_data[:source] %>
</code>
Expand Down
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@

*Reegan Viljoen*

* Fix a bug where component previews would crash with "undefined local variable or method `preview_source`".

Check failure on line 229 in docs/CHANGELOG.md

View workflow job for this annotation

GitHub Actions / prose

[vale] reported by reviewdog 🐶 [Microsoft.Quotes] Punctuation should be inside the quotes. Raw Output: {"message": "[Microsoft.Quotes] Punctuation should be inside the quotes.", "location": {"path": "docs/CHANGELOG.md", "range": {"start": {"line": 229, "column": 1}}}, "severity": "ERROR"}
joelhawksley marked this conversation as resolved.
Show resolved Hide resolved

*Henning Koch*

## 3.12.1

* Ensure content is rendered correctly for forwarded slots.
Expand Down
12 changes: 6 additions & 6 deletions test/sandbox/test/preview_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_returns_template_data_with_no_template
lookup_context = Minitest::Mock.new
lookup_context.expect(:find_template, mock_template, [template_identifier])

template_data = PreviewHelper.find_template_data(
template_data = PreviewHelper.find_template_data_for_preview_source(
lookup_context: lookup_context,
template_identifier: template_identifier
)
Expand All @@ -40,7 +40,7 @@ def test_returns_template_data_with_template_of_different_languages
lookup_context = Minitest::Mock.new
lookup_context.expect(:find_template, mock_template, [template_identifier])

template_data = PreviewHelper.find_template_data(
template_data = PreviewHelper.find_template_data_for_preview_source(
lookup_context: lookup_context,
template_identifier: template_identifier
)
Expand Down Expand Up @@ -68,7 +68,7 @@ def test_returns_template_data_without_dedicated_template
mock = Minitest::Mock.new
mock.expect :map, [expected_template_path]
ViewComponent::Base.stub(:preview_paths, mock) do
template_data = PreviewHelper.find_template_data(
template_data = PreviewHelper.find_template_data_for_preview_source(
lookup_context: lookup_context,
template_identifier: template_identifier
)
Expand Down Expand Up @@ -96,7 +96,7 @@ def test_returns_template_data_with_dedicated_template
mock.expect :map, [expected_template_path]
Rails.application.config.view_component.stub(:preview_paths, mock) do
File.stub(:read, expected_source, [expected_template_path]) do
template_data = PreviewHelper.find_template_data(
template_data = PreviewHelper.find_template_data_for_preview_source(
lookup_context: lookup_context,
template_identifier: template_identifier
)
Expand All @@ -122,7 +122,7 @@ def test_raises_with_no_matching_template
mock.expect :map, []
Rails.application.config.view_component.stub :preview_paths, mock do
exception = assert_raises ViewComponent::NoMatchingTemplatesForPreviewError do
PreviewHelper.find_template_data(
PreviewHelper.find_template_data_for_preview_source(
lookup_context: lookup_context,
template_identifier: template_identifier
)
Expand All @@ -146,7 +146,7 @@ def test_raises_with_conflict_in_template_resolution
mock.expect :map, [template_identifier + ".html.haml", template_identifier + ".html.erb"]
Rails.application.config.view_component.stub :preview_paths, mock do
exception = assert_raises ViewComponent::MultipleMatchingTemplatesForPreviewError do
PreviewHelper.find_template_data(
PreviewHelper.find_template_data_for_preview_source(
lookup_context: lookup_context,
template_identifier: template_identifier
)
Expand Down
Loading