Class: SimpleMenu::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_menu/helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Helper

can be initialized with the items passed through params



5
6
7
8
9
10
# File 'lib/simple_menu/helper.rb', line 5

def initialize(params = {})
  clear
  params.each do |key, options|
    add(key, options[0], options[1], options[2])
  end
end

Instance Method Details

#activeObject



48
49
50
# File 'lib/simple_menu/helper.rb', line 48

def active
  @active
end

#active=(key) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/simple_menu/helper.rb', line 38

def active=(key)
  if key
    @active = key.to_sym
  else
    @active = nil
  end

  self
end

#add(key, label = nil, url = nil, params = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simple_menu/helper.rb', line 17

def add(key, label = nil, url = nil, params = {})
  if key.kind_of? Hash
    key.each do |key, options|
      add(key, options[0], options[1], options[2])
    end
  else
    key = key.to_sym
    if items.has_key? key
      raise("key #{key} has already been taken")
    end

    items[key] = [label, url, params]
  end

  self
end

#clearObject



12
13
14
15
# File 'lib/simple_menu/helper.rb', line 12

def clear
  @items = ActiveSupport::OrderedHash.new
  self
end

#itemsObject



34
35
36
# File 'lib/simple_menu/helper.rb', line 34

def items
  @items
end

#to_sObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/simple_menu/helper.rb', line 52

def to_s
  html = []

  items.each do |key, options|
    if key == active
      html << '<li class="active">'
    else
      html << '<li>'
    end

    html << "<a href=\"#{options[1]}\">#{options[0]}</a>"
    html << '</li>'
  end

  if html.length > 0
    "<ul>#{html.join('')}</ul>".html_safe
  else
    ""
  end
end