Class: Menus::Controller::MenuItems
- Inherits:
-
Zen::Controller::AdminController
- Object
- Ramaze::Controller
- Zen::Controller::BaseController
- Zen::Controller::AdminController
- Menus::Controller::MenuItems
- Defined in:
- lib/zen/package/menus/lib/menus/controller/menu_items.rb
Overview
The MenuItems controller allows users to manage menu items of a menu group. In order to manage menu items you must first navigate to a menu group and click the link "Manage menu items" (see Menus for more information). Once you've reached this page you'll see an overview that looks like the image below.
Editing or creating a menu item can be done by either clicking the name of a menu item or by clicking the "Add menu item" button. In both cases you'll end up with a form looking like the one in the following image:
In this form you can specify the following fields:
- Name: the name of the menu item.
- URL: the URL of the menu item.
- Order: a number that indicates the sort order when the menu is built.
- Parent: a parent menu item. This allows you to easily create sub menus.
- HTML class: a space separated list of classes to apply to the HTML element.
- HTML ID: a single ID to apply to the HTML element.
Note that the name, URL, HTML class and HTML ID fields have a maximum length of 255 characters.
Used Permissions
- show_menu_item
- new_menu_item
- edit_menu_item
- delete_menu_item
Events
All events in this controller receive an instance of
Model::MenuItem. Just like other packages the event
delete_menu_item receives an instance that has already been destroyed.
Instance Method Summary (collapse)
-
- (Object) delete
Delete all specified menu items based on the values in the POST array "menu_item_ids".
-
- (Object) edit(menu_id, id)
Allow the user to edit an existing navigation item.
-
- (Object) index(menu_id)
Shows an overview of all the menu items for a menu group.
-
- (Object) new(menu_id)
Allow the user to create a new menu item for the current menu.
-
- (Object) save
Saves an existing menu iten or creates a new one using the supplied POST data.
Instance Method Details
- (Object) delete
Delete all specified menu items based on the values in the POST array "menu_item_ids".
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/zen/package/menus/lib/menus/controller/menu_items.rb', line 244 def delete (:delete_menu_item) post = request.subset(:menu_item_ids) if !post['menu_item_ids'] or post['menu_item_ids'].empty? (:error, lang('menu_items.errors.no_delete')) redirect_referrer end post['menu_item_ids'].each do |id| = ::Menus::Model::MenuItem[id] next if .nil? Zen::Event.call(:before_delete_menu_item, ) begin .destroy rescue => e Ramaze::Log.error(e.inspect) (:error, lang('menu_items.errors.delete') % id) redirect_referrer end Zen::Event.call(:after_delete_menu_item, ) end (:success, lang('menu_items.success.delete')) redirect_referrer end |
- (Object) edit(menu_id, id)
Allow the user to edit an existing navigation item.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/zen/package/menus/lib/menus/controller/menu_items.rb', line 139 def edit(, id) (:edit_menu_item) () ( Menus.a(lang('menus.titles.index'), :index), MenuItems.a(lang('menu_items.titles.index'), :index, ), lang('menu_items.titles.edit') ) @menu_id = if flash[:form_data] @menu_item = flash[:form_data] else @menu_item = (id, ) end render_view(:form) end |
- (Object) index(menu_id)
Shows an overview of all the menu items for a menu group.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/zen/package/menus/lib/menus/controller/menu_items.rb', line 78 def index() (:show_menu_item) = () ( Menus.a(lang('menus.titles.index'), :index), lang('menu_items.titles.index') ) @menu_id = @menu_items = search do |query| ::Menus::Model::MenuItem.search(query) \ .filter(:menu_id => ) \ .order(:id.asc) end @menu_items ||= ::Menus::Model::MenuItem \ .filter(:menu_id => ) \ .order(:id.asc) @menu_items = paginate(@menu_items) end |
- (Object) new(menu_id)
Allow the user to create a new menu item for the current menu.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/zen/package/menus/lib/menus/controller/menu_items.rb', line 109 def new() (:new_menu_item) () ( Menus.a(lang('menus.titles.index'), :index), MenuItems.a(lang('menu_items.titles.index'), :index, ), lang('menu_items.titles.new') ) @menu_id = if flash[:form_data] @menu_item = flash[:form_data] else @menu_item = ::Menus::Model::MenuItem.new end render_view(:form) end |
- (Object) save
Saves an existing menu iten or creates a new one using the supplied POST data.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/zen/package/menus/lib/menus/controller/menu_items.rb', line 173 def save post = request.subset( :id, :parent_id, :name, :url, :sort_order, :html_class, :html_id, :menu_id ) if post['parent_id'].empty? or post['parent_id'] == post['id'] post['parent_id'] = nil end # Determine if we're saving changes made to an existing menu item or # if we're going to create a new one. if post.key?('id') and !post['id'].empty? (:edit_menu_item) = (post['id'], post['menu_id']) save_action = :save before_event = :before_edit_menu_item after_event = :after_edit_menu_item else (:new_menu_item) = ::Menus::Model::MenuItem.new save_action = :new before_event = :before_new_menu_item after_event = :after_new_menu_item end post.delete('id') # Set our notifications success = lang("menu_items.success.#{save_action}") error = lang("menu_items.errors.#{save_action}") # Time to save the data begin post.each { |k, v| .send("#{k}=", v) } Zen::Event.call(before_event, ) .save rescue => e Ramaze::Log.error(e.inspect) (:error, error) flash[:form_data] = flash[:form_errors] = .errors redirect_referrer end Zen::Event.call(after_event, ) (:success, success) redirect(MenuItems.r(:edit, ., .id)) end |