active_link_to 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +43 -24
- data/Rakefile +1 -1
- data/active_link_to.gemspec +4 -3
- data/lib/active_link_to.rb +2 -1
- data/lib/active_link_to/active_link_to.rb +47 -31
- data/lib/active_link_to/version.rb +1 -1
- data/test/active_link_to_test.rb +101 -67
- data/test/test_helper.rb +22 -19
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd9a67de186dfcf5e517ed55994051723962da91
|
4
|
+
data.tar.gz: 153e11c947d56e41e642e45a94295581a868c7f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 421d92f774bf0e904a8dede4c10910bc2fcd28ae9c7e39cc8f3142628557827224e3aa4bfbb9a67ba52527498e8cdf3e6525f814298cf26140d1ec6406ae3b7c
|
7
|
+
data.tar.gz: 771b308ba2db04c506d3d7870c98be228388c09dfd8e79f0915721eeba9303f452014d4d4334f72d42f914ab49a7690e7b1ee1bdf0a2027386612ffc7fa45b9f
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
Creates a link tag of the given name using a URL created by the set of options. Please see documentation for [link_to](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-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
5
|
|
6
6
|
## Install
|
7
|
-
When installing for Rails 3 applications add this to the Gemfile: `gem 'active_link_to'` and run `bundle install`.
|
7
|
+
When installing for Rails 3/4/5 applications add this to the Gemfile: `gem 'active_link_to'` and run `bundle install`.
|
8
8
|
|
9
9
|
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.
|
10
10
|
|
@@ -20,7 +20,7 @@ active_link_to 'Users', '/users'
|
|
20
20
|
This is exactly the same as:
|
21
21
|
|
22
22
|
```ruby
|
23
|
-
active_link_to 'Users', '/users', :
|
23
|
+
active_link_to 'Users', '/users', active: :inclusive
|
24
24
|
# => <a href="/users" class="active">Users</a>
|
25
25
|
```
|
26
26
|
|
@@ -28,15 +28,18 @@ active_link_to 'Users', '/users', :active => :inclusive
|
|
28
28
|
Here's a list of available options that can be used as the `:active` value
|
29
29
|
|
30
30
|
```
|
31
|
-
|
32
|
-
*
|
33
|
-
*
|
34
|
-
*
|
31
|
+
<<<<<<< HEAD
|
32
|
+
* Boolean -> true | false
|
33
|
+
* Symbol -> :exclusive | :inclusive | :exact
|
34
|
+
* Regex -> /regex/
|
35
|
+
* Controller/Action Pair -> [[:controller], [:action_a, :action_b]]
|
36
|
+
* Controller/Specific Action Pair -> [controller: :action_a, controller_b: :action_b]
|
37
|
+
* Hash -> { param_a: 1, param_b: 2 }
|
35
38
|
```
|
36
39
|
|
37
40
|
## More Examples
|
38
41
|
Most of the functionality of `active_link_to` depends on the current
|
39
|
-
url. Specifically, `request.
|
42
|
+
url. Specifically, `request.original_fullpath` value. We covered the basic example
|
40
43
|
already, so let's try something more fun.
|
41
44
|
|
42
45
|
We want to highlight a link that matches immediate url, but not the children
|
@@ -44,13 +47,13 @@ nodes. Most commonly used for 'home' links.
|
|
44
47
|
|
45
48
|
```ruby
|
46
49
|
# For URL: /users will be active
|
47
|
-
active_link_to 'Users', users_path, :
|
50
|
+
active_link_to 'Users', users_path, active: :exclusive
|
48
51
|
# => <a href="/users" class="active">Users</a>
|
49
52
|
```
|
50
53
|
|
51
54
|
```ruby
|
52
55
|
# But for URL: /users/123 it will not be active
|
53
|
-
active_link_to 'Users', users_path, :
|
56
|
+
active_link_to 'Users', users_path, active: :exclusive
|
54
57
|
# => <a href="/users">Users</a>
|
55
58
|
```
|
56
59
|
|
@@ -58,14 +61,14 @@ If we need to set link to be active based on some regular expression, we can do
|
|
58
61
|
that as well. Let's try to activate links urls of which begin with 'use':
|
59
62
|
|
60
63
|
```ruby
|
61
|
-
active_link_to 'Users', users_path, :
|
64
|
+
active_link_to 'Users', users_path, active: /^\/use/
|
62
65
|
```
|
63
66
|
|
64
|
-
If we need to set link to be active based on an exact match,
|
65
|
-
that as well:
|
67
|
+
If we need to set link to be active based on an exact match, for example on
|
68
|
+
filter made via a query string, we can do that as well:
|
66
69
|
|
67
70
|
```ruby
|
68
|
-
active_link_to 'Users', users_path, :
|
71
|
+
active_link_to 'Users', users_path(role_eq: 'admin'), active: :exact
|
69
72
|
```
|
70
73
|
|
71
74
|
What if we need to mark link active for all URLs that match a particular controller,
|
@@ -73,44 +76,60 @@ or action, or both? Or any number of those at the same time? Sure, why not:
|
|
73
76
|
|
74
77
|
```ruby
|
75
78
|
# For matching multiple controllers and actions:
|
76
|
-
active_link_to 'User Edit', edit_user_path(@user), :
|
79
|
+
active_link_to 'User Edit', edit_user_path(@user), active: [['people', 'news'], ['show', 'edit']]
|
80
|
+
|
81
|
+
# For matching specific controllers and actions:
|
82
|
+
active_link_to 'User Edit', edit_user_path(@user), :active => [people: :show, news: :edit]
|
77
83
|
|
78
84
|
# for matching all actions under given controllers:
|
79
|
-
active_link_to 'User Edit', edit_user_path(@user), :
|
85
|
+
active_link_to 'User Edit', edit_user_path(@user), active: [['people', 'news'], []]
|
80
86
|
|
81
87
|
# for matching all controllers for a particular action
|
82
|
-
active_link_to 'User Edit', edit_user_path(@user), :
|
88
|
+
active_link_to 'User Edit', edit_user_path(@user), active: [[], ['edit']]
|
83
89
|
```
|
84
90
|
|
85
91
|
Sometimes it should be as easy as giving link true or false value:
|
86
92
|
|
87
93
|
```ruby
|
88
|
-
active_link_to 'Users', users_path, :
|
94
|
+
active_link_to 'Users', users_path, active: true
|
95
|
+
```
|
96
|
+
|
97
|
+
If we need to set link to be active based on `params`, we can do that as well:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
active_link_to 'Admin users', users_path(role_eq: 'admin'), :active => { role_eq: 'admin' }
|
89
101
|
```
|
90
102
|
|
91
103
|
## More Options
|
92
104
|
You can specify active and inactive css classes for links:
|
93
105
|
|
94
106
|
```ruby
|
95
|
-
active_link_to 'Users', users_path, :
|
107
|
+
active_link_to 'Users', users_path, class_active: 'enabled'
|
96
108
|
# => <a href="/users" class="enabled">Users</a>
|
97
109
|
|
98
|
-
active_link_to 'News', news_path, :
|
110
|
+
active_link_to 'News', news_path, class_inactive: 'disabled'
|
99
111
|
# => <a href="/news" class="disabled">News</a>
|
100
112
|
```
|
101
113
|
|
102
114
|
Sometimes you want to replace link tag with a span if it's active:
|
103
115
|
|
104
116
|
```ruby
|
105
|
-
active_link_to 'Users', users_path, :
|
117
|
+
active_link_to 'Users', users_path, active_disable: true
|
106
118
|
# => <span class="active">Users</span>
|
107
119
|
```
|
108
120
|
|
109
121
|
If you are constructing navigation menu it might be helpful to wrap links in another tag, like `<li>`:
|
110
122
|
|
111
123
|
```ruby
|
112
|
-
active_link_to 'Users', users_path, :
|
113
|
-
# => <li class="active"><a href="/users"
|
124
|
+
active_link_to 'Users', users_path, wrap_tag: :li
|
125
|
+
# => <li class="active"><a href="/users">Users</a></li>
|
126
|
+
```
|
127
|
+
|
128
|
+
You can specify css classes for the `wrap_tag`:
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
active_link_to 'Users', users_path, wrap_tag: :li, wrap_class: 'nav-item'
|
132
|
+
# => <li class="nav-item active"><a href="/users">Users</a></li>
|
114
133
|
```
|
115
134
|
|
116
135
|
## Helper Methods
|
@@ -126,10 +145,10 @@ is_active_link?(users_path, :inclusive)
|
|
126
145
|
`active_link_to_class` will return the css class:
|
127
146
|
|
128
147
|
```
|
129
|
-
active_link_to_class(users_path, :
|
148
|
+
active_link_to_class(users_path, active: :inclusive)
|
130
149
|
# => 'active'
|
131
150
|
```
|
132
151
|
|
133
152
|
### Copyright
|
134
153
|
|
135
|
-
Copyright (c) 2009-
|
154
|
+
Copyright (c) 2009-17 Oleg Khabarov. See LICENSE for details.
|
data/Rakefile
CHANGED
data/active_link_to.gemspec
CHANGED
@@ -11,10 +11,11 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.homepage = "http://github.com/comfy/active_link_to"
|
12
12
|
s.summary = "ActionView helper to render currently active links"
|
13
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
|
-
|
14
|
+
|
15
15
|
s.files = `git ls-files`.split("\n")
|
16
16
|
s.platform = Gem::Platform::RUBY
|
17
17
|
s.require_paths = ['lib']
|
18
|
-
|
18
|
+
|
19
19
|
s.add_dependency 'actionpack'
|
20
|
-
|
20
|
+
s.add_dependency 'addressable'
|
21
|
+
end
|
data/lib/active_link_to.rb
CHANGED
@@ -7,8 +7,8 @@ module ActiveLinkTo
|
|
7
7
|
# :disable_active => Boolean
|
8
8
|
# :wrap_tag => Symbol
|
9
9
|
# Example usage:
|
10
|
-
# active_link_to('/users', :
|
11
|
-
# active_link_to(users_path, :
|
10
|
+
# active_link_to('/users', class_active: 'enabled')
|
11
|
+
# active_link_to(users_path, active: :exclusive, wrap_tag: :li)
|
12
12
|
def active_link_to(*args, &block)
|
13
13
|
if block_given?
|
14
14
|
name = capture(&block)
|
@@ -24,7 +24,7 @@ module ActiveLinkTo
|
|
24
24
|
active_options = { }
|
25
25
|
link_options = { }
|
26
26
|
html_options.each do |k, v|
|
27
|
-
if [:active, :class_active, :class_inactive, :active_disable, :wrap_tag].member?(k)
|
27
|
+
if [:active, :class_active, :class_inactive, :active_disable, :wrap_tag, :wrap_class].member?(k)
|
28
28
|
active_options[k] = v
|
29
29
|
else
|
30
30
|
link_options[k] = v
|
@@ -32,11 +32,20 @@ module ActiveLinkTo
|
|
32
32
|
end
|
33
33
|
|
34
34
|
css_class = link_options.delete(:class).to_s + ' '
|
35
|
-
css_class << active_link_to_class(url, active_options)
|
36
|
-
css_class.strip!
|
37
35
|
|
38
|
-
wrap_tag
|
36
|
+
wrap_tag = active_options[:wrap_tag].present? ? active_options[:wrap_tag] : nil
|
37
|
+
wrap_class = active_options[:wrap_class].present? ? active_options[:wrap_class] + ' ' : ''
|
38
|
+
|
39
|
+
if wrap_tag.present?
|
40
|
+
wrap_class << active_link_to_class(url, active_options)
|
41
|
+
wrap_class.strip!
|
42
|
+
else
|
43
|
+
css_class << active_link_to_class(url, active_options)
|
44
|
+
css_class.strip!
|
45
|
+
end
|
46
|
+
|
39
47
|
link_options[:class] = css_class if css_class.present?
|
48
|
+
link_options['aria-current'] = 'page' if is_active_link?(url, active_options[:active])
|
40
49
|
|
41
50
|
link = if active_options[:active_disable] === true && is_active_link?(url, active_options[:active])
|
42
51
|
content_tag(:span, name, link_options)
|
@@ -44,12 +53,12 @@ module ActiveLinkTo
|
|
44
53
|
link_to(name, url, link_options)
|
45
54
|
end
|
46
55
|
|
47
|
-
wrap_tag ? content_tag(wrap_tag, link, :
|
56
|
+
wrap_tag ? content_tag(wrap_tag, link, class: (wrap_class if wrap_class.present?)) : link
|
48
57
|
end
|
49
58
|
|
50
59
|
# Returns css class name. Takes the link's URL and its params
|
51
60
|
# Example usage:
|
52
|
-
# active_link_to_class('/root', :
|
61
|
+
# active_link_to_class('/root', class_active: 'on', class_inactive: 'off')
|
53
62
|
#
|
54
63
|
def active_link_to_class(url, options = {})
|
55
64
|
if is_active_link?(url, options[:active])
|
@@ -74,29 +83,36 @@ module ActiveLinkTo
|
|
74
83
|
# is_active_link?('/root', ['users', ['show', 'edit']])
|
75
84
|
#
|
76
85
|
def is_active_link?(url, condition = nil)
|
77
|
-
|
78
|
-
url
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
86
|
+
@is_active_link ||= {}
|
87
|
+
@is_active_link[[url, condition]] ||= begin
|
88
|
+
original_url = url
|
89
|
+
url = Addressable::URI::parse(url).path
|
90
|
+
path = request.original_fullpath
|
91
|
+
case condition
|
92
|
+
when :inclusive, nil
|
93
|
+
!path.match(/^#{Regexp.escape(url).chomp('/')}(\/.*|\?.*)?$/).blank?
|
94
|
+
when :exclusive
|
95
|
+
!path.match(/^#{Regexp.escape(url)}\/?(\?.*)?$/).blank?
|
96
|
+
when :exact
|
97
|
+
path == original_url
|
98
|
+
when Regexp
|
99
|
+
!path.match(condition).blank?
|
100
|
+
when Array
|
101
|
+
controllers = [*condition[0]]
|
102
|
+
actions = [*condition[1]]
|
103
|
+
(controllers.blank? || controllers.member?(params[:controller])) &&
|
104
|
+
(actions.blank? || actions.member?(params[:action])) ||
|
105
|
+
controllers.any? do |controller, action|
|
106
|
+
params[:controller] == controller.to_s && params[:action] == action.to_s
|
107
|
+
end
|
108
|
+
when TrueClass
|
109
|
+
true
|
110
|
+
when FalseClass
|
111
|
+
false
|
112
|
+
when Hash
|
113
|
+
condition.all? do |key, value|
|
114
|
+
params[key].to_s == value.to_s
|
115
|
+
end
|
100
116
|
end
|
101
117
|
end
|
102
118
|
end
|
data/test/active_link_to_test.rb
CHANGED
@@ -4,87 +4,87 @@ class ActiveLinkToTest < MiniTest::Test
|
|
4
4
|
|
5
5
|
def test_is_active_link_booleans_test
|
6
6
|
assert is_active_link?('/', true)
|
7
|
-
|
7
|
+
refute is_active_link?('/', false)
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_is_active_link_symbol_inclusive
|
11
|
-
|
11
|
+
set_path('/root')
|
12
12
|
assert is_active_link?('/root', :inclusive)
|
13
13
|
|
14
|
-
|
14
|
+
set_path('/root?param=test')
|
15
15
|
assert is_active_link?('/root', :inclusive)
|
16
16
|
|
17
|
-
|
17
|
+
set_path('/root/child/sub-child')
|
18
18
|
assert is_active_link?('/root', :inclusive)
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
set_path('/other')
|
21
|
+
refute is_active_link?('/root', :inclusive)
|
22
22
|
end
|
23
23
|
|
24
24
|
def test_is_active_link_symbol_inclusive_implied
|
25
|
-
|
25
|
+
set_path('/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
|
-
|
31
|
-
|
30
|
+
set_path('/root/abc')
|
31
|
+
refute is_active_link?('/root/a', :inclusive)
|
32
32
|
end
|
33
33
|
|
34
34
|
def test_is_active_link_symbol_inclusive_with_last_slash
|
35
|
-
|
35
|
+
set_path('/root/abc')
|
36
36
|
assert is_active_link?('/root/')
|
37
37
|
end
|
38
38
|
|
39
39
|
def test_is_active_link_symbol_inclusive_with_last_slash_and_similar_path
|
40
|
-
|
41
|
-
|
40
|
+
set_path('/root_path')
|
41
|
+
refute is_active_link?('/root/')
|
42
42
|
end
|
43
43
|
|
44
44
|
def test_is_active_link_symbol_inclusive_with_link_params
|
45
|
-
|
45
|
+
set_path('/root?param=test')
|
46
46
|
assert is_active_link?('/root?attr=example')
|
47
47
|
end
|
48
48
|
|
49
49
|
def test_is_active_link_symbol_exclusive
|
50
|
-
|
50
|
+
set_path('/root')
|
51
51
|
assert is_active_link?('/root', :exclusive)
|
52
52
|
|
53
|
-
|
53
|
+
set_path('/root?param=test')
|
54
54
|
assert is_active_link?('/root', :exclusive)
|
55
55
|
|
56
|
-
|
57
|
-
|
56
|
+
set_path('/root/child')
|
57
|
+
refute is_active_link?('/root', :exclusive)
|
58
58
|
end
|
59
59
|
|
60
60
|
def test_is_active_link_symbol_exclusive_with_link_params
|
61
|
-
|
61
|
+
set_path('/root?param=test')
|
62
62
|
assert is_active_link?('/root?attr=example', :exclusive)
|
63
63
|
end
|
64
64
|
|
65
65
|
def test_is_active_link_symbol_exact
|
66
|
-
|
66
|
+
set_path('/root?param=test')
|
67
67
|
assert is_active_link?('/root?param=test', :exact)
|
68
68
|
|
69
|
-
|
69
|
+
set_path('/root?param=test')
|
70
70
|
refute is_active_link?('/root?param=exact', :exact)
|
71
71
|
|
72
|
-
|
72
|
+
set_path('/root')
|
73
73
|
refute is_active_link?('/root?param=test', :exact)
|
74
74
|
|
75
|
-
|
75
|
+
set_path('/root?param=test')
|
76
76
|
refute is_active_link?('/root', :exact)
|
77
77
|
end
|
78
78
|
|
79
79
|
def test_is_active_link_regex
|
80
|
-
|
80
|
+
set_path('/root')
|
81
81
|
assert is_active_link?('/', /^\/root/)
|
82
82
|
|
83
|
-
|
83
|
+
set_path('/root/child')
|
84
84
|
assert is_active_link?('/', /^\/r/)
|
85
85
|
|
86
|
-
|
87
|
-
|
86
|
+
set_path('/other')
|
87
|
+
refute is_active_link?('/', /^\/r/)
|
88
88
|
end
|
89
89
|
|
90
90
|
def test_is_active_link_array
|
@@ -98,39 +98,67 @@ class ActiveLinkToTest < MiniTest::Test
|
|
98
98
|
assert is_active_link?('/', ['controller', ['action', 'action_b']])
|
99
99
|
assert is_active_link?('/', [['controller', 'controller_b'], 'action'])
|
100
100
|
|
101
|
-
|
102
|
-
|
101
|
+
refute is_active_link?('/', ['controller_a', 'action'])
|
102
|
+
refute is_active_link?('/', ['controller', 'action_a'])
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_is_active_link_array_with_hash
|
106
|
+
params[:controller], params[:action] = 'controller', 'action'
|
107
|
+
|
108
|
+
assert is_active_link?('/', [controller: :action])
|
109
|
+
assert is_active_link?('/', ['controller' => 'action'])
|
110
|
+
|
111
|
+
refute is_active_link?('/', [controller_b: :action])
|
112
|
+
refute is_active_link?('/', [controller: :action_b])
|
113
|
+
refute is_active_link?('/', [controller_b: :action_b])
|
114
|
+
|
115
|
+
params[:controller], params[:action] = 'controller_b', 'action_b'
|
116
|
+
|
117
|
+
assert is_active_link?('/', [controller: :action, controller_b: :action_b])
|
103
118
|
end
|
104
119
|
|
105
120
|
def test_is_active_link_hash
|
106
121
|
params[:a] = 1
|
107
122
|
|
108
|
-
assert is_active_link?('/', {:
|
109
|
-
assert is_active_link?('/', {:
|
123
|
+
assert is_active_link?('/', {a: 1})
|
124
|
+
assert is_active_link?('/', {a: 1, b: nil})
|
110
125
|
|
111
|
-
|
112
|
-
|
126
|
+
refute is_active_link?('/', {a: 1, b: 2})
|
127
|
+
refute is_active_link?('/', {a: 2})
|
113
128
|
|
114
129
|
params[:b] = 2
|
115
130
|
|
116
|
-
assert is_active_link?('/', {:
|
117
|
-
assert is_active_link?('/', {:
|
131
|
+
assert is_active_link?('/', {a: 1, b: 2})
|
132
|
+
assert is_active_link?('/', {a: 1, b: 2, c: nil})
|
118
133
|
|
119
|
-
assert is_active_link?('/', {:
|
120
|
-
assert is_active_link?('/', {:
|
134
|
+
assert is_active_link?('/', {a: 1})
|
135
|
+
assert is_active_link?('/', {b: 2})
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_is_active_link_with_anchor
|
139
|
+
set_path('/foo')
|
140
|
+
assert is_active_link?('/foo#anchor', :exclusive)
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_is_active_link_with_memoization
|
144
|
+
set_path('/')
|
145
|
+
assert is_active_link?('/', :exclusive)
|
146
|
+
|
147
|
+
set_path('/other', false)
|
148
|
+
assert is_active_link?('/', :exclusive)
|
121
149
|
end
|
122
150
|
|
123
151
|
def test_active_link_to_class
|
124
|
-
|
152
|
+
set_path('/root')
|
125
153
|
assert_equal 'active', active_link_to_class('/root')
|
126
|
-
assert_equal 'on', active_link_to_class('/root', :
|
154
|
+
assert_equal 'on', active_link_to_class('/root', class_active: 'on')
|
127
155
|
|
128
156
|
assert_equal '', active_link_to_class('/other')
|
129
|
-
assert_equal 'off', active_link_to_class('/other', :
|
157
|
+
assert_equal 'off', active_link_to_class('/other', class_inactive: 'off')
|
130
158
|
end
|
131
159
|
|
132
160
|
def test_active_link_to
|
133
|
-
|
161
|
+
set_path('/root')
|
134
162
|
link = active_link_to('label', '/root')
|
135
163
|
assert_html link, 'a.active[href="/root"]', 'label'
|
136
164
|
|
@@ -139,61 +167,67 @@ class ActiveLinkToTest < MiniTest::Test
|
|
139
167
|
end
|
140
168
|
|
141
169
|
def test_active_link_to_with_existing_class
|
142
|
-
|
143
|
-
link = active_link_to('label', '/root', :
|
170
|
+
set_path('/root')
|
171
|
+
link = active_link_to('label', '/root', class: 'current')
|
144
172
|
assert_html link, 'a.current.active[href="/root"]', 'label'
|
145
173
|
|
146
|
-
link = active_link_to('label', '/other', :
|
174
|
+
link = active_link_to('label', '/other', class: 'current')
|
147
175
|
assert_html link, 'a.current[href="/other"]', 'label'
|
148
176
|
end
|
149
177
|
|
150
178
|
def test_active_link_to_with_custom_classes
|
151
|
-
|
152
|
-
link = active_link_to('label', '/root', :
|
179
|
+
set_path('/root')
|
180
|
+
link = active_link_to('label', '/root', class_active: 'on')
|
153
181
|
assert_html link, 'a.on[href="/root"]', 'label'
|
154
182
|
|
155
|
-
link = active_link_to('label', '/other', :
|
183
|
+
link = active_link_to('label', '/other', class_inactive: 'off')
|
156
184
|
assert_html link, 'a.off[href="/other"]', 'label'
|
157
185
|
end
|
158
186
|
|
159
187
|
def test_active_link_to_with_wrap_tag
|
160
|
-
|
161
|
-
link = active_link_to('label', '/root', :
|
162
|
-
assert_html link, 'li.active a
|
188
|
+
set_path('/root')
|
189
|
+
link = active_link_to('label', '/root', wrap_tag: :li)
|
190
|
+
assert_html link, 'li.active a[href="/root"]', 'label'
|
163
191
|
|
164
|
-
link = active_link_to('label', '/root', :
|
165
|
-
assert_html link, 'li.active span
|
192
|
+
link = active_link_to('label', '/root', wrap_tag: :li, active_disable: true)
|
193
|
+
assert_html link, 'li.active span', 'label'
|
166
194
|
|
167
|
-
link = active_link_to('label', '/root', :
|
168
|
-
assert_html link, 'li.
|
195
|
+
link = active_link_to('label', '/root', wrap_tag: :li, class: 'testing')
|
196
|
+
assert_html link, 'li.active a.testing[href="/root"]', 'label'
|
169
197
|
end
|
170
198
|
|
171
199
|
def test_active_link_to_with_active_disable
|
172
|
-
|
173
|
-
link = active_link_to('label', '/root', :
|
200
|
+
set_path('/root')
|
201
|
+
link = active_link_to('label', '/root', active_disable: true)
|
174
202
|
assert_html link, 'span.active', 'label'
|
175
203
|
end
|
176
204
|
|
177
205
|
def test_should_not_modify_passed_params
|
178
|
-
|
179
|
-
params = {
|
206
|
+
set_path('/root')
|
207
|
+
params = {class: 'testing', active: :inclusive}
|
180
208
|
out = active_link_to 'label', '/root', params
|
181
209
|
assert_html out, 'a.testing.active[href="/root"]', 'label'
|
182
|
-
assert_equal ({:
|
210
|
+
assert_equal ({class: 'testing', active: :inclusive }), params
|
183
211
|
end
|
184
212
|
|
185
|
-
def
|
186
|
-
|
187
|
-
link = active_link_to('label', '/root', :
|
188
|
-
assert_html link, 'li.active a
|
213
|
+
def test_active_link_to_wrap_tag_class
|
214
|
+
set_path('/root')
|
215
|
+
link = active_link_to('label', '/root', wrap_tag: :li)
|
216
|
+
assert_html link, 'li.active a[href="/root"]', 'label'
|
189
217
|
|
190
|
-
link = active_link_to('label', '/other', :
|
218
|
+
link = active_link_to('label', '/other', wrap_tag: :li)
|
191
219
|
assert_html link, 'li a[href="/other"]', 'label'
|
192
220
|
end
|
193
221
|
|
194
|
-
def
|
195
|
-
|
196
|
-
link = active_link_to('label', '
|
197
|
-
assert_html link, 'a.active[href="
|
222
|
+
def test_active_link_to_with_aria
|
223
|
+
set_path('/root')
|
224
|
+
link = active_link_to('label', '/root')
|
225
|
+
assert_html link, 'a.active[href="/root"][aria-current="page"]', 'label'
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_active_link_to_with_utf8
|
229
|
+
set_path('/äöü')
|
230
|
+
link = active_link_to('label', '/äöü')
|
231
|
+
assert_html link, 'a.active[href="/äöü"]', 'label'
|
198
232
|
end
|
199
233
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,38 +1,41 @@
|
|
1
|
-
require '
|
1
|
+
require 'bundler/setup'
|
2
2
|
require 'minitest/autorun'
|
3
3
|
require 'uri'
|
4
4
|
require 'action_view'
|
5
|
-
|
6
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
5
|
require 'active_link_to'
|
9
6
|
|
10
|
-
|
11
|
-
module FakeRequest
|
12
|
-
class Request
|
13
|
-
attr_accessor :fullpath
|
14
|
-
end
|
15
|
-
def request
|
16
|
-
@request ||= Request.new
|
17
|
-
end
|
18
|
-
def params
|
19
|
-
@params ||= {}
|
20
|
-
end
|
21
|
-
end
|
7
|
+
class MiniTest::Test
|
22
8
|
|
23
|
-
|
9
|
+
# need this to simulate requests that drive active_link_helper
|
10
|
+
module FakeRequest
|
11
|
+
class Request
|
12
|
+
attr_accessor :original_fullpath
|
13
|
+
end
|
14
|
+
def request
|
15
|
+
@request ||= Request.new
|
16
|
+
end
|
17
|
+
def params
|
18
|
+
@params ||= {}
|
19
|
+
end
|
20
|
+
end
|
24
21
|
|
25
|
-
|
22
|
+
ActiveLinkTo.send :include, FakeRequest
|
26
23
|
|
27
24
|
include ActionView::Helpers::UrlHelper
|
28
25
|
include ActionView::Helpers::TagHelper
|
29
26
|
include ActiveLinkTo
|
30
27
|
|
28
|
+
def set_path(path, purge_cache = true)
|
29
|
+
request.original_fullpath = path
|
30
|
+
if purge_cache && defined?(@is_active_link)
|
31
|
+
remove_instance_variable(:@is_active_link)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
31
35
|
def assert_html(html, selector, value = nil)
|
32
36
|
doc = Nokogiri::HTML(html)
|
33
37
|
element = doc.at_css(selector)
|
34
38
|
assert element, "No element found at: `#{selector}`"
|
35
39
|
assert_equal value, element.text if value
|
36
40
|
end
|
37
|
-
|
38
41
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleg Khabarov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: addressable
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
description: Helpful method when you need to add some logic that figures out if the
|
28
42
|
link (or more often navigation item) is selected based on the current page or other
|
29
43
|
arbitrary condition
|
@@ -63,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
63
77
|
version: '0'
|
64
78
|
requirements: []
|
65
79
|
rubyforge_project:
|
66
|
-
rubygems_version: 2.
|
80
|
+
rubygems_version: 2.5.1
|
67
81
|
signing_key:
|
68
82
|
specification_version: 4
|
69
83
|
summary: ActionView helper to render currently active links
|