menu_helper 0.0.1 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,142 +1,142 @@
1
- module PluginAWeek #:nodoc:
2
- module Helpers #:nodoc:
3
- module MenuHelper #:nodoc:
4
- # Represents a group of menus. A menu bar can either be the main menu
5
- # bar or a menu bar nested within a menu.
6
- class MenuBar < HtmlElement
7
- # The menus within this menu bar
8
- attr_reader :menus
9
-
10
- def initialize(request_controller, options = {}, html_options = {}, parent_menu = nil) #:nodoc:
11
- super(html_options)
12
-
13
- options.assert_valid_keys(:auto_set_ids)
14
- options.reverse_merge!(:auto_set_ids => true)
15
- @options = options
16
- @request_controller = request_controller
17
- @parent_menu = parent_menu
18
-
19
- @menus = []
20
-
21
- if @parent_menu
22
- self[:id] ||= "#{@parent_menu[:id]}_menubar"
23
- else
24
- self[:id] ||= 'menubar'
25
- end
26
-
27
- yield self if block_given?
28
- end
29
-
30
- # Creates a new menu in this bar with the given id. The content
31
- # within the menu is, by default, set to a humanized version of the id.
32
- #
33
- # == URLs with routes
34
- #
35
- # If you have named routes setup in the application, the menu attempts
36
- # to automatically figure out what URL you're trying to link to. It
37
- # does this by looking at the id of the menu and the id of its parent.
38
- #
39
- # For example, a menu_bar with the id 'home' and a menu with the id
40
- # 'contact_us' will attempt to look for the following named routes as
41
- # the URL to link to (in order of priority):
42
- # 1. contact_us_url
43
- # 2. home_contact_us_url
44
- #
45
- # Example routes.rb:
46
- # ActionController::Routing::Routes.draw do |map|
47
- # map.with_options(:controller => 'home') do |home|
48
- # home.home '', :action => 'index'
49
- # home.home_search 'search', :action => 'search'
50
- # end
51
- #
52
- # map.with_options(:controller => 'about_us') do |about_us|
53
- # about_us.about_us 'about_us', :action => 'index'
54
- # end
55
- # end
56
- #
57
- # Example menubar:
58
- # menu :home do |home|
59
- # menu :about_us
60
- # #=> Links to about_us_url
61
- # menu :search
62
- # #=> Links to home_search_url
63
- # end
64
- #
65
- # == URLs with url_for
66
- #
67
- # If neither of these named routes are being used, the url will be based
68
- # on the options passed into the menu. The url_options takes the same
69
- # values as +url_for+. By default, the name of the controller will be
70
- # guessed in the following order:
71
- # 1. The id of the menu ('contact_us' => ContactUsController)
72
- # 2. The controller of the parent menu/menu bar
73
- # 3. The request controller
74
- #
75
- # To override the default controller being linked to, you can explicitly
76
- # define it like so:
77
- # menu :contact, 'Contact Us', {}, :controller => 'about_us'
78
- #
79
- # Examples:
80
- # menu :home do |home|
81
- # menu :about, 'About Us', :action => 'about_us'
82
- # #=> Links to {:controller => 'home', :action => 'about_us'}
83
- # menu :who_we_are
84
- # #=> Links to {:controller => 'home', :action => 'who_we_are'}
85
- # menu :contact_us, 'Contact Us', :controller => 'contact', :action => 'index'
86
- # #=> Links to {:controller => 'contact', :action => 'index'}
87
- # menu :search
88
- # #=> Links to {:controller => 'search'}
89
- # end
90
- #
91
- # You can also link to an explicit URL like so:
92
- # menu :search, 'http://www.google.com'
93
- #
94
- # == Turning off links
95
- #
96
- # If you don't want a menu to link to a URL, you can turn off linking like so:
97
- # menu :contact_us, 'Contact Us', {}, :auto_link => false
98
- #
99
- # == Defining content and html attributes
100
- #
101
- # By default, the content within a menu will be set as a humanized
102
- # version of the menu's id. Examples of menus which customize the
103
- # content and/or html attributes are below:
104
- #
105
- # menu :contact
106
- # #=> <li id="contact"><a href="/contact">Contact</a></li>
107
- # menu :contact, 'Contact Us'
108
- # #=> <li id="contact"><a href="/contact">Contact Us</a></li>
109
- # menu :contact, :class => 'pretty'
110
- # #=> <li id="contact" class="pretty"><a href="/contact">Contact</a></li>
111
- # menu :contact, 'Contact Us', :class => 'pretty'
112
- # #=> <li id="contact" class="pretty"><a href="/contact">Contact Us</a></li>
113
- #
114
- # == Submenus
115
- #
116
- # Menus can also have their own submenus by passing in a block. You can
117
- # create submenus in the same manner that the main menus are created.
118
- # For example,
119
- #
120
- # menu :about do |about|
121
- # about.menu :who_we_are
122
- # about.menu :contact_us
123
- # end
124
- def menu(id, *args, &block)
125
- menu = Menu.new(id, @request_controller, @parent_menu, *args, &block)
126
- @menus << menu
127
-
128
- menu
129
- end
130
-
131
- # Builds the actual html for the menu bar
132
- def build
133
- html = @menus.inject('') do |html, menu|
134
- html << menu.build(@menus.last == menu)
135
- end
136
-
137
- html.blank? ? html : content_tag('ul', html, @html_options)
138
- end
139
- end
140
- end
141
- end
1
+ module PluginAWeek #:nodoc:
2
+ module MenuHelper
3
+ # Represents a group of menus. A menu bar can either be the main menu
4
+ # bar or a menu bar nested within a menu.
5
+ class MenuBar < HtmlElement
6
+ # The menus within this menu bar
7
+ attr_reader :menus
8
+
9
+ def initialize(request_controller, options = {}, html_options = {}, parent_menu = nil) #:nodoc:
10
+ super(html_options)
11
+
12
+ options.assert_valid_keys(:auto_set_ids)
13
+ options.reverse_merge!(:auto_set_ids => true)
14
+ @options = options
15
+ @request_controller = request_controller
16
+ @parent_menu = parent_menu
17
+
18
+ @menus = []
19
+
20
+ if @parent_menu
21
+ self[:id] ||= "#{@parent_menu[:id]}_menubar"
22
+ else
23
+ self[:id] ||= 'menubar'
24
+ end
25
+
26
+ yield self if block_given?
27
+ end
28
+
29
+ # Creates a new menu in this bar with the given id. The content
30
+ # within the menu is, by default, set to a humanized version of the id.
31
+ #
32
+ # == URLs with routes
33
+ #
34
+ # If you have named routes setup in the application, the menu attempts
35
+ # to automatically figure out what URL you're trying to link to. It
36
+ # does this by looking at the id of the menu and the id of its parent.
37
+ #
38
+ # For example, a menu_bar with the id 'home' and a menu with the id
39
+ # 'contact_us' will attempt to look for the following named routes as
40
+ # the URL to link to (in order of priority):
41
+ # 1. contact_us_url
42
+ # 2. home_contact_us_url
43
+ #
44
+ # Example routes.rb:
45
+ # ActionController::Routing::Routes.draw do |map|
46
+ # map.with_options(:controller => 'home') do |home|
47
+ # home.home '', :action => 'index'
48
+ # home.home_search 'search', :action => 'search'
49
+ # end
50
+ #
51
+ # map.with_options(:controller => 'about_us') do |about_us|
52
+ # about_us.about_us 'about_us', :action => 'index'
53
+ # end
54
+ # end
55
+ #
56
+ # Example menubar:
57
+ # menu :home do |home|
58
+ # menu :about_us
59
+ # #=> Links to about_us_url
60
+ # menu :search
61
+ # #=> Links to home_search_url
62
+ # end
63
+ #
64
+ # == URLs with url_for
65
+ #
66
+ # If neither of these named routes are being used, the url will be based
67
+ # on the options passed into the menu. The url_options takes the same
68
+ # values as +url_for+. By default, the name of the controller will be
69
+ # guessed in the following order:
70
+ # 1. The id of the menu ('contact_us' => ContactUsController)
71
+ # 2. The controller of the parent menu/menu bar
72
+ # 3. The request controller
73
+ #
74
+ # To override the default controller being linked to, you can explicitly
75
+ # define it like so:
76
+ # menu :contact, 'Contact Us', {}, :controller => 'about_us'
77
+ #
78
+ # Examples:
79
+ # menu :home do |home|
80
+ # menu :about, 'About Us', :action => 'about_us'
81
+ # #=> Links to {:controller => 'home', :action => 'about_us'}
82
+ # menu :who_we_are
83
+ # #=> Links to {:controller => 'home', :action => 'who_we_are'}
84
+ # menu :contact_us, 'Contact Us', :controller => 'contact', :action => 'index'
85
+ # #=> Links to {:controller => 'contact', :action => 'index'}
86
+ # menu :search
87
+ # #=> Links to {:controller => 'search'}
88
+ # end
89
+ #
90
+ # You can also link to an explicit URL like so:
91
+ # menu :search, 'http://www.google.com'
92
+ #
93
+ # == Turning off links
94
+ #
95
+ # If you don't want a menu to link to a URL, you can turn off linking like so:
96
+ # menu :contact_us, 'Contact Us', {}, :auto_link => false
97
+ #
98
+ # == Defining content and html attributes
99
+ #
100
+ # By default, the content within a menu will be set as a humanized
101
+ # version of the menu's id. Examples of menus which customize the
102
+ # content and/or html attributes are below:
103
+ #
104
+ # menu :contact
105
+ # #=> <li id="contact"><a href="/contact">Contact</a></li>
106
+ # menu :contact, 'Contact Us'
107
+ # #=> <li id="contact"><a href="/contact">Contact Us</a></li>
108
+ # menu :contact, :class => 'pretty'
109
+ # #=> <li id="contact" class="pretty"><a href="/contact">Contact</a></li>
110
+ # menu :contact, 'Contact Us', :class => 'pretty'
111
+ # #=> <li id="contact" class="pretty"><a href="/contact">Contact Us</a></li>
112
+ #
113
+ # == Submenus
114
+ #
115
+ # Menus can also have their own submenus by passing in a block. You can
116
+ # create submenus in the same manner that the main menus are created.
117
+ # For example,
118
+ #
119
+ # menu :about do |about|
120
+ # about.menu :who_we_are
121
+ # about.menu :contact_us
122
+ # end
123
+ def menu(id, *args, &block)
124
+ menu = Menu.new(id, @request_controller, @parent_menu, *args, &block)
125
+ @menus << menu
126
+
127
+ menu
128
+ end
129
+
130
+ private
131
+ def tag_name
132
+ 'ul'
133
+ end
134
+
135
+ def content
136
+ @menus.inject('') do |html, menu|
137
+ html << menu.html(@menus.last == menu)
138
+ end
139
+ end
140
+ end
141
+ end
142
142
  end
