Class: Menus::Controller::Menus
- Inherits:
-
Zen::Controller::AdminController
- Object
- Ramaze::Controller
- Zen::Controller::BaseController
- Zen::Controller::AdminController
- Menus::Controller::Menus
- Defined in:
- lib/zen/package/menus/lib/menus/controller/menus.rb
Overview
The Menus controller allows you to create custom menu groups. You can
create large menu structures without having to write a single line of
code. In order to start managing your menus you must first navigate to
/admin/menus (there's a navigation item called "Menus" that you can
also use). Once you've reached this page you'll see an overview of all
your existing menus or a message telling you that no menus were found (if
this is the case). An example of such an overview can be seen in the image
below.
This overview allows you to edit, create or remove menu groups as well as managing the menu items of a group. Editing a group can be done by clicking the name of the group, creating a new one can be done by clicking the button "Add menu". In both cases you'll end up with the form shown in the image below.
In this form you can use the following fields:
- Name (required): the name of the menu.
- Slug: a URL friendly version of the name. If none is specified it will be generated automatically based on the name of the menu.
- HTML class: a space separated list of classes to apply to the HTML
element. The format of this field should match the regular exresspion
^[a-zA-Z\-_0-9\s]*$ - HTML ID: an ID to apply to the HTML element. This value should match
the regular expression
^[a-zA-Z\-_0-9]*$. - Description: the description of the menu.
Note that all fields except the description field have a maximum length of 255 characters.
Used Permissions
- show_menu
- new_menu
- edit_menu
- delete_menu
Events
All events in this controller receive an instance of Model::Menu.
Just like other packages the event delete_menu receives an instance
that has already been destroyed.
Instance Method Summary (collapse)
-
- (Object) delete
Deletes a number of navigation menus based on the supplied primary values.
-
- (Object) edit(id)
Show a form that allows the user to edit the details (such as the name and slug) of a menu group.
-
- (Object) index
Shows an overview of all exisitng menus and a few properties of these groups such as the name, slug and the amount of items in that group.
-
- (Object) new
Show a form that can be used to create a new menu group.
-
- (Object) save
Saves any changes made to an existing menu or creates a new menu.
Instance Method Details
- (Object) delete
Deletes a number of navigation menus based on the supplied primary values. These primary values should be stored in a POST array called "menu_ids".
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 275 276 277 |
# File 'lib/zen/package/menus/lib/menus/controller/menus.rb', line 246 def delete (:delete_menu) post = request.params.dup if !post['menu_ids'] or post['menu_ids'].empty? (:error, lang('menus.errors.no_delete')) redirect_referrer end # Time to delete all menus post['menu_ids'].each do |id| = ::Menus::Model::Menu[id] next if .nil? Zen::Event.call(:before_delete_menu, ) begin .destroy rescue => e Ramaze::Log.error(e.inspect) (:error, lang('menus.errors.delete') % id) redirect_referrer end Zen::Event.call(:after_delete_menu, ) end (:success, lang('menus.success.delete')) redirect_referrer end |
- (Object) edit(id)
Show a form that allows the user to edit the details (such as the name and slug) of a menu group. This method can not be used to manage all menu items for this group.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/zen/package/menus/lib/menus/controller/menus.rb', line 154 def edit(id) (:edit_menu) ( Menus.a(lang('menus.titles.index'), :index), @page_title ) if flash[:form_data] @menu = flash[:form_data] else @menu = (id) end render_view(:form) end |
- (Object) index
Shows an overview of all exisitng menus and a few properties of these groups such as the name, slug and the amount of items in that group.
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/zen/package/menus/lib/menus/controller/menus.rb', line 111 def index (:show_menu) (lang('menus.titles.index')) @menus = search do |query| ::Menus::Model::Menu.search(query).order(:id.asc) end @menus ||= ::Menus::Model::Menu.order(:id.asc) @menus = paginate(@menus) end |
- (Object) new
Show a form that can be used to create a new menu group. Once a menu group has been created users can start adding navigation items to the group.
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/zen/package/menus/lib/menus/controller/menus.rb', line 132 def new (:new_menu) ( Menus.a(lang('menus.titles.index'), :index), @page_title ) @menu = ::Menus::Model::Menu.new render_view(:form) end |
- (Object) save
Saves any changes made to an existing menu or creates a new menu.
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 234 |
# File 'lib/zen/package/menus/lib/menus/controller/menus.rb', line 182 def save post = request.subset( :name, :slug, :description, :html_class, :html_id, :id ) # Determine if we're creating a new group or modifying an existing one. if post.key?('id') and !post['id'].empty? (:edit_menu) = (post['id']) save_action = :save before_event = :before_edit_menu after_event = :after_edit_menu else (:new_menu) = ::Menus::Model::Menu.new save_action = :new before_event = :before_new_menu after_event = :after_new_menu end post.delete('id') success = lang("menus.success.#{save_action}") error = lang("menus.errors.#{save_action}") # Let's see if we can insert/update 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(Menus.r(:edit, .id)) end |