Module: NoCms::Menus::MenuHelper
- Defined in:
- app/helpers/no_cms/menus/menu_helper.rb
Instance Method Summary collapse
- #active_menu_item_ids(menu) ⇒ Object
- #cache_key_for_menu(menu, options = {}) ⇒ Object
- #conditional_cache_menu(menu, cache_options = {}) ⇒ Object
- #current_menu_items_in_menu(menu) ⇒ Object
- #current_menu_items_in_menu_at_level(menu, level) ⇒ Object
- #current_roots_in_menu(menu) ⇒ Object
- #leaf_menu_item_ids(menu) ⇒ Object
- #link_to_menu_item(menu_item, link_options = {}) ⇒ Object
- #menu_activation_params ⇒ Object
- #menu_item_path(menu_item) ⇒ Object
- #menu_object ⇒ Object
- #menu_object_name ⇒ Object
- #show_children_submenu(menu_item, options = {}) ⇒ Object
- #show_menu(menu_or_uid, options = {}) ⇒ Object
- #show_submenu(menu_item, options = {}) ⇒ Object
Instance Method Details
#active_menu_item_ids(menu) ⇒ Object
3 4 5 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 3 def ..active_for().map{|i| i.self_and_ancestors.pluck(:id)}.flatten.uniq end |
#cache_key_for_menu(menu, options = {}) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 24 def , = {} [:timestamp] ||= [.updated_at, ..maximum(:updated_at)].max.to_i [:active_menu_items] ||= "menus/#{[:initial_cache_key]}-#{.uid}-" + # We use the menu uid "#{[:timestamp]}" + # And the last updated date from the last updated item (or the menu itself) "-#{[:active_menu_items].join("_")}" # And which menu items should be selected end |
#conditional_cache_menu(menu, cache_options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 11 def , = {} = .dup .reverse_merge! initial_cache_key: 'menu-', cache_enabled: NoCms::Menus.cache_enabled if [:cache_enabled] Rails.cache.fetch (, ) do yield end else yield end end |
#current_menu_items_in_menu(menu) ⇒ Object
129 130 131 132 133 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 129 def = NoCms::Menus::Menu.find_by(uid: ) if .is_a? String return NoCms::Menus::MenuItem.none if .nil? ..active_for end |
#current_menu_items_in_menu_at_level(menu, level) ⇒ Object
141 142 143 144 145 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 141 def , level = NoCms::Menus::Menu.find_by(uid: ) if .is_a? String return NoCms::Menus::MenuItem.none if .nil? ().map{|c| c.self_and_ancestors.where(depth: level-1) }.flatten end |
#current_roots_in_menu(menu) ⇒ Object
135 136 137 138 139 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 135 def = NoCms::Menus::Menu.find_by(uid: ) if .is_a? String return NoCms::Menus::MenuItem.none if .nil? , 1 end |
#leaf_menu_item_ids(menu) ⇒ Object
7 8 9 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 7 def ..leaves_with_draft.no_drafts.pluck(:id) end |
#link_to_menu_item(menu_item, link_options = {}) ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 158 def , ={} # Adding link options (turbolink and rel) ['data-no-turbolink'] = true unless .turbolinks [:rel] = .rel unless .rel.blank? # And finally get the link link_to .name, (), end |
#menu_activation_params ⇒ Object
113 114 115 116 117 118 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 113 def { object: , action: "#{params[:controller]}##{params[:action]}" } end |
#menu_item_path(menu_item) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 147 def # If this menu item points to a route in other engine we need that engines route set = .route_set.nil? ? main_app : send(.route_set) # Now we get the url_for info and if it's a hash then add the :only_path option url_info = .url_for url_info[:only_path] = true if url_info.is_a? Hash # When url_info is an ActiveRecord object we have to use polymorphic_path instead of url_for url_info.is_a?(ActiveRecord::Base) ? .polymorphic_path(url_info) : .url_for(url_info) end |
#menu_object ⇒ Object
120 121 122 123 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 120 def return @menu_object unless @menu_object.nil? @menu_object ||= instance_variable_get("@#{}") end |
#menu_object_name ⇒ Object
125 126 127 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 125 def controller.controller_name.singularize end |
#show_children_submenu(menu_item, options = {}) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 85 def , = {} = .dup [:active_menu_items] ||= . = .reverse_merge(initial_cache_key: "children-submenu-#{.id}") ., .merge(initial_cache_key: "#{[:initial_cache_key]}/#{.id}/#{.updated_at.to_i}") do [:leaves_menu_items] ||= . has_children = (![:depth] || (.depth < [:depth]-1)) && # There's no depth option or we are below that depth AND ![:leaves_menu_items].include?(.id) # This menu item is not a leaf .reverse_merge! current_class: 'active', with_children_class: 'has-children' = .delete :submenu_id if [:submenu_class].is_a? Array [:submenu_class] = [:submenu_class].dup = [:submenu_class].shift else = .delete :submenu_class end content_tag(:ul, id: , class: ) do raw .children.no_drafts.includes(:translations).reorder(position: :asc).map{|c| c, }.join end if has_children end end |
#show_menu(menu_or_uid, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 33 def , = {} = if .is_a? NoCms::Menus::Menu else NoCms::Menus::Menu.find_by(uid: ) end return '' if .nil? [:active_menu_items] ||= , do .reverse_merge! menu_class: 'menu' [:leaves_menu_items] ||= content_tag(:ul, class: [:menu_class]) do raw ..roots.no_drafts.includes(:translations).reorder(position: :asc).map{|r| r, }.join end.to_s end end |
#show_submenu(menu_item, options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/helpers/no_cms/menus/menu_helper.rb', line 56 def , = {} return '' if .draft? [:active_menu_items] ||= . = .reverse_merge(initial_cache_key: "submenu-#{.id}") ., .merge(initial_cache_key: "#{[:initial_cache_key]}/#{.id}/#{.updated_at.to_i}") do [:leaves_menu_items] ||= . has_children = (![:depth] || (.depth < [:depth]-1)) && # There's no depth option or we are below that depth AND ![:leaves_menu_items].include?(.id) # This menu item is not a leaf .reverse_merge! current_class: 'active', with_children_class: 'has-children' item_classes = ['menu_item'] item_classes << [:current_class] if [:active_menu_items].include?(.id) item_classes << [:with_children_class] if has_children item_classes += .css_class.split(' ') unless .css_class.blank? content_tag(:li, class: item_classes.join(' ')) do # And finally get the link content = content += (, ) if has_children content end end end |