Skip to content
This repository
Browse code

Reapply the TemplateFinder first applied in [8669] then reverted in […

…8676]. Closes #10800 [lifofifo]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8683 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 61c90a4ad6eab3623002353ed5867e2f05cb6809 1 parent 32b36b8
Michael Koziarski authored January 21, 2008
5  actionmailer/lib/action_mailer/base.rb
@@ -393,6 +393,11 @@ def deliver(mail)
393 393
       def register_template_extension(extension)
394 394
         template_extensions << extension
395 395
       end
  396
+
  397
+      def template_root=(root)
  398
+        write_inheritable_attribute(:template_root, root)
  399
+        ActionView::TemplateFinder.process_view_paths(root)
  400
+      end
396 401
     end
397 402
 
398 403
     # Instantiate a new mailer object. If +method_name+ is not +nil+, the mailer
16  actionpack/lib/action_controller/base.rb
@@ -5,6 +5,7 @@
5 5
 require 'action_controller/resources'
6 6
 require 'action_controller/url_rewriter'
7 7
 require 'action_controller/status_codes'
  8
+require 'action_view/template_finder'
8 9
 require 'drb'
9 10
 require 'set'
10 11
 
@@ -428,6 +429,7 @@ def view_paths
428 429
 
429 430
       def view_paths=(value)
430 431
         @view_paths = value
  432
+        ActionView::TemplateFinder.process_view_paths(value)
431 433
       end
432 434
 
433 435
       # Adds a view_path to the front of the view_paths array.
@@ -440,6 +442,7 @@ def view_paths=(value)
440 442
       def prepend_view_path(path)
441 443
         @view_paths = superclass.view_paths.dup if @view_paths.nil?
442 444
         view_paths.unshift(*path)
  445
+        ActionView::TemplateFinder.process_view_paths(path)
443 446
       end
444 447
       
445 448
       # Adds a view_path to the end of the view_paths array.
@@ -452,6 +455,7 @@ def prepend_view_path(path)
452 455
       def append_view_path(path)
453 456
         @view_paths = superclass.view_paths.dup if @view_paths.nil?
454 457
         view_paths.push(*path)
  458
+        ActionView::TemplateFinder.process_view_paths(path)
455 459
       end
456 460
       
457 461
       # Replace sensitive parameter data from the request log.
@@ -642,11 +646,11 @@ def session_enabled?
642 646
       
643 647
       # View load paths for controller.
644 648
       def view_paths
645  
-        @template.view_paths
  649
+        @template.finder.view_paths
646 650
       end
647 651
     
648 652
       def view_paths=(value)
649  
-        @template.view_paths = value
  653
+        @template.finder.view_paths = value  # Mutex needed
650 654
       end
651 655
 
652 656
       # Adds a view_path to the front of the view_paths array.
@@ -656,7 +660,7 @@ def view_paths=(value)
656 660
       #   self.prepend_view_path(["views/default", "views/custom"])
657 661
       #
658 662
       def prepend_view_path(path)
659  
-        @template.prepend_view_path(path)
  663
+        @template.finder.prepend_view_path(path)  # Mutex needed
660 664
       end
661 665
       
662 666
       # Adds a view_path to the end of the view_paths array.
@@ -666,7 +670,7 @@ def prepend_view_path(path)
666 670
       #   self.append_view_path(["views/default", "views/custom"])
667 671
       #
668 672
       def append_view_path(path)
669  
-      @template.append_view_path(path)
  673
+        @template.finder.append_view_path(path)  # Mutex needed
670 674
       end
671 675
 
672 676
     protected
@@ -1249,7 +1253,7 @@ def close_session
1249 1253
       end
1250 1254
 
1251 1255
       def template_exists?(template_name = default_template_name)
1252  
-        @template.file_exists?(template_name)
  1256
+        @template.finder.file_exists?(template_name)
1253 1257
       end
1254 1258
 
1255 1259
       def template_public?(template_name = default_template_name)
@@ -1257,7 +1261,7 @@ def template_public?(template_name = default_template_name)
1257 1261
       end
1258 1262
 
