Okay. Here is the solution

In ApplicationController:

before_filter do
    @main_menu = Page.where(active: true, show_in_main_menu: 
true).arrange(order: :position)

    def menu_ids(attributes, mids = [])
      attributes.map do |attribute, sub_attributes|
        if sub_attributes.empty?
          mids << attribute.id.to_s
        else
          mids << attribute.id.to_s
          menu_ids(sub_attributes, mids)
        end
      end
      mids
    end
    @menu_ids = menu_ids(@main_menu)
  end

In ApplicationHelper:

def main_menu(attributes)
    attributes.map do |attribute, sub_attributes|
      if sub_attributes.empty?
        if @menu_ids.include?(attribute.parent_id.to_s)
          content_tag(:li) do
            link_to attribute.name.html_safe, page_path(attribute._id)
          end
        end
      else
        if attribute.parent_id == nil
          content_tag(:li, nil, class: "dropdown") do
            begin
              link_to( attribute.name.html_safe, page_path(attribute._id))+
              (icon('angle-double-right') if !attribute.is_root?)+
              content_tag(:ul, nil) do
                main_menu(sub_attributes)
              end
            end.html_safe
          end
        else
          if @menu_ids.include?(attribute.parent_id.to_s)
            content_tag(:li, nil, class: "dropdown") do
              begin
                link_to( attribute.name.html_safe, page_path(attribute._id))+
                (icon('angle-double-right') if !attribute.is_root?)+
                content_tag(:ul, nil) do
                  main_menu(sub_attributes)
                end
              end.html_safe
            end
          end
        end
      end
    end.join.html_safe
  end

And it finally works like I wanted

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/e5932e67-dfd1-40a9-9523-090e408b6924%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to