Module: WrapItRuby::MenuHelper

Extended by:
MenuHelper
Included in:
MenuHelper, ProxyController
Defined in:
app/helpers/wrap_it_ruby/menu_helper.rb

Overview

Loads and queries the menu configuration from the host app’s config/menu.yml file, or from the database when menu_item_class is configured.

Can be used as a module (extend self) or included in controllers/helpers.

The render_menu method requires the view context (ComponentHelper from rails-active-ui must be available), so call it from views/layouts, not as a bare module method.

Instance Method Summary collapse

Instance Method Details

#all_menu_itemsObject



198
199
200
# File 'app/helpers/wrap_it_ruby/menu_helper.rb', line 198

def all_menu_items
  flatten_items(menu_config)
end

#all_proxy_menu_itemsObject



202
203
204
# File 'app/helpers/wrap_it_ruby/menu_helper.rb', line 202

def all_proxy_menu_items
  all_menu_items.select { |item| item['type'] == 'proxy' }
end


15
# File 'app/helpers/wrap_it_ruby/menu_helper.rb', line 15

def menu_config = load_menu

#proxy_pathsObject



206
207
208
209
210
# File 'app/helpers/wrap_it_ruby/menu_helper.rb', line 206

def proxy_paths
  all_menu_items
    .select { |item| item['type'] == 'proxy' }
    .map    { |item| item['route'] }
end

#render_menuObject

Renders the menu entries using rails-active-ui component helpers. Must be called from inside a Menu { } block in the layout.

Supports arbitrary nesting depth:

- Top-level entries with "items" render as Fomantic-UI simple
  dropdown menu items (hover to open).
- Nested entries with "items" render as flyout sub-dropdowns
  (dropdown icon + nested .menu inside an .item).
- Leaf entries render as plain linked menu items.


26
27
28
29
30
# File 'app/helpers/wrap_it_ruby/menu_helper.rb', line 26

def render_menu
  WrapItRuby::MenuHelper.menu_config.each do |entry|
    render_menu_entry(entry, top_level: true)
  end
end

#render_menu_modalsObject

Renders the menu-settings, edit, and add modals. Call this outside the Menu { } block so modals are not nested inside the menu bar.



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/helpers/wrap_it_ruby/menu_helper.rb', line 35

