0

I have a module that should render a menu depending on the user roles. We used to use cancancan gem but since we got a new specification it's being deprecated.

I have an association that goes like this. A many_to_many relationship with User and Role, another many_to_many relationship with Role and Menu, so a User has a nested many_to_many relationship with Menu.

What I'm trying to do is render a menu depending on the user role. so I have this helper module:

module MenuHelper
      # ! MENU MAPPING ===============================
      MENU_NAMES = {
        orders: '<%= main_menu_tree Spree.t(:orders), icon: "shopping-cart", sub_menu: "orders", url: "#sidebar-orders" %>',
        workflows: '<%= tab *Spree::BackendConfiguration::WORKFLOW_TABS, label: Spree.t(:workflows),  icon: "flash" %>',
      }
      # ! MENU MAPPING ===============================

      # this will return menu names
      # @params {ARRAY} menu_names are the allowed menu that the user can access by its role
      # @returns {ARRAY}
      def user_menus(menu_names = [])
        samp_arr = []
        menu_names.each do |m_n|
          samp_arr << MENU_NAMES[:"#{m_n.downcase}"]
        end
        samp_arr
      end
    end

and on my shared/_menu.html.erb I have this.

<%  spree_current_user.menus.pluck(:name).each do |n|%>
  <ul>
    <%= n%>
  </ul>
<% end %>

on the view though it will turn into a string. I expected this behavior but is there a way to evaluate this as a method? since main_menu_tree and tab is just a method or maybe there is a better way to solve this?

1 Answer 1

1

As you have menu_names you can create a new template (we name it as partial) for each menu, see the example:

app/
  views/
    menus/
      _menu_name_1.html.erb
      _menu_name_2.html.erb

If you create these partials, you should be able to do something like:

<%  spree_current_user.menus.pluck(:name).each do |menu_name| %>
  <ul>
    <%= render "menus/#{menu_name}" %>
  </ul>
<% end %>

It will concatenate the name of menu with the path of the file to render and all should work fine!

Sign up to request clarification or add additional context in comments.

5 Comments

ohhhhh. this will work. I think? but let me try. thank you for this.
Let me know if it helps :)
render "menus/#{menu_name}" is enough
@aRtoo Did it works? Don't forget to accept the answer to help other people ;)
@DimitriusLachi yes this works but I had created a work around so I wont have to create partials. but ill accept this. thanks much sir. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.