Class: ActionView::LookupContext

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

Overview

Action View Lookup Context

LookupContext is the object responsible for holding all information required for looking up templates, i.e. view paths and details. LookupContext is also responsible for generating a key, given to view paths, used in the resolver cache lookup. Since this key is generated only once during the request, it speeds up all cache accesses.

Defined Under Namespace

Modules: Accessors, DetailsCache, ViewPaths Classes: DetailsKey

Constant Summary

Constants included from Accessors

Accessors::DEFAULT_PROCS

Instance Attribute Summary collapse

Attributes included from ViewPaths

#html_fallback_for_js, #view_paths

Attributes included from DetailsCache

#cache

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ViewPaths

#any?, #append_view_paths, #exists?, #find, #find_all, #prepend_view_paths

Methods included from DetailsCache

#details_key, #disable_cache

Constructor Details

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

Returns a new instance of LookupContext.



228
229
230
231
232
233
234
235
236
# File 'actionview/lib/action_view/lookup_context.rb', line 228

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

  @details = initialize_details({}, details)
  @view_paths = build_view_paths(view_paths)
end

Instance Attribute Details

#prefixesObject

Returns the value of attribute prefixes



16
17
18
# File 'actionview/lib/action_view/lookup_context.rb', line 16

def prefixes
  @prefixes
end

#registered_detailsObject

Returns the value of attribute registered_details



18
19
20
# File 'actionview/lib/action_view/lookup_context.rb', line 18

def registered_details
  @registered_details
end

Class Method Details

.register_detail(name, &block) ⇒ Object



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

def self.register_detail(name, &block)
  registered_details << name
  Accessors::DEFAULT_PROCS[name] = block

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

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

Instance Method Details

#digest_cacheObject



238
239
240
# File 'actionview/lib/action_view/lookup_context.rb', line 238

def digest_cache
  @digest_cache ||= DetailsKey.digest_cache(@details)
end

#formats=(values) ⇒ Object

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



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'actionview/lib/action_view/lookup_context.rb', line 259

def formats=(values)
  if values
    values = values.dup
    values.concat(default_formats) if values.delete "*/*"
    values.uniq!

    unless values.all? { |v| Template::Types.symbols.include?(v) }
      invalid_values = values - Template::Types.symbols
      raise ArgumentError, "Invalid formats: #{invalid_values.map(&:inspect).join(", ")}"
    end

    if (values.length == 1) && (values[0] == :js)
      values << :html
      @html_fallback_for_js = true
    end
  end
  super(values)
end

#localeObject

Override locale to return a symbol instead of array.



279
280
281
# File 'actionview/lib/action_view/lookup_context.rb', line 279

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 has a copy of the original I18n configuration and it’s acting as proxy, which we need to skip.



286
287
288
289
290
291
292
293
# File 'actionview/lib/action_view/lookup_context.rb', line 286

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

  super(default_locale)
end

#with_prepended_formats(formats) ⇒ Object



242
243
244
245
246
247
# File 'actionview/lib/action_view/lookup_context.rb', line 242

def with_prepended_formats(formats)
  details = @details.dup
  details[:formats] = formats

  self.class.new(@view_paths, details, @prefixes)
end