Skip to content

Commit

Permalink
Ensure to return the correct template name for namespaced views
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jun 6, 2014
1 parent 3a6da6c commit b2dfcdd
Show file tree
Hide file tree
Showing 10 changed files with 471 additions and 51 deletions.
99 changes: 97 additions & 2 deletions lib/lotus/view/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'lotus/utils/string'
require 'lotus/view/rendering/template_name'
require 'lotus/view/rendering/layout_finder'

module Lotus
Expand Down Expand Up @@ -124,11 +124,106 @@ def format(value = nil)
#
# Articles::Show.template # => 'articles/single_article'
# Articles::JsonShow.template # => 'articles/single_article'
#
# @example With namespace
# require 'lotus/view'
#
# module Furnitures
# View = Lotus::View.duplicate
#
# View.configure do
# namespace 'Furnitures'
# end
#
# class Standalone
# include Furnitures::View
# end
#
# module Catalog
# class Index
# Furnitures::View
# end
# end
# end
#
# Furnitures::Standalone.template # => 'standalone'
# Furnitures::Catalog::Index.template # => 'catalog/index'
#
# @example With nested namespace
# require 'lotus/view'
#
# module Frontend
# View = Lotus::View.duplicate
#
# View.configure do
# namespace 'Frontend::Views'
# end
#
# class StandaloneView
# include Frontend::View
# end
#
# module Views
# class Standalone
# include Frontend::View
# end
#
# module Sessions
# class New
# include Frontend::View
# end
# end
# end
# end
#
# Frontend::StandaloneView.template # => 'standalone_view'
# Frontend::Views::Standalone.template # => 'standalone'
# Frontend::Views::Sessions::New.template # => 'sessions/new'
#
# @example With deeply nested namespace
# require 'lotus/view'
#
# module Bookshelf
# module Web
# View = Lotus::View.duplicate
#
# View.configure do
# namespace 'Bookshelf::Web::Views'
# end
#
# module Views
# module Books
# class Show
# include Bookshelf::Web::View
# end
# end
# end
# end
#
# module Api
# View = Lotus::View.duplicate
#
# View.configure do
# namespace 'Bookshelf::Api::Views'
# end
#
# module Views
# module Books
# class Show
# include Bookshelf::Api::View
# end
# end
# end
# end
# end
#
# Bookshelf::Web::Views::Books::Index.template # => 'books/index'
# Bookshelf::Api::Views::Books::Index.template # => 'books/index'
def template(value = nil)
if value
@@template = value
else
@@template ||= Utils::String.new(name).underscore
@@template ||= Rendering::TemplateName.new(name, configuration.namespace).to_s
end
end

Expand Down
37 changes: 37 additions & 0 deletions lib/lotus/view/rendering/template_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'lotus/utils/string'

module Lotus
module View
module Rendering
# @since 0.2.0
class TemplateName
NAMESPACE_SEPARATOR = '::'.freeze

def initialize(name, namespace)
@name = name
compile!(namespace)
end

def to_s
@name
end

private
def compile!(namespace)
tokens(namespace) {|token| replace!(token) }
@name = Utils::String.new(@name).underscore
end

def tokens(namespace)
namespace.to_s.split(NAMESPACE_SEPARATOR).each do |token|
yield token
end
end

def replace!(token)
@name.gsub!(%r{\A#{ token }#{ NAMESPACE_SEPARATOR }}, '')
end
end
end
end
end
10 changes: 9 additions & 1 deletion test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ module CardDeck
configuration.reset!
configure do
namespace CardDeck
root __dir__ + '/fixtures/templates/card_deck'
root __dir__ + '/fixtures/templates/card_deck/app/templates'
layout :application
end
end
Expand All @@ -216,7 +216,15 @@ class ApplicationLayout
include Lotus::Layout
end

class StandaloneView
include CardDeck::View
end

module Views
class Standalone
include CardDeck::View
end

module Dashboard
class Index
include CardDeck::View
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Dashboard</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions test/fixtures/templates/hello_world.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Hello, World!</h1>
2 changes: 1 addition & 1 deletion test/integration/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

it 'sets a root path' do
card_configuration = CardDeck::View.configuration
card_configuration.root.must_equal(Pathname.new('test/fixtures/templates/card_deck').realpath)
card_configuration.root.must_equal(Pathname.new('test/fixtures/templates/card_deck/app/templates').realpath)
end

it 'sets a layout' do
Expand Down
Loading

0 comments on commit b2dfcdd

Please sign in to comment.