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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
require 'active_support/core_ext/object/try'
require 'active_support/core_ext/object/blank'
module ActionView
# LookupContext is the object responsible to hold all information required to lookup
# templates, i.e. view paths and details. The LookupContext is also responsible to
# generate a key, given to view paths, used in the resolver cache lookup. Since
# this key is generated just once during the request, it speeds up all cache accesses.
class LookupContext #:nodoc:
mattr_accessor :fallbacks
@@fallbacks = [FileSystemResolver.new(""), FileSystemResolver.new("/")]
mattr_accessor :registered_details
self.registered_details = {}
def self.register_detail(name, options = {})
registered_details[name] = lambda do |value|
value = Array(value.presence || yield)
value |= [nil] unless options[:allow_nil] == false
value
end
end
register_detail(:formats) { Mime::SET.symbols }
register_detail(:locale) { [I18n.locale] }
class DetailsKey #:nodoc:
attr_reader :details
alias :eql? :equal?
@details_keys = Hash.new
def self.get(details)
@details_keys[details] ||= new(details)
end
def initialize(details)
@details, @hash = details, details.hash
end
end
def initialize(view_paths, details = {})
@details_key = nil
self.view_paths = view_paths
self.details = details
end
module ViewPaths
attr_reader :view_paths
# Whenever setting view paths, makes a copy so we can manipulate then in
# instance objects as we wish.
def view_paths=(paths)
@view_paths = ActionView::Base.process_view_paths(paths)
end
def find(name, prefix = nil, partial = false)
@view_paths.find(name, prefix, partial || false, details, details_key)
end
def find_all(name, prefix = nil, partial = false)
@view_paths.find_all(name, prefix, partial || false, details, details_key)
end
def exists?(name, prefix = nil, partial = false)
@view_paths.exists?(name, prefix, partial || false, details, details_key)
end
# Add fallbacks to the view paths. Useful in cases you are rendering a file.
def with_fallbacks
added_resolvers = 0
self.class.fallbacks.each do |resolver|
next if view_paths.include?(resolver)
view_paths.push(resolver)
added_resolvers += 1
end
yield
ensure
added_resolvers.times { view_paths.pop }
end
end
module Details
attr_reader :details
def details=(details)
@details = normalize_details(details)
@details_key = nil if @details_key && @details_key.details != @details
end
def details_key
@details_key ||= DetailsKey.get(@details)
end
# Shortcut to read formats from details.
def formats
@details[:formats].compact
end
# Shortcut to set formats in details.
def formats=(value)
self.details = @details.merge(:formats => value)
end
# Shortcut to read locale.
def locale
I18n.locale
end
# Shortcut to set locale in details and I18n.
def locale=(value)
I18n.locale = value
unless I18n.config.respond_to?(:lookup_context)
self.details = @details.merge(:locale => value)
end
end
# Update the details keys by merging the given hash into the current
# details hash. If a block is given, the details are modified just during
# the execution of the block and reverted to the previous value after.
def update_details(new_details)
old_details = @details
self.details = old_details.merge(new_details)
if block_given?
begin
yield
ensure
self.details = old_details
end
end
end
protected
def normalize_details(details)
details = details.dup
# TODO: Refactor this concern out of the resolver
details.delete(:formats) if details[:formats] == [:"*/*"]
self.class.registered_details.each do |k, v|
details[k] = v.call(details[k])
end
details.freeze
end
end
include Details
include ViewPaths
end
end
|