Skip to content

Commit

Permalink
Fix templates reloading in development when using custom view path [#…
Browse files Browse the repository at this point in the history
…2012 state:resolved]
  • Loading branch information
josh committed Feb 20, 2009
1 parent 7c0e008 commit f8ea9f8
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 97 deletions.
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -254,8 +254,8 @@ def render(options = {}, local_assigns = {}, &block) #:nodoc:
if options[:layout]
_render_with_layout(options, local_assigns, &block)
elsif options[:file]
tempalte = self.view_paths.find_template(options[:file], template_format)
tempalte.render_template(self, options[:locals])
template = self.view_paths.find_template(options[:file], template_format)
template.render_template(self, options[:locals])
elsif options[:partial]
render_partial(options)
elsif options[:inline]
Expand Down
77 changes: 37 additions & 40 deletions actionpack/lib/action_view/reloadable_template.rb
Expand Up @@ -27,49 +27,50 @@ def [](path)
end
else
load_all_templates_from_dir(templates_dir_from_path(path))
@paths[path]
# don't ever hand out a template without running a stale check
(new_template = @paths[path]) && new_template.reset_cache_if_stale!
end
end

def register_template_from_file(template_file_path)
if !@paths[template_relative_path = template_file_path.split("#{@path}/").last] && File.file?(template_file_path)
register_template(ReloadableTemplate.new(template_relative_path, self))
private
def register_template_from_file(template_full_file_path)
if !@paths[relative_path = relative_path_for_template_file(template_full_file_path)] && File.file?(template_full_file_path)
register_template(ReloadableTemplate.new(relative_path, self))
end
end
end

def register_template(template)
template.accessible_paths.each do |path|
@paths[path] = template
def register_template(template)
template.accessible_paths.each do |path|
@paths[path] = template
end
end
end

# remove (probably deleted) template from cache
def unregister_template(template)
template.accessible_paths.each do |template_path|
@paths.delete(template_path) if @paths[template_path] == template
end
# fill in any newly created gaps
@paths.values.uniq.each do |template|
template.accessible_paths.each {|path| @paths[path] ||= template}
# remove (probably deleted) template from cache
def unregister_template(template)
template.accessible_paths.each do |template_path|
@paths.delete(template_path) if @paths[template_path] == template
end
# fill in any newly created gaps
@paths.values.uniq.each do |template|
template.accessible_paths.each {|path| @paths[path] ||= template}
end
end
end

# load all templates from the directory of the requested template
def load_all_templates_from_dir(dir)
# hit disk only once per template-dir/request
@disk_cache[dir] ||= template_files_from_dir(dir).each {|template_file| register_template_from_file(template_file)}
end

def templates_dir_from_path(path)
dirname = File.dirname(path)
File.join(@path, dirname == '.' ? '' : dirname)
end
# load all templates from the directory of the requested template
def load_all_templates_from_dir(dir)
# hit disk only once per template-dir/request
@disk_cache[dir] ||= template_files_from_dir(dir).each {|template_file| register_template_from_file(template_file)}
end

# get all the template filenames from the dir
def template_files_from_dir(dir)
Dir.glob(File.join(dir, '*'))
end
def templates_dir_from_path(path)
dirname = File.dirname(path)
File.join(@path, dirname == '.' ? '' : dirname)
end

# get all the template filenames from the dir
def template_files_from_dir(dir)
Dir.glob(File.join(dir, '*'))
end
end

module Unfreezable
Expand All @@ -78,7 +79,6 @@ def freeze; self; end

def initialize(*args)
super
@compiled_methods = []

# we don't ever want to get frozen
extend Unfreezable
Expand Down Expand Up @@ -106,14 +106,11 @@ def reset_cache_if_stale!
self
end

# remove any compiled methods that look like they might belong to me
def undef_my_compiled_methods!
@compiled_methods.each {|comp_method| ActionView::Base::CompiledTemplates.send(:remove_method, comp_method)}
@compiled_methods.clear
end

