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
-
#build_item_class(i, options, active_ids) ⇒ Object
Builds css class fot one menu item.
-
#build_items(item, limit, index, hierarchical = false, root_path = "") ⇒ Object
Recursive building menu items.
- #build_items_with_css(item, limit, index, css_c) ⇒ Object
-
#build_menu(initial_id, position, limit = 1, with_nav = true, nav_class = "navmenu", hierarchical = false) ⇒ Object
(also: #create_menu)
Exceptions.
-
#build_menu_by_name(initial_name, limit = 1) ⇒ Object
Builds menu tree for specified arguments, returns html.
-
#build_menu_extended(menu_parent_id, options = {}) ⇒ Object
get “/*menu_path/:menu_id” => “content#show” get “/:menu_id” => “content#show”.
-
#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.
-
#build_menu_items_extended(i, index, active_ids = [], options) ⇒ Object
Builds menu starting from given menu element (real menu only) - displays all its children.
- #build_menu_with_css(initial_id, position, limit = 1, css_c = false) ⇒ Object
-
#isMenu?(obj) ⇒ Boolean
Raises IsNotAMokioMenuErrorr if obj isn’t a Mokio::Menu object.
-
#menu_content(menu, limit = nil) ⇒ Object
Returns active contents added to menu.
-
#menu_content_all(menu) ⇒ Object
Returns all contents added to menu.
-
#menu_content_titles(menu, limit = nil) ⇒ Object
Returns active contents titles added to menu.
-
#menu_locale_root_id ⇒ Object
Returns menu root id for active locale, raises IsNotMenuRootError when root was not found.
-
#menu_root_id(name) ⇒ Object
Returns menu root id for given name, raises IsNotMenuRootError when root was not found.
-
#menu_slug(menu) ⇒ Object
Returns menus slug.
-
#menu_static_modules(menu, limit = nil) ⇒ Object
Returns static_modules added to menu.
-
#menu_static_modules_titles(menu, limit = nil) ⇒ Object
Returns static_modules titles added to menu.
-
#set_options_defaults(options) ⇒ Object
Sets default values for build_menu_extended.
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, , active_ids) item_class = [] item_class << [:item_class] item_class << i.css_class if i.has_children? item_class << [:item_with_children_class] else item_class << [:item_without_children_class] end item_class << [: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:
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 (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 (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 (, = {}) () html = "" html << "<nav #{"class='#{[:nav_class]}'" if [:nav_class]} id='menuMain' #{[:nav_html] if [:nav_html]} >" if [:with_nav] html << "<div class='#{[:ul_wrapper_class]}'>" unless [:ul_wrapper_class].nil? html << "<ul #{"class='#{[:ul_class]}'" if [:ul_class]}>" begin = Mokio::Menu.find() if [:include_menu_parent] html << (, 1, .ancestor_ids + [:active_ids], ) elsif ![:children_content] .children.order_default.each do |i| html << (i, 1, i.ancestor_ids + [:active_ids], ) end else html << (, 1, .ancestor_ids + [:active_ids], ) end rescue => e MOKIO_LOG.error "BUILD MENU ERROR: #{e}" end html << "</ul>" html << "</div>" unless [:ul_wrapper_class].nil? html << "</nav>" if [: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 (, lang_code, = {}) lang = Mokio::Lang.find_by_shortname(lang_code) = Mokio::Menu.where(lang_id: lang.id, name: ) unless lang.blank? (.first.id, ) unless .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 (i, index, active_ids = [], ) return "" if index > [:limit] html = "" if i.visible && i.active item_class = build_item_class(i, , active_ids) html << "<li #{"class='#{item_class}'" unless item_class.blank?} >" locale_prefix = "/#{I18n.locale.to_s}" if [:with_locale] locale_prefix ||= '' if i.external_link.blank? html << "<a #{"class='#{[:a_class]}'" if [:a_class]} href='#{locale_prefix}#{i.real_slug([:hierarchical])}'>#{i.name}</a>" else html << "<a #{"class='#{[:a_class]}'" if [: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 << (item_child, index + 1, active_ids, ) end content_item_class = [item_class, [:content_item_class]].compact.join(" ") i.contents.displayed.order_default.each do |content| next if (([:content_type].blank? || ([:content_type].exclude?(content.type.to_s))) && (![:all_content_types])) items_html << "<li #{"class='#{content_item_class}'" unless content_item_class.blank?}>" items_html << "<a #{"class='#{[:a_class]}'" if [: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='#{[:ul_nested_wrapper_class]}'>" unless [:ul_nested_wrapper_class].nil? html << "<ul #{"class='#{[:ul_nested_class]}'" if [:ul_nested_class]}>" html << items_html html << "</ul>" html << "</div>" unless [: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 (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
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 |
#menu_content(menu, limit = nil) ⇒ Object
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 (, limit = nil) isMenu?() contents = .contents.active unless limit contents ||= .contents.active.limit(limit) contents end |
#menu_content_all(menu) ⇒ Object
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 () isMenu?() .contents end |
#menu_content_titles(menu, limit = nil) ⇒ Object
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 (, limit = nil) isMenu?() contents = (, limit) titles = [] contents.each do |c| titles << c.title end titles end |
#menu_locale_root_id ⇒ Object
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 begin Mokio::Menu.find_by_name(I18n.locale).id rescue ActiveRecord::RecordNotFound, NoMethodError raise Exceptions::IsNotMenuRootError.new(:name, I18n.locale) end end |
#menu_root_id(name) ⇒ Object
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 (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 |
#menu_slug(menu) ⇒ Object
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 () isMenu?() .slug end |
#menu_static_modules(menu, limit = nil) ⇒ Object
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 (, limit = nil) isMenu?() modules = .static_modules unless limit modules ||= .static_modules.limit(limit) modules end |
#menu_static_modules_titles(menu, limit = nil) ⇒ Object
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 (, limit = nil) isMenu?() modules = (, 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 () [:limit] = 999999 unless .has_key? :limit [:include_menu_parent] = false unless .has_key? :include_menu_parent [:hierarchical] = true unless .has_key? :hierarchical [:with_nav] = true unless .has_key? :with_nav [:nav_class] = "nav_menu" unless .has_key? :nav_class [:active_class] = "active" unless .has_key? :active_class [:content_type] = "" unless .has_key? :content_type [:all_content_types] = false unless .has_key? :all_content_types [:with_locale] = false unless .has_key? :with_locale [:active_ids] = [] unless .has_key? :active_ids [:nav_html] = '' unless .has_key? :nav_html [:children_content] = false unless .has_key? :children_content end |