active_link_to 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +3 -0
- data/LICENSE +1 -1
- data/README.md +79 -56
- data/Rakefile +1 -18
- data/active_link_to.gemspec +18 -42
- data/lib/active_link_to.rb +2 -98
- data/lib/active_link_to/active_link_to.rb +96 -0
- data/lib/active_link_to/version.rb +3 -0
- data/test/active_link_to_test.rb +51 -41
- metadata +31 -16
- data/VERSION +0 -1
- data/init.rb +0 -1
data/Gemfile
ADDED
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,103 +1,126 @@
|
|
1
|
-
|
2
|
-
================
|
3
|
-
|
1
|
+
# ActiveLinkTo
|
4
2
|
Creates a link tag of the given name using a URL created by the set of options. Please see documentation for `link_to`, as `active_link_to` is basically a wrapper for it. This method accepts an optional :active parameter that dictates if the given link will have an extra css class attached that marks it as 'active'.
|
5
3
|
|
6
4
|
## Install
|
7
|
-
|
8
5
|
When installing for Rails 3 applications add this to the Gemfile: `gem 'active_link_to'` and run `bundle install`.
|
9
6
|
|
10
7
|
For older Rails apps add `config.gem 'active_link_to'` in config/environment.rb and run `rake gems:install`. Or just checkout this repo into /vendor/plugins directory.
|
11
8
|
|
12
9
|
## Super Simple Example
|
13
|
-
Here's a link that will have class attached if it happens to be rendered
|
14
|
-
on page with path `/users` or any child of that page like `/users/123`
|
10
|
+
Here's a link that will have a class attached if it happens to be rendered
|
11
|
+
on page with path `/users` or any child of that page, like `/users/123`
|
15
12
|
|
16
|
-
|
17
|
-
|
13
|
+
```ruby
|
14
|
+
active_link_to 'Users', '/users'
|
15
|
+
# => <a href="/users" class="active">Users</a>
|
16
|
+
```
|
18
17
|
|
19
18
|
This is exactly the same as:
|
20
19
|
|
21
|
-
|
22
|
-
|
20
|
+
```ruby
|
21
|
+
active_link_to 'Users', '/users', :active => :inclusive
|
22
|
+
# => <a href="/users" class="active">Users</a>
|
23
|
+
```
|
23
24
|
|
24
25
|
## Active Options
|
25
|
-
Here's available options that can be used as the `:active` value
|
26
|
+
Here's a list of available options that can be used as the `:active` value
|
26
27
|
|
28
|
+
```
|
27
29
|
* Boolean -> true | false
|
28
30
|
* Symbol -> :exclusive | :inclusive
|
29
31
|
* Regex -> /regex/
|
30
|
-
* Controller/Action Pair -> [[:controller], [:
|
32
|
+
* Controller/Action Pair -> [[:controller], [:action_a, :action_b]]
|
33
|
+
```
|
31
34
|
|
32
35
|
## More Examples
|
33
36
|
Most of the functionality of `active_link_to` depends on the current
|
34
37
|
url. Specifically, `request.fullpath` value. We covered the basic example
|
35
38
|
already, so let's try something more fun.
|
36
39
|
|
37
|
-
We want to highlight
|
38
|
-
nodes
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
40
|
+
We want to highlight a link that matches immediate url, but not the children
|
41
|
+
nodes. Most commonly used for 'home' links.
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# For URL: /users will be active
|
45
|
+
active_link_to 'Users', users_path, :active => :exclusive
|
46
|
+
# => <a href="/users" class="active">Users</a>
|
47
|
+
```
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# But for URL: /users/123 it will not be active
|
51
|
+
active_link_to 'Users', users_path, :active => :exclusive
|
52
|
+
# => <a href="/users">Users</a>
|
53
|
+
```
|
47
54
|
|
48
55
|
If we need to set link to be active based on some regular expression, we can do
|
49
56
|
that as well. Let's try to activate links urls of which begin with 'use':
|
50
|
-
|
51
|
-
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
active_link_to 'Users', users_path, :active => /^\/use/
|
60
|
+
```
|
52
61
|
|
53
62
|
What if we need to mark link active for all URLs that match a particular controller,
|
54
63
|
or action, or both? Or any number of those at the same time? Sure, why not:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
# For matching multiple controllers and actions:
|
67
|
+
active_link_to 'User Edit', edit_user_path(@user), :active => [['people', 'news'], ['show', 'edit']]
|
68
|
+
|
69
|
+
# for matching all actions under given controllers:
|
70
|
+
active_link_to 'User Edit', edit_user_path(@user), :active => [['people', 'news'], []]
|
71
|
+
|
72
|
+
# for matching all controllers for a particular action
|
73
|
+
active_link_to 'User Edit', edit_user_path(@user), :active => [[], ['edit']]
|
74
|
+
```
|
55
75
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
# for matching all controllers for a particular action
|
63
|
-
active_link_to 'User Edit', edit_user_path(@user), :active => [[], ['edit']]
|
64
|
-
|
65
|
-
Sometimes it should be easy as setting a true or false:
|
66
|
-
|
67
|
-
active_link_to 'Users', users_path, :active => true
|
76
|
+
Sometimes it should be as easy as giving link true or false value:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
active_link_to 'Users', users_path, :active => true
|
80
|
+
```
|
68
81
|
|
69
82
|
## More Options
|
70
83
|
You can specify active and inactive css classes for links:
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
active_link_to 'Users', users_path, :class_active => 'enabled'
|
87
|
+
# => <a href="/users" class="enabled">Users</a>
|
88
|
+
|
89
|
+
active_link_to 'News', news_path, :class_inactive => 'disabled'
|
90
|
+
# => <a href="/news" class="disabled">News</a>
|
91
|
+
```
|
71
92
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
active_link_to 'Users', users_path, :wrap_link => :li
|
86
|
-
# => <li class="active"><a href="/users">Users</a></li>
|
93
|
+
Sometimes you want to replace link tag with a span if it's active:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
active_link_to 'Users', users_path, :disable_active => true
|
97
|
+
# => <span class="active">Users</span>
|
98
|
+
```
|
99
|
+
|
100
|
+
If you are constructing navigation menu it might be helpful to wrap links in another tag, like `<li>`:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
active_link_to 'Users', users_path, :wrap_tag => :li
|
104
|
+
# => <li class="active"><a href="/users">Users</a></li>
|
105
|
+
```
|
87
106
|
|
88
107
|
## Helper Methods
|
89
108
|
You may directly use methods that `active_link_to` relies on.
|
90
109
|
|
91
110
|
`is_active_link?` will return true or false based on the URL and value of the `:active` parameter:
|
92
111
|
|
93
|
-
|
94
|
-
|
112
|
+
```ruby
|
113
|
+
is_active_link?(users_path, :inclusive)
|
114
|
+
# => true
|
115
|
+
```
|
95
116
|
|
96
117
|
`active_link_to_class` will return the css class:
|
97
118
|
|
98
|
-
|
99
|
-
|
119
|
+
```
|
120
|
+
active_link_to_class(users_path, :active => :inclusive)
|
121
|
+
# => 'active'
|
122
|
+
```
|
100
123
|
|
101
124
|
### Copyright
|
102
125
|
|
103
|
-
Copyright (c) 2009 Oleg Khabarov, The Working Group Inc. See LICENSE for details.
|
126
|
+
Copyright (c) 2009-13 Oleg Khabarov, The Working Group Inc. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,27 +1,10 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require 'rake'
|
3
|
-
|
4
|
-
begin
|
5
|
-
require 'jeweler'
|
6
|
-
Jeweler::Tasks.new do |gem|
|
7
|
-
gem.name = 'active_link_to'
|
8
|
-
gem.summary = 'Marks currently active links'
|
9
|
-
gem.description = 'Extremely helpful when you need to add some logic that figures out if the link (or more often navigation item) is selected based on the current page or other arbitrary condition'
|
10
|
-
gem.email = 'oleg@theworkinggroup.ca'
|
11
|
-
gem.homepage = 'http://github.com/twg/active_link_to'
|
12
|
-
gem.authors = ['Oleg Khabarov']
|
13
|
-
end
|
14
|
-
Jeweler::GemcutterTasks.new
|
15
|
-
rescue LoadError
|
16
|
-
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
17
|
-
end
|
18
|
-
|
19
2
|
require 'rake/testtask'
|
3
|
+
|
20
4
|
Rake::TestTask.new(:test) do |test|
|
21
5
|
test.libs << 'lib' << 'test'
|
22
6
|
test.pattern = 'test/**/*_test.rb'
|
23
7
|
test.verbose = true
|
24
8
|
end
|
25
9
|
|
26
|
-
task :test => :check_dependencies
|
27
10
|
task :default => :test
|
data/active_link_to.gemspec
CHANGED
@@ -1,44 +1,20 @@
|
|
1
|
-
#
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
-
# -*- encoding: utf-8 -*-
|
1
|
+
# encoding: utf-8
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
s.version = "1.0.0"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Oleg Khabarov"]
|
12
|
-
s.date = %q{2011-07-22}
|
13
|
-
s.description = %q{Extremely helpful when you need to add some logic that figures out if the link (or more often navigation item) is selected based on the current page or other arbitrary condition}
|
14
|
-
s.email = %q{oleg@theworkinggroup.ca}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE",
|
17
|
-
"README.md"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
"LICENSE",
|
21
|
-
"README.md",
|
22
|
-
"Rakefile",
|
23
|
-
"VERSION",
|
24
|
-
"active_link_to.gemspec",
|
25
|
-
"init.rb",
|
26
|
-
"lib/active_link_to.rb",
|
27
|
-
"test/active_link_to_test.rb",
|
28
|
-
"test/test_helper.rb"
|
29
|
-
]
|
30
|
-
s.homepage = %q{http://github.com/twg/active_link_to}
|
31
|
-
s.require_paths = ["lib"]
|
32
|
-
s.rubygems_version = %q{1.6.2}
|
33
|
-
s.summary = %q{Marks currently active links}
|
34
|
-
|
35
|
-
if s.respond_to? :specification_version then
|
36
|
-
s.specification_version = 3
|
37
|
-
|
38
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
39
|
-
else
|
40
|
-
end
|
41
|
-
else
|
42
|
-
end
|
43
|
-
end
|
3
|
+
$:.unshift File.expand_path('../lib', __FILE__)
|
4
|
+
require 'active_link_to/version'
|
44
5
|
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "active_link_to"
|
8
|
+
s.version = ActiveLinkTo::VERSION
|
9
|
+
s.authors = ["Oleg Khabarov"]
|
10
|
+
s.email = ["oleg@khabarov.ca"]
|
11
|
+
s.homepage = "http://github.com/twg/active_link_to"
|
12
|
+
s.summary = "ActionView helper to render currently active links"
|
13
|
+
s.description = "Helpful method when you need to add some logic that figures out if the link (or more often navigation item) is selected based on the current page or other arbitrary condition"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.platform = Gem::Platform::RUBY
|
17
|
+
s.require_paths = ['lib']
|
18
|
+
|
19
|
+
s.add_dependency 'actionpack'
|
20
|
+
end
|
data/lib/active_link_to.rb
CHANGED
@@ -1,98 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
# Wrapper around link_to. Accepts following params:
|
5
|
-
# :active => Boolean | Symbol | Regex | Controller/Action Pair
|
6
|
-
# :class_active => String
|
7
|
-
# :class_inactive => String
|
8
|
-
# :disable_active => Boolean
|
9
|
-
# :wrap_tag => Symbol
|
10
|
-
# Example usage:
|
11
|
-
# active_link_to('/users', :class_active => 'enabled')
|
12
|
-
# active_link_to(users_path, :active => :exclusive, :wrap_tag => :li)
|
13
|
-
def active_link_to(*args, &block)
|
14
|
-
if block_given?
|
15
|
-
name = capture(&block)
|
16
|
-
options = args[0] || {}
|
17
|
-
html_options = args[1] || {}
|
18
|
-
else
|
19
|
-
name = args[0]
|
20
|
-
options = args[1] || {}
|
21
|
-
html_options = args[2] || {}
|
22
|
-
end
|
23
|
-
url = url_for(options)
|
24
|
-
|
25
|
-
active_options = { }
|
26
|
-
link_options = { }
|
27
|
-
html_options.each do |k, v|
|
28
|
-
if [:active, :class_active, :class_inactive, :active_disable, :wrap_tag].member?(k)
|
29
|
-
active_options[k] = v
|
30
|
-
else
|
31
|
-
link_options[k] = v
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
css_class = link_options.delete(:class).to_s + ' '
|
36
|
-
css_class << active_link_to_class(url, active_options)
|
37
|
-
css_class.strip!
|
38
|
-
|
39
|
-
wrap_tag = active_options[:wrap_tag].present? ? active_options[:wrap_tag] : nil
|
40
|
-
link_options[:class] = css_class if css_class.present? && !wrap_tag
|
41
|
-
|
42
|
-
link = if active_options[:active_disable] === true && is_active_link?(url, active_options[:active])
|
43
|
-
content_tag(:span, name, link_options)
|
44
|
-
else
|
45
|
-
link_to(name, url, link_options)
|
46
|
-
end
|
47
|
-
|
48
|
-
wrap_tag ? content_tag(wrap_tag, link, :class => css_class) : link
|
49
|
-
end
|
50
|
-
|
51
|
-
# Returns css class name. Takes the link's URL and its params
|
52
|
-
# Example usage:
|
53
|
-
# active_link_to_class('/root', :class_active => 'on', :class_inactive => 'off')
|
54
|
-
#
|
55
|
-
def active_link_to_class(url, options = {})
|
56
|
-
if is_active_link?(url, options[:active])
|
57
|
-
options[:class_active] || 'active'
|
58
|
-
else
|
59
|
-
options[:class_inactive] || ''
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
# Returns true or false based on the provided path and condition
|
64
|
-
# Possible condition values are:
|
65
|
-
# Boolean -> true | false
|
66
|
-
# Symbol -> :exclusive | :inclusive
|
67
|
-
# Regex -> /regex/
|
68
|
-
# Controller/Action Pair -> [[:controller], [:action_a, :action_b]]
|
69
|
-
# Example usage:
|
70
|
-
# is_active_link?('/root', true)
|
71
|
-
# is_active_link?('/root', :exclusive)
|
72
|
-
# is_active_link?('/root', /^\/root/)
|
73
|
-
# is_active_link?('/root', ['users', ['show', 'edit']])
|
74
|
-
#
|
75
|
-
def is_active_link?(url, condition = nil)
|
76
|
-
url = url_for(url).sub(/\?.*/, '') # ignore GET params
|
77
|
-
case condition
|
78
|
-
when :inclusive, nil
|
79
|
-
!request.fullpath.match(/^#{Regexp.escape(url)}(\/.*|\?.*)?$/).blank?
|
80
|
-
when :exclusive
|
81
|
-
!request.fullpath.match(/^#{Regexp.escape(url)}\/?(\?.*)?$/).blank?
|
82
|
-
when Regexp
|
83
|
-
!request.fullpath.match(condition).blank?
|
84
|
-
when Array
|
85
|
-
controllers = [*condition[0]]
|
86
|
-
actions = [*condition[1]]
|
87
|
-
(controllers.blank? || controllers.member?(params[:controller])) &&
|
88
|
-
(actions.blank? || actions.member?(params[:action]))
|
89
|
-
when TrueClass
|
90
|
-
true
|
91
|
-
when FalseClass
|
92
|
-
false
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
end
|
97
|
-
|
98
|
-
ActionView::Base.send :include, ActiveLinkTo
|
1
|
+
require 'active_link_to/active_link_to'
|
2
|
+
require 'active_link_to/version'
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module ActiveLinkTo
|
2
|
+
|
3
|
+
# Wrapper around link_to. Accepts following params:
|
4
|
+
# :active => Boolean | Symbol | Regex | Controller/Action Pair
|
5
|
+
# :class_active => String
|
6
|
+
# :class_inactive => String
|
7
|
+
# :disable_active => Boolean
|
8
|
+
# :wrap_tag => Symbol
|
9
|
+
# Example usage:
|
10
|
+
# active_link_to('/users', :class_active => 'enabled')
|
11
|
+
# active_link_to(users_path, :active => :exclusive, :wrap_tag => :li)
|
12
|
+
def active_link_to(*args, &block)
|
13
|
+
if block_given?
|
14
|
+
name = capture(&block)
|
15
|
+
options = args[0] || {}
|
16
|
+
html_options = args[1] || {}
|
17
|
+
else
|
18
|
+
name = args[0]
|
19
|
+
options = args[1] || {}
|
20
|
+
html_options = args[2] || {}
|
21
|
+
end
|
22
|
+
url = url_for(options)
|
23
|
+
|
24
|
+
active_options = { }
|
25
|
+
link_options = { }
|
26
|
+
html_options.each do |k, v|
|
27
|
+
if [:active, :class_active, :class_inactive, :active_disable, :wrap_tag].member?(k)
|
28
|
+
active_options[k] = v
|
29
|
+
else
|
30
|
+
link_options[k] = v
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
css_class = link_options.delete(:class).to_s + ' '
|
35
|
+
css_class << active_link_to_class(url, active_options)
|
36
|
+
css_class.strip!
|
37
|
+
|
38
|
+
wrap_tag = active_options[:wrap_tag].present? ? active_options[:wrap_tag] : nil
|
39
|
+
link_options[:class] = css_class if css_class.present?
|
40
|
+
|
41
|
+
link = if active_options[:active_disable] === true && is_active_link?(url, active_options[:active])
|
42
|
+
content_tag(:span, name, link_options)
|
43
|
+
else
|
44
|
+
link_to(name, url, link_options)
|
45
|
+
end
|
46
|
+
|
47
|
+
wrap_tag ? content_tag(wrap_tag, link, :class => css_class) : link
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns css class name. Takes the link's URL and its params
|
51
|
+
# Example usage:
|
52
|
+
# active_link_to_class('/root', :class_active => 'on', :class_inactive => 'off')
|
53
|
+
#
|
54
|
+
def active_link_to_class(url, options = {})
|
55
|
+
if is_active_link?(url, options[:active])
|
56
|
+
options[:class_active] || 'active'
|
57
|
+
else
|
58
|
+
options[:class_inactive] || ''
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Returns true or false based on the provided path and condition
|
63
|
+
# Possible condition values are:
|
64
|
+
# Boolean -> true | false
|
65
|
+
# Symbol -> :exclusive | :inclusive
|
66
|
+
# Regex -> /regex/
|
67
|
+
# Controller/Action Pair -> [[:controller], [:action_a, :action_b]]
|
68
|
+
# Example usage:
|
69
|
+
# is_active_link?('/root', true)
|
70
|
+
# is_active_link?('/root', :exclusive)
|
71
|
+
# is_active_link?('/root', /^\/root/)
|
72
|
+
# is_active_link?('/root', ['users', ['show', 'edit']])
|
73
|
+
#
|
74
|
+
def is_active_link?(url, condition = nil)
|
75
|
+
url = url_for(url).sub(/\?.*/, '') # ignore GET params
|
76
|
+
case condition
|
77
|
+
when :inclusive, nil
|
78
|
+
!request.fullpath.match(/^#{Regexp.escape(url).chomp('/')}(\/.*|\?.*)?$/).blank?
|
79
|
+
when :exclusive
|
80
|
+
!request.fullpath.match(/^#{Regexp.escape(url)}\/?(\?.*)?$/).blank?
|
81
|
+
when Regexp
|
82
|
+
!request.fullpath.match(condition).blank?
|
83
|
+
when Array
|
84
|
+
controllers = [*condition[0]]
|
85
|
+
actions = [*condition[1]]
|
86
|
+
(controllers.blank? || controllers.member?(params[:controller])) &&
|
87
|
+
(actions.blank? || actions.member?(params[:action]))
|
88
|
+
when TrueClass
|
89
|
+
true
|
90
|
+
when FalseClass
|
91
|
+
false
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
ActionView::Base.send :include, ActiveLinkTo
|
data/test/active_link_to_test.rb
CHANGED
@@ -1,71 +1,81 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class ActiveLinkToTest < Test::Unit::TestCase
|
4
|
-
|
4
|
+
|
5
5
|
def test_is_active_link_booleans
|
6
6
|
assert is_active_link?('/', true)
|
7
7
|
assert !is_active_link?('/', false)
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def test_is_active_link_symbol_inclusive
|
11
11
|
request.fullpath = '/root'
|
12
12
|
assert is_active_link?('/root', :inclusive)
|
13
|
-
|
13
|
+
|
14
14
|
request.fullpath = '/root?param=test'
|
15
15
|
assert is_active_link?('/root', :inclusive)
|
16
|
-
|
16
|
+
|
17
17
|
request.fullpath = '/root/child/sub-child'
|
18
18
|
assert is_active_link?('/root', :inclusive)
|
19
|
-
|
19
|
+
|
20
20
|
request.fullpath = '/other'
|
21
21
|
assert !is_active_link?('/root', :inclusive)
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
def test_is_active_link_symbol_inclusive_implied
|
25
25
|
request.fullpath = '/root/child/sub-child'
|
26
26
|
assert is_active_link?('/root')
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
def test_is_active_link_symbol_inclusive_similar_path
|
30
30
|
request.fullpath = '/root/abc'
|
31
31
|
assert !is_active_link?('/root/a', :inclusive)
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
|
+
def test_is_active_link_symbol_inclusive_with_last_slash
|
35
|
+
request.fullpath = '/root/abc'
|
36
|
+
assert is_active_link?('/root/')
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_is_active_link_symbol_inclusive_with_last_slash_and_similar_path
|
40
|
+
request.fullpath = '/root_path'
|
41
|
+
assert !is_active_link?('/root/')
|
42
|
+
end
|
43
|
+
|
34
44
|
def test_is_active_link_symbol_inclusive_with_link_params
|
35
45
|
request.fullpath = '/root?param=test'
|
36
46
|
assert is_active_link?('/root?attr=example')
|
37
47
|
end
|
38
|
-
|
48
|
+
|
39
49
|
def test_is_active_link_symbol_exclusive
|
40
50
|
request.fullpath = '/root'
|
41
51
|
assert is_active_link?('/root', :exclusive)
|
42
|
-
|
52
|
+
|
43
53
|
request.fullpath = '/root?param=test'
|
44
54
|
assert is_active_link?('/root', :exclusive)
|
45
|
-
|
55
|
+
|
46
56
|
request.fullpath = '/root/child'
|
47
57
|
assert !is_active_link?('/root', :exclusive)
|
48
58
|
end
|
49
|
-
|
59
|
+
|
50
60
|
def test_is_active_link_symbol_exclusive_with_link_params
|
51
61
|
request.fullpath = '/root?param=test'
|
52
62
|
assert is_active_link?('/root?attr=example', :exclusive)
|
53
63
|
end
|
54
|
-
|
64
|
+
|
55
65
|
def test_is_active_link_regex
|
56
66
|
request.fullpath = '/root'
|
57
67
|
assert is_active_link?('/', /^\/root/)
|
58
|
-
|
68
|
+
|
59
69
|
request.fullpath = '/root/child'
|
60
70
|
assert is_active_link?('/', /^\/r/)
|
61
|
-
|
71
|
+
|
62
72
|
request.fullpath = '/other'
|
63
73
|
assert !is_active_link?('/', /^\/r/)
|
64
74
|
end
|
65
|
-
|
75
|
+
|
66
76
|
def test_is_active_link_array
|
67
77
|
params[:controller], params[:action] = 'controller', 'action'
|
68
|
-
|
78
|
+
|
69
79
|
assert is_active_link?('/', [['controller'], ['action']])
|
70
80
|
assert is_active_link?('/', [['controller'], ['action', 'action_b']])
|
71
81
|
assert is_active_link?('/', [['controller', 'controller_b'], ['action']])
|
@@ -73,71 +83,71 @@ class ActiveLinkToTest < Test::Unit::TestCase
|
|
73
83
|
assert is_active_link?('/', ['controller', 'action'])
|
74
84
|
assert is_active_link?('/', ['controller', ['action', 'action_b']])
|
75
85
|
assert is_active_link?('/', [['controller', 'controller_b'], 'action'])
|
76
|
-
|
86
|
+
|
77
87
|
assert !is_active_link?('/', ['controller_a', 'action'])
|
78
88
|
assert !is_active_link?('/', ['controller', 'action_a'])
|
79
89
|
end
|
80
|
-
|
90
|
+
|
81
91
|
def test_active_link_to_class
|
82
92
|
request.fullpath = '/root'
|
83
93
|
assert_equal 'active', active_link_to_class('/root')
|
84
94
|
assert_equal 'on', active_link_to_class('/root', :class_active => 'on')
|
85
|
-
|
95
|
+
|
86
96
|
assert_equal '', active_link_to_class('/other')
|
87
97
|
assert_equal 'off', active_link_to_class('/other', :class_inactive => 'off')
|
88
98
|
end
|
89
|
-
|
99
|
+
|
90
100
|
def test_active_link_to
|
91
101
|
request.fullpath = '/root'
|
92
102
|
link = active_link_to('label', '/root')
|
93
|
-
assert_equal '<a
|
94
|
-
|
103
|
+
assert_equal '<a class="active" href="/root">label</a>', link
|
104
|
+
|
95
105
|
link = active_link_to('label', '/other')
|
96
106
|
assert_equal '<a href="/other">label</a>', link
|
97
107
|
end
|
98
|
-
|
108
|
+
|
99
109
|
def test_active_link_to_with_existing_class
|
100
110
|
request.fullpath = '/root'
|
101
111
|
link = active_link_to('label', '/root', :class => 'current')
|
102
|
-
assert_equal '<a
|
103
|
-
|
112
|
+
assert_equal '<a class="current active" href="/root">label</a>', link
|
113
|
+
|
104
114
|
link = active_link_to('label', '/other', :class => 'current')
|
105
|
-
assert_equal '<a
|
115
|
+
assert_equal '<a class="current" href="/other">label</a>', link
|
106
116
|
end
|
107
|
-
|
117
|
+
|
108
118
|
def test_active_link_to_with_custom_classes
|
109
119
|
request.fullpath = '/root'
|
110
120
|
link = active_link_to('label', '/root', :class_active => 'on')
|
111
|
-
assert_equal '<a
|
112
|
-
|
121
|
+
assert_equal '<a class="on" href="/root">label</a>', link
|
122
|
+
|
113
123
|
link = active_link_to('label', '/other', :class_inactive => 'off')
|
114
|
-
assert_equal '<a
|
124
|
+
assert_equal '<a class="off" href="/other">label</a>', link
|
115
125
|
end
|
116
|
-
|
126
|
+
|
117
127
|
def test_active_link_to_with_wrap_tag
|
118
128
|
request.fullpath = '/root'
|
119
129
|
link = active_link_to('label', '/root', :wrap_tag => :li)
|
120
|
-
assert_equal '<li class="active"><a href="/root">label</a></li>', link
|
121
|
-
|
130
|
+
assert_equal '<li class="active"><a class="active" href="/root">label</a></li>', link
|
131
|
+
|
122
132
|
link = active_link_to('label', '/root', :wrap_tag => :li, :active_disable => true)
|
123
|
-
assert_equal '<li class="active"><span>label</span></li>', link
|
124
|
-
|
133
|
+
assert_equal '<li class="active"><span class="active">label</span></li>', link
|
134
|
+
|
125
135
|
link = active_link_to('label', '/root', :wrap_tag => :li, :class => 'testing')
|
126
|
-
assert_equal '<li class="testing active"><a href="/root">label</a></li>', link
|
136
|
+
assert_equal '<li class="testing active"><a class="testing active" href="/root">label</a></li>', link
|
127
137
|
end
|
128
|
-
|
138
|
+
|
129
139
|
def test_active_link_to_with_active_disable
|
130
140
|
request.fullpath = '/root'
|
131
141
|
link = active_link_to('label', '/root', :active_disable => true)
|
132
142
|
assert_equal '<span class="active">label</span>', link
|
133
143
|
end
|
134
|
-
|
144
|
+
|
135
145
|
def test_should_not_modify_passed_params
|
136
146
|
request.fullpath = '/root'
|
137
147
|
params = { :class => 'testing', :active => :inclusive }
|
138
148
|
out = active_link_to 'label', '/root', params
|
139
|
-
assert_equal '<a
|
149
|
+
assert_equal '<a class="testing active" href="/root">label</a>', out
|
140
150
|
assert_equal ({:class => 'testing', :active => :inclusive }), params
|
141
151
|
end
|
142
|
-
|
152
|
+
|
143
153
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_link_to
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,29 +9,44 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
12
|
+
date: 2013-05-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: actionpack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Helpful method when you need to add some logic that figures out if the
|
31
|
+
link (or more often navigation item) is selected based on the current page or other
|
32
|
+
arbitrary condition
|
33
|
+
email:
|
34
|
+
- oleg@khabarov.ca
|
19
35
|
executables: []
|
20
36
|
extensions: []
|
21
|
-
extra_rdoc_files:
|
22
|
-
- LICENSE
|
23
|
-
- README.md
|
37
|
+
extra_rdoc_files: []
|
24
38
|
files:
|
39
|
+
- .gitignore
|
40
|
+
- Gemfile
|
25
41
|
- LICENSE
|
26
42
|
- README.md
|
27
43
|
- Rakefile
|
28
|
-
- VERSION
|
29
44
|
- active_link_to.gemspec
|
30
|
-
- init.rb
|
31
45
|
- lib/active_link_to.rb
|
46
|
+
- lib/active_link_to/active_link_to.rb
|
47
|
+
- lib/active_link_to/version.rb
|
32
48
|
- test/active_link_to_test.rb
|
33
49
|
- test/test_helper.rb
|
34
|
-
has_rdoc: true
|
35
50
|
homepage: http://github.com/twg/active_link_to
|
36
51
|
licenses: []
|
37
52
|
post_install_message:
|
@@ -52,8 +67,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
67
|
version: '0'
|
53
68
|
requirements: []
|
54
69
|
rubyforge_project:
|
55
|
-
rubygems_version: 1.
|
70
|
+
rubygems_version: 1.8.23
|
56
71
|
signing_key:
|
57
72
|
specification_version: 3
|
58
|
-
summary:
|
73
|
+
summary: ActionView helper to render currently active links
|
59
74
|
test_files: []
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.0.0
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'active_link_to'
|