def compile!(render_symbol, local_assigns)
super
@compiled_methods << render_symbol
ActionView::Base::CompiledTemplates.public_instance_methods.grep(/#{Regexp.escape(method_name_without_locals)}(?:_locals_)?/).each do |m|
ActionView::Base::CompiledTemplates.send(:remove_method, m)
end
end

end
Expand Down
24 changes: 10 additions & 14 deletions actionpack/lib/action_view/template.rb
Expand Up @@ -6,12 +6,7 @@ class Path

def initialize(path)
raise ArgumentError, "path already is a Path class" if path.is_a?(Path)
@path = expand_path(path).freeze
end

def expand_path(path)
# collapse any directory dots in path ('.' or '..')
path.starts_with?('/') ? File.expand_path(path) : File.expand_path(path, '/').from(1)
@path = (path.ends_with?(File::SEPARATOR) ? path.to(-2) : path).freeze
end

def to_s
Expand Down Expand Up @@ -45,22 +40,23 @@ def eql?(path)
# will never match +hello/index.html.erb+.
def [](path)
end

def load!
end

def self.new_and_loaded(path)
returning new(path) do |path|
path.load!
end
end

private
def relative_path_for_template_file(full_file_path)
full_file_path.split("#{@path}/").last
end
end

class EagerPath < Path
def initialize(path)
super
end

def load!
return if @loaded

Expand All @@ -79,7 +75,7 @@ def [](path)
load! unless @loaded
@paths[path]
end

private
def templates_in_path
(Dir.glob("#{@path}/**/*/**") | Dir.glob("#{@path}/**")).each do |file|
Expand All @@ -88,7 +84,7 @@ def templates_in_path
end

def create_template(file)
Template.new(file.split("#{self}/").last, self)
Template.new(relative_path_for_template_file(file), self)
end
end

Expand Down
26 changes: 11 additions & 15 deletions actionpack/test/controller/view_paths_test.rb
Expand Up @@ -42,45 +42,41 @@ def teardown
ActiveSupport::Deprecation.behavior = @old_behavior
end

def assert_view_path_strings_are_equal(expected, actual)
assert_equal(expected.map {|path| path.sub(/\.\//, '')}, actual)
end

def test_template_load_path_was_set_correctly
assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)
assert_equal [FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)
end

def test_controller_appends_view_path_correctly
@controller.append_view_path 'foo'
assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo'], @controller.view_paths.map(&:to_s)
assert_equal [FIXTURE_LOAD_PATH, 'foo'], @controller.view_paths.map(&:to_s)

@controller.append_view_path(%w(bar baz))
assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)
assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)

@controller.append_view_path(FIXTURE_LOAD_PATH)
assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)
assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)
end

def test_controller_prepends_view_path_correctly
@controller.prepend_view_path 'baz'
assert_view_path_strings_are_equal ['baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)
assert_equal ['baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)

@controller.prepend_view_path(%w(foo bar))
assert_view_path_strings_are_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)
assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)

@controller.prepend_view_path(FIXTURE_LOAD_PATH)
assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)
assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)
end

def test_template_appends_view_path_correctly
@controller.instance_variable_set :@template, ActionView::Base.new(TestController.view_paths, {}, @controller)
class_view_paths = TestController.view_paths

@controller.append_view_path 'foo'
assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo'], @controller.view_paths.map(&:to_s)
assert_equal [FIXTURE_LOAD_PATH, 'foo'], @controller.view_paths.map(&:to_s)

@controller.append_view_path(%w(bar baz))
assert_view_path_strings_are_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)
assert_equal [FIXTURE_LOAD_PATH, 'foo', 'bar', 'baz'], @controller.view_paths.map(&:to_s)
assert_equal class_view_paths, TestController.view_paths
end

Expand All @@ -89,10 +85,10 @@ def test_template_prepends_view_path_correctly
class_view_paths = TestController.view_paths

@controller.prepend_view_path 'baz'
assert_view_path_strings_are_equal ['baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)
assert_equal ['baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)

@controller.prepend_view_path(%w(foo bar))
assert_view_path_strings_are_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)
assert_equal ['foo', 'bar', 'baz', FIXTURE_LOAD_PATH], @controller.view_paths.map(&:to_s)
assert_equal class_view_paths, TestController.view_paths
end

Expand Down

0 comments on commit f8ea9f8

Please sign in to comment.