Module: Mokio::FrontendHelpers::MenuHelper

Defined in:
lib/mokio/frontend_helpers/menu_helper.rb

Overview

Frontend helper methods used with Mokio::Menu objects

Instance Method Summary collapse

Instance Method Details

#build_item_class(i, options, active_ids) ⇒ Object

Builds css class fot one menu item



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 248

def build_item_class(i, options, active_ids)

  item_class = []

  item_class << options[:item_class]
  item_class << i.css_class

  if i.has_children?
    item_class << options[:item_with_children_class]
  else
    item_class << options[:item_without_children_class]
  end

  item_class << options[:active_class] if i.slug == params[:menu_id] || i.slug == request.original_fullpath.match(/(\D+\/{1}|\D+)/)[0].gsub('/', '') || active_ids.include?(i.id)
  item_class.compact.join(" ")
end

#build_items(item, limit, index, hierarchical = false, root_path = "") ⇒ Object

Recursive building menu items

Attributes

  • item - Mokio::Menu object

  • limit - how deep should builder look for children

  • index - how deep is function already



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
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 74

def build_items(item, limit, index, hierarchical  = false, root_path = "")
  return "" if index > limit || !item.children.present?

  html = "<ul class='#{index == 1 ? "menu" : "sub-menu"}'>"
  item.children.order_default.each do |i|
    if i.visible && i.active
      html << "<li class='#{"menu-item-has-children" if i.children.present?} #{"active" if i.slug == params[:menu_id] || i.slug == request.original_fullpath.match(/(\D+\/{1}|\D+)/)[0].gsub('/', '')}'>"

      if i.external_link.blank?
        if hierarchical
          html << "<a href='#{root_path}/#{i.slug}'>#{i.name}</a>"
        else
          html << "<a href='/#{i.slug}'>#{i.name}</a>"
        end
      else
        html << "<a href='#{i.external_link}' rel='#{(i.follow || i.follow.nil?) ? "follow" : "nofollow"}' target='#{i.target.blank? ? '_self' : i.target}'>#{i.name}</a>"
      end

      html << build_items(i, limit, index + 1, hierarchical, root_path + "/#{i.slug}")
      html << "</li>"
    end
  end
  html << "</ul>"
  html.html_safe
end

#build_items_with_css(item, limit, index, css_c) ⇒ Object



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 454

def build_items_with_css(item, limit, index ,css_c)
  return "" if index > limit || !item.children.present?


  html = "<ul class='#{index == 1 ? css_c[0] :  css_c[1]}'>"
  item.children.order_default.each do |i|
    if i.visible && i.active
      html << "<li class='#{ css_c[2]  if i.children.present?} #{"active" if i.slug == params[:menu_id] || i.slug == request.original_fullpath.match(/(\D+\/{1}|\D+)/)[0].gsub('/', '')}'>"

      if i.external_link.blank?
        html << "<a href='/#{i.slug}'>#{i.name}</a>"
      else
        html << "<a href='#{i.external_link}' rel='#{i.follow ? "follow" : "nofollow"}' target='#{i.target.blank? ? '_self' : i.target}'>#{i.name}</a>"
      end
      html << build_items_with_css(i, limit, index + 1,css_c)

      html << "</li>"
    end
  end
  html << "</ul>"
  html.html_safe
end

#build_menu(initial_id, position, limit = 1, with_nav = true, nav_class = "navmenu", hierarchical = false) ⇒ Object Also known as: create_menu

Exceptions

  • IsNotMenuRootError when initial_id is not root’s id



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 27

def build_menu(initial_id, position, limit = 1, with_nav = true, nav_class = "navmenu", hierarchical = false)
  root = Mokio::Menu.find_by_id(initial_id)
  #
  # throw exception when initial_id isn't root's id
  #
  raise Exceptions::IsNotMenuRootError.new(:id, initial_id) if root.ancestry
  html = ""
  html = "<nav class='#{nav_class}' id='menuMain'>" if with_nav

  root.children.each do |item|
    html << build_items(item, limit, 1, hierarchical, "") if (item.name == position || item.id == position) && item.children.present? && item.active
  end

  html << "</nav>"  if with_nav
  html.html_safe
end

#build_menu_by_name(initial_name, limit = 1) ⇒ Object

Builds menu tree for specified arguments, returns html

Attributes

  • initial_name - parent menu position name

  • limit - how deep should builder look for children, count starts after position



53
54
55
56
57
58
59
60
61
62
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 53

def build_menu_by_name(initial_name,limit = 1)
  lang = Mokio::Lang.default
  position = Mokio::Menu.find_by_lang_id_and_name(lang.id,initial_name)

  html = "<nav class='navmenu' id='menuMain'>"
  html << build_items(position, limit, 1) if position.children.present? && position.active
  html << "</nav>"
  html.html_safe

end

#build_menu_extended(menu_parent_id, options = {}) ⇒ Object

