Sha256: 231501810e10d9c86b5e88e193229547707f4e205c3d5b0b9e679ca5356bd54b

Contents?: true

Size: 1.52 KB

Versions: 4

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module ActiveElement
  module Components
    # A navigation bar providing links to areas within the application.
    class Navbar
      include Translations

      attr_reader :controller

      def initialize(controller, items: nil, fixed: true)
        @controller = controller
        @items = items
        @fixed = fixed
      end

      def template
        'active_element/components/navbar'
      end

      def locals
        {
          component: self,
          items: items,
          fixed: fixed
        }
      end

      def active_path_class(current_navbar_item:)
        if ActiveMenuLink.new(
          rails_component: RailsComponent.new(Rails),
          navbar_items: items,
          current_path: controller.request.path,
          current_navbar_item: current_navbar_item,
          controller_path: controller.controller_path,
          action_name: controller.action_name
        ).active?
          'active'
        end
      end

      private

      attr_reader :fixed

      def items
        return @items unless @items.nil?

        ActiveElement.eager_load_controllers

        @items ||= user_routes(controller.active_element.current_user).available.select(&:primary?).map do |route|
          { path: route.path, title: route.title, spec: route.spec }
        end
      end

      def user_routes(user)
        ActiveElement::Routes.new(
          permissions: user&.permissions,
          rails_component: ActiveElement::RailsComponent.new(Rails)
        )
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_element-0.0.14 lib/active_element/components/navbar.rb
active_element-0.0.13 lib/active_element/components/navbar.rb
active_element-0.0.12 lib/active_element/components/navbar.rb
active_element-0.0.11 lib/active_element/components/navbar.rb