ienders-tastymenu 1.0.0

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.
@@ -0,0 +1,25 @@
1
+ = tastymenu
2
+
3
+ == DESCRIPTION:
4
+
5
+ tastymenu adds a helper method for generating menus (could be dropdown, etc, depending on your CSS/Javascript).
6
+ Please note that this plugin borrows heavily from obrie's http://github.com/pluginaweek/menu_helper/.
7
+
8
+ == INSTALLATION:
9
+
10
+ $ gem sources -a http://gems.github.com/ (if you havn't already, which is unlikely)
11
+ $ gem install ienders-tastymenu
12
+
13
+ == USAGE:
14
+
15
+ In say, your main layout erb file:
16
+
17
+ tastymenu do |main|
18
+ main.menu :users
19
+ main.menu :orders
20
+ main.menu :about do |about|
21
+ about.menu :faq, '/faq'
22
+ about.menu :contact_us
23
+ end
24
+ main.menu :search, 'Search!', 'http://www.google.com', :class => 'search'
25
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gemspec|
6
+ gemspec.name = "tastymenu"
7
+ gemspec.files = FileList['{lib}/**/*'] + %w(init.rb Rakefile README.txt)
8
+ gemspec.summary = "Tasty Menu generation. Nice and easy."
9
+ gemspec.description = "Tasty Menu generation. One stop shop for easy nested menus."
10
+ gemspec.email = "ian.enders@gmail.com"
11
+ gemspec.homepage = "http://github.com/ienders/tastymenu"
12
+ gemspec.authors = ["Ian Enders"]
13
+ end
14
+ rescue LoadError
15
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
16
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'tastymenu'
@@ -0,0 +1,12 @@
1
+ require 'tastymenu/course'
2
+ require 'tastymenu/item'
3
+
4
+ module Tastymenu
5
+ def tastymenu(html_options = {}, &block)
6
+ Tastymenu::Course.new(@controller, nil, html_options, &block).html
7
+ end
8
+ end
9
+
10
+ ActionController::Base.class_eval do
11
+ helper Tastymenu
12
+ end
@@ -0,0 +1,48 @@
1
+ module Tastymenu
2
+ class Course
3
+ include ActionView::Helpers::TagHelper
4
+
5
+ attr_reader :request_controller, :parent_menu, :menus
6
+ delegate :[], :[]=, :to => '@html_options'
7
+
8
+ def initialize(request_controller, parent_menu, html_options = {})
9
+ @html_options = html_options.symbolize_keys
10
+ @parent_menu = parent_menu
11
+ @request_controller = request_controller
12
+ @menus = []
13
+ self[:class] = "#{self[:class]} menu menu-#{level}".strip
14
+ yield self if block_given?
15
+ end
16
+
17
+ def level
18
+ @level ||= begin
19
+ level = 1
20
+ menu = parent_menu
21
+ while menu
22
+ level += 1
23
+ menu = menu.parent_menu
24
+ end
25
+ level
26
+ end
27
+ end
28
+
29
+ def selected?
30
+ @parent_menu && menus.any? {|menu| menu.selected?}
31
+ end
32
+
33
+ def menu(name, url_options = {}, html_options = {}, &block)
34
+ menu = Tastymenu::Item.new(self, name, url_options, html_options, &block)
35
+ @menus << menu
36
+ menu
37
+ end
38
+
39
+ def html
40
+ html_options = @html_options.dup
41
+ html_options[:class] = "#{html_options[:class]} selected".strip if selected?
42
+ content_tag('ul',
43
+ @menus.inject('') {|html, menu| html << menu.html(@menus.last == menu) },
44
+ html_options
45
+ )
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,76 @@
1
+ module Tastymenu
2
+ class Item
3
+ include ActionView::Helpers::TagHelper
4
+ include ActionView::Helpers::UrlHelper
5
+
6
+ attr_reader :name, :url_options, :parent_menu_bar
7
+
8
+ delegate :request_controller, :parent_menu, :level, :to => :parent_menu_bar
9
+ delegate :menu, :to => '@sub_items'
10
+ delegate :[], :[]=, :to => '@html_options'
11
+
12
+ def initialize(parent_menu_bar, name, url_options = {}, html_options = {})
13
+ @options = html_options.slice(:link)
14
+ html_options.except!(:link)
15
+
16
+ @html_options = html_options.symbolize_keys
17
+ @parent_menu_bar = parent_menu_bar
18
+ @name = name.to_s
19
+ @controller = request_controller
20
+
21
+ @content = content_tag(:span, @name.underscore.titleize)
22
+
23
+ url, @url_options = build_url(url_options)
24
+ @content = link_to(@content, url) if @options[:link] != false
25
+
26
+ id_prefix = parent_menu_bar[:id] || parent_menu && parent_menu[:id]
27
+ self[:id] ||= "#{id_prefix}-#{@name}" if id_prefix
28
+ self[:class] = "#{self[:class]} menuitem menuitem-#{level}".strip
29
+
30
+ @sub_items = Tastymenu::Course.new(request_controller, self)
31
+ yield @sub_items if block_given?
32
+ end
33
+
34
+ def selected?
35
+ current_page?(url_options) || @sub_items.selected?
36
+ end
37
+
38
+ def html(last = false)
39
+ html_options = @html_options.dup
40
+ html_options[:class] = "#{html_options[:class]} selected".strip if selected?
41
+ html_options[:class] = "#{html_options[:class]} last".strip if last
42
+ content = @content
43
+ if @sub_items.menus.any?
44
+ content << @sub_items.html
45
+ end
46
+ content_tag('li', content, html_options)
47
+ end
48
+
49
+ private
50
+ def build_url(options = {})
51
+ if options.blank? && route_options = (named_route(name) || named_route(name, false))
52
+ options = route_options
53
+ elsif options.is_a?(Hash)
54
+ options[:controller] ||= controller(options)
55
+ options[:action] ||= name unless options[:controller] == name
56
+ options[:only_path] ||= false
57
+ end
58
+
59
+ url = options.is_a?(Hash) ? url_for(options) : options
60
+ return url, options
61
+ end
62
+
63
+ def controller(options)
64
+ options[:controller] ||
65
+ (begin; "#{name.camelize}Controller".constantize.controller_path; rescue; end) ||
66
+ parent_menu && parent_menu.url_options[:controller] ||
67
+ request_controller.class.controller_path
68
+ end
69
+
70
+ def named_route(name, include_parent = true)
71
+ name = "#{parent_menu.name}_#{name}" if parent_menu && include_parent
72
+ method_name = "hash_for_#{name}_url"
73
+ request_controller.send(method_name) if request_controller.respond_to?(method_name)
74
+ end
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ienders-tastymenu
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ian Enders
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Tasty Menu generation. One stop shop for easy nested menus.
17
+ email: ian.enders@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ files:
25
+ - README.txt
26
+ - Rakefile
27
+ - init.rb
28
+ - lib/tastymenu.rb
29
+ - lib/tastymenu/course.rb
30
+ - lib/tastymenu/item.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/ienders/tastymenu
33
+ licenses:
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: Tasty Menu generation. Nice and easy.
58
+ test_files: []
59
+