Skip to content

Commit

Permalink
Lotus::View::Layout => Lotus::Layout
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Mar 18, 2014
1 parent 7ea5837 commit b2759f8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 117 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -115,7 +115,7 @@ Layouts are wrappers for views, they can be utilized by applications to reuse ma

```ruby
class ApplicationLayout
include Lotus::View::Layout
include Lotus::Layout

def page_title
'Title:'
Expand Down
50 changes: 50 additions & 0 deletions lib/lotus/layout.rb
@@ -0,0 +1,50 @@
require 'lotus/view/rendering/layout_registry'
require 'lotus/view/rendering/layout_scope'
require 'lotus/view/rendering/null_layout'

module Lotus
module Layout
def self.included(base)
base.class_eval do
extend Lotus::View::Dsl.dup
extend ClassMethods
end

Lotus::View.layouts.add(base)
end

module ClassMethods
SUFFIX = '_layout'.freeze

def registry
@@registry ||= View::Rendering::LayoutRegistry.new(self)
end

def template
super.gsub(suffix, '')
end

def suffix
SUFFIX
end

protected
def load!
registry.freeze
end
end

def initialize(scope, rendered)
@scope, @rendered = View::Rendering::LayoutScope.new(self, scope), rendered
end

def render
template.render(@scope, &Proc.new{@rendered})
end

protected
def template
self.class.registry.resolve({format: @scope.format})
end
end
end
2 changes: 1 addition & 1 deletion lib/lotus/view.rb
Expand Up @@ -4,7 +4,7 @@
require 'lotus/view/inheritable'
require 'lotus/view/rendering'
require 'lotus/view/dsl'
require 'lotus/view/layout'
require 'lotus/layout'
require 'lotus/presenter'

module Lotus
Expand Down
52 changes: 0 additions & 52 deletions lib/lotus/view/layout.rb

This file was deleted.

2 changes: 1 addition & 1 deletion test/fixtures.rb
Expand Up @@ -32,7 +32,7 @@ class View
end

class ApplicationLayout
include Lotus::View::Layout
include Lotus::Layout

def title
'Title:'
Expand Down
57 changes: 5 additions & 52 deletions test/layout_test.rb
@@ -1,57 +1,10 @@
require 'test_helper'

describe Lotus::View do
describe 'layout' do
describe 'when Lotus::View.layout is nil' do
describe "and it isn't specified at view level" do
it 'has NullLayout' do
HelloWorldView.layout.must_equal(Lotus::View::Rendering::NullLayout)
end
end

describe "and it is specified at view level" do
it 'has the specified value' do
AppView.layout.must_equal(ApplicationLayout)
end

describe "and a subclass has a different value" do
it 'has the specified value' do
AppViewLayout.layout.must_equal(Lotus::View::Rendering::NullLayout)
end
end
end
end

describe 'when Lotus::View.layout has a value' do
before do
Lotus::View.layout = :global

App::View.class_eval { @layout = nil }
App::View.layout(Lotus::View::Rendering::LayoutFinder.new(App::View).find)
end

after do
Lotus::View.layout = nil
Lotus::View.layout.freeze
end

describe "and it isn't specified at view level" do
it 'returns the global value' do
App::View.layout.must_equal(Lotus::View.layout)
end
end

describe "and it is specified at view level" do
it 'has the specified value' do
AppView.layout.must_equal(ApplicationLayout)
end

describe "and a subclass has a different value" do
it 'has the specified value' do
AppViewLayout.layout.must_equal(Lotus::View::Rendering::NullLayout)
end
end
end
describe Lotus::Layout do
describe 'rendering from layout' do
it 'renders partial' do
rendered = IndexView.render({format: :html}, {})
rendered.must_match %(<div id="sidebar"></div>)
end
end
end
10 changes: 0 additions & 10 deletions test/view/layout_test.rb

This file was deleted.

0 comments on commit b2759f8

Please sign in to comment.