Sha256: 748c7c12c8b1a63475548ba6610a85c68faae498f5cb3edd7f74e3015c6f90f5

Contents?: true

Size: 1.71 KB

Versions: 19

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Admin
  module MenuHelper
    def header_tabs(group)
      tag.ul(class: group.to_s) do
        safe_join(menu_items_for(group).map do |item|
          tag.li do
            path = instance_eval(&item.path)
            link_to(item.label,
                    path,
                    class: (current_menu_item?(item) ? "current" : ""))
          end
        end)
      end
    end

    protected

    def menu_item_candidates
      routed_menu_items
        .select { |_, routing| routing[:controller] == params[:controller] }
    end

    def find_menu_candidate(&block)
      menu_item_candidates
        .select { |item, routing| block.call(item, routing) }
        .try(&:first)
        .try(&:first)
    end

    def menu_candidate_by_current_proc
      find_menu_candidate do |item, _|
        item.options[:current] && instance_eval(&item.options[:current])
      end
    end

    def current_menu_item
      menu_candidate_by_current_proc ||
        find_menu_candidate { |_, r| r[:action] == params[:action] } ||
        find_menu_candidate { |_, r| r[:action] == "index" } ||
        find_menu_candidate { |_, _| true }
    end

    def current_menu_item?(item)
      item == current_menu_item
    end

    def menu_items
      PagesCore::AdminMenuItem.items
    end

    def menu_items_for(group)
      menu_items
        .select { |item| item.group == group }
        .reject do |item|
          item.options[:if] && !instance_eval(&item.options[:if])
        end
    end

    def routed_menu_items
      routes = Rails.application.routes
      menu_items
        .select { |item| item.path.is_a?(Proc) }
        .map { |item| [item, routes.recognize_path(instance_eval(&item.path))] }
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
pages_core-3.12.4 app/helpers/admin/menu_helper.rb
pages_core-3.12.3 app/helpers/admin/menu_helper.rb
pages_core-3.12.2 app/helpers/admin/menu_helper.rb
pages_core-3.12.1 app/helpers/admin/menu_helper.rb
pages_core-3.12.0 app/helpers/admin/menu_helper.rb
pages_core-3.11.3 app/helpers/admin/menu_helper.rb
pages_core-3.11.2 app/helpers/admin/menu_helper.rb
pages_core-3.11.1 app/helpers/admin/menu_helper.rb
pages_core-3.11.0 app/helpers/admin/menu_helper.rb
pages_core-3.10.2 app/helpers/admin/menu_helper.rb
pages_core-3.10.1 app/helpers/admin/menu_helper.rb
pages_core-3.9.2 app/helpers/admin/menu_helper.rb
pages_core-3.9.1 app/helpers/admin/menu_helper.rb
pages_core-3.9.0 app/helpers/admin/menu_helper.rb
pages_core-3.8.3 app/helpers/admin/menu_helper.rb
pages_core-3.8.2 app/helpers/admin/menu_helper.rb
pages_core-3.8.1 app/helpers/admin/menu_helper.rb
pages_core-3.8.0 app/helpers/admin/menu_helper.rb
pages_core-3.7.0 app/helpers/admin/menu_helper.rb