From f391d7aa92f6625649a33c40e5605732b38ddbad Mon Sep 17 00:00:00 2001 From: Johny Ho Date: Fri, 31 May 2024 17:21:21 -0400 Subject: [PATCH] Fix issue with views that don't have a virtual_path The layout patch needed to account for inline renders like `render(plain: "hello")` or `send_data` that don't have a virtual path. Without this fix, applications using props_template were throwing: ``` undefined method 'virtual_path' for an instance of ActionView::Template::Text ``` --- lib/props_template/layout_patch.rb | 2 +- spec/layout_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/props_template/layout_patch.rb b/lib/props_template/layout_patch.rb index 57ad953..8e30ad7 100644 --- a/lib/props_template/layout_patch.rb +++ b/lib/props_template/layout_patch.rb @@ -1,7 +1,7 @@ module Props module LayoutPatch def render_template(view, template, layout_name, locals) - unless view.respond_to?(:active_template_virtual_path) || template.is_a?(ActionView::Template::Text) + if !view.respond_to?(:active_template_virtual_path) && template.respond_to?(:virtual_path) view.instance_eval <<~RUBY, __FILE__, __LINE__ + 1 def active_template_virtual_path; "#{template.virtual_path}";end RUBY diff --git a/spec/layout_spec.rb b/spec/layout_spec.rb index e847b3e..33e1db1 100644 --- a/spec/layout_spec.rb +++ b/spec/layout_spec.rb @@ -33,4 +33,14 @@ def self.controller_path expect(json.strip).to eql('{"success":"ok"}') end end + + it "allows rendering of templates that do not have a virtual_path" do + view_path = File.join(File.dirname(__FILE__), "./fixtures") + controller = TestController.new + controller.prepend_view_path(view_path) + + expect( + controller.render_to_string(plain: "some text") + ).to eql "some text" + end end