Module: Ramaze::Helper::Link

Included in:
Action, Paginate::Paginator, Pager
Defined in:
lib/ramaze/helper/link.rb

Overview

Helper::Link is included into the Controller by default

Usage is shown in spec/ramaze/helper/link.rb and the rdocs below.

Instance Method Summary collapse

Instance Method Details

Give it a path with character to split at and one to join the crumbs with. It will generate a list of links that act as pointers to previous pages on this path.

Example:

breadcrumbs('/path/to/somewhere')

# results in this, newlines added for readability:

<a href="/path">path</a>/
<a href="/path/to">to</a>/
<a href="/path/to/somewhere">somewhere</a>

Optionally a href prefix can be specified which generate link names a above, but with the prefix prepended to the href path.

Example:

breadcrumbs('/path/to/somewhere', '/', '/', '/mycontroller/action')

# results in this, newlines added for readability:

<a href="/mycontroller/action/path">path</a>/
<a href="/mycontroller/action/path/to">to</a>/
<a href="/mycontroller/action/path/to/somewhere">somewhere</a>


112
113
114
115
116
117
118
119
120
# File 'lib/ramaze/helper/link.rb', line 112

def breadcrumbs(path, split = '/', join = '/', href_prefix = '')
  atoms = path.split(split).reject{|a| a.empty?}
  crumbs = atoms.inject([]){|s,v| s << [s.last,v]}
  bread = crumbs.map do |a|
    href_path = href_prefix + a*'/'
    A(a[-1], :href=>(href_path))
  end
  bread.join(join)
end

#R(*atoms) ⇒ Object

Builds links out of segments.

Pass it strings, symbols, controllers and it will produce a link out of it. Paths to Controllers are obtained from Global.mapping.

For brevity, the mapping for the example below is following:

{ MC => '/', OC => '/o', ODC => '/od' }

Usage:

R(MC) #=> '/'
R(OC) #=> '/o'
R(ODC) #=> '/od'
R(MC, :foo) #=> '/foo'
R(OC, :foo) #=> '/o/foo'
R(ODC, :foo) #=> '/od/foo'
R(MC, :foo, :bar => :x) #=> '/foo?bar=x'


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ramaze/helper/link.rb', line 56

def R(*atoms)
  args, atoms = atoms.flatten.partition{|a| a.is_a?(Hash) }
  args = args.flatten.inject{|s,v| s.merge!(v) }

  map = Global.mapping.invert

  atoms.map! do |atom|
    if atom.is_a?(Ramaze::Controller)
      map[atom.class]
    else
      map[atom]
    end or atom.to_s
  end

  front = [Global.prefix, *atoms].join('/').squeeze('/')

  if args
    rear = args.inject('?'){|s,(k,v)| s << "#{Rack::Utils.escape(k)}=#{Rack::Utils.escape(v)};"}[0..-2]
    front + rear
  else
    front
  end
end

#Rs(*atoms) ⇒ Object

Uses R with Controller.current as first element.



82
83
84
85
# File 'lib/ramaze/helper/link.rb', line 82

def Rs(*atoms)
  atoms.unshift Controller.current unless atoms.first.is_a?(Controller)
  R(*atoms)
end