Functional tests now work properly, bearing in mind whether a user is logged in or...
[depot.git] / vendor / rails / actionpack / lib / action_view / paths.rb
1 module ActionView #:nodoc:
2 class PathSet < Array #:nodoc:
3 def self.type_cast(obj)
4 if obj.is_a?(String)
5 if Base.warn_cache_misses && defined?(Rails) && Rails.initialized?
6 Base.logger.debug "[PERFORMANCE] Processing view path during a " +
7 "request. This an expense disk operation that should be done at " +
8 "boot. You can manually process this view path with " +
9 "ActionView::Base.process_view_paths(#{obj.inspect}) and set it " +
10 "as your view path"
11 end
12 Path.new(obj)
13 else
14 obj
15 end
16 end
17
18 def initialize(*args)
19 super(*args).map! { |obj| self.class.type_cast(obj) }
20 end
21
22 def <<(obj)
23 super(self.class.type_cast(obj))
24 end
25
26 def concat(array)
27 super(array.map! { |obj| self.class.type_cast(obj) })
28 end
29
30 def insert(index, obj)
31 super(index, self.class.type_cast(obj))
32 end
33
34 def push(*objs)
35 super(*objs.map { |obj| self.class.type_cast(obj) })
36 end
37
38 def unshift(*objs)
39 super(*objs.map { |obj| self.class.type_cast(obj) })
40 end
41
42 class Path #:nodoc:
43 def self.eager_load_templates!
44 @eager_load_templates = true
45 end
46
47 def self.eager_load_templates?
48 @eager_load_templates || false
49 end
50
51 attr_reader :path, :paths
52 delegate :to_s, :to_str, :hash, :inspect, :to => :path
53
54 def initialize(path, load = true)
55 raise ArgumentError, "path already is a Path class" if path.is_a?(Path)
56 @path = path.freeze
57 reload! if load
58 end
59
60 def ==(path)
61 to_str == path.to_str
62 end
63
64 def eql?(path)
65 to_str == path.to_str
66 end
67
68 def [](path)
69 raise "Unloaded view path! #{@path}" unless @loaded
70 @paths[path]
71 end
72
73 def loaded?
74 @loaded ? true : false
75 end
76
77 def load
78 reload! unless loaded?
79 self
80 end
81
82 # Rebuild load path directory cache
83 def reload!
84 @paths = {}
85
86 templates_in_path do |template|
87 # Eager load memoized methods and freeze cached template
88 template.freeze if self.class.eager_load_templates?
89
90 @paths[template.path] = template
91 @paths[template.path_without_extension] ||= template
92 end
93
94 @paths.freeze
95 @loaded = true
96 end
97
98 private
99 def templates_in_path
100 (Dir.glob("#{@path}/**/*/**") | Dir.glob("#{@path}/**")).each do |file|
101 unless File.directory?(file)
102 yield Template.new(file.split("#{self}/").last, self)
103 end
104 end
105 end
106 end
107
108 def load
109 each { |path| path.load }
110 end
111
112 def reload!
113 each { |path| path.reload! }
114 end
115
116 def [](template_path)
117 each do |path|
118 if template = path[template_path]
119 return template
120 end
121 end
122 nil
123 end
124 end
125 end