Skip to content

Commit eba4c14

Browse files
Default preview controller should not crash with config.action_controller.include_all_helpers = false (#2045)
* Default preview controller should not crash with `config.action_controller.include_all_helpers = false` (fixes #1654) * PreviewHelper: Rename private #find_template_data method to something less generic The default Rails configuration loads the PreviewHelper for every controller. We should use specific method names to prevent name clashes with user-provided helpers. * Apply suggestions from code review * add nocov * Update docs/CHANGELOG.md --------- Co-authored-by: Joel Hawksley <joel@hawksley.org> Co-authored-by: Joel Hawksley <joelhawksley@github.com>
1 parent c057d4b commit eba4c14

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed

app/controllers/concerns/view_component/preview_actions.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ module PreviewActions
1414

1515
# Including helpers here ensures that we're loading the
1616
# latest version of helpers if code-reloading is enabled
17-
helper :all if include_all_helpers
17+
if include_all_helpers
18+
helper :all
19+
else
20+
# :nocov:
21+
# Always provide the #view_source helper
22+
helper PreviewHelper
23+
# :nocov:
24+
end
1825
end
1926

2027
def index

app/helpers/preview_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def prism_js_source_url
2222
serve_static_preview_assets? ? asset_path("prism.min.js", skip_pipeline: true) : "https://cdn.jsdelivr.net/npm/prismjs@1.28.0/prism.min.js"
2323
end
2424

25-
def find_template_data(lookup_context:, template_identifier:)
25+
def find_template_data_for_preview_source(lookup_context:, template_identifier:)
2626
template = lookup_context.find_template(template_identifier)
2727

2828
if Rails.version.to_f >= 6.1 || template.source.present?

app/views/view_components/_preview_source.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<%= h @preview.preview_source(@example_name) %>
88
</code>
99
<% else %>
10-
<% template_data = find_template_data(lookup_context: @view_renderer.lookup_context, template_identifier: @render_args[:template]) %>
10+
<% template_data = find_template_data_for_preview_source(lookup_context: @view_renderer.lookup_context, template_identifier: @render_args[:template]) %>
1111
<code class="language-<%= template_data[:prism_language_name] %>">
1212
<%= h template_data[:source] %>
1313
</code>

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ nav_order: 5
226226

227227
*Reegan Viljoen*
228228

229+
* Fix a bug where component previews would crash with "undefined local variable or method `preview_source`."
230+
231+
*Henning Koch*
232+
229233
## 3.12.1
230234

231235
* Ensure content is rendered correctly for forwarded slots.

test/sandbox/test/preview_helper_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_returns_template_data_with_no_template
1717
lookup_context = Minitest::Mock.new
1818
lookup_context.expect(:find_template, mock_template, [template_identifier])
1919

20-
template_data = PreviewHelper.find_template_data(
20+
template_data = PreviewHelper.find_template_data_for_preview_source(
2121
lookup_context: lookup_context,
2222
template_identifier: template_identifier
2323
)
@@ -40,7 +40,7 @@ def test_returns_template_data_with_template_of_different_languages
4040
lookup_context = Minitest::Mock.new
4141
lookup_context.expect(:find_template, mock_template, [template_identifier])
4242

43-
template_data = PreviewHelper.find_template_data(
43+
template_data = PreviewHelper.find_template_data_for_preview_source(
4444
lookup_context: lookup_context,
4545
template_identifier: template_identifier
4646
)
@@ -68,7 +68,7 @@ def test_returns_template_data_without_dedicated_template
6868
mock = Minitest::Mock.new
6969
mock.expect :map, [expected_template_path]
7070
ViewComponent::Base.stub(:preview_paths, mock) do
71-
template_data = PreviewHelper.find_template_data(
71+
template_data = PreviewHelper.find_template_data_for_preview_source(
7272
lookup_context: lookup_context,
7373
template_identifier: template_identifier
7474
)
@@ -96,7 +96,7 @@ def test_returns_template_data_with_dedicated_template
9696
mock.expect :map, [expected_template_path]
9797
Rails.application.config.view_component.stub(:preview_paths, mock) do
9898
File.stub(:read, expected_source, [expected_template_path]) do
99-
template_data = PreviewHelper.find_template_data(
99+
template_data = PreviewHelper.find_template_data_for_preview_source(
100100
lookup_context: lookup_context,
101101
template_identifier: template_identifier
102102
)
@@ -122,7 +122,7 @@ def test_raises_with_no_matching_template
122122
mock.expect :map, []
123123
Rails.application.config.view_component.stub :preview_paths, mock do
124124
exception = assert_raises ViewComponent::NoMatchingTemplatesForPreviewError do
125-
PreviewHelper.find_template_data(
125+
PreviewHelper.find_template_data_for_preview_source(
126126
lookup_context: lookup_context,
127127
template_identifier: template_identifier
128128
)
@@ -146,7 +146,7 @@ def test_raises_with_conflict_in_template_resolution
146146
mock.expect :map, [template_identifier + ".html.haml", template_identifier + ".html.erb"]
147147
Rails.application.config.view_component.stub :preview_paths, mock do
148148
exception = assert_raises ViewComponent::MultipleMatchingTemplatesForPreviewError do
149-
PreviewHelper.find_template_data(
149+
PreviewHelper.find_template_data_for_preview_source(
150150
lookup_context: lookup_context,
151151
template_identifier: template_identifier
152152
)

0 commit comments

Comments
 (0)