get “/*menu_path/:menu_id” => “content#show” get “/:menu_id” => “content#show”



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
197
198
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 172

def build_menu_extended(menu_parent_id, options = {})

  set_options_defaults(options)

  html = ""
  html << "<nav #{"class='#{options[:nav_class]}'" if options[:nav_class]} id='menuMain' #{options[:nav_html] if options[:nav_html]} >" if options[:with_nav]
  html << "<div class='#{options[:ul_wrapper_class]}'>" unless options[:ul_wrapper_class].nil?
  html << "<ul #{"class='#{options[:ul_class]}'" if options[:ul_class]}>"
  begin
    menu_parent = Mokio::Menu.find(menu_parent_id)
    if options[:include_menu_parent]
      html << build_menu_items_extended(menu_parent, 1, menu_parent.ancestor_ids + options[:active_ids], options)
    elsif !options[:children_content]
      menu_parent.children.order_default.each do |i|
        html << build_menu_items_extended(i, 1, i.ancestor_ids + options[:active_ids], options)
      end
    else
      html << build_menu_items_extended(menu_parent, 1, menu_parent.ancestor_ids + options[:active_ids], options)
    end
  rescue => e
    MOKIO_LOG.error "BUILD MENU ERROR: #{e}"
  end
  html << "</ul>"
  html << "</div>" unless options[:ul_wrapper_class].nil?
  html << "</nav>"  if options[:with_nav]
  html.html_safe
end

#build_menu_extended_lang(menu_position_name, lang_code, options = {}) ⇒ Object

Finds proper menu element - based on lang and menu position and calls build_menu_extended for this menu element



106
107
108
109
110
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 106

def build_menu_extended_lang(menu_position_name, lang_code, options = {})
  lang = Mokio::Lang.find_by_shortname(lang_code)
  menu_parent = Mokio::Menu.where(lang_id: lang.id, name: menu_position_name) unless lang.blank?
  build_menu_extended(menu_parent.first.id, options) unless menu_parent.blank?
end

#build_menu_items_extended(i, index, active_ids = [], options) ⇒ Object

Builds menu starting from given menu element (real menu only)

  • displays all its children



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
235
236
237
238
239
240
241
242
243
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 203

def build_menu_items_extended (i, index, active_ids = [], options)

  return "" if index > options[:limit]
  html = ""
  if i.visible && i.active
    item_class = build_item_class(i, options, active_ids)
    html << "<li #{"class='#{item_class}'" unless item_class.blank?} >"

    locale_prefix = "/#{I18n.locale.to_s}" if options[:with_locale]
    locale_prefix ||= ''
    if i.external_link.blank?
      html << "<a #{"class='#{options[:a_class]}'" if options[:a_class]} href='#{locale_prefix}#{i.real_slug(options[:hierarchical])}'>#{i.name}</a>"
    else
      html << "<a #{"class='#{options[:a_class]}'" if options[:a_class]} href='#{i.external_link}' #{"rel='nofollow'" unless i.follow  || i.follow.nil?} #{"target='#{i.target}'" unless (i.target.blank? || i.target == '_self') }>#{i.name}</a>"
    end

    items_html = ""

    i.children.order_default.each do |item_child|
      items_html << build_menu_items_extended(item_child, index + 1, active_ids, options)
    end

    content_item_class = [item_class, options[:content_item_class]].compact.join(" ")
    i.contents.displayed.order_default.each do |content|
      next if ((options[:content_type].blank? || (options[:content_type].exclude?(content.type.to_s))) && (!options[:all_content_types]))
      items_html << "<li #{"class='#{content_item_class}'" unless content_item_class.blank?}>"
      items_html << "<a #{"class='#{options[:a_class]}'" if options[:a_class]} href='#{locale_prefix}#{content.slug}'>#{content.title}</a>" if content.respond_to?("slug")
      items_html << "</li>"
    end

    unless items_html.empty?
      html << "<div class='#{options[:ul_nested_wrapper_class]}'>" unless options[:ul_nested_wrapper_class].nil?
      html << "<ul #{"class='#{options[:ul_nested_class]}'" if options[:ul_nested_class]}>"
      html << items_html
      html << "</ul>"
      html << "</div>" unless options[:ul_nested_wrapper_class].nil?
    end
    html << "</li>"
  end
  html.html_safe
end

#build_menu_with_css(initial_id, position, limit = 1, css_c = false) ⇒ Object



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 478

def build_menu_with_css(initial_id, position, limit = 1, css_c = false)

  if css_c == false
    css_c = ["menu","sub-menu","menu-item-has-children"]
  end

  root = Mokio::Menu.find_by_id(initial_id)
  #
  # throw exception when initial_id isn't root's id
  #
  raise Exceptions::IsNotMenuRootError.new(:id, initial_id) if root.ancestry
  html = ""
  root.children.each do |item|
    html << build_items_with_css(item, limit, 1,css_c) if (item.name == position || item.id == position) && item.children.present? && item.active
  end
  html.html_safe
end

#isMenu?(obj) ⇒ Boolean

Raises IsNotAMokioMenuErrorr if obj isn’t a Mokio::Menu object

Attributes

  • obj - any object

Exceptions

  • IsNotAMokioMenuError when obj is not a Mokio::Menu object

Returns:

  • (Boolean)

Raises:



295
296
297
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 295

def isMenu?(obj)
  raise Exceptions::IsNotAMokioMenuError.new(obj) unless obj.is_a?(Mokio::Menu)
end

Returns active contents added to menu

Attributes

  • menu - Mokio::Menu object

  • limit - Limit contents count

Exceptions

  • IsNotAMokioMenuError when obj is not a Mokio::Menu object



327
328
329
330
331
332
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 327

def menu_content(menu, limit = nil)
  isMenu?(menu)
  contents   = menu.contents.active unless limit
  contents ||= menu.contents.active.limit(limit)
  contents
end

Returns all contents added to menu

Attributes

  • menu - Mokio::Menu object

Exceptions

  • IsNotAMokioMenuError when obj is not a Mokio::Menu object



310
311
312
313
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 310

def menu_content_all(menu)
  isMenu?(menu)
  menu.contents
end

Returns active contents titles added to menu

Attributes

  • menu - Mokio::Menu object

  • limit - Limit contents count

Exceptions

  • IsNotAMokioMenuError when obj is not a Mokio::Menu object



346
347
348
349
350
351
352
353
354
355
356
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 346

def menu_content_titles(menu, limit = nil)
  isMenu?(menu)
  contents = menu_content(menu, limit)
  titles   = []

  contents.each do |c|
    titles << c.title
  end

  titles
end

Returns menu root id for active locale, raises IsNotMenuRootError when root was not found

Exceptions

  • IsNotMenuRootError when initial_id is not root’s id



424
425
426
427
428
429
430
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 424

def menu_locale_root_id
  begin
    Mokio::Menu.find_by_name(I18n.locale).id
  rescue ActiveRecord::RecordNotFound, NoMethodError
    raise Exceptions::IsNotMenuRootError.new(:name, I18n.locale)
  end
end

Returns menu root id for given name, raises IsNotMenuRootError when root was not found

Attributes

  • name - searched menu name

Exceptions

  • IsNotMenuRootError when initial_id is not root’s id



443
444
445
446
447
448
449
450
451
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 443

def menu_root_id(name)
  begin
    root = Mokio::Menu.find_by_name(name)
    raise Exceptions::IsNotMenuRootError.new(:name, name) if root.ancestry
    root.id
  rescue ActiveRecord::RecordNotFound, NoMethodError
    raise Exceptions::IsNotMenuRootError.new(:name, name)
  end
end

Returns menus slug

Attributes

  • menu - Mokio::Menu object

Exceptions

  • IsNotAMokioMenuError when obj is not a Mokio::Menu object



412
413
414
415
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 412

def menu_slug(menu)
  isMenu?(menu)
  menu.slug
end

Returns static_modules added to menu

Attributes

  • menu - Mokio::Menu object

  • limit - Limit static_modules count

Exceptions

  • IsNotAMokioMenuError when obj is not a Mokio::Menu object



370
371
372
373
374
375
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 370

def menu_static_modules(menu, limit = nil)
  isMenu?(menu)
  modules   = menu.static_modules unless limit
  modules ||= menu.static_modules.limit(limit)
  modules
end

Returns static_modules titles added to menu

Attributes

  • menu - Mokio::Menu object

  • limit - Limit static_modules count

Exceptions

  • IsNotAMokioMenuError when obj is not a Mokio::Menu object



389
390
391
392
393
394
395
396
397
398
399
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 389

def menu_static_modules_titles(menu, limit = nil)
  isMenu?(menu)
  modules = menu_static_modules(menu, limit)
  titles  = []

  modules.each do |m|
    titles << m.title
  end

  titles
end

#set_options_defaults(options) ⇒ Object

Sets default values for build_menu_extended



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/mokio/frontend_helpers/menu_helper.rb', line 268

def set_options_defaults(options)
  options[:limit] = 999999 unless options.has_key? :limit
  options[:include_menu_parent] = false unless options.has_key? :include_menu_parent
  options[:hierarchical] = true unless options.has_key? :hierarchical
  options[:with_nav] = true unless options.has_key? :with_nav
  options[:nav_class] = "nav_menu" unless options.has_key? :nav_class
  options[:active_class] =  "active" unless options.has_key? :active_class
  options[:content_type] = "" unless options.has_key? :content_type
  options[:all_content_types] = false unless options.has_key? :all_content_types
  options[:with_locale] = false unless options.has_key? :with_locale
  options[:active_ids] = [] unless options.has_key? :active_ids
  options[:nav_html] = '' unless options.has_key? :nav_html
  options[:children_content] = false unless options.has_key? :children_content
end