Skip to content

Commit

Permalink
Modify PartialFinder to search for partial template under directory o…
Browse files Browse the repository at this point in the history
…f view template, if not found search recursively from root.
  • Loading branch information
farrel committed Jun 28, 2015
1 parent b19c3ae commit 086a4d3
Showing 1 changed file with 29 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

0 comments on commit 086a4d3

Please sign in to comment.