Skip to content

Commit

Permalink
Merge pull request #606 from liram11/fix-partial-rendering
Browse files Browse the repository at this point in the history
Fix rendering of partials in previews
  • Loading branch information
allmarkedup authored Apr 17, 2024
2 parents eb3ca54 + 8319d04 commit 776fc81
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 37 deletions.
2 changes: 1 addition & 1 deletion app/controllers/lookbook/page_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def render_page(page, locals = {})
@next_page = @pages.next(@page)
@previous_page = @pages.previous(@page)

content = ActionViewAnnotationsHandler.call(disable_annotations: true) do
content = ActionViewConfigHandler.call do
render_to_string inline: @page.content, locals: {
page: @page,
next_page: @next_page,
Expand Down
1 change: 1 addition & 0 deletions config/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ shared:
display_option_controls: true
preview_layout: ~
preview_disable_action_view_annotations: true
preview_disable_action_view_partial_prefixes: true
preview_type_default: view_component
preview_sort_scenarios: false
preview_disable_error_handling: false
Expand Down
8 changes: 8 additions & 0 deletions docs/src/_data/config_options.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ previews:
example: config.lookbook.preview_disable_action_view_annotations = false
description: Turns off action view filename annotations when generating rendered component source.

- name: preview_disable_action_view_partial_prefixes
types: Boolean
default: "true"
example: config.lookbook.preview_disable_action_view_partial_prefixes = false
description: |
Turns off action view automatic partial namespace prefixing when generating rendered component source.
This option enables short-hand syntax (`render @model`) in your partials.
- name: preview_sort_scenarios
types: Boolean
default: "false"
Expand Down
32 changes: 17 additions & 15 deletions lib/lookbook/preview_controller_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,23 @@ def render_scenario_to_string(preview, scenario)
opts[:assigns] = @render_args[:assigns] || {}
opts[:locals] = locals if locals.present?

rendered = render_to_string(template, **opts)

if scenario.after_render_method.present?
render_context = Store.new({
preview: preview,
scenario: scenario,
params: user_request_parameters
})
rendered = @preview.after_render(method: scenario.after_render_method, html: rendered, context: render_context)
end
with_action_view_settings do
rendered = render_to_string(template, **opts)

with_optional_action_view_annotations do
if scenario.after_render_method.present?
render_context = Store.new({
preview: preview,
scenario: scenario,
params: user_request_parameters
})
rendered = @preview.after_render(method: scenario.after_render_method, html: rendered, context: render_context)
end
render html: rendered
end
end

def render_in_layout_to_string(template, locals, opts = {})
with_optional_action_view_annotations do
with_action_view_settings do
html = render_to_string(template, locals: locals, **determine_layout(opts[:layout]))
if opts[:append_html].present?
html += opts[:append_html]
Expand All @@ -49,9 +48,12 @@ def render_in_layout_to_string(template, locals, opts = {})

protected

def with_optional_action_view_annotations(&block)
disable = Lookbook.config.preview_disable_action_view_annotations
ActionViewAnnotationsHandler.call(disable_annotations: disable, &block)
def with_action_view_settings(&block)
ActionViewConfigHandler.call(
disable_annotations: Lookbook.config.preview_disable_action_view_annotations,
disable_partial_prefixes: Lookbook.config.preview_disable_action_view_partial_prefixes,
&block
)
end

def user_request_parameters
Expand Down
21 changes: 0 additions & 21 deletions lib/lookbook/services/templates/action_view_annotations_handler.rb

This file was deleted.

50 changes: 50 additions & 0 deletions lib/lookbook/services/templates/action_view_config_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module Lookbook
class ActionViewConfigHandler < Service
attr_reader :disable_annotations, :disable_partial_prefixes

def initialize(disable_annotations: true, disable_partial_prefixes: true)
@disable_annotations = disable_annotations
@disable_partial_prefixes = disable_partial_prefixes
end

def call
handle_annotations
handle_partial_prefixes

yield
ensure
restore_annotations
restore_partial_prefixes
end

private

def handle_annotations
return unless disable_annotations && ActionView::Base.respond_to?(:annotate_rendered_view_with_filenames)

@original_annotations_value = ActionView::Base.annotate_rendered_view_with_filenames
ActionView::Base.annotate_rendered_view_with_filenames = false
end

def restore_annotations
return if @original_annotations_value.nil?

ActionView::Base.annotate_rendered_view_with_filenames = @original_annotations_value
@original_annotations_value = nil
end

def handle_partial_prefixes
return unless disable_partial_prefixes && ActionView::Base.respond_to?(:prefix_partial_path_with_controller_namespace)

@original_partial_prefix_value = ActionView::Base.prefix_partial_path_with_controller_namespace
ActionView::Base.prefix_partial_path_with_controller_namespace = false
end

def restore_partial_prefixes
return if @original_partial_prefix_value.nil?

ActionView::Base.prefix_partial_path_with_controller_namespace = @original_partial_prefix_value
@original_partial_prefix_value = nil
end
end
end
104 changes: 104 additions & 0 deletions spec/lib/services/templates/action_view_config_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
require "rails_helper"

ANNOTATIONS_SUPPORTED = ActionView::Base.respond_to?(:annotate_rendered_view_with_filenames)
PARTIAL_PREFIX_SUPPORTED = ActionView::Base.respond_to?(:prefix_partial_path_with_controller_namespace)

RSpec.describe Lookbook::ActionViewConfigHandler do
let(:original_annotations_value) { true }
let(:original_partial_prefix_value) { true }

context "with ActionView annotations supported", if: ANNOTATIONS_SUPPORTED do
before do
allow(ActionView::Base).to receive(:annotate_rendered_view_with_filenames).and_return(original_annotations_value)
end

context "with default values" do
it "disables annotations" do
expect(ActionView::Base).to receive(:annotate_rendered_view_with_filenames=).with(false)

described_class.call do
expect(ActionView::Base).to receive(:annotate_rendered_view_with_filenames=).with(original_annotations_value)
end
end
end

context "with custom settings" do
let(:disable_annotations) { true }

subject { described_class.call(disable_annotations: disable_annotations) {} }

it "disables annotations" do
expect(ActionView::Base).to receive(:annotate_rendered_view_with_filenames=).with(false)

described_class.call(disable_annotations: disable_annotations) do
expect(ActionView::Base).to receive(:annotate_rendered_view_with_filenames=).with(original_annotations_value)
end
end

describe "when disable_annotations is false" do
let(:disable_annotations) { false }

it "doesn't touch corresponding ActionView::Base constant" do
expect(ActionView::Base).to_not receive(:annotate_rendered_view_with_filenames=)

subject
end
end
end
end

context "without ActionView annotations supported", if: !ANNOTATIONS_SUPPORTED do
it "doesn't touch annotate_rendered_view_with_filenames ActionView::Base constant" do
expect(ActionView::Base).to_not receive(:annotate_rendered_view_with_filenames=)

described_class.call(disable_annotations: true) {}
end
end

context "with ActionView partial prefix supported", if: PARTIAL_PREFIX_SUPPORTED do
before do
allow(ActionView::Base).to receive(:prefix_partial_path_with_controller_namespace).and_return(original_partial_prefix_value)
end

context "with default values" do
it "disables partial prefixing" do
expect(ActionView::Base).to receive(:prefix_partial_path_with_controller_namespace=).with(false)

described_class.call do
expect(ActionView::Base).to receive(:prefix_partial_path_with_controller_namespace=).with(original_partial_prefix_value)
end
end
end

context "with custom settings" do
let(:disable_partial_prefixes) { true }
subject { described_class.call(disable_partial_prefixes: disable_partial_prefixes) {} }

it "disables partial prefixes" do
expect(ActionView::Base).to receive(:prefix_partial_path_with_controller_namespace=).with(false)

described_class.call(disable_partial_prefixes: disable_partial_prefixes) do
expect(ActionView::Base).to receive(:prefix_partial_path_with_controller_namespace=).with(original_partial_prefix_value)
end
end

describe "when disable_partial_prefixes is false" do
let(:disable_partial_prefixes) { false }

it "doesn't touch corresponding ActionView::Base constant" do
expect(ActionView::Base).to_not receive(:prefix_partial_path_with_controller_namespace=)

subject
end
end
end
end

context "without ActionView partial prefix supported", if: !PARTIAL_PREFIX_SUPPORTED do
it "doesn't touch prefix_partial_path_with_controller_namespace ActionView::Base constant" do
expect(ActionView::Base).to_not receive(:prefix_partial_path_with_controller_namespace=)

described_class.call(disable_partial_prefixes: true) {}
end
end
end
12 changes: 12 additions & 0 deletions spec/lib/stores/config_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@
end
end

context "preview_disable_action_view_partial_prefixes" do
it "is set" do
expect(config.preview_disable_action_view_partial_prefixes).to be true
end

it "can be changed" do
config.preview_disable_action_view_partial_prefixes = false

expect(config.preview_disable_action_view_partial_prefixes).to be false
end
end

context "preview_embeds.enabled" do
it "defaults to true" do
expect(config.preview_embeds.enabled).to be true
Expand Down

0 comments on commit 776fc81

Please sign in to comment.