Add option to set default app layout for previews
Add integration test Update Changelog Fix test error for no layout previews Refactor logic for determining preview layout Better wording of method comment Fix rebase merge issue Move custom layout tests into separate file Incorporate feedback from juanmanuelramallo Fix for "interpreted as argument prefix" warning Add render string back in for index action
This commit is contained in:
Родитель
5b2c32d6a4
Коммит
3e1dac7f07
|
@ -1,5 +1,9 @@
|
|||
# master
|
||||
|
||||
* Add `default_preview_layout` configuration option to load custom app/views/layouts.
|
||||
|
||||
*Jared White, Juan Manuel Ramallo*
|
||||
|
||||
# 2.17.0
|
||||
|
||||
* Slots return stripped HTML, removing leading and trailing whitespace.
|
||||
|
|
|
@ -16,18 +16,19 @@ class ViewComponentsController < Rails::ApplicationController # :nodoc:
|
|||
def index
|
||||
@previews = ViewComponent::Preview.all
|
||||
@page_title = "Component Previews"
|
||||
render "view_components/index", **determine_layout
|
||||
end
|
||||
|
||||
def previews
|
||||
if params[:path] == @preview.preview_name
|
||||
@page_title = "Component Previews for #{@preview.preview_name}"
|
||||
render "view_components/previews"
|
||||
render "view_components/previews", **determine_layout
|
||||
else
|
||||
prepend_application_view_paths
|
||||
prepend_preview_examples_view_path
|
||||
@example_name = File.basename(params[:path])
|
||||
@render_args = @preview.render_args(@example_name, params: params.permit!)
|
||||
layout = @render_args[:layout]
|
||||
layout = determine_layout(@render_args[:layout])[:layout]
|
||||
template = @render_args[:template]
|
||||
locals = @render_args[:locals]
|
||||
opts = {}
|
||||
|
@ -39,6 +40,10 @@ class ViewComponentsController < Rails::ApplicationController # :nodoc:
|
|||
|
||||
private
|
||||
|
||||
def default_preview_layout # :doc:
|
||||
ViewComponent::Base.default_preview_layout
|
||||
end
|
||||
|
||||
def show_previews? # :doc:
|
||||
ViewComponent::Base.show_previews
|
||||
end
|
||||
|
@ -61,6 +66,22 @@ class ViewComponentsController < Rails::ApplicationController # :nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
# Returns either {} or {layout: value} depending on configuration
|
||||
def determine_layout(layout_override = nil)
|
||||
return {} unless defined?(Rails.root)
|
||||
|
||||
layout_declaration = {}
|
||||
|
||||
if !layout_override.nil?
|
||||
# Allow component-level override, even if false (thus no layout rendered)
|
||||
layout_declaration[:layout] = layout_override
|
||||
elsif default_preview_layout.present?
|
||||
layout_declaration[:layout] = default_preview_layout
|
||||
end
|
||||
|
||||
layout_declaration
|
||||
end
|
||||
|
||||
def prepend_application_view_paths
|
||||
prepend_view_path Rails.root.join("app/views") if defined?(Rails.root)
|
||||
end
|
||||
|
|
|
@ -7,6 +7,14 @@ module ViewComponent # :nodoc:
|
|||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
# Set a custom default preview layout through app configuration:
|
||||
#
|
||||
# config.view_component.default_preview_layout = "component_preview"
|
||||
#
|
||||
# This affects preview index pages as well as individual component previews
|
||||
#
|
||||
mattr_accessor :default_preview_layout, instance_writer: false
|
||||
|
||||
# Set the location of component previews through app configuration:
|
||||
#
|
||||
# config.view_component.preview_paths << "#{Rails.root}/lib/component_previews"
|
||||
|
|
|
@ -41,3 +41,10 @@ def modify_file(file, content)
|
|||
File.open(filename, "wb+") { |f| f.write(old_content) }
|
||||
end
|
||||
end
|
||||
|
||||
def with_default_preview_layout(layout)
|
||||
old_value = ViewComponent::Base.default_preview_layout
|
||||
ViewComponent::Base.default_preview_layout = layout
|
||||
yield
|
||||
ViewComponent::Base.default_preview_layout = old_value
|
||||
end
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
class DefaultPreviewLayoutIntegrationTest < ActionDispatch::IntegrationTest
|
||||
test "preview index renders custom application layout if configured" do
|
||||
with_default_preview_layout("admin") do
|
||||
get "/rails/view_components"
|
||||
assert_select "title", "ViewComponent - Admin - Test"
|
||||
end
|
||||
end
|
||||
|
||||
test "preview index of a component renders custom application layout if configured" do
|
||||
with_default_preview_layout("admin") do
|
||||
get "/rails/view_components/preview_component"
|
||||
assert_select "title", "ViewComponent - Admin - Test"
|
||||
end
|
||||
end
|
||||
|
||||
test "component preview renders custom application layout if configured" do
|
||||
with_default_preview_layout("admin") do
|
||||
get "/rails/view_components/preview_component/default"
|
||||
assert_select "title", "ViewComponent - Admin - Test"
|
||||
assert_select ".preview-component .btn", "Click me!"
|
||||
end
|
||||
end
|
||||
|
||||
test "component preview renders standard Rails layout if configured false" do
|
||||
with_default_preview_layout(false) do
|
||||
get "/rails/view_components/preview_component/default"
|
||||
|
||||
assert_select "title", "ViewComponent - Test"
|
||||
assert_select ".preview-component .btn", "Click me!"
|
||||
end
|
||||
end
|
||||
|
||||
test "preview renders without layout even if default layout is configured" do
|
||||
with_default_preview_layout("admin") do
|
||||
get "/rails/view_components/no_layout/default"
|
||||
assert_select("div", "hello,world!")
|
||||
refute_includes response.body, "ViewComponent - Admin - Test"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -281,6 +281,7 @@ class IntegrationTest < ActionDispatch::IntegrationTest
|
|||
get "/rails/view_components/no_layout/default"
|
||||
|
||||
assert_select("div", "hello,world!")
|
||||
refute_includes response.body, "ViewComponent - Test"
|
||||
end
|
||||
|
||||
test "test preview renders application's layout by default" do
|
||||
|
|
Загрузка…
Ссылка в новой задаче