module EffectiveMenusHelper def render_menu(menu, options = {}, &block) menu = Effective::Menu.find_by_title(menu) if menu.kind_of?(String) return "".html_safe if !menu.present? if (effectively_editting? && EffectivePages.authorized?(controller, :edit, menu) rescue false) options[:menu_id] = menu.id form_for(menu, :url => '/') { |form| options[:form] = form } end menu_items = menu.menu_items if menu.new_record? menu_items = menu_items.to_a.sort! { |a, b| a.lft <=> b.lft } end if block_given? && options[:form].blank? render_menu_items(menu_items, options) { yield } else render_menu_items(menu_items, options) end # if options[:for_editor] #else # Rails.cache.fetch(menu) { render_menu_items(menu.menu_items, options) } #end end def render_menu_items(items, options = {}, &block) if options[:form].present? && options[:form].kind_of?(ActionView::Helpers::FormBuilder) == false raise 'Expecting ActionView::Helpers::FormBuilder object for :form => option' end html = '' if options[:form] html << "' end end html.html_safe end private # This is where we actually build out an li item def render_menu_item(item, caret, options) html = "" url = ( if item.menuable.kind_of?(Effective::Page) effective_pages.page_path(item.menuable) elsif item.divider? nil elsif (item.special || '').end_with?('_path') self.send(item.special) rescue nil elsif item.url.present? item.url end ).presence || '#' classes = (item.classes || '').split(' ') classes << 'active' if EffectivePages.menu[:apply_active_class] && request.try(:fullpath) == url classes << 'divider' if item.divider? classes << 'dropdown' if item.dropdown? classes = classes.join(' ') if item.leaf? html << (classes.present? ? "
  • " : "
  • ") if !item.divider? || options[:form] # Show the URL in edit mode, but not normally html << render_menu_item_anchor_tag(item, caret, url) end if options[:form] html << render(:partial => 'admin/menu_items/item', :locals => { :item => item, :form => options[:form] }) end html << "
  • " else html << (classes.present? ? "
  • " : "
  • ") html << render_menu_item_anchor_tag(item, caret, url) if options[:form] html << render(:partial => 'admin/menu_items/item', :locals => { :item => item, :form => options[:form] }) end end html end def render_menu_item_anchor_tag(item, caret, url) new_window_html = (item.new_window? ? " target='_blank'" : "") if item.special == 'destroy_user_session_path' "#{item.title}" elsif item.dropdown? ''.tap do |html| html << "#{item.title}" html << "" if caret html << "" end else "#{item.title}" end end end