Current Page siblings ordered by priority

Hi,

I’m trying to display a Menu of sibling pages ordered by specific page parameter, so far, without success.

I have tried the following:

  • query the sitemap resources: easy ordering but can’t filter by path as it’s not an property
  • get current_resource siblings: it finds no siblings at all, not sure if a bug or bad implementation
  • filter the resources using the select method: works but still can’t apply order
  • navtree extension: also works but is an extra big dependency and still can’t order by param (seems so)

My next step would be creating a helper and performing some magic there, but I would appreciate any suggestion how to do this in a simpler manner.

Thanks!
Miljan

I ended up creating a simple helper that iterate over specific path storing the Resource Link. Additionally it iterate over the subfolder of the same name as the file emulating a one level tree. As for the ordering I will prefix the files names with a number but will remove it later on hooking in to the sitemap.

I used the navtree extension as reference and code example, the result is:

module SidebarHelpers

    def sidebar_menu(path)
        data = {}

        Dir.foreach(path) do |filename|

            # skip hidden files
            next if (filename[0] == '.')
            next if (filename == '..' || filename == '.')

            full_path = File.join(path, filename)

            # skip folders
            next if File.directory?(full_path)

            resource = sitemap.find_resource_by_path(sitemap.file_to_path(full_path))
            childs   = false

            if File.directory?(full_path[/(.*?)(?=\.)/])
                childs = sidebar_menu(full_path[/(.*?)(?=\.)/])
            end

            data.store(resource, childs)

        end

        data
    end

    def discover_title(page = current_page)
        page.data.title || page.render({layout: false}).match(/<h1.+>(.*?)<\/h1>/) do |m|
            m ? m[1] : page.url.split(/\//).last.titleize
        end
    end

end

Requires some coding in the template as well, ERB in this case:

<% sidebar_menu(sitemap.app.root+'/'+sitemap.app.settings.source).each do |page, childs| %>

	<li>
		<%= link_to(discover_title(page), page) %>
		<% if (current_page == page || current_page.parent == page) && childs %>
    	<ul class="uk-list">
    		<% childs.each do |subpage| %>
    		<li><%= link_to(discover_title(subpage[0]), subpage[0]) %></li>
    		<% end %>
    	</ul>
		<% end %>
	</li>

<% end %>