Skip to content

Commit

Permalink
Ensure outermost locals to not shadow innermost inside templates/part…
Browse files Browse the repository at this point in the history
…ials. Closes #3
  • Loading branch information
jodosha committed Apr 7, 2014
1 parent 6e425f3 commit 593f1a5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/lotus/view/rendering/layout_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def renderer(options)
def _options(options)
options.dup.tap do |opts|
opts.merge!(format: format)
opts[:locals] ||= {}
opts[:locals].merge!(locals)
opts[:locals] = locals
opts[:locals].merge!(options.fetch(:locals){ Hash[] })
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ def errors
end
end

class AlternativeNew
include Lotus::View
end

class Create
include Lotus::View
template 'articles/new'
Expand Down Expand Up @@ -185,3 +181,9 @@ class Show
include Lotus::View
end
end

module Nodes
class Parent
include Lotus::View
end
end
1 change: 0 additions & 1 deletion test/fixtures/templates/articles/alternative_new.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions test/fixtures/templates/nodes/child.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<li><%= node.name %></li>
6 changes: 6 additions & 0 deletions test/fixtures/templates/nodes/parent.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1><%= node.name %></h1>
<ul>
<% node.children.each do |child| %>
<%= render template: 'nodes/child', locals: { node: child } %>
<% end %>
</ul>
15 changes: 11 additions & 4 deletions test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,20 @@
rendered.must_match %(<input type="hidden" name="secret" value="23" />)
end

# @issue https://github.com/lotus/view/issues/3
it 'renders a template within another template' do
article = OpenStruct.new(title: nil)
parent = OpenStruct.new(children: [], name: 'parent')
child1 = OpenStruct.new(children: [], name: 'child1')
child2 = OpenStruct.new(children: [], name: 'child2')

rendered = Articles::AlternativeNew.render(format: :html, article: article)
parent.children.push(child1)
parent.children.push(child2)

rendered.must_match %(<h1>New Article</h1>)
rendered.must_match %(<input type="hidden" name="secret" value="23" />)
rendered = Nodes::Parent.render(format: :html, node: parent)

rendered.must_match %(<h1>parent</h1>)
rendered.must_match %(<li>child1</li>)
rendered.must_match %(<li>child2</li>)
end

it 'uses HAML engine' do
Expand Down

0 comments on commit 593f1a5

Please sign in to comment.