Sha256: bb296484a8a5edbd0afb6ef6e6cc11f66ebbd1af98b812f03dee98302e4a0a1d

Contents?: true

Size: 934 Bytes

Versions: 11

Compression:

Stored size: 934 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, children: [] }
        end

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ecom_core-1.1.24 app/services/ecom/core/menu_service.rb
ecom_core-1.1.23 app/services/ecom/core/menu_service.rb
ecom_core-1.1.22 app/services/ecom/core/menu_service.rb
ecom_core-1.1.21 app/services/ecom/core/menu_service.rb
ecom_core-1.1.20 app/services/ecom/core/menu_service.rb
ecom_core-1.1.19 app/services/ecom/core/menu_service.rb
ecom_core-1.1.18 app/services/ecom/core/menu_service.rb
ecom_core-1.1.17 app/services/ecom/core/menu_service.rb
ecom_core-1.1.16 app/services/ecom/core/menu_service.rb
ecom_core-1.1.15 app/services/ecom/core/menu_service.rb
ecom_core-1.1.14 app/services/ecom/core/menu_service.rb