Skip to content

Commit

Permalink
InheritViews::PathSet now has public find_parent_template, which has …
Browse files Browse the repository at this point in the history
…exact same API as find_template, and also raises MissingTemplate. This fixes the spec in 08db524
  • Loading branch information
ianwhite committed Feb 9, 2009
1 parent 08db524 commit ad0976d
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions lib/inherit_views.rb
Expand Up @@ -98,26 +98,28 @@ class PathSet < ::ActionView::PathSet

alias_method :orig_find_template, :find_template

# look for a parent template if a standard one can't be found
def find_template(original_template_path, format = nil)
super
rescue ::ActionView::MissingTemplate => e
find_parent_template(original_template_path, format) || raise(e)
rescue ::ActionView::MissingTemplate
find_parent_template(original_template_path, format)
end


# given a template_path and format, returns a parent template, or raise ActionView::MissingTemplate
def find_parent_template(template_path, format)
# first, we grab the inherit view paths that are 'above' the given template_path
if inherit_view_paths.present? && (starting_path = inherit_view_paths.detect {|path| template_path.starts_with?("#{path}/")})
paths_above_template_path = inherit_view_paths.slice(inherit_view_paths.index(starting_path)+1..-1)
parent_paths = inherit_view_paths.slice(inherit_view_paths.index(starting_path)+1..-1)
# then, search through each path, substituting the inherit view path, returning the first found
paths_above_template_path.each do |path|
parent_paths.each do |path|
begin
return orig_find_template(template_path.sub(/^#{starting_path}/, path), format)
rescue ::ActionView::MissingTemplate
next
end
end
end
nil
raise ::ActionView::MissingTemplate.new(self, template_path, format)
end
memoize :find_parent_template
end
Expand All @@ -126,6 +128,7 @@ def find_parent_template(template_path, format)
module ActionView
def self.included(base)
base.class_eval do
# use InheritViews::PathSet, and give it my controller's inherit_view_paths
def view_paths=(value)
@view_paths = InheritViews::PathSet.new(value)
ensure
Expand Down

0 comments on commit ad0976d

Please sign in to comment.