Skip to content

Instantly share code, notes, and snippets.

@ugisozols
Last active December 19, 2015 12:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ugisozols/5955777 to your computer and use it in GitHub Desktop.
Save ugisozols/5955777 to your computer and use it in GitHub Desktop.
Refinery::Pages::MenuPresenter guide draft

Additional menus

This guide will show you how to:

  • configure and use Refinery::Pages::MenuPresenter
  • use a decorator to add 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.

Decoration Refinery::Page class

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 presenter

Now lets focus on the presener itself. When you instantiate it it is also possible to configure its css/html using this instance. I will use a Rails helper to instatite 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 polute 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 div instead of default nav htl tag and I also want it to have footer_menu css class and id instead of default menu one:

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

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

Rendering menu in 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

and add this code above existing one:

<%= footer_menu.to_html %>

Taking a look at it

Before I fire up Rails server I have created 3 pages called First footer page, Second footer page and Third footer page. Each of these pages have show_in_menu attribute set to false (so that it 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 looks like:

@parndt
Copy link

parndt commented Jul 9, 2013

@kalleth also wrote some good text perhaps you can collaborate :)

@parndt
Copy link

parndt commented Jul 9, 2013

Looking good; this is a commonly requested task

@ugisozols
Copy link
Author

Didn't know @kalleth wrote something - would have stolen most of his stuff in the first place :P

@ugisozols
Copy link
Author

We should probably create a whole new section for guides called Presenters.

@parndt
Copy link

parndt commented Jul 9, 2013

Agreed

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