Class: Waiter::Menu::Drawer

Inherits:
Object
  • Object
show all
Defined in:
lib/waiter/menu/drawer.rb

Overview

Outputs a Menu into HTML

Instance Method Summary collapse

Constructor Details

#initialize(menu, context) ⇒ Drawer

Returns a new instance of Drawer.



8
9
10
11
# File 'lib/waiter/menu/drawer.rb', line 8

def initialize(menu, context)
  @context = context
  @menu = menu
end

Instance Method Details

#drawObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/waiter/menu/drawer.rb', line 13

def draw
   :div, :id => "menu" do
    out = ActiveSupport::SafeBuffer.new

    @menu.items.each do |(name, menu)|
      menu[:controller] = name unless menu[:controller]

      # Allow an :if option which will only display the menu if true
      if !menu[:if] or menu[:if].call
        # If there is a :selected option, use that instead of trying to determine the selected menu item
        selected = menu_selected?(name, menu[:controller], menu[:action] || menu[:actions], menu.delete(:controllers))

        out << (:span, :class => selected ? "selected" : nil) do
          out2 = ActiveSupport::SafeBuffer.new
          out2 << link_to(I18n.t(@menu.options[:string_prefix].to_s + name.to_s), { :controller => menu.delete(:controller).to_s.absolutify, :action => (menu.delete(:action) || menu.delete(:actions).andand.first) }.merge(menu))
          out2 << draw_submenu(@menu.submenus[name]) if @menu.submenus[name]
          out2
        end
      end
    end

    out << '<br class="clear" />'.html_safe
    out
  end
end

#draw_submenu(submenu) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/waiter/menu/drawer.rb', line 39

def draw_submenu(submenu)
   :table, :class => "dropdown", :cellspacing => 0 do

    out = ActiveSupport::SafeBuffer.new

    submenu.items.each do |(name, menu)|
      out << (:tr) do
         :td do
          controller = menu.delete(:controller) || submenu.options[:controller]
          action =
              menu.delete(:action) || begin
                controller_class = "#{controller}_controller".classify.constantize
                controller_class.action_methods.include?(name.to_s) ? name : nil
              end rescue nil || :index

          link_to I18n.t(submenu.options[:string_prefix].to_s + name.to_s), { :controller => controller.to_s.absolutify, :action => action }.merge(menu)
        end
      end
    end

    out
  end
end