data/lib/menu_helper.rb CHANGED
@@ -2,77 +2,76 @@ require 'menu_helper/html_element'
2
2
  require 'menu_helper/menu_bar'
3
3
  require 'menu_helper/menu'
4
4
 
5
- module PluginAWeek #:nodoc:
6
- module Helpers #:nodoc:
7
- # Provides a builder for generating html menubars. The structure of the
8
- # menubars/menus is based on lists and should be styled using css.
9
- module MenuHelper
10
- # Creates a new 1st-level menu bar. The first parameter is the menubar's
11
- # configuration options. The second parameter is the menubar's html
12
- # options. Both of these parameters are optional.
13
- #
14
- # Configuration options:
15
- # * <tt>auto_set_id</tt> - Whether or not to automatically add ids to each menu.
16
- #
17
- # Examples:
18
- # menu_bar {}, :id => 'menus', :class => 'pretty' do |main|
19
- # main.menu :home
20
- # main.menu :about_us do |about_us|
21
- # about_us.who_we_are
22
- # about_us.what_we_do
23
- # about_us.where_we_are
24
- # about_us.contact, 'Contact', 'mailto:contact@us.com'
25
- # end
26
- # end
27
- # #=>
28
- # <ul id="menus" class="pretty">
29
- # <li id="about_us">About Us
30
- # <ul id="about_us_menubar">
31
- # <li id="who_we_are"><a href="/about_us/who_we_are">Who We Are</a></li>
32
- # <li id="what_we_do"><a href="/about_us/what_we_do">What We Do</a></li>
33
- # <li id="contact"><a href="mailto:contact@us.com">Contact</a></li>
34
- # </ul>
35
- # </li>
36
- # </ul>
37
- #
38
- # == Menu Selection
39
- #
40
- # The currently selected menu is based on the current page that the user
41
- # is currently on. If the url that the menu links to is the same as the
42
- # current page, then that menu will be selected. This menu uses
43
- # ActionView::Helpers::UrlHelper#current_page? to determine whether or not
44
- # it is the currently selected menu.
45
- #
46
- # If the menu that is selected is nested within another menu, then those
47
- # menus will be selected as well.
48
- #
49
- # A "selected" menu is indicated by an additional class html attribute
50
- # that is added to the list item.
51
- #
52
- # For example, if a submenu is selected, the html generated from the
53
- # above full example would look like so:
54
- #
55
- # <ul id="menus" class="pretty">
56
- # <li id="about_us" class="selected">About Us
57
- # <ul id="about_us_menubar">
58
- # <li id="who_we_are" class="selected"><a href="/about_us/who_we_are">Who We Are</a></li>
59
- # <li id="what_we_do"><a href="/about_us/what_we_do">What We Do</a></li>
60
- # <li id="contact"><a href="mailto:contact@us.com">Contact</a></li>
61
- # </ul>
62
- # </li>
63
- # </ul>
64
- #
65
- # == Menu Creation
66
- #
67
- # For more information about how menus are created, see the documentation
68
- # for MenuBar#menu.
69
- def menu_bar(*args, &block)
70
- MenuBar.new(@controller, *args, &block).build
71
- end
72
- end
73
- end
74
- end
75
-
76
- ActionController::Base.class_eval do
77
- helper PluginAWeek::Helpers::MenuHelper
5
+ module PluginAWeek #:nodoc:
6
+ # Provides a builder for generating html menubars. The structure of the
7
+ # menubars/menus is based on lists and should be styled using css.
8
+ module MenuHelper
9
+ # Creates a new 1st-level menu bar. The first parameter is the menubar's
10
+ # configuration options. The second parameter is the menubar's html
11
+ # options. Both of these parameters are optional.
12
+ #
13
+ # Configuration options:
14
+ # * +auto_set_id+ - Whether or not to automatically add ids to each menu.
15
+ #
16
+ # == Examples
17
+ #
18
+ # menu_bar {}, :id => 'menus', :class => 'pretty' do |main|
19
+ # main.menu :home
20
+ # main.menu :about_us do |about_us|
21
+ # about_us.who_we_are
22
+ # about_us.what_we_do
23
+ # about_us.where_we_are
24
+ # about_us.contact, 'Contact', 'mailto:contact@us.com'
25
+ # end
26
+ # end
27
+ # #=>
28
+ # <ul id="menus" class="pretty">
29
+ # <li id="about_us">About Us
30
+ # <ul id="about_us_menubar">
31
+ # <li id="who_we_are"><a href="/about_us/who_we_are">Who We Are</a></li>
32
+ # <li id="what_we_do"><a href="/about_us/what_we_do">What We Do</a></li>
33
+ # <li id="contact"><a href="mailto:contact@us.com">Contact</a></li>
34
+ # </ul>
35
+ # </li>
36
+ # </ul>
37
+ #
38
+ # == Menu Selection
39
+ #
40
+ # The currently selected menu is based on the current page that the user
41
+ # is currently on. If the url that the menu links to is the same as the
42
+ # current page, then that menu will be selected. This menu uses
43
+ # ActionView::Helpers::UrlHelper#current_page? to determine whether or not
44
+ # it is the currently selected menu.
45
+ #
46
+ # If the menu that is selected is nested within another menu, then those
47
+ # menus will be selected as well.
48
+ #
49
+ # A "selected" menu is indicated by an additional class html attribute
50
+ # that is added to the list item.
51
+ #
52
+ # For example, if a submenu is selected, the html generated from the
53
+ # above full example would look like so:
54
+ #
55
+ # <ul id="menus" class="pretty">
56
+ # <li id="about_us" class="selected">About Us
57
+ # <ul id="about_us_menubar">
58
+ # <li id="who_we_are" class="selected"><a href="/about_us/who_we_are">Who We Are</a></li>
59
+ # <li id="what_we_do"><a href="/about_us/what_we_do">What We Do</a></li>
60
+ # <li id="contact"><a href="mailto:contact@us.com">Contact</a></li>
61
+ # </ul>
62
+ # </li>
63
+ # </ul>
64
+ #
65
+ # == Menu Creation
66
+ #
67
+ # For more information about how menus are created, see the documentation
68
+ # for MenuBar#menu.
69
+ def menu_bar(*args, &block)
70
+ MenuBar.new(@controller, *args, &block).html
71
+ end
72
+ end
73
+ end
74
+
75
+ ActionController::Base.class_eval do
76
+ helper PluginAWeek::MenuHelper
78
77
  end
