Class: Hackathon::Starter::MenuItem

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/hackathon_starter_navigator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ MenuItem

Returns a new instance of MenuItem.



12
13
14
# File 'app/models/hackathon_starter_navigator.rb', line 12

def initialize params={}
  @children ||= {}
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



11
12
13
# File 'app/models/hackathon_starter_navigator.rb', line 11

def children
  @children
end

#labelObject

Returns the value of attribute label.



8
9
10
# File 'app/models/hackathon_starter_navigator.rb', line 8

def label
  @label
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'app/models/hackathon_starter_navigator.rb', line 10

def parent
  @parent
end

#pathObject

Returns the value of attribute path.



9
10
11
# File 'app/models/hackathon_starter_navigator.rb', line 9

def path
  @path
end

#positionObject

Returns the value of attribute position.



7
8
9
# File 'app/models/hackathon_starter_navigator.rb', line 7

def position
  @position
end

Class Method Details

.head(position) ⇒ Object



77
78
79
80
# File 'app/models/hackathon_starter_navigator.rb', line 77

def head position
  return position.split('/').first if position
  nil
end

.tail(position) ⇒ Object



81
82
83
84
# File 'app/models/hackathon_starter_navigator.rb', line 81

def tail position
  return position.split('/')[1..-1].join('/') if position
  nil
end

Instance Method Details

#add(item) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/hackathon_starter_navigator.rb', line 15

def add item
  if MenuItem.tail(item.position) == ''
    @children[item.position] = item
  else
    head = MenuItem.head item.position
    tail = MenuItem.tail item.position
    item.position = tail
    @children[head] = MenuItem.new unless @children[head]
    @children[head].add item
  end
end

#draw(root = false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/hackathon_starter_navigator.rb', line 26

def draw root=false
  output = []
  if root
    output << "<div class='navbar navbar-default navbar-fixed-top' role='navigation'>"
    output << "<div class='container'>"
    output << "<div class='navbar-header'>"
    output << "<button class='navbar-toggle' type='button' data-toggle='collapse' data-target='.navbar-collapse'>"
    output << "<span class='sr-only'>Toggle Navigation</span>"
    output << "<span class='icon-bar'></span>"
    output << "<span class='icon-bar'></span>"
    output << "<span class='icon-bar'></span>"
    output << "</button>"
    url = Rails.application.routes.url_helpers.send((path.to_s + "_path").to_sym)
    output << "<a href='#{url}' class='navbar-brand'>"
    output << label
    output << "</a>"
    output << "</div>"
    output << "<div class='collapse navbar-collapse'>"
    output << "<ul class='nav navbar-nav'>"
    @children.each do |label,child|
      output << child.draw
    end
    output << "</ul>"
    output << "</div>"
    output << "</div>"
    output << "</div>"
  else
    url = Rails.application.routes.url_helpers.send((path.to_s + "_path").to_sym) rescue path
    if @children.count > 0
      output << "<li class='dropdown'>"
      output << "<a href='#{url}' data-toggle='dropdown'>"
      output << "#{label}<span class='caret'></span>"
      output << "</a>"
      output << "<ul class='dropdown-menu' role='menu'>"
      @children.each do |label,child|
        output << child.draw
      end
      output << "</ul>"
      output << "</li>"
    else
      output << "<li>"
      output << "<a href='#{url}'>#{@label}</a>"
      output << "</li>"
    end
  end
  output.join
end

#to_sObject



73
74
75
# File 'app/models/hackathon_starter_navigator.rb', line 73

def to_s
  "Item(#{@position}): #{label} -> #{path}"
end