forked from nicolas-brousse/view_component
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't add ActionDispatch::Static middleware continued (ViewComponent#…
…1892) * Don't add ActionDispatch::Static middleware In the case that rails do not serve static files is not necessary to add ActionDispatch::Static middleware. Making possible to deploy ViewComponenets previews on a production environment where Nginx or Apache is managing the static files. * Apply suggestions from code review Co-authored-by: Joel Hawksley <[email protected]> * Update index.md * add: prism cdn if no asset precompiler found * edit: change prism cdn version to 1.28 * Update app/helpers/preview_helper.rb Co-authored-by: Cameron Dutro <[email protected]> * Update app/helpers/preview_helper.rb Co-authored-by: Cameron Dutro <[email protected]> * Update app/helpers/preview_helper.rb Co-authored-by: Cameron Dutro <[email protected]> * Update app/helpers/preview_helper.rb Co-authored-by: Cameron Dutro <[email protected]> * Update lib/view_component/engine.rb Co-authored-by: Cameron Dutro <[email protected]> * edit: fix brocken tests * edit: refactor tests * edit: fix for rails > 6.1 * edit: fix lint issues * Update lib/view_component/engine.rb Co-authored-by: Hans Lemuet <[email protected]> --------- Co-authored-by: Daniel Gonzalez <[email protected]> Co-authored-by: Joel Hawksley <[email protected]> Co-authored-by: Cameron Dutro <[email protected]> Co-authored-by: Hans Lemuet <[email protected]>
- Loading branch information
Showing
7 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module PreviewHelper | ||
include ActionView::Helpers::AssetUrlHelper if Rails.version.to_f < 6.1 | ||
|
||
AVAILABLE_PRISM_LANGUAGES = %w[ruby erb haml] | ||
FALLBACK_LANGUAGE = "ruby" | ||
|
||
|
@@ -10,6 +12,14 @@ def preview_source | |
render "preview_source" | ||
end | ||
|
||
def prism_css_source_url | ||
serve_static_preview_assets? ? asset_path("prism.css", skip_pipeline: true) : "https://cdn.jsdelivr.net/npm/[email protected]/themes/prism.min.css" | ||
end | ||
|
||
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:) | ||
template = lookup_context.find_template(template_identifier) | ||
|
||
|
@@ -62,4 +72,8 @@ def prism_language_name_by_template_path(template_file_path:) | |
|
||
language | ||
end | ||
|
||
def serve_static_preview_assets? | ||
ViewComponent::Base.config.show_previews && Rails.application.config.public_file_server.enabled | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,5 +155,37 @@ def test_raises_with_conflict_in_template_resolution | |
assert_equal("Found multiple templates for #{template_identifier}.", exception.message) | ||
end | ||
end | ||
|
||
def test_prism_css_source_url_with_asset_pipeline | ||
Rails.application.config.public_file_server.stub(:enabled, true) do | ||
if Rails.version.to_f >= 6.1 | ||
assert_equal "/assets/prism.css", PreviewHelper.prism_css_source_url | ||
else | ||
assert_equal "/prism.css", PreviewHelper.prism_css_source_url | ||
end | ||
end | ||
end | ||
|
||
def test_prism_css_source_url_with_no_asset_pipeline | ||
Rails.application.config.public_file_server.stub(:enabled, false) do | ||
assert_equal "https://cdn.jsdelivr.net/npm/[email protected]/themes/prism.min.css", PreviewHelper.prism_css_source_url | ||
end | ||
end | ||
|
||
def test_prism_js_source_with_asset_pipeline | ||
Rails.application.config.public_file_server.stub(:enabled, true) do | ||
if Rails.version.to_f >= 6.1 | ||
assert_equal "/assets/prism.min.js", PreviewHelper.prism_js_source_url | ||
else | ||
assert_equal "/prism.min.js", PreviewHelper.prism_js_source_url | ||
end | ||
end | ||
end | ||
|
||
def test_prism_js_source_url_with_no_asset_pipeline | ||
Rails.application.config.public_file_server.stub(:enabled, false) do | ||
assert_equal "https://cdn.jsdelivr.net/npm/[email protected]/prism.min.js", PreviewHelper.prism_js_source_url | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class ViewComponent::EngineTest < ActionDispatch::IntegrationTest | ||
def test_serve_static_previews? | ||
app.config.public_file_server.enabled = false | ||
refute ViewComponent::Engine.instance.serve_static_preview_assets?(app.config) | ||
end | ||
end |