-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #606 from liram11/fix-partial-rendering
Fix rendering of partials in previews
- Loading branch information
Showing
8 changed files
with
193 additions
and
37 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
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
21 changes: 0 additions & 21 deletions
21
lib/lookbook/services/templates/action_view_annotations_handler.rb
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
lib/lookbook/services/templates/action_view_config_handler.rb
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,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
104
spec/lib/services/templates/action_view_config_handler_spec.rb
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,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 |
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