Module: Edgar::MenuHelper

Includes:
MenuConfig
Included in:
EdgarHelper
Defined in:
app/helpers/edgar/menu_helper.rb

Overview

draw Edgar menu based on application's menu configuration at config/edgar/menu_config.rb.

Edgar::MenuConfig should have 'top' module-function which returns arrays each entry is one of the followings:

  • item which contains link_to arg

  • controller menu list which contains controller name

    • special entry is '_separator' which is not controller name, rather generates just menu separator.

See test/dummy/config/edgar/menu_config.rb as an example.

SEE ALSO

draw_menu

method to draw menu

FILE

config/initializer/menu.rb

menu configuration at application

Instance Method Summary (collapse)

Instance Method Details

- (Object) draw_controller_menu(name, route_proxy, menu_items) (private)

draw controller menu for arg

INPUTS

name

menu name

route_proxy

for url_for() method calling

menu_items

array of string(menu item name)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/edgar/menu_helper.rb', line 61

def draw_controller_menu(name, route_proxy, menu_items)
  link_to(v(name.to_s), '#') +
  (:ul) do
    out = ''
    for menu_item in menu_items do
      out += (:li) do
        case menu_item
        when '_separator'
          '<hr>'.html_safe
          # <hr> background doesn't work as I expected so that
          # use <div> with border color
          #'<div class=separator>x</div>'.html_safe
        else
          ctrl_str  = menu_item
          if permitted_on?(ctrl_str)
            # NOTE: controller string begins with '/' below.  This ensures
            # top name space.  See Rails API url_for().  For example,
            # Album::Book page, url is like /PREFIX/album/books/....
            # If there is no beginning '/', User controler page means Album::User.
            # This is not expected behavior at menu.
            link_to(
                "#{ctrl_str.camelize}Controller".constantize.new.send(:human_name),
                url_for(
                    controller:   '/' + ctrl_str,
                    only_path:    true))
          end
        end
      end
    end
    out.html_safe
  end
end

- (Object) draw_item(*link_to_args) (private)

draw menu item

INPUTS

link_to_args

link_to args



47
48
49
50
51
52
53
# File 'app/helpers/edgar/menu_helper.rb', line 47

def draw_item(*link_to_args)
  (:li) do
    link_to(
        v(link_to_args[0]),
        *link_to_args[1, link_to_args.size - 1])
  end
end

- (Object) draw_menu



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/edgar/menu_helper.rb', line 23

def draw_menu
  (:ul, :id=>'edgar_menu', :class=>'edgar_menu') do
    out = ''
    for menu in Edgar::MenuConfig.top
      out += (:li) do
        case menu[0]
        when :item
          draw_item(*menu[1])
        when :controller
          draw_controller_menu(menu[1], menu[2], menu[3])
        else
          "unknown menu type: #{menu[0]}"
        end
      end
    end
    out.html_safe
  end
end