Skip to content

Commit

Permalink
Auto-load template handlers based on unmatched extensions [#1540 stat…
Browse files Browse the repository at this point in the history
…e:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
nex3 authored and josh committed Dec 15, 2008
1 parent a392f34 commit e8c1915
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
10 changes: 3 additions & 7 deletions actionpack/lib/action_view/template.rb
Expand Up @@ -98,10 +98,6 @@ def load!
end

private
def valid_extension?(extension)
Template.template_handler_extensions.include?(extension)
end

def find_full_path(path, load_paths)
load_paths = Array(load_paths) + [nil]
load_paths.each do |load_path|
Expand All @@ -115,11 +111,11 @@ def find_full_path(path, load_paths)
# [base_path, name, format, extension]
def split(file)
if m = file.match(/^(.*\/)?([^\.]+)\.?(\w+)?\.?(\w+)?\.?(\w+)?$/)
if valid_extension?(m[5]) # Multipart formats
if Template.valid_extension?(m[5]) # Multipart formats
[m[1], m[2], "#{m[3]}.#{m[4]}", m[5]]
elsif valid_extension?(m[4]) # Single format
elsif Template.valid_extension?(m[4]) # Single format
[m[1], m[2], m[3], m[4]]
elsif valid_extension?(m[3]) # No format
elsif Template.valid_extension?(m[3]) # No format
[m[1], m[2], nil, m[3]]
else # No extension
[m[1], m[2], m[3], nil]
Expand Down
25 changes: 24 additions & 1 deletion actionpack/lib/action_view/template_handlers.rb
Expand Up @@ -28,6 +28,10 @@ def register_template_handler(extension, klass)
@@template_handlers[extension.to_sym] = klass
end

def valid_extension?(extension)
template_handler_extensions.include?(extension) || init_path_for_extension(extension)
end

def template_handler_extensions
@@template_handlers.keys.map(&:to_s).sort
end
Expand All @@ -38,7 +42,26 @@ def register_default_template_handler(extension, klass)
end

def handler_class_for_extension(extension)
(extension && @@template_handlers[extension.to_sym]) || @@default_template_handlers
(extension && @@template_handlers[extension.to_sym] || autoload_handler_class(extension)) ||
@@default_template_handlers
end

private
def autoload_handler_class(extension)
return if Gem.loaded_specs[extension]
return unless init_path = init_path_for_extension(extension)
Gem.activate(extension)
load(init_path)
handler_class_for_extension(extension)
end

# Returns the path to the rails/init.rb file for the given extension,
# or nil if no gem provides it.
def init_path_for_extension(extension)
return unless spec = Gem.searcher.find(extension.to_s)
returning File.join(spec.full_gem_path, 'rails', 'init.rb') do |path|
return unless File.file?(path)
end
end
end
end

2 comments on commit e8c1915

@methodmissing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is specific to my environment, but noticed Gem.searcher hanging for ages when extension is nil …. which it probably should never be, either …

The following yields respectable boot times again :

http://gist.github.com/37681

@josh
Copy link
Contributor

@josh josh commented on e8c1915 Dec 19, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I heard some other reports. Let reverts this in the mean time.

Please sign in to comment.