simple_navigation 1.1

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.
data/Manifest ADDED
@@ -0,0 +1,5 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ init.rb
5
+ lib/simple_navigation.rb
data/README.rdoc ADDED
@@ -0,0 +1,125 @@
1
+ = Simple Navigation
2
+
3
+ Ruby on Rails gem/plugin for drop down/tabbed navigation.
4
+
5
+ == Requirements
6
+
7
+ * Rails 2.3.x (may work with older versions, not tested)
8
+
9
+ == Install
10
+
11
+ Add the following line to your /config/environment.rb file:
12
+
13
+ config.gem "mexpolk-simple_navigation",
14
+ :lib => "simple_navigation",
15
+ :source => "http://gems.github.com"
16
+
17
+ And from the command line run:
18
+
19
+ rake gems:install
20
+
21
+ ===Install as a Plugin
22
+
23
+ If your rather prefer to install it as a plugin, from your application
24
+ directory simply run:
25
+
26
+ script/plugin install git://github.com/mexpolk/simple_navigation.git
27
+
28
+ And you're done!
29
+
30
+ == Usage
31
+
32
+ To create your menus create config/initializers/simple_navigation.rb file like
33
+ this:
34
+
35
+ SimpleNavigation::Builder.config do |map|
36
+ map.navigation :default do |navigation|
37
+
38
+ # Root menu without child elements (menus) that points to /dashboard
39
+ navigation.menu :home, :url => { :controller => "home", :action => "index"}
40
+
41
+ # Root menu with child menus without anchor link
42
+ navigation.menu :contacts do |contacts|
43
+
44
+ # Child menu with many possible urls (or many controllers and actions)
45
+ contacts.menu :list, :url => { :controller => "contacts", :action => "index" } do |contact_list|
46
+
47
+ # This menu will marked as current when you're on the following
48
+ # controllers and actions (including the controller and action
49
+ # specified in the :url option):
50
+ contact_list.connect :controller => "contacts" # ...current on any action from this controller
51
+ contact_list.connect :controller => "people", :except => "new"
52
+ contact_list.connect :controller => "companies", :except => "new"
53
+
54
+ end
55
+
56
+ # Another submenu that points to /person/new
57
+ contacts.menu :new_person, :url => { :controller => "people", :action => "new" }
58
+
59
+ # Another submenu that points to /company/new
60
+ contacts.menu :new_company, :url => { :controller => "companies", :action => "new" }
61
+
62
+ end
63
+
64
+ # Another root menu with nested submenus
65
+ navigation.tab :admin, :url => { :controller => "users", :action => "index" } do |admin|
66
+ admin.menu :users, :url => { :controller => "users", :action => "index" } do |users|
67
+ users.menu :reports, :url => { :controller => "user_reports", :action => "index" } do |reports|
68
+ reports.menu :activity, :url => { :controller => "user_reports", :action => "activity" }
69
+ reports.menu :login_atempts, :url => { :controller => "user_reports", :action => "login_atempts" }
70
+ end
71
+ users.menu :new_user, :url => { :controller => "users", :action => "new" }
72
+ end
73
+ end
74
+
75
+ end
76
+ end
77
+
78
+ To render you newly created menu called :default, in your default layout
79
+ (layout/application.erb):
80
+
81
+ <%= simple_navigation :default %>
82
+
83
+ ==Internationalization (i18n)
84
+
85
+ If you want to use internationalization, set the option :i18n => true like this:
86
+
87
+ SimpleNavigation::Builder.config do |map|
88
+ map.navigation :default, :i18n => true do |navigation|
89
+ ...
90
+ end
91
+ end
92
+
93
+ And add to your config/locales files (e.g. es-MX.yml) the following:
94
+
95
+ es-MX:
96
+ simple_navigation:
97
+ default: # The name of your navigation menu
98
+ home: # The name of your root menu
99
+ title: "Inicio" # The translated title for your root menu
100
+ menus:
101
+ index: "Panel de Control" # The title for index action child menu
102
+ new: "Nueva Página" # The title for new action child menu
103
+
104
+ == License
105
+
106
+ Copyright (c) 2008 Iván "Mexpolk" Torres
107
+
108
+ Permission is hereby granted, free of charge, to any person obtaining
109
+ a copy of this software and associated documentation files (the
110
+ "Software"), to deal in the Software without restriction, including
111
+ without limitation the rights to use, copy, modify, merge, publish,
112
+ distribute, sublicense, and/or sell copies of the Software, and to
113
+ permit persons to whom the Software is furnished to do so, subject to
114
+ the following conditions:
115
+
116
+ The above copyright notice and this permission notice shall be
117
+ included in all copies or substantial portions of the Software.
118
+
119
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
120
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
121
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
122
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
123
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
124
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
125
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('simple_navigation', '1.1') do |e|
6
+ e.description = "Easy navigation menu gem for Ruby on Rails"
7
+ e.url = "http://github.com/mexpolk/simple_navigation"
8
+ e.author = "Ivan Torres"
9
+ e.email = "mexpolk@gmail.com"
10
+ e.ignore_pattern = ["tmp/*", "script/*"]
11
+ e.development_dependencies = []
12
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "simple_navigation"
@@ -0,0 +1,226 @@
1
+ module SimpleNavigation
2
+
3
+ module Helper
4
+
5
+ attr_accessor :current_menu_id
6
+
7
+ def simple_navigation(name)
8
+
9
+ # Load navigation hash
10
+ navigation = SimpleNavigation::Builder.navigation[name.to_sym]
11
+
12
+ # Reset current menu
13
+ self.current_menu_id = nil
14
+
15
+ html_attributes = { :id => navigation[:id],
16
+ :class => 'simple_navigation', :depth => 0 }
17
+
18
+ # Render root menus
19
+ content_tag(:ul,
20
+ navigation[:menus].map{ |menu| render_menu(menu, :depth => 0) },
21
+ html_attributes)
22
+
23
+ end # simple_navigation(name)
24
+
25
+ def render_menu(menu, options = {})
26
+
27
+ # Set default html attributes
28
+ list_html_attributes = { :id => [menu[:id], 'menus'].join('_'), :depth => 0 }
29
+ menu_html_attributes = { :id => menu[:id], :drop_down => false, :class => 'menu' }
30
+
31
+ # Detect menu depth
32
+ list_html_attributes[:depth] = options[:depth] + 1 if options.has_key?(:depth)
33
+
34
+ # Detect if has submenus
35
+ menu_html_attributes.merge!(:drop_down => true) if menu.has_key?(:menus)
36
+
37
+ # Render submenus first so we can detect if current menu
38
+ # is between child menu's
39
+ menus = ''
40
+ menus = content_tag(:ul,
41
+ menu[:menus].map{ |child| render_menu(child, options) },
42
+ list_html_attributes) if
43
+ menu.has_key?(:menus)
44
+
45
+ # Is this menu is the current?
46
+ if current_menu?(menu)
47
+ menu_html_attributes[:class] << ' current'
48
+ self.current_menu_id = menu[:id]
49
+ # Is the current menu under this menu?
50
+ elsif self.current_menu_id
51
+ menu_html_attributes[:class] << ' current_child' if
52
+ self.current_menu_id.to_s.match(/^#{menu[:id]}/)
53
+ end
54
+
55
+ # Render menu
56
+ content_tag(:li,
57
+ render_menu_title(menu) + menus,
58
+ menu_html_attributes)
59
+
60
+ end # render_menu(menu)
61
+
62
+ def render_menu_title(menu)
63
+ title = ''
64
+ if menu[:options][:i18n]
65
+ if menu.has_key?(:title)
66
+ title = t(menu[:translation], :default => menu[:title])
67
+ else
68
+ title = t(menu[:translation], :default => menu[:name].to_s.titleize)
69
+ end
70
+ else
71
+ if menu.has_key?(:title)
72
+ title = menu[:title]
73
+ else
74
+ title = menu[:name].to_s.titleize
75
+ end
76
+ end
77
+ title = link_to(title, url_for(menu[:url])) if menu.has_key?(:url)
78
+ title
79
+ end # render_menu_title(menu)
80
+
81
+ protected
82
+
83
+ def current_menu?(menu)
84
+ current = (controller.params[:controller] == menu[:url][:controller].gsub(/^\//, "")) &&
85
+ (controller.params[:action] == menu[:url][:action])
86
+ if menu.has_key?(:urls)
87
+ (menu[:urls].is_a?(Array) ? menu[:urls] : [menu[:urls]]).each do |controllers|
88
+ (controllers.is_a?(Array) ? controllers : [controllers]).each do |c|
89
+ current |= controller.params[:controller] == c[:controller].gsub(/^\//, "")
90
+ if c.has_key?(:only)
91
+ current &= (c[:only].is_a?(Array) ? c[:only] : [c[:only]]).include?(controller.params[:action])
92
+ end
93
+ if c.has_key?(:except)
94
+ current &= !((c[:except].is_a?(Array) ? c[:except] : [c[:except]]).include?(controller.params[:action]))
95
+ end
96
+ end
97
+ end
98
+ end
99
+ current
100
+ end # current_menu?
101
+ end # Helper
102
+
103
+ class Configuration
104
+
105
+ attr_accessor :navigation
106
+
107
+ def initialize
108
+ self.navigation = {}
109
+ end
110
+
111
+ def config(&block)
112
+ builder = Builder.new
113
+ yield builder
114
+ builder.navigations.each { |tmp| self.navigation[tmp[:name]] = tmp }
115
+ end
116
+
117
+ class Builder
118
+
119
+ attr_accessor :navigations, :prefix
120
+
121
+ def initialize
122
+ self.navigations = []
123
+ end
124
+
125
+ # Create a new navigation
126
+ def navigation(name, options = {}, &block)
127
+ options.merge!(:i18n => false) unless options.has_key?(:i18n)
128
+ navigation = Navigation.new(name, options)
129
+ yield navigation
130
+ self.navigations << navigation.build
131
+ end
132
+
133
+ # Render new navigation
134
+ def build
135
+ { :navigations => navigations }
136
+ end
137
+
138
+ class Navigation
139
+
140
+ attr_accessor :id, :menus, :name, :options, :translation
141
+
142
+ def initialize(name, options = {})
143
+ options.merge!(:i18n => false) unless options.has_key?(:i18n)
144
+ self.translation = name
145
+ self.id = ['simple_navigation', name].join('_')
146
+ self.menus = []
147
+ self.name = name
148
+ self.options = options
149
+ end
150
+
151
+ # Create a new root menu
152
+ def menu(name, *args, &block)
153
+ title = args.first.is_a?(String) ? args.first : nil
154
+ options = args.last.is_a?(::Hash) ? args.last : {}
155
+ options.merge!(:i18n => self.options[:i18n])
156
+ options.merge!(:translation => [self.translation, 'menus'].join('.'))
157
+ options.merge!(:prefix => [self.id, 'menus'].join('_'))
158
+ menu = Menu.new(name, title, options)
159
+ yield menu if block
160
+ self.menus << menu.build
161
+ end
162
+
163
+ # render menu
164
+ def build
165
+ { :id => self.id.to_sym,
166
+ :name => self.name.to_sym,
167
+ :menus => self.menus,
168
+ :options => self.options }
169
+ end
170
+
171
+ class Menu
172
+
173
+ attr_accessor :id, :menus, :name, :options, :title, :translation, :url, :urls
174
+
175
+ def initialize(name, title = nil, options = {})
176
+ self.id = [options[:prefix], name].join('_')
177
+ self.menus = []
178
+ self.name = name
179
+ self.title = title
180
+ self.translation = [options[:translation], name].join('.')
181
+ self.url = options[:url]
182
+ self.urls = []
183
+ options.delete(:translation)
184
+ options.delete(:url)
185
+ self.options = options
186
+ end
187
+
188
+ # Create a new child menu
189
+ def menu(name, *args, &block)
190
+ title = args.first.is_a?(String) ? args.first : nil
191
+ options = args.last.is_a?(::Hash) ? args.last : {}
192
+ options.merge!(:i18n => self.options[:i18n])
193
+ options.merge!(:translation => [self.translation, 'menus'].join('.'))
194
+ options.merge!(:prefix => [self.id, 'menus'].join('_'))
195
+ menu = Menu.new(name, title, options)
196
+ yield menu if block
197
+ self.menus << menu.build
198
+ end
199
+
200
+ def connect(options = {})
201
+ options[:controller] = self.url[:controller] unless options.has_key?(:controller)
202
+ self.urls << options
203
+ end
204
+
205
+ def build
206
+ menu = { :id => self.id.to_sym, :name => self.name.to_sym,
207
+ :options => self.options }
208
+ # Add keys with values only:
209
+ menu.merge!(:menus => self.menus) unless self.menus.empty?
210
+ menu.merge!(:title => self.title) unless self.title.nil?
211
+ menu.merge!(:translation => [self.translation, 'title'].join('.')) if self.options[:i18n] == true
212
+ menu.merge!(:url => self.url) unless self.url.nil?
213
+ menu.merge!(:urls => self.urls) unless self.urls.empty?
214
+ # Return menu hash
215
+ menu
216
+ end
217
+ end # Menu
218
+ end # Navigation
219
+ end # Builder
220
+ end # Configuration
221
+
222
+ Builder = Configuration.new
223
+
224
+ end # SimpleNavigation
225
+
226
+ ActionView::Base.send :include, SimpleNavigation::Helper
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{simple_navigation}
5
+ s.version = "1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Ivan Torres"]
9
+ s.date = %q{2009-08-06}
10
+ s.description = %q{Easy navigation menu gem for Ruby on Rails}
11
+ s.email = %q{mexpolk@gmail.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/simple_navigation.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "init.rb", "lib/simple_navigation.rb", "simple_navigation.gemspec"]
14
+ s.homepage = %q{http://github.com/mexpolk/simple_navigation}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Simple_navigation", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{simple_navigation}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Easy navigation menu gem for Ruby on Rails}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_navigation
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.1"
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Torres
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-06 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Easy navigation menu gem for Ruby on Rails
17
+ email: mexpolk@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/simple_navigation.rb
25
+ files:
26
+ - Manifest
27
+ - README.rdoc
28
+ - Rakefile
29
+ - init.rb
30
+ - lib/simple_navigation.rb
31
+ - simple_navigation.gemspec
32
+ has_rdoc: true
33
+ homepage: http://github.com/mexpolk/simple_navigation
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --line-numbers
39
+ - --inline-source
40
+ - --title
41
+ - Simple_navigation
42
+ - --main
43
+ - README.rdoc
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "1.2"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project: simple_navigation
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Easy navigation menu gem for Ruby on Rails
65
+ test_files: []
66
+