Skip to content

Instantly share code, notes, and snippets.

@wakproductions
Created June 15, 2020 16:51
Show Gist options
  • Save wakproductions/bc14555f2050cbb266215b25d3cb5e4e to your computer and use it in GitHub Desktop.
Save wakproductions/bc14555f2050cbb266215b25d3cb5e4e to your computer and use it in GitHub Desktop.
Refinery CMS - Bootstrap Menu Presenter
/ views/refinery/_header.html.slim
nav#logo.navbar.navbar-light.navbar-expand(itemtype = "http://schema.org/Organization")
= link_to(Refinery::Core.site_name, refinery.root_path, class: 'navbar-brand', itemprop: 'url')
= cache [I18n.locale, 'main_menu', @page, Refinery::Page.fast_menu.map(&:updated_at).max] do
= bootstrap_main_menu(refinery_menu_pages, self).to_html
# helpers/application_helper.rb
module ApplicationHelper
def bootstrap_main_menu(items, context)
presenter = Refinery::Pages::BootstrapMainMenuPresenter.new(items, context)
presenter.menu_role = 'navigation'
presenter
end
end
# presenters/refinery/pages/bootstrap_main_menu_presenter.rb
module Refinery
module Pages
class BootstrapMainMenuPresenter < MenuPresenter
config_accessor :dropdown_item_tag,
:dropdown_item_tag_css,
:dropdown_menu_tag,
:dropdown_menu_tag_css,
:list_dropdown_css,
:list_item_tag_css,
:menu_items_css,
:menu_item_link_dropdown_toggle_css,
:menu_item_link_tag_css
self.active_css = :active
self.dropdown_item_tag = :div
self.dropdown_item_tag_css = 'dropdown-item'
self.dropdown_menu_tag = :div
self.dropdown_menu_tag_css = 'dropdown-menu'
self.menu_item_link_dropdown_toggle_css = 'dropdown-toggle'
self.menu_item_link_tag_css = 'nav-link'
self.menu_items_css = 'navbar-nav'
self.list_dropdown_css = 'dropdown'
self.list_item_tag_css = 'nav-item'
self.selected_css = :show
private
def render_menu(items)
if items.present?
content_tag(list_tag, :class => menu_items_css) do
render_menu_items(items)
end
end
end
def render_menu_items(menu_items)
menu_items.each_with_index.inject(ActiveSupport::SafeBuffer.new) do |buffer, (item, index)|
buffer << render_menu_item(item, index)
end
end
def check_for_dropdown_item(menu_item)
( menu_item != roots.first ) && ( menu_item_children(menu_item).count > 0 )
end
def menu_item_css(menu_item, index)
css = []
css << list_item_tag_css
css << active_css if descendant_item_selected?(menu_item)
css << selected_css if selected_item?(menu_item)
css << list_dropdown_css if check_for_dropdown_item(menu_item)
# not using these right now
# css << first_css if index == 0
# css << last_css if index == menu_item.shown_siblings.length
css.reject(&:blank?).presence
end
def menu_item_link_css(menu_item)
css = []
css << menu_item_link_tag_css
css << menu_item_link_dropdown_toggle_css if check_for_dropdown_item(menu_item)
css.reject(&:blank?).presence
end
def menu_item_link(menu_item)
if check_for_dropdown_item(menu_item)
link_to(
menu_item.title,
check_for_dropdown_item(menu_item) ? '#' : context.refinery.url_for(menu_item.url),
class: menu_item_link_css(menu_item),
role: 'button',
'data-toggle': 'dropdown',
'aria-haspopup': true
)
else
link_to(
menu_item.title,
check_for_dropdown_item(menu_item) ? '#' : context.refinery.url_for(menu_item.url),
class: menu_item_link_css(menu_item)
)
end
end
def render_menu_item(menu_item, index)
content_tag(list_item_tag, :class => menu_item_css(menu_item,index)) do
@cont = context.refinery.url_for(menu_item.url)
buffer = ActiveSupport::SafeBuffer.new
buffer << menu_item_link(menu_item)
buffer << render_menu_dropdown(menu_item)
buffer
end
end
def render_dropdown_item_child(menu_item)
content_tag(dropdown_item_tag, :class => dropdown_item_tag_css) do
link_to(menu_item.title, context.refinery.url_for(menu_item.url))
end
end
def render_menu_dropdown(parent_menu_item)
return unless menu_item_children(parent_menu_item).present?
content_tag(dropdown_menu_tag, :class => dropdown_menu_tag_css) do
strings = menu_item_children(parent_menu_item).each_with_object(ActiveSupport::SafeBuffer.new) do |mi, str|
str << render_dropdown_item_child(mi)
end
strings
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment