nav 0.2.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +20 -0
- data/README.rdoc +74 -0
- data/lib/nav/builder.rb +89 -0
- data/lib/nav/version.rb +1 -1
- data/lib/nav.rb +4 -96
- metadata +10 -7
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Rudolf Schmidt
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
= Nav
|
2
|
+
Navigation made easy
|
3
|
+
|
4
|
+
== Installation
|
5
|
+
# system wide
|
6
|
+
gem install nav
|
7
|
+
|
8
|
+
# in your Gemfile
|
9
|
+
gem 'nav'
|
10
|
+
|
11
|
+
== Usage
|
12
|
+
=== Basics
|
13
|
+
Nav is automatically available within ActionView::Base and can be used like so:
|
14
|
+
<%= nav do |n| %>
|
15
|
+
<% n.action "Home", "/" %>
|
16
|
+
<% n.action "Login", login_url %>
|
17
|
+
<% end %>
|
18
|
+
|
19
|
+
# When rendered, this generates the following:
|
20
|
+
<ul>
|
21
|
+
<li class="first current first_current">
|
22
|
+
<a href="/">Home</a>
|
23
|
+
</li>
|
24
|
+
<li class="last after_current last_after_current">
|
25
|
+
<a href="/login">Login</a>
|
26
|
+
</li>
|
27
|
+
</ul>
|
28
|
+
|
29
|
+
It will determine the current page you are on and add the :current: class to the <li> element. Also,
|
30
|
+
the element before and after the current element have the classes :before_current: and :after_current:.
|
31
|
+
Additionally, Nav will mark the first and last <li> element of the list as :first: and :last:. Like so,
|
32
|
+
you can apply styles accordingly.
|
33
|
+
|
34
|
+
=== Adding attributes to the nav element
|
35
|
+
You can give any possible html option to nav, like classes, etc.
|
36
|
+
<%= nav :class => 'main' do |n| %>
|
37
|
+
...
|
38
|
+
<% end %>
|
39
|
+
|
40
|
+
# results in
|
41
|
+
<ul class='main'>
|
42
|
+
...
|
43
|
+
</ul>
|
44
|
+
|
45
|
+
=== Adding attributes to an action
|
46
|
+
You are able to add specific behaviour when defining an :action. For instance, if you want to disable a specific
|
47
|
+
element, you may pass :disabled: to it:
|
48
|
+
<%= nav do |n| %>
|
49
|
+
<%= n.action "Disabled", "/", :disabled => true %>
|
50
|
+
<% end %>
|
51
|
+
|
52
|
+
This will add a "disabled" class to the <li> element.
|
53
|
+
|
54
|
+
Sometimes you may want to define yourself which of the elements is the current one. You can pass :current: as
|
55
|
+
option. This can be done in various ways.
|
56
|
+
# Pass true or false to the :current: argument
|
57
|
+
<%= nav do |n| %>
|
58
|
+
<% n.action "My special current", "/", :current => true %>
|
59
|
+
<% end %>
|
60
|
+
|
61
|
+
# Pass a regular expression to the :current: argument. For instance, the following will mark any url as current
|
62
|
+
# that has "account", followed by a "/" and any typee of numeric value:
|
63
|
+
<%= nav do |n| %>
|
64
|
+
<% n.action "My special current", "/", :current => /account\/\d+/ %>
|
65
|
+
<% end %>
|
66
|
+
|
67
|
+
# Lastly, you may also pass a proc:
|
68
|
+
<%= nav do |n| %>
|
69
|
+
<% n.action "My special current", "/", :current => Proc.new { !current_user.nil? } %>
|
70
|
+
<% end %>
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
Copyright (c) 2011 Rudolf Schmidt, released under the MIT license
|
data/lib/nav/builder.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
module Nav
|
2
|
+
class Builder
|
3
|
+
def initialize( template, options = {} )
|
4
|
+
@template, @options = template, options
|
5
|
+
@actions = []
|
6
|
+
|
7
|
+
yield self if block_given?
|
8
|
+
end
|
9
|
+
|
10
|
+
def action( name, options = {}, html_options = {} )
|
11
|
+
wrapper_options = {
|
12
|
+
:current => html_options.delete(:current),
|
13
|
+
:disabled => html_options.delete(:disabled),
|
14
|
+
:force_current => html_options.delete(:force_current),
|
15
|
+
:url => options,
|
16
|
+
:prepend => html_options.delete(:prepend),
|
17
|
+
:append => html_options.delete(:append)
|
18
|
+
}
|
19
|
+
|
20
|
+
@actions << [link_to(name, options, html_options), wrapper_options, options]
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_s
|
24
|
+
content_tag( :ul, actions.html_safe, @options ).html_safe if actions?
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def actions
|
31
|
+
@actions.collect { |a| action_wrapper(*a) }.join
|
32
|
+
end
|
33
|
+
|
34
|
+
def actions?
|
35
|
+
@actions.any?
|
36
|
+
end
|
37
|
+
|
38
|
+
def action_wrapper( contents, options = {}, url_for_options = {} )
|
39
|
+
present = [contents, options, url_for_options] # the one we're dealing with
|
40
|
+
present_index = @actions.index( present )
|
41
|
+
|
42
|
+
before_present = @actions.at( present_index - 1 ) if present_index > 0
|
43
|
+
after_present = @actions.at( present_index + 1 ) if present_index < @actions.size
|
44
|
+
|
45
|
+
classes = []
|
46
|
+
classes << "first" if @actions.first == present
|
47
|
+
classes << "last" if @actions.last == present
|
48
|
+
classes << "current" if current?( *present )
|
49
|
+
classes << "disabled" if options.delete(:disabled)
|
50
|
+
classes << "before_current" if after_present && current?( *after_present )
|
51
|
+
classes << "after_current" if before_present && current?( *before_present )
|
52
|
+
classes << classes.join("_") if classes.any?
|
53
|
+
|
54
|
+
contents = options[:prepend].to_s + contents + options[:append].to_s
|
55
|
+
|
56
|
+
content_tag :li, contents.html_safe, :class => classes.join(" ")
|
57
|
+
end
|
58
|
+
|
59
|
+
def current?( contents, options = {}, url_for_options = {} )
|
60
|
+
current = options[:current]
|
61
|
+
|
62
|
+
is_current = case current
|
63
|
+
when TrueClass then true
|
64
|
+
when Regexp then request_uri.match(current).nil? ? false : true
|
65
|
+
when Proc then current.call
|
66
|
+
else false
|
67
|
+
end
|
68
|
+
|
69
|
+
return true if is_current && !options[:disabled] && options[:force_current]
|
70
|
+
return true if is_current || !url_for_options.is_a?(Symbol) && @template.current_page?(url_for_options) && url_for_options != {} && !options[:disabled]
|
71
|
+
|
72
|
+
false
|
73
|
+
end
|
74
|
+
|
75
|
+
def request_uri
|
76
|
+
@request_uri or @request_uri = @template.request.request_uri
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
def content_tag( *args )
|
81
|
+
@template.content_tag( *args ).html_safe
|
82
|
+
end
|
83
|
+
|
84
|
+
def link_to( *args )
|
85
|
+
@template.link_to( *args ).html_safe
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
data/lib/nav/version.rb
CHANGED
data/lib/nav.rb
CHANGED
@@ -1,104 +1,12 @@
|
|
1
|
-
require_relative "nav/version"
|
2
|
-
|
3
1
|
module Nav
|
4
2
|
|
5
|
-
|
6
|
-
builder = Builder.new( self, options )
|
7
|
-
yield( builder )
|
8
|
-
|
9
|
-
builder.to_html if builder.actions?
|
10
|
-
end
|
11
|
-
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
class Builder
|
16
|
-
|
17
|
-
def initialize( template, options = {} )
|
18
|
-
@template, @options = template, options
|
19
|
-
|
20
|
-
@actions = []
|
21
|
-
end
|
22
|
-
|
23
|
-
|
24
|
-
def action( name, options = {}, html_options = {} )
|
25
|
-
wrapper_options = {
|
26
|
-
:current => html_options.delete(:current),
|
27
|
-
:disabled => html_options.delete(:disabled),
|
28
|
-
:force_current => html_options.delete(:force_current),
|
29
|
-
:url => options,
|
30
|
-
:prepend => html_options.delete(:prepend),
|
31
|
-
:append => html_options.delete(:append)
|
32
|
-
}
|
33
|
-
|
34
|
-
@actions << [link_to(name, options, html_options), wrapper_options, options]
|
35
|
-
end
|
36
|
-
|
37
|
-
def actions?; @actions.any?; end
|
38
|
-
|
39
|
-
def to_html
|
40
|
-
content_tag( :ul, actions.html_safe, @options ).html_safe
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
private
|
3
|
+
autoload :Builder, File.dirname(__FILE__) + '/nav/builder'
|
45
4
|
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
def action_wrapper( contents, options = {}, url_for_options = {} )
|
51
|
-
present = [contents, options, url_for_options] # the one we're dealing with
|
52
|
-
present_index = @actions.index( present )
|
53
|
-
|
54
|
-
before_present = @actions.at( present_index - 1 ) if present_index > 0
|
55
|
-
after_present = @actions.at( present_index + 1 ) if present_index < @actions.size
|
56
|
-
|
57
|
-
classes = []
|
58
|
-
classes << "first" if @actions.first == present
|
59
|
-
classes << "last" if @actions.last == present
|
60
|
-
classes << "current" if current?( *present )
|
61
|
-
classes << "disabled" if options.delete(:disabled)
|
62
|
-
classes << "before_current" if after_present && current?( *after_present )
|
63
|
-
classes << "after_current" if before_present && current?( *before_present )
|
64
|
-
classes << classes.join("_") if classes.any?
|
65
|
-
|
66
|
-
contents = options[:prepend].to_s + contents + options[:append].to_s
|
67
|
-
|
68
|
-
content_tag :li, contents.html_safe, :class => classes.join(" ")
|
69
|
-
end
|
70
|
-
|
71
|
-
def current?( contents, options = {}, url_for_options = {} )
|
72
|
-
current = options[:current]
|
73
|
-
|
74
|
-
is_current = case current
|
75
|
-
when TrueClass then true
|
76
|
-
when Regexp then request_uri.match(current).nil? ? false : true
|
77
|
-
when Proc then current.call
|
78
|
-
else false
|
79
|
-
end
|
80
|
-
|
81
|
-
return true if is_current && !options[:disabled] && options[:force_current]
|
82
|
-
return true if is_current || !url_for_options.is_a?(Symbol) && @template.current_page?(url_for_options) && url_for_options != {} && !options[:disabled]
|
83
|
-
|
84
|
-
false
|
85
|
-
end
|
86
|
-
|
87
|
-
def request_uri
|
88
|
-
@request_uri or @request_uri = @template.request.request_uri
|
89
|
-
end
|
90
|
-
|
91
|
-
|
92
|
-
def content_tag( *args )
|
93
|
-
@template.content_tag( *args ).html_safe
|
94
|
-
end
|
95
|
-
|
96
|
-
def link_to( *args )
|
97
|
-
@template.link_to( *args ).html_safe
|
98
|
-
end
|
99
|
-
|
5
|
+
def nav( options = {}, &block )
|
6
|
+
Nav::Builder.new( self, options, &block )
|
100
7
|
end
|
101
8
|
|
102
9
|
end
|
103
10
|
|
104
11
|
ActionView::Base.send :include, Nav
|
12
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nav
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2
|
4
|
+
version: 0.3.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-10-31 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionpack
|
16
|
-
requirement: &
|
16
|
+
requirement: &16805620 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *16805620
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rr
|
27
|
-
requirement: &
|
27
|
+
requirement: &16804580 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *16804580
|
36
36
|
description: Simple nagivation builder
|
37
37
|
email:
|
38
38
|
executables: []
|
@@ -41,8 +41,11 @@ extra_rdoc_files: []
|
|
41
41
|
files:
|
42
42
|
- .gitignore
|
43
43
|
- Gemfile
|
44
|
+
- LICENSE.txt
|
45
|
+
- README.rdoc
|
44
46
|
- Rakefile
|
45
47
|
- lib/nav.rb
|
48
|
+
- lib/nav/builder.rb
|
46
49
|
- lib/nav/version.rb
|
47
50
|
- nav.gemspec
|
48
51
|
- test/nav_test.rb
|
@@ -67,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
70
|
version: '0'
|
68
71
|
requirements: []
|
69
72
|
rubyforge_project: nav
|
70
|
-
rubygems_version: 1.8.
|
73
|
+
rubygems_version: 1.8.10
|
71
74
|
signing_key:
|
72
75
|
specification_version: 3
|
73
76
|
summary: Simple nagivation builder
|