Class: Zen::Package::Menu

Inherits:
Object
  • Object
show all
Includes:
Ramaze::Helper::ACL, Ramaze::Trinity
Defined in:
lib/zen/package/menu.rb

Overview

Class that represents a single navigation item that optionally contains a number of sub elements.

Since:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Ramaze::Helper::ACL

#authorize_user!, #get_permissions, #user_authorized?

Constructor Details

- (Menu) initialize(title, url, options = {}) {|self| ... }

Creates a new instance of the class and optionally processes all sub navigation items.

Parameters:

  • title (String)

    The title or language key of the navigation element.

  • url (String)

    The URL of the navigation element.

  • options (Hash) (defaults to: {})

    A hash containing various options for the element.

Options Hash (options):

  • :permission (Object)

    A symbol containing the name of the permission required to show the navigation item.

Yields:

  • (self)

Since:

  • 0.3



36
37
38
39
40
41
42
# File 'lib/zen/package/menu.rb', line 36

def initialize(title, url, options = {})
  @title, @url = title, url
  @children    = []
  @options     = options

  yield(self) if block_given?
end

Instance Attribute Details

- (Object) children (readonly)

All the child elements of the current navigation element.

Since:

  • 0.3



20
21
22
# File 'lib/zen/package/menu.rb', line 20

def children
  @children
end

- (Object) url (readonly)

A string containing the URL of the current element.

Since:

  • 0.3



17
18
19
# File 'lib/zen/package/menu.rb', line 17

def url
  @url
end

Instance Method Details

- (String|NilClass) html(permissions = [])

Builds the HTML for the current navigation menu using Ramaze::Gestalt.

Parameters:

  • permissions (Array) (defaults to: [])

    An array of permissions for the current user.

Returns:

  • (String|NilClass)

    The HTML of the navigation menu.

Since:

  • 0.3



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/zen/package/menu.rb', line 77

def html(permissions = [])
  # Skip the navigation menu and all it's child elements if the user isn't
  # allowed to view it.
  return if !user_authorized?(@options[:permission])

  g        = Ramaze::Gestalt.new
  children = []

  g.li do
    g.a(title, :href => url, :title => title)

    unless @children.empty?
      @children.each do |child|
        html = child.html(permissions)

        unless html.nil?
          children << html
        end
      end

      unless children.empty?
        g.ul do
          g.out << children
        end
      end
    end
  end

  return g.to_s
end

Adds a new child element to the navigation menu.

See Also:

  • #initialize()

Since:

  • 0.3



50
51
52
# File 'lib/zen/package/menu.rb', line 50

def menu(title, url, options = {}, &block)
  @children << self.class.new(title, url, options, &block)
end

- (String) title

Returns the title of the navigation item. If possible it will be translated, otherwise the original value will be used.

Returns:

  • (String)

Since:

  • 0.3



61
62
63
64
65
66
67
# File 'lib/zen/package/menu.rb', line 61

def title
  begin
    return lang(@title)
  rescue
    return @title
  end
end