Class: ActionView::LookupContext

Inherits:
Object
  • Object
show all
Includes:
Accessors, DetailsCache, ViewPaths
Defined in:
lib/action_view/lookup_context.rb

Overview

Action View Lookup Context

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.

Defined Under Namespace

Modules: Accessors, DetailsCache, ViewPaths Classes: DetailsKey

Constant Summary collapse

@@fallbacks =
FallbackFileSystemResolver.instances

Instance Attribute Summary collapse

Attributes included from ViewPaths

#view_paths

Attributes included from DetailsCache

#cache

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ViewPaths

#exists?, #find, #find_all, #with_fallbacks

Methods included from DetailsCache

#details_key, #disable_cache

Constructor Details

#initialize(view_paths, details = {}, prefixes = []) ⇒ LookupContext

Returns a new instance of LookupContext.



181
182
183
184
185
186
187
188
189
# File 'lib/action_view/lookup_context.rb', line 181

def initialize(view_paths, details = {}, prefixes = [])
  @details, @details_key = {}, nil
  @frozen_formats, @skip_default_locale = false, false
  @cache = true
  @prefixes = prefixes

  self.view_paths = view_paths
  initialize_details(details)
end

Instance Attribute Details

#prefixesObject

:nodoc:



13
14
15
# File 'lib/action_view/lookup_context.rb', line 13

def prefixes
  @prefixes
end

Class Method Details

.register_detail(name, options = {}, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/action_view/lookup_context.rb', line 21

def self.register_detail(name, options = {}, &block)
  self.registered_details << name
  initialize = registered_details.map { |n| "@details[:#{n}] = details[:#{n}] || default_#{n}" }

  Accessors.send :define_method, :"default_#{name}", &block
  Accessors.module_eval <<-METHOD, __FILE__, __LINE__ + 1
    def #{name}
      @details[:#{name}]
    end

    def #{name}=(value)
      value = value.present? ? Array.wrap(value) : default_#{name}
      _set_detail(:#{name}, value) if value != @details[:#{name}]
    end

    remove_possible_method :initialize_details
    def initialize_details(details)
      #{initialize.join("\n")}
    end
  METHOD
end

Instance Method Details

#formats=(values) ⇒ Object

Override formats= to expand [“/”] values and automatically add :html as fallback to :js.



202
203
204
205
206
207
208
# File 'lib/action_view/lookup_context.rb', line 202

def formats=(values)
  if values
    values.concat(default_formats) if values.delete "*/*"
    values << :html if values == [:js]
  end
  super(values)
end

#freeze_formats(formats, unless_frozen = false) ⇒ Object

Freeze the current formats in the lookup context. By freezing them, you that next template lookups are not going to modify the formats. The con use this, to ensure that formats won’t be further modified (as it does



194
195
196
197
198
# File 'lib/action_view/lookup_context.rb', line 194

def freeze_formats(formats, unless_frozen=false) #:nodoc:
  return if unless_frozen && @frozen_formats
  self.formats = formats
  @frozen_formats = true
end

#localeObject

Override locale to return a symbol instead of array.



217
218
219
# File 'lib/action_view/lookup_context.rb', line 217

def locale
  @details[:locale].first
end

#locale=(value) ⇒ Object

Overload locale= to also set the I18n.locale. If the current I18n.config object responds to original_config, it means that it’s has a copy of the original I18n configuration and it’s acting as proxy, which we need to skip.



224
225
226
227
228
229
230
231
# File 'lib/action_view/lookup_context.rb', line 224

def locale=(value)
  if value
    config = I18n.config.respond_to?(:original_config) ? I18n.config.original_config : I18n.config
    config.locale = value
  end

  super(@skip_default_locale ? I18n.locale : default_locale)
end

#skip_default_locale!Object

Do not use the default locale on template lookup.



211
212
213
214
# File 'lib/action_view/lookup_context.rb', line 211

def skip_default_locale!
  @skip_default_locale = true
  self.locale = nil
end

#with_layout_formatObject

A method which only uses the first format in the formats array for layout lookup.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/action_view/lookup_context.rb', line 234

def with_layout_format
  if formats.size == 1
    yield
  else
    old_formats = formats
    _set_detail(:formats, formats[0,1])

    begin
      yield
    ensure
      _set_detail(:formats, old_formats)
    end
  end
end