1259 1263
       def template_exempt_from_layout?(template_name = default_template_name)
1260  
-        extension = @template && @template.pick_template_extension(template_name)
  1264
+        extension = @template && @template.finder.pick_template_extension(template_name)
1261 1265
         name_with_extension = !template_name.include?('.') && extension ? "#{template_name}.#{extension}" : template_name
1262 1266
         @@exempt_from_layout.any? { |ext| name_with_extension =~ ext }
1263 1267
       end
1  actionpack/lib/action_controller/dispatcher.rb
@@ -139,6 +139,7 @@ def prepare_application(force = false)
139 139
 
140 140
       if unprepared || force
141 141
         run_callbacks :prepare_dispatch
  142
+        ActionView::TemplateFinder.reload! unless ActionView::Base.cache_template_loading
142 143
         self.unprepared = false
143 144
       end
144 145
     end
13  actionpack/lib/action_controller/layout.rb
@@ -208,12 +208,6 @@ def normalize_conditions(conditions)
208 208
           conditions.inject({}) {|hash, (key, value)| hash.merge(key => [value].flatten.map {|action| action.to_s})}
209 209
         end
210 210
         
211  
-        def layout_directory_exists_cache
212  
-          @@layout_directory_exists_cache ||= Hash.new do |h, dirname|
213  
-            h[dirname] = File.directory? dirname
214  
-          end
215  
-        end
216  
-        
217 211
         def default_layout_with_format(format, layout)
218 212
           list = layout_list
