Skip to content

Commit

Permalink
Merge 086a4d3 into b170f79
Browse files Browse the repository at this point in the history
  • Loading branch information
farrel committed Jun 28, 2015
2 parents b170f79 + 086a4d3 commit 2d8707a
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/lotus/view/rendering/partial_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,36 @@ 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
def find
if partial_template_exists_under_view?
View::Template.new partial_template_under_view_path
else
super
end
end

protected
def partial_template_exists_under_view?
File.exists?(partial_template_under_view_path)
end

def partial_template_under_view_path
Dir.glob("#{[root,view_template_dir, template_name].join(separator)}.#{format}.#{engines}").first.to_s
end

def view_template_dir
*all, last = @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
10 changes: 10 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ class NestedView
root __dir__ + '/fixtures/templates'
end


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

class MissingTemplateView
include Lotus::View
end
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/templates/organisations/_partial.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Organisation Partial
1 change: 1 addition & 0 deletions test/fixtures/templates/organisations/action.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render(partial: 'partial') %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Order Template Partial
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render(partial: 'partial') %>
13 changes: 13 additions & 0 deletions test/partial_finder_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'test_helper'

describe Lotus::View::Rendering::PartialFinder do
it 'finds the correct partial' do
path = Organisations::OrderTemplates::Action.root.join('organisations/order_templates/action.html.erb')
template = Lotus::View::Template.new(path)

inner_resource_action = Organisations::OrderTemplates::Action.new(template, {})
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'
end
end
5 changes: 5 additions & 0 deletions test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
rendered.must_match %(<h1>Nested</h1>)
end

it 'finds and renders partials in nested directories when the class name of the view contains template' do
rendered = Organisations::OrderTemplates::Action.render(format: :html)
rendered.must_match %(Order Template Partial)
end

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

Expand Down

0 comments on commit 2d8707a

Please sign in to comment.