Skip to content

Commit

Permalink
Merge pull request #7 from thoughtbot/fix-layout-iessues
Browse files Browse the repository at this point in the history
Fix nil templates
  • Loading branch information
jho406 committed Jun 28, 2023
2 parents e6e96ad + 0610ed3 commit 5309fca
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
1 change: 0 additions & 1 deletion lib/props_template/extensions/partial_renderer.rb
Expand Up @@ -81,7 +81,6 @@ def block_opts_to_render_opts(builder, options)
partial, pass_opts = [*options[:partial]]
pass_opts ||= {}
pass_opts[:locals] ||= {}
pass_opts[:locals][:json] = @builder
pass_opts[:partial] = partial
pass_opts[:formats] = [:json]
pass_opts.delete(:handlers)
Expand Down
4 changes: 2 additions & 2 deletions lib/props_template/handler.rb
Expand Up @@ -8,8 +8,8 @@ class Handler
def self.call(template, source = nil)
source ||= template.source
# this juggling is required to keep line numbers right in the error
%{__already_defined = defined?(json); json||=Props::Template.new(self); #{source};
json.result! unless (__already_defined && __already_defined != "method")
%{ __finalize = !defined?(@__json); @__json ||= Props::Template.new(self); json = @__json; #{source};
json.result! if __finalize
}
end
end
Expand Down
24 changes: 10 additions & 14 deletions lib/props_template/layout_patch.rb
@@ -1,28 +1,24 @@
module Props
module LayoutPatch
def render(context, options)
options[:locals] ||= {}
options[:locals][:json] = nil

@details = extract_details(options)
template = determine_template(options)

if template.respond_to?(:handler) && template.handler == Props::Handler && options[:layout]
def render_template(view, template, layout_name, locals)
if template.respond_to?(:handler) && template.handler == Props::Handler && layout_name
prepend_formats(template.format)
render_props_template(context, template, options[:layout], options[:locals])
render_props_template(view, template, layout_name, locals)
else
super(context, options)
super
end
end

def render_props_template(view, template, path, locals)
layout_locals = locals.dup
layout_locals.delete(:json)
layout_locals[:virtual_path_of_template] = template.virtual_path

layout = resolve_props_layout(path, layout_locals.keys, [formats.first])
body = layout.render(view, layout_locals) do |json|
locals[:json] = json
body = if layout
layout.render(view, layout_locals) do |json|
template.render(view, locals)
end
else
template.render(view, locals)
end

Expand All @@ -44,7 +40,7 @@ def resolve_props_layout(layout, keys, formats)
end
end
when Proc
resolve_layout(layout.call(@lookup_context, formats), keys, formats)
resolve_props_layout(layout.call(@lookup_context, formats), keys, formats)
else
layout
end
Expand Down
12 changes: 12 additions & 0 deletions spec/layout_spec.rb
Expand Up @@ -21,4 +21,16 @@ def self.controller_path

expect(json.strip).to eql('{"data":{"success":"ok"}}')
end

context "when no layout exists" do
it "skips the layout" do
view_path = File.join(File.dirname(__FILE__), "./fixtures")
controller = TestController.new
controller.prepend_view_path(view_path)

json = controller.render_to_string("200")

expect(json.strip).to eql('{"success":"ok"}')
end
end
end

0 comments on commit 5309fca

Please sign in to comment.