Skip to content

Commit

Permalink
Fix view rendering so layout methods can be called
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkadwill committed Jun 4, 2015
1 parent d2cd761 commit 99ac02b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/lotus/view/rendering/layout_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ def respond_to_missing?(m, include_all)
def method_missing(m, *args, &blk)
if @scope.respond_to?(m)
@scope.__send__(m, *args, &blk)
elsif @layout.respond_to?(m)
@layout.__send__(m, *args, &blk)
elsif layout.respond_to?(m)
layout.__send__(m, *args, &blk)
else
super
end
Expand All @@ -228,6 +228,10 @@ def _options(options)
opts[:locals].merge!(options.fetch(:locals){ ::Hash.new })
end
end

def layout
@layout || @layout.class.layout.new(@scope, "")
end
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion lib/lotus/view/rendering/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Scope < LayoutScope
# @api private
# @since 0.1.0
def initialize(view, locals = {})
@view, @locals = view, locals
@view, @locals, @layout = view, locals, layout
end

# Returns an inspect String
Expand Down Expand Up @@ -70,6 +70,15 @@ def method_missing(m, *args, &block)
end
)
end

private
def layout
if @view.class.respond_to?(:layout)
@view.class.layout.new(self, "")
else
nil
end
end
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ class StoreLayout
def head
%(<meta name="lotusrb-version" content="0.3.1">)
end

def user_name
"Joe Blogs"
end
end

module Home
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<%= user_name %>
yeah
5 changes: 5 additions & 0 deletions test/layout_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class MissingLayout
rendered.must_match %(yeah)
end

it 'methods defined in layout are available from the view' do
rendered = Store::Views::Home::Index.render(format: :html)
rendered.must_match %(Joe Blogs)
end

it 'renders content to return value from view' do
rendered = Store::Views::Products::Show.render(format: :html)
rendered.must_match %(Product)
Expand Down

0 comments on commit 99ac02b

Please sign in to comment.