123456789_123456789_123456789_123456789_123456789_

Class: ActionView::LookupContext

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Modules:
Classes:
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: actionview/lib/action_view/lookup_context.rb

Overview

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.

Class Method Summary

Instance Attribute Summary

ViewPaths - Included

DetailsCache - Included

Instance Method Summary

ViewPaths - Included

#any_templates?,
#append_view_path

Append a path to the list of view paths for the current LookupContext.

#details_for_lookup,
#lookup_context

LookupContext is the object responsible for holding all information required for looking up templates, i.e. view paths and details.

#prepend_view_path

Prepend a path to the list of view paths for the current LookupContext.

#template_exists?,
#_prefixes

The prefixes used in render "foo" shortcuts.

DetailsCache - Included

#disable_cache

Temporary skip passing the details_key forward.

#_set_detail,
#details_key

Calculate the details key.

Constructor Details

.new(view_paths, details = {}, prefixes = []) ⇒ LookupContext

[ GitHub ]

  
# File 'actionview/lib/action_view/lookup_context.rb', line 234

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

Class Method Details

.register_detail(name, &block)

[ GitHub ]

  
# File 'actionview/lib/action_view/lookup_context.rb', line 25

def self.register_detail(name, &block)
  self.default_procs = self.default_procs.merge(name => block).freeze

  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

.registered_details

[ GitHub ]

  
# File 'actionview/lib/action_view/lookup_context.rb', line 21

def self.registered_details
  self.default_procs.keys
end

Instance Attribute Details

#default_procs (rw)

[ GitHub ]

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

singleton_class.attr_accessor :default_procs

#formats=(values) (writeonly)

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

[ GitHub ]

  
# File 'actionview/lib/action_view/lookup_context.rb', line 265

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

    unless Template::Types.valid_symbols?(values)
      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

#locale (rw)

Override locale to return a symbol instead of array.

[ GitHub ]

  
# File 'actionview/lib/action_view/lookup_context.rb', line 285

def locale
  @details[:locale].first
end

#locale=(value) (rw)

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.

[ GitHub ]

  
# File 'actionview/lib/action_view/lookup_context.rb', line 292

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

#prefixes (rw)

[ GitHub ]

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

attr_accessor :prefixes

Instance Method Details

#digest_cache

[ GitHub ]

  
# File 'actionview/lib/action_view/lookup_context.rb', line 244

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

#initialize_details(target, details) (private)

[ GitHub ]

  
# File 'actionview/lib/action_view/lookup_context.rb', line 255

def initialize_details(target, details)
  LookupContext.registered_details.each do |k|
    target[k] = details[k] || LookupContext.default_procs[k].call
  end
  target
end

#with_prepended_formats(formats)

[ GitHub ]

  
# File 'actionview/lib/action_view/lookup_context.rb', line 248

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

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