Skip to content

Commit

Permalink
Merge branch 'master' into 0.4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jul 10, 2015
2 parents c119619 + 6e56475 commit a5485a5
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,10 @@
# Lotus::View
View layer for Lotus

## v0.4.3 - 2015-07-10
### Fixed
- [Farrel Lifson] Force partial finder to be explicit when to templates have the same name.

## v0.4.2 - 2015-06-23
### Fixed
- [Tom Kadwill] Ensure views to use methods defined by the associated layout.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -460,7 +460,7 @@ module Articles

class RssIndex < Index
format :rss
layout nil
layout false
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/view/rendering/null_layout.rb
Expand Up @@ -13,7 +13,7 @@ module Rendering
# module Articles
# class Show
# include Lotus::View
# layout nil
# layout false
# end
# end
#
Expand Down
32 changes: 32 additions & 0 deletions lib/lotus/view/rendering/partial_finder.rb
Expand Up @@ -22,7 +22,39 @@ class PartialFinder < TemplateFinder
# "_sidebar.html.erb"
PREFIX = '_'.freeze

# Find a template for a partial. Initially it will look for the
# partial template under the directory of the parent directory
# view template, if not found it will search recursivly from
# the view root.
#
# @return [Lotus::View::Template] the requested template
#
# @see Lotus::View::Rendering::TemplateFinder#find
#
# @since 0.4.3
# @api private
def find
if path = partial_template_under_view_path
View::Template.new path
else
super
end
end

protected
# @since 0.4.3
# @api private
def partial_template_under_view_path
_find(view_template_dir).first
end

# @since 0.4.3
# @api private
def view_template_dir
*all, _ = @view.template.split(separator)
all.join(separator)
end

def template_name
*all, last = partial_name.split(separator)
all.push( last.prepend(prefix) ).join(separator)
Expand Down
17 changes: 15 additions & 2 deletions lib/lotus/view/rendering/templates_finder.rb
Expand Up @@ -69,12 +69,19 @@ def initialize(view)
# Lotus::View::Rendering::TemplatesFinder.new(Articles::Show).find
# # => [#<Lotus::View::Template:0x007f8a0a86a970 ... @file="/path/to/templates/articles/show.html.erb">]
def find
Dir.glob( "#{ [root, recursive, template_name].join(separator) }.#{ format }.#{ engines }" ).map do |template|
View::Template.new template
_find.map do |template|
View::Template.new(template)
end
end

protected

# @api private
# @since 0.4.3
def _find(lookup = search_path)
Dir.glob( "#{ [root, lookup, template_name].join(separator) }.#{ format }.#{ engines }" )
end

# @api private
# @since 0.1.0
def template_name
Expand All @@ -87,6 +94,12 @@ def root
@view.root
end

# @api private
# @since 0.4.3
def search_path
recursive
end

# @api private
# @since 0.2.0
def recursive
Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/view/version.rb
Expand Up @@ -3,6 +3,6 @@ module View
# Defines the version
#
# @since 0.1.0
VERSION = '0.4.2'.freeze
VERSION = '0.4.3'.freeze
end
end
4 changes: 2 additions & 2 deletions lotus-view.gemspec
Expand Up @@ -6,8 +6,8 @@ require 'lotus/view/version'
Gem::Specification.new do |spec|
spec.name = 'lotus-view'
spec.version = Lotus::View::VERSION
spec.authors = ['Luca Guidi']
spec.email = ['me@lucaguidi.com']
spec.authors = ['Luca Guidi', 'Trung Lê', 'Alfonso Uceda Pompa']
spec.email = ['me@lucaguidi.com', 'trung.le@ruby-journal.com', 'uceda73@gmail.com']
spec.description = %q{View layer for Lotus}
spec.summary = %q{View layer for Lotus, with a separation between views and templates}
spec.homepage = 'http://lotusrb.org'
Expand Down
23 changes: 19 additions & 4 deletions test/fixtures.rb
Expand Up @@ -51,7 +51,7 @@ class AppView
end

class AppViewLayout < AppView
layout nil
layout false
end

class AppViewRoot < AppView
Expand All @@ -63,6 +63,21 @@ class NestedView
root __dir__ + '/fixtures/templates'
end


module Organisations
class Action
include Lotus::View
root __dir__ + '/fixtures/templates'
end

module OrderTemplates
class Action
include Lotus::View
root __dir__ + '/fixtures/templates'
end
end
end

class MissingTemplateView
include Lotus::View
end
Expand Down Expand Up @@ -96,12 +111,12 @@ def title

class RssIndex < Index
format :rss
layout nil
layout false
end

class AtomIndex < RssIndex
format :atom
layout nil
layout false
end

class New
Expand Down Expand Up @@ -279,7 +294,7 @@ class Index

class JsonIndex < Index
format :json
layout nil
layout false
end

class RssIndex < Index
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/templates/organisations/_partial.html.erb
@@ -0,0 +1 @@
Organisation Partial
2 changes: 2 additions & 0 deletions test/fixtures/templates/organisations/action.html.erb
@@ -0,0 +1,2 @@
<%= render(partial: 'partial') %>
<%= render(partial: 'shared/sidebar') %>
@@ -0,0 +1 @@
Order Template Partial
@@ -0,0 +1,2 @@
<%= render(partial: 'partial') %>
<%= render(partial: 'shared/sidebar') %>
11 changes: 11 additions & 0 deletions test/partial_finder_test.rb
@@ -0,0 +1,11 @@
require 'test_helper'

describe Lotus::View::Rendering::PartialFinder do
it 'finds the correct partial' do
partial_finder = Lotus::View::Rendering::PartialFinder.new(Organisations::OrderTemplates::Action, partial: 'partial', format: 'html')
partial_finder.find.render(format: 'html').must_match 'Order Template Partial'

partial_finder = Lotus::View::Rendering::PartialFinder.new(Organisations::Action, partial: 'partial', format: 'html')
partial_finder.find.render(format: 'html').must_match 'Organisation Partial'
end
end
10 changes: 10 additions & 0 deletions test/rendering_test.rb
Expand Up @@ -80,6 +80,16 @@
rendered.must_match %(<h1>Nested</h1>)
end

it 'finds and renders partials in the directory of the view template parent directory' do
rendered = Organisations::OrderTemplates::Action.render(format: :html)
rendered.must_match %(Order Template Partial)
rendered.must_match %(<div id="sidebar"></div>)

rendered = Organisations::Action.render(format: :html)
rendered.must_match %(Organisation Partial)
rendered.must_match %(<div id="sidebar"></div>)
end

it 'decorates locals' do
map = Map.new(['Rome', 'Cambridge'])

Expand Down
2 changes: 1 addition & 1 deletion test/version_test.rb
Expand Up @@ -2,6 +2,6 @@

describe Lotus::View::VERSION do
it 'returns current version' do
Lotus::View::VERSION.must_equal '0.4.2'
Lotus::View::VERSION.must_equal '0.4.3'
end
end

0 comments on commit a5485a5

Please sign in to comment.