Class: Para::Breadcrumbs::Breadcrumb

Inherits:
Object
  • Object
show all
Includes:
ActionDispatch::Routing::PolymorphicRoutes, Helpers::ResourceName
Defined in:
lib/para/breadcrumbs/breadcrumb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::ResourceName

#resource_name

Constructor Details

#initialize(resource_or_identifier, path, controller, *options) ⇒ Breadcrumb

Returns a new instance of Breadcrumb.



9
10
11
12
13
14
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)

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



77
78
79
80
81
82
83
# 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

Instance Attribute Details

#_pathObject (readonly)

Returns the value of attribute _path.



7
8
9
# File 'lib/para/breadcrumbs/breadcrumb.rb', line 7

def _path
  @_path
end

#controllerObject (readonly)

Returns the value of attribute controller.



7
8
9
# File 'lib/para/breadcrumbs/breadcrumb.rb', line 7

def controller
  @controller
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/para/breadcrumbs/breadcrumb.rb', line 7

def options
  @options
end

#resource_or_identifierObject (readonly)

Returns the value of attribute resource_or_identifier.



7
8
9
# File 'lib/para/breadcrumbs/breadcrumb.rb', line 7

def resource_or_identifier
  @resource_or_identifier
end

Instance Method Details

#active?(request) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/para/breadcrumbs/breadcrumb.rb', line 43

def active?(request)
  path == request.path || path == '#'
end

#pathObject

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



29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

#titleObject



16
17
18
19
20
21
22
23
24
# 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