Class: Menus::Controller::Menus

Inherits:
Zen::Controller::AdminController show all
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.

Menus Overview

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.

Edit Menu

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.

Examples:

Automatically add a menu item

Zen::Event.listen(:new_menu) do |menu|
  menu.add_menu_item(:name => 'Home', :url => '/', :html_id => 'home')
end

Remove duplicate menu items when editing a menu

Zen::Event.listen(:edit_menu) do |menu|
  urls = []

  menu.items.each do |item|
    if urls.include?(item.url)
      item.destroy
    else
      urls << item.url
    end
  end
end

Since:

Instance Method Summary (collapse)

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".

Since:

  • 0.2a



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
  authorize_user!(:delete_menu)

  post = request.params.dup

  if !post['menu_ids'] or post['menu_ids'].empty?
    message(:error, lang('menus.errors.no_delete'))
    redirect_referrer
  end

  # Time to delete all menus
  post['menu_ids'].each do |id|
    menu = ::Menus::Model::Menu[id]

    next if menu.nil?
    Zen::Event.call(:before_delete_menu, menu)

    begin
      menu.destroy
    rescue => e
      Ramaze::Log.error(e.inspect)
      message(:error, lang('menus.errors.delete') % id)

      redirect_referrer
    end

    Zen::Event.call(:after_delete_menu, menu)
  end

  message(: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.

Parameters:

  • id (Fixnum)

    The ID of the menu to edit.

Since:

  • 0.2a



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)
  authorize_user!(:edit_menu)

  set_breadcrumbs(
    Menus.a(lang('menus.titles.index'), :index),
    @page_title
  )

  if flash[:form_data]
    @menu = flash[:form_data]
  else
    @menu = validate_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.

Since:

  • 0.2a



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
  authorize_user!(:show_menu)

  set_breadcrumbs(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.

Since:

  • 0.2a



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
  authorize_user!(:new_menu)

  set_breadcrumbs(
    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.

Since:

  • 0.2a



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?
    authorize_user!(:edit_menu)

    menu         = validate_menu(post['id'])
    save_action  = :save
    before_event = :before_edit_menu
    after_event  = :after_edit_menu
  else
    authorize_user!(:new_menu)

    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| menu.send("#{k}=", v) }
    Zen::Event.call(before_event, menu)

    menu.save
  rescue => e
    Ramaze::Log.error(e.inspect)
    message(:error, error)

    flash[:form_data]   = menu
    flash[:form_errors] = menu.errors

    redirect_referrer
  end

  Zen::Event.call(after_event, menu)

  message(:success, success)
  redirect(Menus.r(:edit, menu.id))
end