def render_menu_modals
  # Settings modal — sortable tree + add button
  concat tag.div(id: 'menu-settings-modal', class: 'ui large modal') {
    safe_join([
                tag.i(class: 'close icon'),
                tag.div(class: 'header') { tag.i(class: 'bars icon') + ' Menu Settings' },
                tag.div(class: 'scrolling content') {
                  tag.div(id: 'menu-tree-container') { sortable_menu_tree }
                },
                tag.div(class: 'actions') {
                  safe_join([
                              tag.button(class: 'ui green button', onclick: 'menuSettingsShowAdd()') {
                                tag.i(class: 'plus icon') + ' Add Item'
                              }
                            ])
                }
              ])
  }

  # Edit modal — stacked on top of settings modal
  concat tag.div(id: 'menu-edit-modal', class: 'ui small modal') {
    safe_join([
                tag.i(class: 'close icon'),
                tag.div(class: 'header') { 'Edit Menu Item' },
                tag.div(class: 'content') {
                  tag.form(class: 'ui form', id: 'menu-edit-form') {
                    safe_join([
                                tag.input(type: 'hidden', name: 'id', id: 'menu-edit-id'),
                                tag.div(class: 'two fields') {
                                  safe_join([
                                              tag.div(class: 'field') {
                                                tag.label {
                                                  'Label'
                                                } + tag.input(type: 'text', name: 'label', id: 'menu-edit-label')
                                              },
                                              tag.div(class: 'field') {
                                                tag.label {
                                                  'Icon'
                                                } + tag.input(type: 'text', name: 'icon', id: 'menu-edit-icon',
                                                              placeholder: 'e.g. server')
                                              }
                                            ])
                                },
                                tag.div(class: 'two fields', id: 'menu-edit-proxy-fields') {
                                  safe_join([
                                              tag.div(class: 'field') {
                                                tag.label {
                                                  'Route'
                                                } + tag.input(type: 'text', name: 'route', id: 'menu-edit-route',
                                                              placeholder: '/path')
                                              },
                                              tag.div(class: 'field') {
                                                tag.label {
                                                  'URL'
                                                } + tag.input(type: 'text', name: 'url', id: 'menu-edit-url',
                                                              placeholder: 'upstream.example.com')
                                              }
                                            ])
                                },
                                tag.div(class: 'field') {
                                  tag.label { 'Type' } +
                                  tag.select(name: 'item_type', id: 'menu-edit-type', class: 'ui dropdown',
                                             onchange: "menuSettingsToggleProxyFields('menu-edit')") {
                                    safe_join([
                                                tag.option(value: 'group') { 'Group' },
                                                tag.option(value: 'proxy') { 'Proxy' }
                                              ])
                                  }
                                }
                              ])
                  }
                },
                tag.div(class: 'actions') {
                  safe_join([
                              tag.button(class: 'ui red left floated button', onclick: 'menuSettingsDelete()') {
                                tag.i(class: 'trash icon') + ' Delete'
                              },
                              tag.button(class: 'ui button', onclick: "$('#menu-edit-modal').modal('hide')") {
                                'Cancel'
                              },
                              tag.button(class: 'ui green button', onclick: 'menuSettingsSave()') {
                                tag.i(class: 'save icon') + ' Save'
                              }
                            ])
                }
              ])
  }

  # Add modal — stacked on top of settings modal
  concat tag.div(id: 'menu-add-modal', class: 'ui small modal') {
    safe_join([
                tag.i(class: 'close icon'),
                tag.div(class: 'header') { 'Add Menu Item' },
                tag.div(class: 'content') {
                  tag.form(class: 'ui form', id: 'menu-add-form') {
                    safe_join([
                                tag.div(class: 'field') {
                                  tag.label { 'Type' } +
                                  tag.select(name: 'item_type', id: 'menu-add-type', class: 'ui dropdown',
                                             onchange: "menuSettingsToggleProxyFields('menu-add')") {
                                    safe_join([
                                                tag.option(value: 'group') { 'Group' },
                                                tag.option(value: 'proxy') { 'Proxy' }
                                              ])
                                  }
                                },
                                tag.div(class: 'two fields') {
                                  safe_join([
                                              tag.div(class: 'field') {
                                                tag.label {
                                                  'Label'
                                                } + tag.input(type: 'text', name: 'label', id: 'menu-add-label')
                                              },
                                              tag.div(class: 'field') {
                                                tag.label {
                                                  'Icon'
                                                } + tag.input(type: 'text', name: 'icon', id: 'menu-add-icon',
                                                              placeholder: 'e.g. server')
                                              }
                                            ])
                                },
                                tag.div(class: 'two fields', id: 'menu-add-proxy-fields', style: 'display:none') {
                                  safe_join([
                                              tag.div(class: 'field') {
                                                tag.label {
                                                  'Route'
                                                } + tag.input(type: 'text', name: 'route', id: 'menu-add-route',
                                                              placeholder: '/path')
                                              },
                                              tag.div(class: 'field') {
                                                tag.label {
                                                  'URL'
                                                } + tag.input(type: 'text', name: 'url', id: 'menu-add-url',
                                                              placeholder: 'upstream.example.com')
                                              }
                                            ])
                                },
                                tag.div(class: 'field') {
                                  tag.label { 'Parent' } +
                                  tag.select(name: 'parent_id', id: 'menu-add-parent', class: 'ui dropdown') {
                                    safe_join([
                                                tag.option(value: '') { 'Root (top level)' },
                                                *menu_group_options
                                              ])
                                  }
                                }
                              ])
                  }
                },
                tag.div(class: 'actions') {
                  safe_join([
                              tag.button(class: 'ui button', onclick: "$('#menu-add-modal').modal('hide')") {
                                'Cancel'
                              },
                              tag.button(class: 'ui green button', onclick: 'menuSettingsCreate()') {
                                tag.i(class: 'plus icon') + ' Add'
                              }
                            ])
                }
              ])
  }
end

#reset_menu_cache!Object



212
213
214
# File 'app/helpers/wrap_it_ruby/menu_helper.rb', line 212

def reset_menu_cache!
  @menu_config = nil
end