219 213
           if list.grep(%r{layouts/#{layout}\.#{format}(\.[a-z][0-9a-z]*)+$}).empty?
@@ -313,13 +307,8 @@ def action_has_layout?
313 307
         end
314 308
       end
315 309
       
316  
-      # Does a layout directory for this class exist?
317  
-      # we cache this info in a class level hash
318 310
       def layout_directory?(layout_name)
319  
-        view_paths.find do |path| 
320  
-          next unless template_path = Dir[File.join(path, 'layouts', layout_name) + ".*"].first
321  
-          self.class.send!(:layout_directory_exists_cache)[File.dirname(template_path)]
322  
-        end
  311
+        @template.finder.find_template_extension_from_handler(File.join('layouts', layout_name))
323 312
       end
324 313
   end
325 314
 end
2  actionpack/lib/action_view.rb
@@ -26,6 +26,8 @@
26 26
 require 'action_view/template_handlers/erb'
27 27
 require 'action_view/template_handlers/rjs'
28 28
 
  29
+require 'action_view/template_finder'
  30
+
29 31
 require 'action_view/base'
30 32
 require 'action_view/partials'
31 33
 require 'action_view/template_error'
148  actionpack/lib/action_view/base.rb
@@ -150,9 +150,9 @@ class ActionViewError < StandardError #:nodoc:
150 150
   class Base
151 151
     include ERB::Util
152 152
 
153  
-    attr_reader   :first_render
  153
+    attr_reader   :first_render, :finder
154 154
     attr_accessor :base_path, :assigns, :template_extension
155  
-    attr_accessor :controller, :view_paths
  155
+    attr_accessor :controller
156 156
 
157 157
     attr_reader :logger, :response, :headers
158 158
     attr_internal :cookies, :flash, :headers, :params, :request, :response, :session
@@ -204,12 +204,6 @@ module CompiledTemplates #:nodoc:
204 204
     @@template_args = {}
205 205
     # Count the number of inline templates
206 206
     @@inline_template_count = 0
207  
-    # Maps template paths without extension to their file extension returned by pick_template_extension.
208  
-    # If for a given path, path.ext1 and path.ext2 exist on the file system, the order of extensions
209  
-    # used by pick_template_extension determines whether ext1 or ext2 will be stored.
210  
-    @@cached_template_extension = {}
211  
-    # Maps template paths / extensions to 
212  
-    @@cached_base_paths = {}
213 207
 
214 208
     # Cache public asset paths
215 209
     cattr_reader :computed_public_paths
@@ -241,10 +235,11 @@ def self.load_helpers #:nodoc:
241 235
     # return the rendered template as a string.
242 236
     def self.register_template_handler(extension, klass)
243 237
       @@template_handlers[extension.to_sym] = klass
  238
+      TemplateFinder.update_extension_cache_for(extension.to_s)
244 239
     end
245 240
 
246 241
     def self.template_handler_extensions
247  
-      @@template_handler_extensions ||= @@template_handlers.keys.map(&:to_s).sort
  242
+      @@template_handlers.keys.map(&:to_s).sort
248 243
     end
249 244
 
250 245
     def self.register_default_template_handler(extension, klass)
@@ -265,11 +260,11 @@ def self.handler_class_for_extension(extension)
265 260
     register_template_handler :rxml, TemplateHandlers::Builder
266 261
 
267 262
     def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc:
268  
-      @view_paths = view_paths.respond_to?(:find) ? view_paths.dup : [*view_paths].compact
269 263
       @assigns = assigns_for_first_render
270 264
       @assigns_added = nil
271 265
       @controller = controller
272  
-      @logger = controller && controller.logger 
  266
+      @logger = controller && controller.logger
  267
+      @finder = TemplateFinder.new(self, view_paths)
273 268
     end
274 269
 
275 270
     # Renders the template present at <tt>template_path</tt>. If <tt>use_full_path</tt> is set to true, 
@@ -289,17 +284,20 @@ def render_file(template_path, use_full_path = true, local_assigns = {}) #:nodoc
289 284
         END_ERROR
290 285
       end
291 286
       
  287
+      # Clear the forward slash at the beginning if exists
  288
+      template_path = template_path.sub(/^\//, '') if use_full_path
  289
+
292 290
       @first_render ||= template_path
293  
-      template_path_without_extension, template_extension = path_and_extension(template_path)
  291
+      template_path_without_extension, template_extension = @finder.path_and_extension(template_path)
294 292
       if use_full_path
295 293
         if template_extension
296  
-          template_file_name = full_template_path(template_path_without_extension, template_extension)
  294
+          template_file_name = @finder.pick_template(template_path_without_extension, template_extension)
297 295
         else
298  
-          template_extension = pick_template_extension(template_path).to_s
  296
+          template_extension = @finder.pick_template_extension(template_path).to_s
299 297
           unless template_extension
300  
-            raise ActionViewError, "No template found for #{template_path} in #{view_paths.inspect}"
  298
+            raise ActionViewError, "No template found for #{template_path} in #{@finder.view_paths.inspect}"
301 299
           end
302  
-          template_file_name = full_template_path(template_path, template_extension)
  300
+          template_file_name = @finder.pick_template(template_path, template_extension)
303 301
           template_extension = template_extension.gsub(/^.+\./, '') # strip off any formats
304 302
         end
305 303
       else
@@ -309,7 +307,7 @@ def render_file(template_path, use_full_path = true, local_assigns = {}) #:nodoc
309 307
       template_source = nil # Don't read the source until we know that it is required
310 308
 
311 309
       if template_file_name.blank?
312  
-        raise ActionViewError, "Couldn't find template file for #{template_path} in #{view_paths.inspect}"
  310
+        raise ActionViewError, "Couldn't find template file for #{template_path} in #{@finder.view_paths.inspect}"
313 311
       end
314 312
 
315 313
       begin
@@ -319,7 +317,8 @@ def render_file(template_path, use_full_path = true, local_assigns = {}) #:nodoc
319 317
           e.sub_template_of(template_file_name)
320 318
           raise e
321 319
         else
322  
-          raise TemplateError.new(find_base_path_for("#{template_path_without_extension}.#{template_extension}") || view_paths.first, template_file_name, @assigns, template_source, e)
  320
+          raise TemplateError.new(@finder.find_base_path_for("#{template_path_without_extension}.#{template_extension}") ||
  321
+                @finder.view_paths.first, template_file_name, @assigns, template_source, e)
323 322
         end
324 323
       end
325 324
     end
@@ -371,45 +370,6 @@ def render_template(template_extension, template, file_path = nil, local_assigns
371 370
       end
372 371
     end
373 372
 
374  
-    # Gets the full template path with base path for the given template_path and extension.
375  
-    #
376  
-    #   full_template_path('users/show', 'html.erb')
377  
-    #   # => '~/rails/app/views/users/show.html.erb
378  
-    #
379  
-    def full_template_path(template_path, extension)
380  
-      if @@cache_template_extensions
381  
-        (@@cached_base_paths[template_path] ||= {})[extension.to_s] ||= find_full_template_path(template_path, extension)
382  
-      else
383  
-        find_full_template_path(template_path, extension)
384  
-      end
385  
-    end
386  
-
387  
-    # Gets the extension for an existing template with the given template_path.
388  
-    # Returns the format with the extension if that template exists.
389  
-    #
390  
-    #   pick_template_extension('users/show')
391  
-    #   # => 'html.erb'
392  
-    #
393  
-    #   pick_template_extension('users/legacy')
394  
-    #   # => "rhtml"
395  
-    #
396  
-    def pick_template_extension(template_path)#:nodoc:
397  
-      if @@cache_template_extensions
398  
-        (@@cached_template_extension[template_path] ||= {})[template_format] ||= find_template_extension_for(template_path)
399  
-      else
400  
-        find_template_extension_for(template_path)
401  
-      end
402  
-    end
403  
-
404  
-    def file_exists?(template_path)#:nodoc:
405  
-      template_file_name, template_file_extension = path_and_extension(template_path)
406  
-      if template_file_extension
407  
-        template_exists?(template_file_name, template_file_extension)
408  
-      else
409  
-        template_exists?(template_file_name, pick_template_extension(template_path))
410  
-      end
411  
-    end
412  
-
413 373
     # Returns true is the file may be rendered implicitly.
414 374
     def file_public?(template_path)#:nodoc:
415 375
       template_path.split('/').last[0,1] != '_'
@@ -422,83 +382,12 @@ def template_format
422 382
       @template_format = format.blank? ? :html : format.to_sym
423 383
     end
424 384
 
425  
-    # Adds a view_path to the front of the view_paths array.
426  
-    # This change affects the current request only.
427  
-    #
428  
-    #   @template.prepend_view_path("views/default")
429  
-    #   @template.prepend_view_path(["views/default", "views/custom"])
430  
-    #
431  
-    def prepend_view_path(path)
432  
-      @view_paths.unshift(*path)
433  
-    end
434  
-    
435  
-    # Adds a view_path to the end of the view_paths array.
436  
-    # This change affects the current request only.
437  
-    #
438  
-    #   @template.append_view_path("views/default")
439  
-    #   @template.append_view_path(["views/default", "views/custom"])
440  
-    #
441  
-    def append_view_path(path)
442  
-      @view_paths.push(*path)
443  
-    end
444  
-
445 385
     private
446 386
       def wrap_content_for_layout(content)
447 387
         original_content_for_layout = @content_for_layout
448 388
         @content_for_layout = content
449 389
         returning(yield) { @content_for_layout = original_content_for_layout }
450 390
       end
451  
-  
452  
-      def find_full_template_path(template_path, extension)
453  
-        file_name = "#{template_path}.#{extension}"
454  
-        base_path = find_base_path_for(file_name)
455  
-        base_path.blank? ? "" : "#{base_path}/#{file_name}"
456  
-      end
457  
-
458  
-      # Asserts the existence of a template.
459  
-      def template_exists?(template_path, extension)
460  
-        file_path = full_template_path(template_path, extension)
461  
-        !file_path.blank? && @@method_names.has_key?(file_path) || File.exist?(file_path)
462  
-      end
463  
-
464  
-      # Splits the path and extension from the given template_path and returns as an array.
465  
-      def path_and_extension(template_path)
466  
-        template_path_without_extension = template_path.sub(/\.(\w+)$/, '')
467  
-        [ template_path_without_extension, $1 ]
468  
-      end
469  
-      
470  
-      # Returns the view path that contains the given relative template path.
471  
-      def find_base_path_for(template_file_name)
472  
-        view_paths.find { |p| File.file?(File.join(p, template_file_name)) }
473  
-      end
474  
-
475  
-      # Returns the view path that the full path resides in.
476  
-      def extract_base_path_from(full_path)
477  
-        view_paths.find { |p| full_path[0..p.size - 1] == p }
478  
-      end
479  
-
480  
-      # Determines the template's file extension, such as rhtml, rxml, or rjs.
481  
-      def find_template_extension_for(template_path)
482  
-        find_template_extension_from_handler(template_path, true) ||
483  
-        find_template_extension_from_handler(template_path) ||
484  
-        find_template_extension_from_first_render()
485  
-      end
486  
-
487  
-      def find_template_extension_from_handler(template_path, formatted = nil)
488  
-        checked_template_path = formatted ? "#{template_path}.#{template_format}" : template_path
489  
-
490  
-        self.class.template_handler_extensions.each do |extension|
491  
-          if template_exists?(checked_template_path, extension)
492  
-            return formatted ? "#{template_format}.#{extension}" : extension.to_s
493  
-          end
494  
-        end
495  
-        nil
496  
-      end
497  
-      
498  
-      # Determine the template extension from the <tt>@first_render</tt> filename
499  
-      def find_template_extension_from_first_render
500  
-        File.basename(@first_render.to_s)[/^[^.]+\.(.+)$/, 1]
501  
-      end
502 391
 
503 392
       # This method reads a template file.
504 393
       def read_template_file(template_path, extension)
@@ -603,7 +492,8 @@ def compile_template(handler, template, file_name, local_assigns)
603 492
             logger.debug "Backtrace: #{e.backtrace.join("\n")}"
604 493
           end
605 494
 
606  
-          raise TemplateError.new(extract_base_path_from(file_name) || view_paths.first, file_name || template, @assigns, template, e)
  495
+          raise TemplateError.new(@finder.extract_base_path_from(file_name) ||
  496
+                @finder.view_paths.first, file_name || template, @assigns, template, e)
607 497
         end
608 498
 
609 499
         @@compile_time[render_symbol] = Time.now
2  actionpack/lib/action_view/helpers/cache_helper.rb
@@ -32,7 +32,7 @@ module CacheHelper
32 32
       #      <i>Topics listed alphabetically</i>
33 33
       #    <% end %>
34 34
       def cache(name = {}, options = nil, &block)
35  
-        template_extension = find_template_extension_for(first_render)[/\.?(\w+)$/, 1].to_sym
  35
+        template_extension = @finder.pick_template_extension(first_render)[/\.?(\w+)$/, 1].to_sym
36 36
         handler = Base.handler_class_for_extension(template_extension)
37 37
         handler.new(@controller).cache_fragment(block, name, options)
38 38
       end
165  actionpack/lib/action_view/template_finder.rb
... ...
@@ -0,0 +1,165 @@
  1
+module ActionView #:nodoc:
  2
+  class TemplateFinder #:nodoc:
  3
+
  4
+    class InvalidViewPath < StandardError #:nodoc:
  5
+    end
  6
+
  7
+    cattr_reader :processed_view_paths
  8
+    @@processed_view_paths = Hash.new {|hash, key| hash[key] = []}
  9
+
  10
+    cattr_reader :file_extension_cache
  11
+    @@file_extension_cache = Hash.new {|hash, key|
  12
+      hash[key] = Hash.new {|hash, key| hash[key] = []}
  13
+    }
  14
+
  15
+    class << self #:nodoc:
  16
+
  17
+      # This method is not thread safe. Mutex should be used whenever this is accessed from an instance method
  18
+      def process_view_paths(*view_paths)
  19
+        view_paths.flatten.compact.each do |dir|
  20
+          next if @@processed_view_paths.has_key?(dir)
  21
+
  22
+          @@processed_view_paths[dir] = []
  23
+          Dir.glob("#{dir}/**/*").each do |file|
  24
+            unless File.directory?(file)
  25
+              @@processed_view_paths[dir] << file.split(dir).last.sub(/^\//, '')
  26
+
  27
+              # Build extension cache
  28
+              extension = file.split(".").last
  29
+              if template_handler_extensions.include?(extension)
  30
+                key = file.split(dir).last.sub(/^\//, '').sub(/\.(\w+)$/, '')
  31
+                @@file_extension_cache[dir][key] << extension
  32
+              end
  33
+            end
  34
+          end
  35
+        end
  36
+      end
  37
+
  38
+      def update_extension_cache_for(extension)
  39
+        @@processed_view_paths.keys.each do |dir|
  40
+          Dir.glob("#{dir}/**/*.#{extension}").each do |file|
  41
+            key = file.split(dir).last.sub(/^\//, '').sub(/\.(\w+)$/, '')
  42
+            @@file_extension_cache[dir][key] << extension
  43
+          end
  44
+        end
  45
+      end
  46
+
  47
+      def template_handler_extensions
  48
+        ActionView::Base.template_handler_extensions
  49
+      end
  50
+
  51
+      def reload!
  52
+        view_paths = @@processed_view_paths.keys
  53
+
  54
+        @@processed_view_paths = Hash.new {|hash, key| hash[key] = []}
  55
+        @@file_extension_cache = Hash.new {|hash, key|
  56
+          hash[key] = Hash.new {|hash, key| hash[key] = []}
  57
+        }
  58
+
  59
+        process_view_paths(view_paths)
  60
+      end
  61
+    end
  62
+
  63
+    attr_accessor :view_paths
  64
+
  65
+    def initialize(*args)
  66
+      @template = args.shift
  67
+
  68
+      @view_paths = args.flatten
  69
+      @view_paths = @view_paths.respond_to?(:find) ? @view_paths.dup : [*@view_paths].compact
  70
+      check_view_paths(@view_paths)
  71
+    end
  72
+
  73
+    def prepend_view_path(path)
  74
+      @view_paths.unshift(*path)
  75
+
  76
+      self.class.process_view_paths(path)
  77
+    end
  78
+
  79
+    def append_view_path(path)
  80
+      @view_paths.push(*path)
  81
+
  82
+      self.class.process_view_paths(path)
  83
+    end
  84
+
  85
+    def view_paths=(path)
  86
+      @view_paths = path
  87
+      self.class.process_view_paths(path)
  88
+    end
  89
+
  90
+    def pick_template(template_path, extension)
  91
+      file_name = "#{template_path}.#{extension}"
  92
+      base_path = find_base_path_for(file_name)
  93
+      base_path.blank? ? false : "#{base_path}/#{file_name}"
  94
+    end
  95
+    alias_method :template_exists?, :pick_template
  96
+
  97
+    def file_exists?(template_path)
  98
+      # Clear the forward slash in the beginning if exists
  99
+      template_path = template_path.sub(/^\//, '')
  100
+
  101
+      template_file_name, template_file_extension = path_and_extension(template_path)
  102
+
  103
+      if template_file_extension
  104
+        template_exists?(template_file_name, template_file_extension)
  105
+      else
  106
+        template_exists?(template_file_name, pick_template_extension(template_path))
  107
+      end
  108
+    end
  109
+
  110
+    def find_base_path_for(template_file_name)
  111
+      @view_paths.find { |path| @@processed_view_paths[path].include?(template_file_name) }
  112
+    end
  113
+
  114
+    # Returns the view path that the full path resides in.
  115
+    def extract_base_path_from(full_path)
  116
+      @view_paths.find { |p| full_path[0..p.size - 1] == p }
  117
+    end
  118
+
  119
+    # Gets the extension for an existing template with the given template_path.
  120
+    # Returns the format with the extension if that template exists.
  121
+    #
  122
+    #   pick_template_extension('users/show')
  123
+    #   # => 'html.erb'
  124
+    #
  125
+    #   pick_template_extension('users/legacy')
  126
+    #   # => "rhtml"
  127
+    #
  128
+    def pick_template_extension(template_path)
  129
+      find_template_extension_from_handler(template_path) || find_template_extension_from_first_render
  130
+    end
  131
+
  132
+    def find_template_extension_from_handler(template_path)
  133
+      formatted_template_path = "#{template_path}.#{@template.template_format}"
  134
+
  135
+      view_paths.each do |path|
  136
+        if (extensions = @@file_extension_cache[path][formatted_template_path]).any?
  137
+          return "#{@template.template_format}.#{extensions.first}"
  138
+        elsif (extensions = @@file_extension_cache[path][template_path]).any?
  139
+          return extensions.first.to_s
  140
+        end
  141
+      end
  142
+      nil
  143
+    end
  144
+
  145
+    # Splits the path and extension from the given template_path and returns as an array.
  146
+    def path_and_extension(template_path)
  147
+      template_path_without_extension = template_path.sub(/\.(\w+)$/, '')
  148
+      [ template_path_without_extension, $1 ]
  149
+    end
  150
+
  151
+    # Determine the template extension from the <tt>@first_render</tt> filename
  152
+    def find_template_extension_from_first_render
  153
+      File.basename(@template.first_render.to_s)[/^[^.]+\.(.+)$/, 1]
  154
+    end
  155
+
  156
+    private
  157
+
  158
+    def check_view_paths(view_paths)
  159
+      view_paths.each do |path|
  160
+        raise(InvalidViewPath, "Unprocessed view path found in #{view_paths.inspect}") unless @@processed_view_paths.has_key?(path)
  161
+      end
  162
+    end
  163
+
  164
+  end
  165
+end
6  actionpack/test/controller/mime_responds_test.rb
@@ -437,11 +437,7 @@ def test_format_with_custom_response_type_and_request_headers_with_only_one_layo
437 437
 end
438 438
 
439 439
 class AbstractPostController < ActionController::Base
440  
-  class << self
441  
-    def view_paths
442  
-      [ File.dirname(__FILE__) + "/../fixtures/post_test/" ]
443  
-    end
444  
-  end
  440
+  self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/"
445 441
 end
446 442
 
447 443
 # For testing layouts which are set automatically
10  actionpack/test/controller/new_render_test.rb
@@ -78,11 +78,6 @@ def render_file_not_using_full_path
78 78
     @secret = 'in the sauce'
79 79
     render :file => 'test/render_file_with_ivar', :use_full_path => true
80 80
   end
81  
- 
82  
-  def render_file_not_using_full_path_with_relative_path
83  
-    @secret = 'in the sauce'
84  
-    render :file => 'test/../test/render_file_with_ivar', :use_full_path => true
85  
-  end
86 81
   
87 82
   def render_file_not_using_full_path_with_dot_in_path
88 83
     @secret = 'in the sauce'
@@ -488,11 +483,6 @@ def test_render_file_not_using_full_path
488 483
     assert_equal "The secret is in the sauce\n", @response.body
489 484
   end
490 485
 
491  
-  def test_render_file_not_using_full_path_with_relative_path
492  
-    get :render_file_not_using_full_path_with_relative_path
493  
-    assert_equal "The secret is in the sauce\n", @response.body
494  
-  end
495  
-
496 486
   def test_render_file_not_using_full_path_with_dot_in_path
497 487
     get :render_file_not_using_full_path_with_dot_in_path
498 488
     assert_equal "The secret is in the sauce\n", @response.body
9  actionpack/test/controller/render_test.rb
@@ -20,6 +20,10 @@ def render_hello_world
20 20
     render :template => "test/hello_world"
21 21
   end
22 22
 
  23
+  def render_hello_world_with_forward_slash
  24
+    render :template => "/test/hello_world"
  25
+  end
  26
+
23 27
   def render_hello_world_from_variable
24 28
     @person = "david"
25 29
     render :text => "hello #{@person}"
@@ -221,6 +225,11 @@ def test_render
221 225
     assert_template "test/hello_world"
222 226
   end
223 227
 
  228
+  def test_render_with_forward_slash
  229
+    get :render_hello_world_with_forward_slash
  230
+    assert_template "test/hello_world"
  231
+  end
  232
+
224 233
   def test_render_from_variable
225 234
     get :render_hello_world_from_variable
226 235
     assert_equal "hello david", @response.body
6  actionpack/test/controller/view_paths_test.rb
@@ -16,7 +16,7 @@ def hello_world() end
16 16
     def hello_world_at_request_time() render(:action => 'hello_world') end
17 17
     private
18 18
     def add_view_path
19  
-      self.class.view_paths.unshift "#{LOAD_PATH_ROOT}/override"
  19
+      prepend_view_path "#{LOAD_PATH_ROOT}/override"
20 20
     end
21 21
   end
22 22
   
@@ -27,7 +27,6 @@ def hello_world; render(:template => 'test/hello_world'); end
27 27
   
28 28
   def setup
29 29
     TestController.view_paths = nil
30  
-    ActionView::Base.cache_template_extensions = false
31 30
 
32 31
     @request  = ActionController::TestRequest.new
33 32
     @response = ActionController::TestResponse.new
@@ -45,7 +44,6 @@ def setup
45 44
   
46 45
   def teardown
47 46
     ActiveSupport::Deprecation.behavior = @old_behavior
48  
-    ActionView::Base.cache_template_extensions = true
49 47
   end
50 48
   
51 49
   def test_template_load_path_was_set_correctly
@@ -99,7 +97,7 @@ def test_view_paths
99 97
   end
100 98
   
101 99
   def test_view_paths_override
102  
-    TestController.view_paths.unshift "#{LOAD_PATH_ROOT}/override"
  100
+    TestController.prepend_view_path "#{LOAD_PATH_ROOT}/override"
103 101
     get :hello_world
104 102
     assert_response :success
105 103
     assert_equal "Hello overridden world!", @response.body
43  actionpack/test/template/template_finder_test.rb
... ...
@@ -0,0 +1,43 @@
  1
+require 'abstract_unit'
  2
+
  3
+class TemplateFinderTest < Test::Unit::TestCase
  4
+
  5
+  LOAD_PATH_ROOT = File.join(File.dirname(__FILE__), '..', 'fixtures')
  6
+
  7
+  def setup
  8
+    ActionView::TemplateFinder.process_view_paths(LOAD_PATH_ROOT)
  9
+    ActionView::Base::register_template_handler :mab, Class.new(ActionView::TemplateHandler)
  10
+    @template = ActionView::Base.new
  11
+    @finder = ActionView::TemplateFinder.new(@template, LOAD_PATH_ROOT)
  12
+  end
  13
+
  14
+  def test_should_raise_exception_for_unprocessed_view_path
  15
+    assert_raises ActionView::TemplateFinder::InvalidViewPath do
  16
+      ActionView::TemplateFinder.new(@template, File.dirname(__FILE__))
  17
+    end
  18
+  end
  19
+
  20
+  def test_should_cache_file_extension_properly
  21
+    assert_equal ["builder", "erb", "rhtml", "rjs", "rxml", "mab"].sort,
  22
+                 ActionView::TemplateFinder.file_extension_cache[LOAD_PATH_ROOT].values.flatten.uniq.sort
  23
+
  24
+    assert_equal Dir.glob("#{LOAD_PATH_ROOT}/**/*.{erb,rjs,rjs,builder,rxml,mab}").size,
  25
+                 ActionView::TemplateFinder.file_extension_cache[LOAD_PATH_ROOT].keys.size
  26
+  end
  27
+
  28
+  def test_should_cache_dir_content_properly
  29
+    assert ActionView::TemplateFinder.processed_view_paths[LOAD_PATH_ROOT]
  30
+    assert_equal Dir.glob("#{LOAD_PATH_ROOT}/**/*").find_all {|f| !File.directory?(f) }.size,
  31
+                 ActionView::TemplateFinder.processed_view_paths[LOAD_PATH_ROOT].size
  32
+  end
  33
+
  34
+  uses_mocha 'Template finder tests' do
  35
+
  36
+    def test_should_update_extension_cache_when_template_handler_is_registered
  37
+      ActionView::TemplateFinder.expects(:update_extension_cache_for).with("funky")
  38
+      ActionView::Base::register_template_handler :funky, Class.new(ActionView::TemplateHandler)
  39
+    end
  40
+
  41
+  end
  42
+
  43
+end

0 notes on commit 61c90a4

Please sign in to comment.
Something went wrong with that request. Please try again.