module ActionView::LookupContext::ViewPaths

Helpers related to template lookup using the lookup context information.

Attributes

html_fallback_for_js[R]
view_paths[R]

Public Instance Methods

any?(name, prefixes = [], partial = false) click to toggle source
# File lib/action_view/lookup_context.rb, line 140
def any?(name, prefixes = [], partial = false)
  @view_paths.exists?(*args_for_any(name, prefixes, partial))
end
Also aliased as: any_templates?
any_templates?(name, prefixes = [], partial = false)
Alias for: any?
exists?(name, prefixes = [], partial = false, keys = [], **options) click to toggle source
# File lib/action_view/lookup_context.rb, line 135
def exists?(name, prefixes = [], partial = false, keys = [], **options)
  @view_paths.exists?(*args_for_lookup(name, prefixes, partial, keys, options))
end
Also aliased as: template_exists?
find(name, prefixes = [], partial = false, keys = [], options = {}) click to toggle source
# File lib/action_view/lookup_context.rb, line 126
def find(name, prefixes = [], partial = false, keys = [], options = {})
  @view_paths.find(*args_for_lookup(name, prefixes, partial, keys, options))
end
Also aliased as: find_template
find_all(name, prefixes = [], partial = false, keys = [], options = {}) click to toggle source
# File lib/action_view/lookup_context.rb, line 131
def find_all(name, prefixes = [], partial = false, keys = [], options = {})
  @view_paths.find_all(*args_for_lookup(name, prefixes, partial, keys, options))
end
find_template(name, prefixes = [], partial = false, keys = [], options = {})
Alias for: find
template_exists?(name, prefixes = [], partial = false, keys = [], **options)
Alias for: exists?
with_fallbacks() click to toggle source

Adds fallbacks to the view paths. Useful in cases when you are rendering a :file.

# File lib/action_view/lookup_context.rb, line 147
      def with_fallbacks
        view_paths = build_view_paths((@view_paths.paths + self.class.fallbacks).uniq)

        if block_given?
          raise ArgumentError, <<~eowarn.squish
          Calling `with_fallbacks` with a block is not supported. Call methods on
          the lookup context returned by `with_fallbacks` instead.
          eowarn
        else
          ActionView::LookupContext.new(view_paths, @details, @prefixes)
        end
      end

Private Instance Methods

args_for_any(name, prefixes, partial) click to toggle source
# File lib/action_view/lookup_context.rb, line 187
def args_for_any(name, prefixes, partial)
  name, prefixes = normalize_name(name, prefixes)
  details, details_key = detail_args_for_any
  [name, prefixes, partial || false, details, details_key]
end
args_for_lookup(name, prefixes, partial, keys, details_options) click to toggle source
# File lib/action_view/lookup_context.rb, line 167
def args_for_lookup(name, prefixes, partial, keys, details_options)
  name, prefixes = normalize_name(name, prefixes)
  details, details_key = detail_args_for(details_options)
  [name, prefixes, partial || false, details, details_key, keys]
end
build_view_paths(paths) click to toggle source

Whenever setting view paths, makes a copy so that we can manipulate them in instance objects as we wish.

# File lib/action_view/lookup_context.rb, line 163
def build_view_paths(paths)
  ActionView::PathSet.new(Array(paths))
end
detail_args_for(options) click to toggle source

Compute details hash and key according to user options (e.g. passed from render).

# File lib/action_view/lookup_context.rb, line 174
def detail_args_for(options) # :doc:
  return @details, details_key if options.empty? # most common path.
  user_details = @details.merge(options)

  if @cache
    details_key = DetailsKey.details_cache_key(user_details)
  else
    details_key = nil
  end

  [user_details, details_key]
end
detail_args_for_any() click to toggle source
# File lib/action_view/lookup_context.rb, line 193
def detail_args_for_any
  @detail_args_for_any ||= begin
    details = {}

    registered_details.each do |k|
      if k == :variants
        details[k] = :any
      else
        details[k] = Accessors::DEFAULT_PROCS[k].call
      end
    end

    if @cache
      [details, DetailsKey.details_cache_key(details)]
    else
      [details, nil]
    end
  end
end
normalize_name(name, prefixes) click to toggle source

Support legacy foo.erb names even though we now ignore .erb as well as incorrectly putting part of the path in the template name instead of the prefix.

# File lib/action_view/lookup_context.rb, line 216
def normalize_name(name, prefixes)
  prefixes = prefixes.presence
  parts    = name.to_s.split("/")
  parts.shift if parts.first.empty?
  name = parts.pop

  return name, prefixes || [""] if parts.empty?

  parts    = parts.join("/")
  prefixes = prefixes ? prefixes.map { |p| "#{p}/#{parts}" } : [parts]

  return name, prefixes
end