class Object

Public Class Methods

new(pattern = nil) click to toggle source
Calls superclass method
# File lib/versioncake/view_additions_rails5.rb, line 17
def initialize(pattern = nil)
  @pattern = pattern || ":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:versions,}{.:handlers,}"
  super()
end

Public Instance Methods

build_path_regex() click to toggle source
# File lib/versioncake/view_additions_rails7.rb, line 9
def build_path_regex
  handlers = ActionView::Template::Handlers.extensions.map { |x| Regexp.escape(x) }.join("|")
  formats = ActionView::Template::Types.symbols.map { |x| Regexp.escape(x) }.join("|")
  locales = "[a-z]{2}(?:-[A-Z]{2})?"
  variants = "[^.]*"

  %r{
      \A
      (?:(?<prefix>.*)/)?
      (?<partial>_)?
      (?<action>.*?)
      (?:\.(?<locale>#{locales}))??
      (?:\.(?<format>#{formats}))??
      (?:\+(?<variant>#{variants}))??
      (?:\.(?<versions>v[0-9]+))??
      (?:\.(?<handler>#{handlers}))?
      \z
    }x
end
build_template(locals) click to toggle source
# File lib/versioncake/view_additions_rails7.rb, line 140
def build_template(locals)
  ActionView::Template.new(
    @source,
    @identifier,
    details.handler_class,

    format: details.format_or_default,
    variant: variant&.to_s,
    virtual_path: @virtual_path,
    version: version&.to_s,

    locals: locals.map(&:to_s)
  )
end
extract_handler_and_format(path, default_formats) click to toggle source

The default extract handler expects that the handler is the last extension and the format is the next one. Since we are replacing the DEFAULT_PATTERN, we need to make sure that we extract the format from the correct position.

The version may be stuck inbetween the format and the handler. This is actually pretty tricky because the version is optional and the locale is optional-which means when there are 3 ‘pieces’ it may be the locale, format and handler or the format, version and handler. To check this, we will try one additional time if there are more pieces, which should cover all the cases:

Cases:

1: assume version is in the extension, pieces = ['html','erb']
2: assume version is in the extension, pieces = ['html','v1','erb']
3: assume version is in the extension, pieces = ['en','html','erb']
4: assume version is in the extension, pieces = ['en','html','v1','erb']
# File lib/versioncake/view_additions_rails6.rb, line 35
def extract_handler_and_format(path, default_formats)
  pieces = File.basename(path).split(".")
  pieces.shift

  extension = pieces.pop
  handler = ActionView::Template.handler_for_extension(extension)
  format  = get_format_from_pieces(pieces, Mime)

  [handler, format]
end
extract_handler_and_format_and_variant(path, default_formats=nil) click to toggle source

The default extract handler expects that the handler is the last extension and the format is the next one. Since we are replacing the DEFAULT_PATTERN, we need to make sure that we extract the format from the correct position.

The version may be stuck inbetween the format and the handler. This is actually pretty tricky because the version is optional and the locale is optional-which means when there are 3 ‘pieces’ it may be the locale, format and handler or the format, version and handler. To check this, we will try one additional time if there are more pieces, which should cover all the cases:

Cases:

1: assume version is in the extension, pieces = ['html','erb']
2: assume version is in the extension, pieces = ['html','v1','erb']
3: assume version is in the extension, pieces = ['en','html','erb']
4: assume version is in the extension, pieces = ['en','html','v1','erb']
# File lib/versioncake/view_additions_rails5.rb, line 37
def extract_handler_and_format_and_variant(path, default_formats=nil)
  pieces = File.basename(path).split('.'.freeze)
  pieces.shift

  extension = pieces.pop

  handler = ActionView::Template.handler_for_extension(extension)
  format, variant = get_format_and_variant_from_pieces(pieces, Mime)
  format &&= ActionView::Template::Types[format]

  [handler, format, variant]
end
get_format_and_variant_from_pieces(pieces, format_list) click to toggle source

If there are still pieces and we didn’t find a valid format, we may have a version in the extension, so try one more time to pop the format.

# File lib/versioncake/view_additions_rails5.rb, line 52
def get_format_and_variant_from_pieces(pieces, format_list)
  variant, format = nil
  pieces.reverse.each do |piece|
    piece, variant = piece.split(ActionView::PathResolver::EXTENSIONS[:variants], 2)

    format = format_list[piece]
    break unless format.nil?
  end
  [format, variant]
end
get_format_from_pieces(pieces, format_list) click to toggle source

If there are still pieces and we didn’t find a valid format, we may have a version in the extension, so try one more time to pop the format.

# File lib/versioncake/view_additions_rails6.rb, line 48
def get_format_from_pieces(pieces, format_list)
  format = nil
  pieces.reverse.each do |piece|
    if ActionView::PathResolver::EXTENSIONS.is_a?(Hash) &&
        ActionView::PathResolver::EXTENSIONS.include?(:variants)
      piece = piece.split(ActionView::PathResolver::EXTENSIONS[:variants], 2).first # remove variant from format
    end

    format = format_list[piece]
    break unless format.nil?
  end
  format
end
matches?(requested) click to toggle source
# File lib/versioncake/view_additions_rails7.rb, line 79
def matches?(requested)
  requested.formats_idx[@format] &&
    requested.locale_idx[@locale] &&
    requested.versions_idx[@version] &&
    requested.variants_idx[@variant] &&
    requested.handlers_idx[@handler]
end
parse(path) click to toggle source
# File lib/versioncake/view_additions_rails7.rb, line 29
def parse(path)
  @regex ||= build_path_regex
  match = @regex.match(path)
  path = ActionView::TemplatePath.build(match[:action], match[:prefix] || "", !!match[:partial])
  details = ActionView::TemplateDetails.new(
    match[:locale]&.to_sym,
    match[:handler]&.to_sym,
    match[:format]&.to_sym,
    match[:variant]&.to_sym,
    match[:versions]&.to_sym,
  )

  ActionView::Resolver::PathParser::ParsedPath.new(path, details)
end
sort_key_for(requested) click to toggle source
# File lib/versioncake/view_additions_rails7.rb, line 87
def sort_key_for(requested)
  [
    requested.formats_idx[@format],
    requested.locale_idx[@locale],
    requested.versions_idx[@version],
    requested.variants_idx[@variant],
    requested.handlers_idx[@handler]
  ]
end