module Loaf::ViewExtensions

A mixin to define view extensions

Public Class Methods

new(*) click to toggle source
Calls superclass method
# File lib/loaf/view_extensions.rb, line 13
def initialize(*)
  @_breadcrumbs ||= []
  super
end

Public Instance Methods

add_breadcrumb(name, url, options = {})
Alias for: breadcrumb
breadcrumb(name, url, options = {}) click to toggle source

Adds breadcrumbs inside view.

@param [String] name

the breadcrumb name

@param [Object] url

the breadcrumb url

@param [Hash] options

the breadcrumb options

@api public

Also aliased as: add_breadcrumb
breadcrumb_trail(options = {}) { |Breadcrumb| ... } click to toggle source

Renders breadcrumbs inside view.

@param [Hash] options

@api public

breadcrumbs?() click to toggle source

Checks to see if any breadcrumbs have been added

@return [Boolean]

@api public

current_crumb?(path, pattern = :inclusive) click to toggle source

Check if breadcrumb is current based on the pattern

@param [String] path @param [Object] pattern

the pattern to match on

@api public

# File lib/loaf/view_extensions.rb, line 68
def current_crumb?(path, pattern = :inclusive)
  return false unless request.get? || request.head?

  origin_path = URI::DEFAULT_PARSER.unescape(path).force_encoding(Encoding::BINARY)

  request_uri = request.fullpath
  request_uri = URI::DEFAULT_PARSER.unescape(request_uri)
  request_uri.force_encoding(Encoding::BINARY)

  # strip away trailing slash
  if origin_path.start_with?('/') && origin_path != '/'
    origin_path.chomp!('/')
    request_uri.chomp!('/')
  end

  if %r{^\w+://} =~ origin_path
    origin_path.chomp!('/')
    request_uri.insert(0, "#{request.protocol}#{request.host_with_port}")
  end

  case pattern
  when :inclusive
    !request_uri.match(/^#{Regexp.escape(origin_path)}(\/.*|\?.*)?$/).nil?
  when :exclusive
    !request_uri.match(/^#{Regexp.escape(origin_path)}\/?(\?.*)?$/).nil?
  when :exact
    request_uri == origin_path
  when :force
    true
  when Regexp
    !request_uri.match(pattern).nil?
  when Hash
    query_params = URI.encode_www_form(pattern)
    !request_uri.match(/^#{Regexp.escape(origin_path)}\/?(\?.*)?.*?#{query_params}.*$/).nil?
  else
    raise ArgumentError, "Unknown `:#{pattern}` match option!"
  end
end

Private Instance Methods

_expand_url(url) click to toggle source

Expand url in the current context of the view

@api private

# File lib/loaf/view_extensions.rb, line 121
def _expand_url(url)
  case url
  when String, Symbol
    respond_to?(url) ? send(url) : url
  when Proc
    url.call(self)
  else
    url
  end
end
title_for(name) click to toggle source

Find title translation for a crumb name

@return [String]

@api private

# File lib/loaf/view_extensions.rb, line 114
def title_for(name)
  Translation.find_title(name)
end