aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/paths.rb
blob: 6c6d2ff979e0889c902a57d81be19c0827cff68e (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
module ActionView #:nodoc:
  class PathSet < Array #:nodoc:
    def self.type_cast(obj)
      if obj.is_a?(String)
        Template::EagerPath.new(obj)
      else
        obj
      end
    end

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

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

    def concat(array)
      super(array.map! { |obj| self.class.type_cast(obj) })
    end

    def insert(index, obj)
      super(index, self.class.type_cast(obj))
    end

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

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

    def find_by_parts(path, extension = nil, prefix = nil, partial = false)
      template_path = path.sub(/^\//, '')

      each do |load_path|
        if template = load_path.find_by_parts(template_path, extension, prefix, partial)
          return template
        end
      end

      Template.new(path, self)
    rescue ActionView::MissingTemplate => e
      extension ||= []
      raise ActionView::MissingTemplate.new(self, "#{prefix}/#{path}.{#{extension.join(",")}}")
    end
    
    def find_by_parts?(path, extension = nil, prefix = nil, partial = false)
      template_path = path.sub(/^\//, '')

      each do |load_path|
        return true if template = load_path.find_by_parts(template_path, extension, prefix, partial)
      end      
      false
    end

    def find_template(original_template_path, format = nil)
      return original_template_path if original_template_path.respond_to?(:render)
      template_path = original_template_path.sub(/^\//, '')

      each do |load_path|
        if template = load_path.find_by_parts(template_path, format)
          return template
        # Try to find html version if the format is javascript
        elsif format == :js && template = load_path["#{template_path}.html"]
          return template
        end
      end

      Template.new(original_template_path, self)
    end
  end
end