@@ -0,0 +1 @@
1
+ # Logfile created on Sun May 11 12:49:09 -0400 2008
data/test/test_helper.rb CHANGED
@@ -1,7 +1,5 @@
1
- $:.unshift("#{File.dirname(__FILE__)}/../../../ruby/hash/set_or_append/lib")
2
-
3
1
  # Load the plugin testing framework
4
- $:.unshift("#{File.dirname(__FILE__)}/../../../test/plugin_test_helper/lib")
2
+ $:.unshift("#{File.dirname(__FILE__)}/../../plugin_test_helper/lib")
5
3
  require 'rubygems'
6
4
  require 'plugin_test_helper'
7
5
 
@@ -1,50 +1,74 @@
1
- require File.dirname(__FILE__) + '/../test_helper'
2
-
3
- class HtmlElementTest < Test::Unit::TestCase
4
- class DivElement < PluginAWeek::Helpers::MenuHelper::HtmlElement
5
- def tag_name
6
- 'div'
7
- end
8
- end
9
-
10
- def test_html_options_on_initialization
11
- e = PluginAWeek::Helpers::MenuHelper::HtmlElement.new('class' => 'fancy')
12
- assert_equal 'fancy', e[:class]
13
-
14
- e = PluginAWeek::Helpers::MenuHelper::HtmlElement.new(:class => 'fancy')
15
- assert_equal 'fancy', e[:class]
16
- end
17
-
18
- def test_html_no_content
19
- assert_equal '<></>', PluginAWeek::Helpers::MenuHelper::HtmlElement.new.html
20
- end
21
-
22
- def test_html_with_content
23
- e = DivElement.new
24
- e.instance_eval do
25
- def content
26
- 'hello world'
27
- end
28
- end
29
-
30
- assert_equal '<div>hello world</div>', e.html
31
- end
32
-
33
- def test_html_with_html_options
34
- e = DivElement.new
35
- e[:class] = 'fancy'
36
-
37
- assert_equal '<div class="fancy"></div>', e.html
38
- end
39
-
40
- def test_get_html_option
41
- e = PluginAWeek::Helpers::MenuHelper::HtmlElement.new
42
- assert_nil e[:class]
43
- end
44
-
45
- def test_set_html_option
46
- e = PluginAWeek::Helpers::MenuHelper::HtmlElement.new
47
- e[:float] = 'left'
48
- assert_equal 'left', e[:float]
49
- end
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class HtmlElementByDefaultTest < Test::Unit::TestCase
4
+ def setup
5
+ @element = PluginAWeek::MenuHelper::HtmlElement.new
6
+ end
7
+
8
+ def test_should_generate_an_empty_tag
9
+ assert_equal '<></>', @element.html
10
+ end
11
+
12
+ def test_not_have_any_html_options
13
+ element = PluginAWeek::MenuHelper::HtmlElement.new
14
+ assert_nil element[:class]
15
+ end
16
+ end
17
+
18
+ class HtmlElementTest < Test::Unit::TestCase
19
+ class DivElement < PluginAWeek::MenuHelper::HtmlElement
20
+ def tag_name
21
+ 'div'
22
+ end
23
+ end
24
+
25
+ def test_should_set_html_options_on_initialization
26
+ element = PluginAWeek::MenuHelper::HtmlElement.new(:class => 'fancy')
27
+ assert_equal 'fancy', element[:class]
28
+ end
29
+
30
+ def test_should_symbolize_html_options
31
+ element = PluginAWeek::MenuHelper::HtmlElement.new('class' => 'fancy')
32
+ assert_equal 'fancy', element[:class]
33
+ end
34
+
35
+ def test_should_generate_entire_element_if_content_and_tag_name_specified
36
+ element = DivElement.new
37
+ element.instance_eval do
38
+ def content
39
+ 'hello world'
40
+ end
41
+ end
42
+
43
+ assert_equal '<div>hello world</div>', element.html
44
+ end
45
+
46
+ def test_should_include_html_options_in_element_tag
47
+ element = DivElement.new
48
+ element[:class] = 'fancy'
49
+
50
+ assert_equal '<div class="fancy"></div>', element.html
51
+ end
52
+
53
+ def test_should_save_changes_in_html_options
54
+ element = PluginAWeek::MenuHelper::HtmlElement.new
55
+ element[:float] = 'left'
56
+ assert_equal 'left', element[:float]
57
+ end
58
+ end
59
+
60
+ class HtmlElementWithNoContentTest < Test::Unit::TestCase
61
+ class DivElement < PluginAWeek::MenuHelper::HtmlElement
62
+ def tag_name
63
+ 'div'
64
+ end
65
+ end
66
+
67
+ def setup
68
+ @element = DivElement.new
69
+ end
70
+
71
+ def test_should_generate_empty_tags
72
+ assert_equal '<div></div>', @element.html
73
+ end
50
74
  end