Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout methods fix/235 #71

Merged
merged 1 commit into from
Jun 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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