aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/template/path.rb
blob: 9709549b70b9cb5282aaf5e088e8e379537a63f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
module ActionView
  class Template
    class Path
      attr_reader :path, :paths
      delegate :hash, :inspect, :to => :path
      
      def initialize(options)
        @cache = options[:cache]
      end

      def to_s
        if defined?(RAILS_ROOT)
          path.to_s.sub(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '')
        else
          path.to_s
        end
      end

      def to_str
        path.to_str
      end

      def ==(path)
        to_str == path.to_str
      end

      def eql?(path)
        to_str == path.to_str
      end

      def find_by_parts(name, extensions = nil, prefix = nil, partial = nil)
        path = prefix ? "#{prefix}/" : ""
    
        name = name.to_s.split("/")
        name[-1] = "_#{name[-1]}" if partial
    
        path << name.join("/")

        template = nil

        Array(extensions).each do |extension|
          extensioned_path = extension ? "#{path}.#{extension}" : path
          break if (template = find_template(extensioned_path))
        end
        template || find_template(path)
      end
  
    private
      def create_template(file)
        Template.new(file.split("#{self}/").last, self)
      end
    end

    class FileSystemPath < Path
      def initialize(path, options = {})
        raise ArgumentError, "path already is a Path class" if path.is_a?(Path)        
        
        super(options)
        @path, @paths = path, {}
        
        # **/*/** is a hax for symlinked directories
        load_templates("#{@path}/{**/*,**}/**") if @cache
      end

    private
    
      def load_template(template)
        template.load!
        template.accessible_paths.each do |path|
          @paths[path] = template
        end
      end
    
      def find_template(path)
        load_templates("#{@path}/#{path}{,.*}") unless @cache
        @paths[path]
      end    
    
      def load_templates(glob)
        Dir[glob].each do |file|
          load_template(create_template(file)) unless File.directory?(file)
        end
      end
      
    end
  end
end