Skip to content

Commit

Permalink
Ensure lazy loading and correct namespace of the layout class when us…
Browse files Browse the repository at this point in the history
…ing configuration's "layout" DSL
  • Loading branch information
jodosha committed Jun 11, 2014
1 parent 7339358 commit 1781e70
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/lotus/view/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def duplicate
Configuration.new.tap do |c|
c.namespace = namespace
c.root = root
c.layout = layout
c.layout = @layout # lazy loading of the class
c.load_paths = load_paths.dup
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/lotus/view/rendering/layout_finder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'lotus/utils/string'
require 'lotus/utils/class'
require 'lotus/view/rendering/null_layout'

module Lotus
Expand Down Expand Up @@ -63,7 +64,9 @@ class LayoutFinder
def self.find(layout, namespace = Object)
case layout
when Symbol, String
# TODO Move this low level logic into a Lotus::Utils solution
class_name = "#{ Utils::String.new(layout).classify }#{ SUFFIX }"
namespace = Utils::Class.load!(namespace)
namespace.const_get(class_name)
when Class
layout
Expand Down
38 changes: 38 additions & 0 deletions test/view/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,44 @@ class LazyLayout

conf.namespace.must_equal(CardDeck)
end

describe 'layout lazy loading' do
before do
Lotus::View.configure do
layout :application
end

module LazyApp
View = Lotus::View.duplicate
View.configure do
namespace 'LazyApp::Views'
end

module Views
module Dashboard
class Index
include LazyApp::View
end
end

class ApplicationLayout
end
end
end
end

after do
Lotus::View.configuration.reset!
Object.send(:remove_const, :LazyApp)
end

it 'lazily loads the layout' do
expected = LazyApp::Views::ApplicationLayout

LazyApp::Views::Dashboard::Index.layout.must_equal expected
LazyApp::Views::Dashboard::Index.configuration.layout.must_equal expected
end
end
end

describe '#load!' do
Expand Down

0 comments on commit 1781e70

Please sign in to comment.