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?

R Milushev
  • 4,295
  • 3
  • 27
  • 35
  • 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? – Alex Lynham Oct 17 '13 at 10:23
  • That's correct , I have them installed. Where can I find the HTML for the nav-bar? – R Milushev Oct 17 '13 at 10:46
  • 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. – Alex Lynham Oct 17 '13 at 12:56
  • 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. – R Milushev Oct 17 '13 at 19:31

2 Answers2

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.

Martyn W
  • 188
  • 1
  • 6
  • Your solution looks promising, I am about to implement it and test it. Thank you for the detailed description. – R Milushev Oct 25 '13 at 10:10
  • 1
    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. – R Milushev Oct 25 '13 at 20:50
  • 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`. – givanse Dec 18 '13 at 20:48
  • 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.
  • or
    , or which CSS class to apply to the tag.
  • – Martyn W Dec 19 '13 at 10:09
  • 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 http://stackoverflow.com/questions/17118657/monkey-patching-devise-or-any-rails-gem. I don't understand what goes wrong exactly. – Matthias Berth Jan 25 '14 at 18:52
  • 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.2 – rmiesen Sep 05 '14 at 16:57
  • 1
    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. – Martyn W Sep 06 '14 at 10:59