[326] Configurable preview route
- Added a config attribute to allow users to configure the endpoint to be used for previewing components - Updated views to use a helper path method instead of hardcoded paths - Updated the engine to use the new configuration attribute - Removed unused method from test helper - Added a test helper method to run assertions with a different preview route config - Updated integration test
This commit is contained in:
Родитель
dfd2bf710a
Коммит
e9c451af50
|
@ -9,6 +9,8 @@ class ViewComponentsController < Rails::ApplicationController # :nodoc:
|
|||
before_action :find_preview, only: :previews
|
||||
before_action :require_local!, unless: :show_previews?
|
||||
|
||||
delegate :preview_component_path, to: :url_helpers
|
||||
|
||||
if respond_to?(:content_security_policy)
|
||||
content_security_policy(false)
|
||||
end
|
||||
|
@ -59,4 +61,8 @@ class ViewComponentsController < Rails::ApplicationController # :nodoc:
|
|||
def prepend_application_view_paths
|
||||
prepend_view_path Rails.root.join("app/views") if defined?(Rails.root)
|
||||
end
|
||||
|
||||
def url_helpers
|
||||
Rails.application.routes.url_helpers
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
<% @previews.each do |preview| %>
|
||||
<h3><%= link_to preview.preview_name.titleize, "/rails/view_components/#{preview.preview_name}" %></h3>
|
||||
<h3><%= link_to preview.preview_name.titleize, preview_view_component_path(preview.preview_name) %></h3>
|
||||
<ul>
|
||||
<% preview.examples.each do |preview_example| %>
|
||||
<li><%= link_to preview_example, "/rails/view_components/#{preview.preview_name}/#{preview_example}" %></li>
|
||||
<li><%= link_to preview_example, preview_view_component_path("#{preview.preview_name}/#{preview_example}") %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<h3><%= @preview.preview_name.titleize %></h3>
|
||||
<ul>
|
||||
<% @preview.examples.each do |example| %>
|
||||
<li><%= link_to example, "/rails/view_components/#{@preview.preview_name}/#{example}" %></li>
|
||||
<li><%= link_to example, preview_view_component_path("#{@preview.preview_name}/#{example}") %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
|
|
@ -14,6 +14,7 @@ module ViewComponent
|
|||
|
||||
if options.show_previews
|
||||
options.preview_path ||= defined?(Rails.root) ? "#{Rails.root}/test/components/previews" : nil
|
||||
options.preview_route ||= "/rails/view_components"
|
||||
end
|
||||
|
||||
ActiveSupport.on_load(:view_component) do
|
||||
|
@ -64,8 +65,8 @@ module ViewComponent
|
|||
|
||||
if options.show_previews
|
||||
app.routes.prepend do
|
||||
get "/rails/view_components" => "view_components#index", :internal => true
|
||||
get "/rails/view_components/*path" => "view_components#previews", :internal => true
|
||||
get options.preview_route, to: "view_components#index", as: "preview_view_components", internal: true
|
||||
get "#{options.preview_route}/*path", to: "view_components#previews", as: "preview_view_component", internal: true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -20,6 +20,14 @@ module ViewComponent # :nodoc:
|
|||
# Defaults to +true+ for development environment
|
||||
#
|
||||
mattr_accessor :show_previews, instance_writer: false
|
||||
|
||||
# Set the entry route for component previews through app configuration:
|
||||
#
|
||||
# config.view_component.preview_route = "/previews"
|
||||
#
|
||||
# Defaults to +/rails/view_components+ when `show_previews' is enabled
|
||||
#
|
||||
mattr_accessor :preview_route, instance_writer: false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,18 +11,11 @@ ENV["RAILS_ENV"] = "test"
|
|||
require File.expand_path("../config/environment.rb", __FILE__)
|
||||
require "rails/test_help"
|
||||
|
||||
def trim_result(content)
|
||||
content = content.to_s.lines.collect(&:strip).join("\n").strip
|
||||
|
||||
doc = Nokogiri::HTML.fragment(content)
|
||||
|
||||
doc.xpath("//text()").each do |node|
|
||||
if node.content.match?(/\S/)
|
||||
node.content = node.content.gsub(/\s+/, " ").strip
|
||||
else
|
||||
node.remove
|
||||
end
|
||||
end
|
||||
|
||||
doc.to_s.strip
|
||||
def with_preview_route(new_value)
|
||||
old_value = Rails.application.config.view_component.preview_route
|
||||
Rails.application.config.view_component.preview_route = new_value
|
||||
app.reloader.reload!
|
||||
yield
|
||||
Rails.application.config.view_component.preview_route = old_value
|
||||
app.reloader.reload!
|
||||
end
|
||||
|
|
|
@ -280,4 +280,17 @@ class IntegrationTest < ActionDispatch::IntegrationTest
|
|||
assert_select("p", text: "Radio clock counter: 1")
|
||||
assert_select("p", text: "Mints counter: 2")
|
||||
end
|
||||
|
||||
test "renders the previews in the configured route" do
|
||||
with_preview_route("/previews") do
|
||||
get "/previews"
|
||||
assert_select "title", "Component Previews"
|
||||
|
||||
get "/previews/preview_component/default"
|
||||
assert_select "title", "ViewComponent - Test"
|
||||
|
||||
get "/previews/preview_component"
|
||||
assert_select "title", "Component Previews for preview_component"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Загрузка…
Ссылка в новой задаче