merb-menus 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/merb-menus/merb_controller.rb +55 -50
- data/lib/merb-menus/version.rb +1 -1
- metadata +2 -2
@@ -1,51 +1,11 @@
|
|
1
1
|
class Merb::Controller
|
2
2
|
before do; Merb::Menus.reset! end
|
3
|
-
before :generate_default_menu
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
Merb::Menus::Menu.new(name).instance_eval &data
|
8
|
-
end
|
9
|
-
|
10
|
-
# ==== Parameters
|
11
|
-
# top<Merb::Menus::Menu>::
|
12
|
-
# sub<Symbol>:: name of desired submenu
|
13
|
-
def get_submenu(top, sub)
|
14
|
-
top.submenus.find {|m| m.name.to_s == sub.to_s}
|
15
|
-
end
|
16
|
-
|
17
|
-
# ==== Parameters
|
18
|
-
# menu<Merb::Menus::Submenu>::
|
19
|
-
# item<Symbol>:: name of desired item
|
20
|
-
def get_item(menu, item)
|
21
|
-
item = menu.items.find{|e| e.name.to_s == item.to_s}
|
22
|
-
end
|
4
|
+
def self.create_menu(name, &data)
|
5
|
+
Merb::Menus::Menu.new(name).instance_eval &data
|
23
6
|
end
|
24
7
|
|
25
|
-
|
26
|
-
self.class.get_submenu(top, sub)
|
27
|
-
end
|
28
|
-
|
29
|
-
def get_item(menu, item)
|
30
|
-
self.class.get_item(menu, item)
|
31
|
-
end
|
32
|
-
|
33
|
-
def menu_item(*args)
|
34
|
-
menu, submenu, item = *args
|
35
|
-
|
36
|
-
if item.nil?
|
37
|
-
item = submenu
|
38
|
-
submenu = menu
|
39
|
-
menu = Merb::Menus.default.name
|
40
|
-
end
|
41
|
-
|
42
|
-
top = Merb::Menus[menu]
|
43
|
-
Merb::Menus.current_menu = top
|
44
|
-
generate_menu(top)
|
45
|
-
submenu = get_submenu(top, submenu)
|
46
|
-
top.current_submenu = submenu
|
47
|
-
submenu.current_item = get_item(submenu, item)
|
48
|
-
end
|
8
|
+
private
|
49
9
|
|
50
10
|
def current_menu
|
51
11
|
Merb::Menus.current_menu
|
@@ -65,24 +25,69 @@ class Merb::Controller
|
|
65
25
|
@__submenu_generation__.item(*args)
|
66
26
|
end
|
67
27
|
|
28
|
+
def get_submenu(top, sub)
|
29
|
+
top.submenus.find {|m| m.name.to_s == sub.to_s}
|
30
|
+
end
|
31
|
+
|
32
|
+
def get_item(submenu, item)
|
33
|
+
submenu.items.find {|e| e.name.to_s == item.to_s}
|
34
|
+
end
|
35
|
+
|
68
36
|
def generate_menu(menu)
|
69
37
|
menu.submenus.each do |m|
|
70
38
|
@__submenu_generation__ = m
|
71
39
|
instance_eval &m.data
|
72
40
|
m.generated!
|
73
41
|
end
|
42
|
+
|
43
|
+
menu_generated!
|
44
|
+
end
|
45
|
+
|
46
|
+
def menu_generated!
|
47
|
+
@__menu_generated__ = true
|
74
48
|
end
|
75
49
|
|
76
|
-
|
50
|
+
def menu_generated?
|
51
|
+
!!@__menu_generated__
|
52
|
+
end
|
77
53
|
|
78
|
-
def
|
79
|
-
|
80
|
-
|
54
|
+
def no_menu!
|
55
|
+
@__no_menu__ = true
|
56
|
+
end
|
57
|
+
|
58
|
+
def no_menu?
|
59
|
+
!!@__no_menu__
|
60
|
+
end
|
61
|
+
|
62
|
+
def _menu_setup(menu, sub, item)
|
63
|
+
raise ArgumentError, "#{menu} is not a Merb::Menu" unless Merb::Menus::Menu === menu
|
64
|
+
|
65
|
+
top = Merb::Menus.current_menu = menu
|
66
|
+
generate_menu(menu)
|
67
|
+
if submenu = top.current_submenu = get_submenu(top, sub)
|
68
|
+
submenu.current_item = get_item(submenu, item)
|
69
|
+
end
|
70
|
+
end
|
81
71
|
|
82
|
-
|
83
|
-
|
84
|
-
|
72
|
+
def menu_item(*args)
|
73
|
+
menu, submenu, item = *args
|
74
|
+
|
75
|
+
if item.nil?
|
76
|
+
item = submenu
|
77
|
+
submenu = menu
|
78
|
+
menu = Merb::Menus.default.name
|
85
79
|
end
|
80
|
+
|
81
|
+
top = Merb::Menus[menu]
|
82
|
+
raise Merb::Menus::NoMenuError, "Menu #{menu} does not exist" unless top
|
83
|
+
|
84
|
+
_menu_setup(top, submenu, item)
|
85
|
+
end
|
86
|
+
|
87
|
+
def generate_default_menu
|
88
|
+
return if no_menu? || menu_generated?
|
89
|
+
|
90
|
+
_menu_setup(Merb::Menus.default, controller_name, action_name)
|
86
91
|
end
|
87
92
|
end
|
88
93
|
|
data/lib/merb-menus/version.rb
CHANGED