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.
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.
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
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)
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.
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 %>
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:
@kalleth also wrote some good text perhaps you can collaborate :)