Sha256: 5bb1cdc99783d285e93ffc251c10146b35bfc77910fbb8b9853e184cbfd84138

Contents?: true

Size: 915 Bytes

Versions: 8

Compression:

Stored size: 915 Bytes

Contents

module Ecom
  module Core
    class MenuService
      def fetch_menu(user)
        role_ids = user.user_roles.map(&:id)
        join_sql = <<-SQL
      INNER JOIN ecom_core_menus_user_roles MUR ON MUR.menu_id = ecom_core_menus.id
      INNER JOIN ecom_core_user_roles UR ON MUR.user_role_id = UR.id
        SQL
        menu = Menu.joins(join_sql).where('UR.id IN (?)', role_ids).distinct
        menu_list = menu.select { |m| m.parent.nil? }.each_with_object([]) do |item, list|
          list << { id: item.id, label: item.label, icon: item.icon, route: item.route, children: [] }
        end

        menu_list.each do |ml|
          ml[:children] = menu.reject { |m| m.parent.nil? }.each_with_object([]) do |item, list|
            list << { id: item.id, label: item.label, icon: item.icon, route: item.route } if item.parent_id == ml[:id]
          end
        end
        menu_list
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ecom_core-1.1.9 app/services/ecom/core/menu_service.rb
ecom_core-1.1.8 app/services/ecom/core/menu_service.rb
ecom_core-1.1.7 app/services/ecom/core/menu_service.rb
ecom_core-1.1.6 app/services/ecom/core/menu_service.rb
ecom_core-1.1.5 app/services/ecom/core/menu_service.rb
ecom_core-1.1.4 app/services/ecom/core/menu_service.rb
ecom_core-1.1.3 app/services/ecom/core/menu_service.rb
ecom_core-1.1.2 app/services/ecom/core/menu_service.rb