diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-02-19 20:55:56 -0600 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-02-19 20:55:56 -0600 |
commit | f8ea9f85d4f1e3e6f3b5d895bef6b013aa4b0690 (patch) | |
tree | 3dcac4ab4c1d170df4ecaa327513c81c742f1fda /actionpack/lib | |
parent | 7c0e008973e594ebf53607362c1dfbe34b693600 (diff) | |
download | rails-f8ea9f85d4f1e3e6f3b5d895bef6b013aa4b0690.tar.gz rails-f8ea9f85d4f1e3e6f3b5d895bef6b013aa4b0690.tar.bz2 rails-f8ea9f85d4f1e3e6f3b5d895bef6b013aa4b0690.zip |
Fix templates reloading in development when using custom view path [#2012 state:resolved]
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_view/base.rb | 4 | ||||
-rw-r--r-- | actionpack/lib/action_view/reloadable_template.rb | 77 | ||||
-rw-r--r-- | actionpack/lib/action_view/template.rb | 24 |
3 files changed, 49 insertions, 56 deletions
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 4198725e0d..65b2062337 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -254,8 +254,8 @@ module ActionView #: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] diff --git a/actionpack/lib/action_view/reloadable_template.rb b/actionpack/lib/action_view/reloadable_template.rb index 3081be60fd..5ef833d75c 100644 --- a/actionpack/lib/action_view/reloadable_template.rb +++ b/actionpack/lib/action_view/reloadable_template.rb @@ -27,49 +27,50 @@ module ActionView #:nodoc: 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 @@ -78,7 +79,6 @@ module ActionView #:nodoc: def initialize(*args) super - @compiled_methods = [] # we don't ever want to get frozen extend Unfreezable @@ -106,14 +106,11 @@ module ActionView #:nodoc: 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 diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index b8e2165ddf..ea838b9b02 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -6,12 +6,7 @@ module ActionView #:nodoc: 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 @@ -45,22 +40,23 @@ module ActionView #:nodoc: # 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 @@ -79,7 +75,7 @@ module ActionView #:nodoc: load! unless @loaded @paths[path] end - + private def templates_in_path (Dir.glob("#{@path}/**/*/**") | Dir.glob("#{@path}/**")).each do |file| @@ -88,7 +84,7 @@ module ActionView #:nodoc: end def create_template(file) - Template.new(file.split("#{self}/").last, self) + Template.new(relative_path_for_template_file(file), self) end end |