Skip to content

Instantly share code, notes, and snippets.

@parndt
Forked from ugisozols/gist:5955777
Last active December 19, 2015 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parndt/5958518 to your computer and use it in GitHub Desktop.
Save parndt/5958518 to your computer and use it in GitHub Desktop.

Additional Menus

This guide will show you how to:

  • configure and use Refinery::Pages::MenuPresenter
  • use a decorator to add a custom method to Refinery::Page class

endprologue.

Background

I have added show_in_footer boolean attribute to pages (this is just a matter of creating and running a migration) and now I want to display these pages in the footer section of my site.

Decorating the Refinery::Page model

First, lets use a decorator to add a class method footer_menu_pages to Refinery::Page class which will return all pages that have show_in_footer set to true. Create a file app/decorators/refinery/models/page_decorator.rb with this content:

Refinery::Page.class_eval do
def self.footer_menu_pages
where :show_in_footer => true
end
end

Creating and configuring the Presenter

Now lets focus on the presener itself. Once instantiated, it is also possible to configure its CSS/HTML using this instance. I will use a Rails helper to instantiate a new instance of Refinery::Pages::MenuPresenter and also configure it there. I’m doing this in a helper because I don’t want to pollute the view with configuration code.

Open ApplicationHelper and add this code:

def footer_menu
menu_items = Refinery::Menu.new(Refinery::Page.footer_menu_pages)

presenter = Refinery::Pages::MenuPresenter.new(menu_items, self) presenter.dom_id = “footer_menu” presenter.css = “footer_menu” presenter.menu_tag = :div presenter

end

We create an instance of Refinery::Menu by passing a collection of footer pages (Refinery::Page.footer_menu_pages) to it. We need it because it will be the data that will be “presented” by Refinery::Pages::MenuPresenter. We assign this instance to a variable called `presenter` in order to configure this presenter. I want my footer menu to be wrapped inside of a div tag instead of the default nav tag and I also want it to have a CSS class and an ID of footer_menu instead of the default menu one:

presenter.dom_id = “footer_menu”
presenter.css = “footer_menu”
presenter.menu_tag = :div

Now that we have configured the menu presenter we need to return it because we’ll have to call .to_html on it in the view later on.

Rendering the Menu in the View

I want my footer menu to appear just above Refinery’s copyright notice. To do that I’ll simply override Refinery’s footer view:

rake refinery:override=refinery/_footer

Next, I’ll add this code above the existing code:

<%= footer_menu.to_html %>

Taking a look at it

Before I fire up Rails server I created 3 pages called First footer page, Second footer page and Third footer page. Each of these pages have the show_in_menu attribute set to false (so that they won’t show up in main menu at the top of the page) and show_in_footer set to true.

Fire up Rails server and go to root page, e.g. http://localhost:3000 – at the bottom of the page you should see links for these 3 pages. Here’s how the generated HTML should look:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment