Sha256: 3a5cbf858a429609766cc2330abafaac95eaffe738ca89d69ae2eccec829bdc6

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

# encoding: utf-8

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

    protected

    def menu_item_candidates
      routes = Rails.application.routes
      menu_items
        .map { |item| [item, routes.recognize_path(instance_eval(&item.path))] }
        .select { |_item, routing| routing[:controller] == params[:controller] }
    end

    def find_menu_candidate
      menu_item_candidates
        .select { |item, routing| yield(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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pages_core-3.4.3 app/helpers/admin/menu_helper.rb
pages_core-3.4.2 app/helpers/admin/menu_helper.rb