aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/view_load_paths.rb
blob: 6e439a009c2944bb80836c34eaf62959e74a905b (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
module ActionView #:nodoc:
  class ViewLoadPaths < Array #:nodoc:
    def self.type_cast(obj)
      obj.is_a?(String) ? LoadPath.new(obj) : obj
    end

    class LoadPath #:nodoc:
      attr_reader :path, :paths
      delegate :to_s, :to_str, :inspect, :to => :path

      def initialize(path)
        @path = path.freeze
        reload!
      end

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

      # Rebuild load path directory cache
      def reload!
        @paths = {}

        files.each do |file|
          @paths[file.path] = file
          @paths[file.path_without_extension] ||= file
        end

        @paths.freeze
      end

      def find_template_file_for_partial_path(template_path, template_format)
        @paths["#{template_path}.#{template_format}"] ||
          @paths[template_path] ||
          @paths[template_path.gsub(/\..*$/, '')]
      end

      private
        # Get all the files and directories in the path
        def files_in_path
          Dir.glob("#{@path}/**/*/**") | Dir.glob("#{@path}/**")
        end

        # Create an array of all the files within the path
        def files
          files_in_path.map do |file|
            TemplateFile.from_full_path(@path, file) unless File.directory?(file)
          end.compact
        end
    end

    def initialize(*args)
      super(*args).map! { |obj| self.class.type_cast(obj) }
    end

    def reload!
      each { |path| path.reload! }
    end

    def <<(obj)
      super(self.class.type_cast(obj))
    end

    def push(*objs)
      delete_paths!(objs)
      super(*objs.map { |obj| self.class.type_cast(obj) })
    end

    def unshift(*objs)
      delete_paths!(objs)
      super(*objs.map { |obj| self.class.type_cast(obj) })
    end

    def template_exists?(file)
      find_load_path_for_path(file) ? true : false
    end

    def find_load_path_for_path(file)
      find { |path| path.paths[file.to_s] }
    end

    def find_template_file_for_path(template_path)
      template_path_without_extension, template_extension = path_and_extension(template_path.to_s)
      each do |path|
        if f = path.find_template_file_for_partial_path(template_path_without_extension, template_extension)
          return f
        end
      end
      nil
    end

    private
      def delete_paths!(paths)
        paths.each { |p1| delete_if { |p2| p1.to_s == p2.to_s } }
      end

      # Splits the path and extension from the given template_path and returns as an array.
      def path_and_extension(template_path)
        template_path_without_extension = template_path.sub(/\.(\w+)$/, '')
        [template_path_without_extension, $1]
      end
  end
end