How to create a sub-menu full of sub pages?

54 views
Skip to first unread message

lawrenc...@gmail.com

unread,
Apr 8, 2015, 4:53:13 PM4/8/15
to refine...@googlegroups.com
I've read this guide:


which is very helpful, however my client has asked something else of me. They want each page to have a sub-menu that lists the sub-pages of that page. So for instance, if they have 2 main pages, each with 3 sub-pages:

Education
     videos
     practice
     documents

Certification
    tests
    practice
    trials

The main navigation is shown via: 

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

and this appears everywhere on the site, and the should show 2 links: 

Education

Certification

However, when I'm on the Education page, I should see a sub-menu that shows: 

     videos
     practice
     documents

and when I am on Certification, I should see a sub-menu that shows:

    tests
    practice
    trials

My first thought, when I thought there only be 2 sub-menus, was to take the example here: 


and then do 2 versions of that. 

However, the client tells me they want to flexibility to create future pages with future sub-menus, so instead, I opened up: 

vendor/bundle/ruby/2.0.0/bundler/gems/refinerycms-c8d8680bd4f5/pages/app/presenters/refinery/pages/

and I thought I might create a custom version of these methods:

      def render_menu_items(menu_items)
        puts " in render_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_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
          buffer << render_menu_item_link(menu_item)
          buffer << render_menu_items(menu_item_children(menu_item))
          buffer
        end
      end

I am wondering though, how do I query for just the sub-pages of 1 page? 



lawrenc...@gmail.com

unread,
Apr 8, 2015, 5:26:41 PM4/8/15
to refine...@googlegroups.com
Would it be considered bad practice if I simply copied:

./vendor/bundle/ruby/2.0.0/bundler/gems/refinerycms-c8d8680bd4f5/pages/app/presenters/refinery/pages/menu_presenter.rb

to:

app/presenters/refinery/pages/menu_presenter.rb

????

If this is bad practice, then what is the idiomatic way to override menu_presenter.rb?

Philip Arndt

unread,
Apr 8, 2015, 6:25:43 PM4/8/15
to refine...@googlegroups.com
Instead of overriding it, why not use inheritance?

# app/presenters/my_menu_presenter.rb
require "refinery/pages/menu_presenter"

class MyMenuPresenter < Refinery::Pages::MenuPresenter
end

Then you can do whatever you want, and specify to use this one instead in your HTML:

<%= MyMenuPresenter.new(refinery_menu_pages, self).to_html %>
Reply all
Reply to author
Forward
0 new messages