class Para::Breadcrumbs::Breadcrumb

Attributes

_path[R]
controller[R]
options[R]
resource_or_identifier[R]

Public Class Methods

new(resource_or_identifier, path, controller, *options) click to toggle source
# File lib/para/breadcrumbs/breadcrumb.rb, line 9
def initialize(resource_or_identifier, path, controller, *options)
  @resource_or_identifier = resource_or_identifier
  @_path = path
  @controller = controller
  @options = options
end

Public Instance Methods

active?(request) click to toggle source
# File lib/para/breadcrumbs/breadcrumb.rb, line 43
def active?(request)
  path == request.path || path == '#'
end
path() click to toggle source

Allow lazy evaluation of routes to define breadcrumbs before being able to access request or routes

# File lib/para/breadcrumbs/breadcrumb.rb, line 29
def path
  @path ||= if _path.is_a?(Symbol)
    find_route_for(_path, *options)
  elsif _path
    _path
  else
    begin
      polymorphic_path(resource_or_identifier)
    rescue
      '#'
    end
  end
end
title() click to toggle source
# File lib/para/breadcrumbs/breadcrumb.rb, line 16
def title
  @title ||= if resource_or_identifier.is_a?(Symbol)
    translate(resource_or_identifier)
  elsif resource_or_identifier.is_a?(ActiveRecord::Base)
    resource_name(resource_or_identifier)
  else
    resource_or_identifier
  end
end

Private Instance Methods

find_route_for(path, *options) click to toggle source
# File lib/para/breadcrumbs/breadcrumb.rb, line 69
def find_route_for(path, *options)
  path = path_method?(path) ? path.to_sym : :"#{ path }_path"
  Rails.application.routes.url_helpers.send(path, *options)
end
method_missing(method_name, *args, &block) click to toggle source

Allow polymorphic_path to work by delegating missing methods ending with _path or _url to be tried on url_helpers

Calls superclass method
# File lib/para/breadcrumbs/breadcrumb.rb, line 77
def method_missing(method_name, *args, &block)
  if path_method?(method_name)
    Rails.application.routes.url_helpers.try(method_name, *args) || super
  else
    super
  end
end
path_method?(path) click to toggle source
# File lib/para/breadcrumbs/breadcrumb.rb, line 85
def path_method?(path)
  path.to_s.match(/_(path|url)\z/)
end
translate(key) click to toggle source
# File lib/para/breadcrumbs/breadcrumb.rb, line 49
def translate(key)
  if controller.admin?
    ::I18n.t("admin.breadcrumbs.#{ resource_or_identifier }")
  else
    # Check if a specific translation has been defined
    if (translation = ::I18n.t("breadcrumbs.#{ resource_or_identifier }", default: '')).present?
      translation
    else
      # If no translation is defined, we check if there's a plural model
      # translation associated to the given key
      begin
        klass = key.to_s.singularize.camelize.constantize
        klass.try(:model_name).try(:human, count: 2) || key
      rescue NameError
        key
      end
    end
  end
end