Class: Gretel::Crumb

Inherits:
Object
  • Object
show all
Defined in:
lib/gretel/crumb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, key, *args) ⇒ Crumb

Initializes a new crumb from the given key. It finds the breadcrumb created in Gretel::Crumbs.layout and renders the block using the arguments supplied in args.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/gretel/crumb.rb', line 5

def initialize(context, key, *args)
  if key.class.respond_to?(:model_name)
    # Enables calling `breadcrumb @product` instead of `breadcrumb :product, @product`
    args.unshift key
    key = key.class.model_name.to_s.underscore.to_sym
  end

  block = Gretel::Crumbs.crumbs[key]
  raise ArgumentError, "Breadcrumb :#{key} not found." unless block
  @key = key
  @context = context
  instance_exec *args, &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Proxy to view context.



64
65
66
# File 'lib/gretel/crumb.rb', line 64

def method_missing(method, *args, &block)
  context.send(method, *args, &block)
end

Instance Attribute Details

#contextObject (readonly)

The current view context.



61
62
63
# File 'lib/gretel/crumb.rb', line 61

def context
  @context
end

#keyObject (readonly)

Key of the breadcrumb.



58
59
60
# File 'lib/gretel/crumb.rb', line 58

def key
  @key
end

Instance Method Details

Sets link of the breadcrumb. You can supply an optional options hash that will be available on the links so you can pass info when rendering the breadcrumbs manually.

link "My Link", my_link_path
link "Without URL"
link "With Options", my_path, title: "Test", other: "Some other value"


26
27
28
29
30
31
32
33
34
# File 'lib/gretel/crumb.rb', line 26

def link(*args)
  options = args.extract_options!
  text, url = args

  # Transform objects to real paths.
  url = url_for(url) if url
  
  links << Gretel::Link.new(key, text, url, options)
end

Holds all of the breadcrumb’s links as a breadcrumb can have multiple links.



37
38
39
# File 'lib/gretel/crumb.rb', line 37

def links
  @links ||= []
end

#parent(*args) ⇒ Object

Sets or gets the parent breadcrumb. If you supply a parent key and optional arguments, it will set the parent. If nothing is supplied, it will return the parent, if this has been set.

Example:

parent :category, category

Or short, which will infer the key from the model’s ‘model_name`:

parent category


50
51
52
53
54
55
# File 'lib/gretel/crumb.rb', line 50

def parent(*args)
  return @parent if args.empty?
  key = args.shift

  @parent = Gretel::Crumb.new(context, key, *args)
end