RefineryCMS: apply bootstrap styles to navigation menu
Asked Answered
S

2

5

I have upgraded Refinery CMS to the newest version (2.1.0), where there is a new approach in rendering the navigation menu :

(in partial _header.html.erb)

<%= Refinery::Pages::MenuPresenter.new(refinery_menu_pages, self).to_html %>

The older version of the same partial :

<%= render(:partial => "/refinery/menu", :locals => {
         :dom_id => 'menu',
         :css => 'menu'
       }) %>

How could I add bootstrap styles to the navbar using MenuPresenter?

Sangfroid answered 17/10, 2013 at 10:8 Comment(4)
I suspect you'll have to edit the HTML to add the required styles. Have you put the Bootstrap css files in your asset pipeline or installed the bootstrap-sass gem?Marcenemarcescent
That's correct , I have them installed. Where can I find the HTML for the nav-bar?Sangfroid
Do a whole directory search for /refinery/_menu.html.erb (or using the haml extension if that's what you're using). It should be in there.Marcenemarcescent
Unfortunately the newest version (2.1.0) does not provide _menu.html.erb anymore. I did rake refinery:override view=refinery/* , but menu file does not appear.Sangfroid
M
12

It can be done, but the solution is not pretty because the Menu Presenter in Refinery 2.1 doesn't support all the right CSS options out of the box. But with a bit of perseverance, this is roughly what to do:

Firstly, create a new blank file here: config/initializers/refinery/monkey_patch_menu_presenter.rb

In this patch file, paste in the contents of this updated version of the menu presenter (published October 2013): menu_presenter.rb

Next, based on the instructions in section 5 of the menu presenter guide, in your app/helpers/application_helper.rb file, add a new method called navigation_menu:

def navigation_menu
  presenter = Refinery::Pages::MenuPresenter.new(refinery_menu_pages, self)
  presenter.css = "navbar-inner"
  presenter.menu_tag = :div
  presenter.list_tag_css = "nav"
  presenter.selected_css = "active"
  presenter.first_css = ""
  presenter.last_css = ""
  presenter.max_depth = 0 # prevents dropdown menus, which don't render correctly
  presenter
end

Finally, in your app/views/refinery/_header.html.erb file (use $ bundle exec rake refinery:override view=refinery/_header if it doesn't exist), replace the call for:

<%= Refinery::Pages::MenuPresenter.new(refinery_menu_pages, self).to_html %>

with:

<div class="navbar">
  <%= navigation_menu.to_html %>
</div>

Ensure that you have the loaded the Bootstrap CSS/JS files and have wrapped the whole page in a <div class="container"> element. Then restart your application for the patch to take affect and hopefully you'll see a familiar bootstrap navigation bar.

Good luck!

Martyn.

Major answered 24/10, 2013 at 22:23 Comment(7)
Your solution looks promising, I am about to implement it and test it. Thank you for the detailed description.Sangfroid
I have tested your solution and it works absolutely fine. I hope your knowledge will help anyone using RefineryCMS v 2.1.0. Once again, thank you.Sangfroid
Do you know of any documentation about the available properties or how to figure them out? The ones defined in the helper function navigation_menu.Harpoon
Unfortunately not - I haven't found where these properties are documented. But if you trace through the code it is not too difficult. Generally the properties are either specifying which HTML tag to use: e.g. <LI> or <DIV>, or which CSS class to apply to the tag.Major
The monkey_patch_menu_presenter.rb worked fine for me in the development environment. In production, however, the patch was not actually applied (or maybe overwritten later). Setting config.cache_classes = false in config/environments/production.rb "solved" the problem. The real fix is to use class_eval as in Refinery::Pages::MenuPresenter.class_eval do ... end. Inspired by #17119157. I don't understand what goes wrong exactly.Itemized
This solution isn't quite a turnkey solution for me. When I try and set the list_tag_css variable to "nav", I get an error from Rails saying it can't find the list_tag_css variable. I am using Refinerycms 2.1.2Radiothorium
Yes, this was a bit of a hack for Refinery 2.1.0; and unfortunately it won't work with other versions of Refinery. A better solution would be to re-write the menu_presenter specifically for BootStrap.Major
R
2

Here a version of above menu_presenter.rb that renders sub-menus as well (This if for Bootstrap 3, RefineryCMS 2.1.1):

require 'active_support/core_ext/string'
require 'active_support/configurable'
require 'action_view/helpers/tag_helper'
require 'action_view/helpers/url_helper'

module Refinery
  module Pages
    class MenuPresenter
      include ActionView::Helpers::TagHelper
      include ActionView::Helpers::UrlHelper
      include ActiveSupport::Configurable

      config_accessor :roots, :menu_tag, :list_tag, :list_item_tag, :css, :dom_id,
                      :max_depth, :selected_css, :first_css, :last_css, :list_tag_css,
                      :link_tag_css
      self.dom_id = 'menu'
      self.css = "collapse navbar-collapse"
      self.menu_tag = :div
      self.list_tag = :ul
      self.list_item_tag = :li
      self.selected_css = 'active'
      self.first_css = :first
      self.last_css = :last
      self.list_tag_css = "nav navbar-nav"

      def roots
        config.roots.presence || collection.roots
      end

      attr_accessor :context, :collection
      delegate :output_buffer, :output_buffer=, :to => :context

      def initialize(collection, context)
        @collection = collection
        @context = context
      end

      def to_html
        render_menu(roots) if roots.present?
      end

      private
      def render_menu(items)
        content_tag(menu_tag, :id => dom_id, :class => css) do
          render_menu_items(items)
        end
      end

      def render_menu_items(menu_items)
        if menu_items.present?
          content_tag(list_tag, :class => list_tag_css) do
            menu_items.each_with_index.inject(ActiveSupport::SafeBuffer.new) do |buffer, (item, index)|
              buffer << render_menu_item(item, index)
            end
          end
        end
      end

      def render_menu_items_children(menu_items)
        if menu_items.present?
          content_tag(list_tag, :class => 'dropdown-menu') do
            menu_items.each_with_index.inject(ActiveSupport::SafeBuffer.new) do |buffer, (item, index)|
              buffer << render_menu_item(item, index)
            end
          end
        end
      end

      def render_menu_item_link_dropdown(menu_item)
        link_to( menu_item.title, context.refinery.url_for(menu_item.url), class: "dropdown-toggle", data: {toggle:"dropdown", target: "#"})
      end

      def render_menu_item_link(menu_item)
        link_to(menu_item.title, context.refinery.url_for(menu_item.url), :class => link_tag_css)
      end

      def render_menu_item(menu_item, index)
        content_tag(list_item_tag, :class => menu_item_css(menu_item, index)) do
          buffer = ActiveSupport::SafeBuffer.new
          # Check for sub menu
          menu_item_children(menu_item).empty? ? buffer << render_menu_item_link(menu_item) : buffer << render_menu_item_link_dropdown(menu_item)
          buffer << render_menu_items_children(menu_item_children(menu_item))
          buffer
        end
      end

      # Determines whether any item underneath the supplied item is the current item according to rails.
      # Just calls selected_item? for each descendant of the supplied item
      # unless it first quickly determines that there are no descendants.
      def descendant_item_selected?(item)
        item.has_children? && item.descendants.any?(&method(:selected_item?))
      end

      def selected_item_or_descendant_item_selected?(item)
        selected_item?(item) || descendant_item_selected?(item)
      end

      # Determine whether the supplied item is the currently open item according to Refinery.
      def selected_item?(item)
        path = context.request.path
        path = path.force_encoding('utf-8') if path.respond_to?(:force_encoding)

        # Ensure we match the path without the locale, if present.
        if %r{^/#{::I18n.locale}/} === path
          path = path.split(%r{^/#{::I18n.locale}}).last.presence || "/"
        end

        # First try to match against a "menu match" value, if available.
        return true if item.try(:menu_match).present? && path =~ Regexp.new(item.menu_match)

        # Find the first url that is a string.
        url = [item.url]
        url << ['', item.url[:path]].compact.flatten.join('/') if item.url.respond_to?(:keys)
        url = url.last.match(%r{^/#{::I18n.locale.to_s}(/.*)}) ? $1 : url.detect{|u| u.is_a?(String)}

        # Now use all possible vectors to try to find a valid match
        [path, URI.decode(path)].include?(url) || path == "/#{item.original_id}"
      end

      def menu_item_css(menu_item, index)
        css = []

        css << selected_css if selected_item_or_descendant_item_selected?(menu_item)
        css << "dropdown" unless menu_item_children(menu_item).empty?
        css << first_css if index == 0
        css << last_css if index == menu_item.shown_siblings.length

        css.reject(&:blank?).presence
      end

      def menu_item_children(menu_item)
        within_max_depth?(menu_item) ? menu_item.children : []
      end

      def within_max_depth?(menu_item)
        !max_depth || menu_item.depth < max_depth
      end

    end
  end
end
Raymundorayna answered 28/12, 2013 at 3:33 Comment(1)
I think the piece of code you placed is not for RefineryCMS 2.1.1 right? It is not released yet I think (github.com/refinery/refinerycms/blob/…) that is 2.1.2Discoverer

© 2022 - 2024 — McMap. All rights reserved.