Skip to content

Commit

Permalink
Sanitize PatHSet#inherit_view_paths to guarantee an array, thus enabl…
Browse files Browse the repository at this point in the history
…ing easier to read code
  • Loading branch information
ianwhite committed Feb 7, 2009
1 parent a72d408 commit 17993be
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/inherit_views.rb
Expand Up @@ -54,7 +54,7 @@ def inherit_views(*paths)
delegate :inherit_views?, :inherit_view_paths, :to => 'self.class'
end
self.inherit_views = true
self.inherit_view_paths = paths if paths.size > 0
self.inherit_view_paths = paths if paths.any?
end
end

Expand All @@ -66,7 +66,7 @@ def inherit_views?

# Instruct the controller that it is, or is not, inheriting views
def inherit_views=(bool)
write_inheritable_attribute('inherit_views', bool ? true : false)
write_inheritable_attribute('inherit_views', bool)
end

# Return the inherit view paths, in order of self to ancestor
Expand Down Expand Up @@ -105,30 +105,35 @@ def view_paths=(value)
# which will be used to look for a matching template if the original template is missing
class PathSet < ::ActionView::PathSet
extend ActiveSupport::Memoizable
attr_accessor :inherit_view_paths

attr_reader :inherit_view_paths

def inherit_view_paths=(paths)
@inherit_view_paths = Array(paths)
end

alias_method :orig_find_template, :find_template

def find_template(original_template_path, format = nil)
super
rescue ::ActionView::MissingTemplate => e
(inherit_view_paths && find_template_from_inherit_view_paths(original_template_path, format)) || raise(e)
find_template_from_inherit_view_paths(original_template_path, format) || raise(e)
end

protected
def find_template_from_inherit_view_paths(template_path, format)
# first, we grab the inherit view paths that are 'above' the given template_path
if 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)
# then, search through each path, substituting the inherit view path, returning the first found
paths_above_template_path.detect do |path|
paths_above_template_path.each do |path|
begin
return orig_find_template(template_path.sub(/^#{starting_path}/, path), format)
rescue ::ActionView::MissingTemplate
next
end
end
end
nil
end
memoize :find_template_from_inherit_view_paths
end
Expand Down

0 comments on commit 17993be

Please sign in to comment.