active_link_to 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -4
- data/Gemfile +8 -1
- data/README.md +27 -18
- data/Rakefile +1 -0
- data/active_link_to.gemspec +1 -1
- data/lib/active_link_to/active_link_to.rb +11 -2
- data/lib/active_link_to/version.rb +1 -1
- data/test/active_link_to_test.rb +54 -16
- data/test/test_helper.rb +13 -5
- metadata +9 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b138b65cf5484c2189fadada7df67d2d4866b6c
|
4
|
+
data.tar.gz: a236a8a8fe79a8997bd14a1f325a91665d9659b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7df0c88235eb79306d046d474fd0f2fa561d17e6ff00c4e0bc124c40417c4fef22798c62330c75b40efc71e2a2e40357f6f398a974c4d279c354c9139f93d83
|
7
|
+
data.tar.gz: 572bdf400d1dfdafcc9aae908b7254264f7b865241e01c1b20c24fc13fc25a89225c6a6eb7fcb79175c9b5d4747a2308103dda972e35e34cd08268e6fba144fb
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# active_link_to
|
2
|
+
[![Gem Version](https://img.shields.io/gem/v/active_link_to.svg?style=flat)](http://rubygems.org/gems/active_link_to) [![Gem Downloads](https://img.shields.io/gem/dt/active_link_to.svg?style=flat)](http://rubygems.org/gems/active_link_to) [![Build Status](https://img.shields.io/travis/comfy/active_link_to.svg?style=flat)](https://travis-ci.org/comfy/active_link_to)
|
3
|
+
|
2
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'.
|
3
5
|
|
4
6
|
## Install
|
@@ -7,7 +9,7 @@ When installing for Rails 3 applications add this to the Gemfile: `gem 'active_l
|
|
7
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.
|
8
10
|
|
9
11
|
## Super Simple Example
|
10
|
-
Here's a link that will have a class attached if it happens to be rendered
|
12
|
+
Here's a link that will have a class attached if it happens to be rendered
|
11
13
|
on page with path `/users` or any child of that page, like `/users/123`
|
12
14
|
|
13
15
|
```ruby
|
@@ -27,7 +29,7 @@ Here's a list of available options that can be used as the `:active` value
|
|
27
29
|
|
28
30
|
```
|
29
31
|
* Boolean -> true | false
|
30
|
-
* Symbol -> :exclusive | :inclusive
|
32
|
+
* Symbol -> :exclusive | :inclusive | :exact
|
31
33
|
* Regex -> /regex/
|
32
34
|
* Controller/Action Pair -> [[:controller], [:action_a, :action_b]]
|
33
35
|
```
|
@@ -39,8 +41,8 @@ already, so let's try something more fun.
|
|
39
41
|
|
40
42
|
We want to highlight a link that matches immediate url, but not the children
|
41
43
|
nodes. Most commonly used for 'home' links.
|
42
|
-
|
43
|
-
```ruby
|
44
|
+
|
45
|
+
```ruby
|
44
46
|
# For URL: /users will be active
|
45
47
|
active_link_to 'Users', users_path, :active => :exclusive
|
46
48
|
# => <a href="/users" class="active">Users</a>
|
@@ -51,18 +53,25 @@ active_link_to 'Users', users_path, :active => :exclusive
|
|
51
53
|
active_link_to 'Users', users_path, :active => :exclusive
|
52
54
|
# => <a href="/users">Users</a>
|
53
55
|
```
|
54
|
-
|
56
|
+
|
55
57
|
If we need to set link to be active based on some regular expression, we can do
|
56
58
|
that as well. Let's try to activate links urls of which begin with 'use':
|
57
59
|
|
58
60
|
```ruby
|
59
61
|
active_link_to 'Users', users_path, :active => /^\/use/
|
60
62
|
```
|
61
|
-
|
63
|
+
|
64
|
+
If we need to set link to be active based on an exact match, we can do
|
65
|
+
that as well:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
active_link_to 'Users', users_path, :active => :exact
|
69
|
+
```
|
70
|
+
|
62
71
|
What if we need to mark link active for all URLs that match a particular controller,
|
63
72
|
or action, or both? Or any number of those at the same time? Sure, why not:
|
64
|
-
|
65
|
-
```ruby
|
73
|
+
|
74
|
+
```ruby
|
66
75
|
# For matching multiple controllers and actions:
|
67
76
|
active_link_to 'User Edit', edit_user_path(@user), :active => [['people', 'news'], ['show', 'edit']]
|
68
77
|
|
@@ -72,13 +81,13 @@ active_link_to 'User Edit', edit_user_path(@user), :active => [['people', 'news'
|
|
72
81
|
# for matching all controllers for a particular action
|
73
82
|
active_link_to 'User Edit', edit_user_path(@user), :active => [[], ['edit']]
|
74
83
|
```
|
75
|
-
|
84
|
+
|
76
85
|
Sometimes it should be as easy as giving link true or false value:
|
77
86
|
|
78
87
|
```ruby
|
79
88
|
active_link_to 'Users', users_path, :active => true
|
80
89
|
```
|
81
|
-
|
90
|
+
|
82
91
|
## More Options
|
83
92
|
You can specify active and inactive css classes for links:
|
84
93
|
|
@@ -89,7 +98,7 @@ active_link_to 'Users', users_path, :class_active => 'enabled'
|
|
89
98
|
active_link_to 'News', news_path, :class_inactive => 'disabled'
|
90
99
|
# => <a href="/news" class="disabled">News</a>
|
91
100
|
```
|
92
|
-
|
101
|
+
|
93
102
|
Sometimes you want to replace link tag with a span if it's active:
|
94
103
|
|
95
104
|
```ruby
|
@@ -101,21 +110,21 @@ If you are constructing navigation menu it might be helpful to wrap links in ano
|
|
101
110
|
|
102
111
|
```ruby
|
103
112
|
active_link_to 'Users', users_path, :wrap_tag => :li
|
104
|
-
# => <li class="active"><a href="/users">Users</a></li>
|
113
|
+
# => <li class="active"><a href="/users" class="active">Users</a></li>
|
105
114
|
```
|
106
|
-
|
115
|
+
|
107
116
|
## Helper Methods
|
108
|
-
You may directly use methods that `active_link_to` relies on.
|
117
|
+
You may directly use methods that `active_link_to` relies on.
|
109
118
|
|
110
119
|
`is_active_link?` will return true or false based on the URL and value of the `:active` parameter:
|
111
|
-
|
120
|
+
|
112
121
|
```ruby
|
113
122
|
is_active_link?(users_path, :inclusive)
|
114
123
|
# => true
|
115
124
|
```
|
116
|
-
|
125
|
+
|
117
126
|
`active_link_to_class` will return the css class:
|
118
|
-
|
127
|
+
|
119
128
|
```
|
120
129
|
active_link_to_class(users_path, :active => :inclusive)
|
121
130
|
# => 'active'
|
@@ -123,4 +132,4 @@ active_link_to_class(users_path, :active => :inclusive)
|
|
123
132
|
|
124
133
|
### Copyright
|
125
134
|
|
126
|
-
Copyright (c) 2009-
|
135
|
+
Copyright (c) 2009-15 Oleg Khabarov, The Working Group Inc. See LICENSE for details.
|
data/Rakefile
CHANGED
data/active_link_to.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.version = ActiveLinkTo::VERSION
|
9
9
|
s.authors = ["Oleg Khabarov"]
|
10
10
|
s.email = ["oleg@khabarov.ca"]
|
11
|
-
s.homepage = "http://github.com/
|
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
|
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module ActiveLinkTo
|
2
|
-
|
2
|
+
|
3
3
|
# Wrapper around link_to. Accepts following params:
|
4
4
|
# :active => Boolean | Symbol | Regex | Controller/Action Pair
|
5
5
|
# :class_active => String
|
@@ -65,19 +65,24 @@ module ActiveLinkTo
|
|
65
65
|
# Symbol -> :exclusive | :inclusive
|
66
66
|
# Regex -> /regex/
|
67
67
|
# Controller/Action Pair -> [[:controller], [:action_a, :action_b]]
|
68
|
+
#
|
68
69
|
# Example usage:
|
70
|
+
#
|
69
71
|
# is_active_link?('/root', true)
|
70
72
|
# is_active_link?('/root', :exclusive)
|
71
73
|
# is_active_link?('/root', /^\/root/)
|
72
74
|
# is_active_link?('/root', ['users', ['show', 'edit']])
|
73
75
|
#
|
74
76
|
def is_active_link?(url, condition = nil)
|
75
|
-
|
77
|
+
original_url = url
|
78
|
+
url = URI::parse(url).path
|
76
79
|
case condition
|
77
80
|
when :inclusive, nil
|
78
81
|
!request.fullpath.match(/^#{Regexp.escape(url).chomp('/')}(\/.*|\?.*)?$/).blank?
|
79
82
|
when :exclusive
|
80
83
|
!request.fullpath.match(/^#{Regexp.escape(url)}\/?(\?.*)?$/).blank?
|
84
|
+
when :exact
|
85
|
+
request.fullpath == original_url
|
81
86
|
when Regexp
|
82
87
|
!request.fullpath.match(condition).blank?
|
83
88
|
when Array
|
@@ -89,6 +94,10 @@ module ActiveLinkTo
|
|
89
94
|
true
|
90
95
|
when FalseClass
|
91
96
|
false
|
97
|
+
when Hash
|
98
|
+
condition.all? do |key, value|
|
99
|
+
params[key].to_s == value.to_s
|
100
|
+
end
|
92
101
|
end
|
93
102
|
end
|
94
103
|
end
|
data/test/active_link_to_test.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
|
1
|
+
require_relative 'test_helper'
|
2
2
|
|
3
|
-
class ActiveLinkToTest < Test
|
3
|
+
class ActiveLinkToTest < MiniTest::Test
|
4
4
|
|
5
|
-
def
|
5
|
+
def test_is_active_link_booleans_test
|
6
6
|
assert is_active_link?('/', true)
|
7
7
|
assert !is_active_link?('/', false)
|
8
8
|
end
|
@@ -62,6 +62,20 @@ class ActiveLinkToTest < Test::Unit::TestCase
|
|
62
62
|
assert is_active_link?('/root?attr=example', :exclusive)
|
63
63
|
end
|
64
64
|
|
65
|
+
def test_is_active_link_symbol_exact
|
66
|
+
request.fullpath = '/root?param=test'
|
67
|
+
assert is_active_link?('/root?param=test', :exact)
|
68
|
+
|
69
|
+
request.fullpath = '/root?param=test'
|
70
|
+
refute is_active_link?('/root?param=exact', :exact)
|
71
|
+
|
72
|
+
request.fullpath = '/root'
|
73
|
+
refute is_active_link?('/root?param=test', :exact)
|
74
|
+
|
75
|
+
request.fullpath = '/root?param=test'
|
76
|
+
refute is_active_link?('/root', :exact)
|
77
|
+
end
|
78
|
+
|
65
79
|
def test_is_active_link_regex
|
66
80
|
request.fullpath = '/root'
|
67
81
|
assert is_active_link?('/', /^\/root/)
|
@@ -88,6 +102,24 @@ class ActiveLinkToTest < Test::Unit::TestCase
|
|
88
102
|
assert !is_active_link?('/', ['controller', 'action_a'])
|
89
103
|
end
|
90
104
|
|
105
|
+
def test_is_active_link_hash
|
106
|
+
params[:a] = 1
|
107
|
+
|
108
|
+
assert is_active_link?('/', {:a => 1})
|
109
|
+
assert is_active_link?('/', {:a => 1, :b => nil})
|
110
|
+
|
111
|
+
assert !is_active_link?('/', {:a => 1, :b => 2})
|
112
|
+
assert !is_active_link?('/', {:a => 2})
|
113
|
+
|
114
|
+
params[:b] = 2
|
115
|
+
|
116
|
+
assert is_active_link?('/', {:a => 1, :b => 2})
|
117
|
+
assert is_active_link?('/', {:a => 1, :b => 2, :c => nil})
|
118
|
+
|
119
|
+
assert is_active_link?('/', {:a => 1})
|
120
|
+
assert is_active_link?('/', {:b => 2})
|
121
|
+
end
|
122
|
+
|
91
123
|
def test_active_link_to_class
|
92
124
|
request.fullpath = '/root'
|
93
125
|
assert_equal 'active', active_link_to_class('/root')
|
@@ -100,62 +132,68 @@ class ActiveLinkToTest < Test::Unit::TestCase
|
|
100
132
|
def test_active_link_to
|
101
133
|
request.fullpath = '/root'
|
102
134
|
link = active_link_to('label', '/root')
|
103
|
-
|
135
|
+
assert_html link, 'a.active[href="/root"]', 'label'
|
104
136
|
|
105
137
|
link = active_link_to('label', '/other')
|
106
|
-
|
138
|
+
assert_html link, 'a[href="/other"]', 'label'
|
107
139
|
end
|
108
140
|
|
109
141
|
def test_active_link_to_with_existing_class
|
110
142
|
request.fullpath = '/root'
|
111
143
|
link = active_link_to('label', '/root', :class => 'current')
|
112
|
-
|
144
|
+
assert_html link, 'a.current.active[href="/root"]', 'label'
|
113
145
|
|
114
146
|
link = active_link_to('label', '/other', :class => 'current')
|
115
|
-
|
147
|
+
assert_html link, 'a.current[href="/other"]', 'label'
|
116
148
|
end
|
117
149
|
|
118
150
|
def test_active_link_to_with_custom_classes
|
119
151
|
request.fullpath = '/root'
|
120
152
|
link = active_link_to('label', '/root', :class_active => 'on')
|
121
|
-
|
153
|
+
assert_html link, 'a.on[href="/root"]', 'label'
|
122
154
|
|
123
155
|
link = active_link_to('label', '/other', :class_inactive => 'off')
|
124
|
-
|
156
|
+
assert_html link, 'a.off[href="/other"]', 'label'
|
125
157
|
end
|
126
158
|
|
127
159
|
def test_active_link_to_with_wrap_tag
|
128
160
|
request.fullpath = '/root'
|
129
161
|
link = active_link_to('label', '/root', :wrap_tag => :li)
|
130
|
-
|
162
|
+
assert_html link, 'li.active a.active[href="/root"]', 'label'
|
131
163
|
|
132
164
|
link = active_link_to('label', '/root', :wrap_tag => :li, :active_disable => true)
|
133
|
-
|
165
|
+
assert_html link, 'li.active span.active', 'label'
|
134
166
|
|
135
167
|
link = active_link_to('label', '/root', :wrap_tag => :li, :class => 'testing')
|
136
|
-
|
168
|
+
assert_html link, 'li.testing.active a.testing.active[href="/root"]', 'label'
|
137
169
|
end
|
138
170
|
|
139
171
|
def test_active_link_to_with_active_disable
|
140
172
|
request.fullpath = '/root'
|
141
173
|
link = active_link_to('label', '/root', :active_disable => true)
|
142
|
-
|
174
|
+
assert_html link, 'span.active', 'label'
|
143
175
|
end
|
144
176
|
|
145
177
|
def test_should_not_modify_passed_params
|
146
178
|
request.fullpath = '/root'
|
147
179
|
params = { :class => 'testing', :active => :inclusive }
|
148
180
|
out = active_link_to 'label', '/root', params
|
149
|
-
|
181
|
+
assert_html out, 'a.testing.active[href="/root"]', 'label'
|
150
182
|
assert_equal ({:class => 'testing', :active => :inclusive }), params
|
151
183
|
end
|
152
184
|
|
153
185
|
def test_no_empty_class_attribute
|
154
186
|
request.fullpath = '/root'
|
155
187
|
link = active_link_to('label', '/root', :wrap_tag => :li)
|
156
|
-
|
188
|
+
assert_html link, 'li.active a.active[href="/root"]', 'label'
|
157
189
|
|
158
190
|
link = active_link_to('label', '/other', :wrap_tag => :li)
|
159
|
-
|
191
|
+
assert_html link, 'li a[href="/other"]', 'label'
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_active_link_to_with_url
|
195
|
+
request.fullpath = '/root'
|
196
|
+
link = active_link_to('label', 'http://example.com/root')
|
197
|
+
assert_html link, 'a.active[href="http://example.com/root"]', 'label'
|
160
198
|
end
|
161
199
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
require '
|
3
|
-
require '
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'uri'
|
4
|
+
require 'action_view'
|
4
5
|
|
5
6
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
6
7
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
@@ -21,10 +22,17 @@ end
|
|
21
22
|
|
22
23
|
ActiveLinkTo.send :include, FakeRequest
|
23
24
|
|
24
|
-
class Test
|
25
|
-
|
25
|
+
class MiniTest::Test
|
26
|
+
|
26
27
|
include ActionView::Helpers::UrlHelper
|
27
28
|
include ActionView::Helpers::TagHelper
|
28
29
|
include ActiveLinkTo
|
29
|
-
|
30
|
+
|
31
|
+
def assert_html(html, selector, value = nil)
|
32
|
+
doc = Nokogiri::HTML(html)
|
33
|
+
element = doc.at_css(selector)
|
34
|
+
assert element, "No element found at: `#{selector}`"
|
35
|
+
assert_equal value, element.text if value
|
36
|
+
end
|
37
|
+
|
30
38
|
end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
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.3
|
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: 2015-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
description: Helpful method when you need to add some logic that figures out if the
|
@@ -33,7 +33,7 @@ executables: []
|
|
33
33
|
extensions: []
|
34
34
|
extra_rdoc_files: []
|
35
35
|
files:
|
36
|
-
- .gitignore
|
36
|
+
- ".gitignore"
|
37
37
|
- Gemfile
|
38
38
|
- LICENSE
|
39
39
|
- README.md
|
@@ -44,7 +44,7 @@ files:
|
|
44
44
|
- lib/active_link_to/version.rb
|
45
45
|
- test/active_link_to_test.rb
|
46
46
|
- test/test_helper.rb
|
47
|
-
homepage: http://github.com/
|
47
|
+
homepage: http://github.com/comfy/active_link_to
|
48
48
|
licenses: []
|
49
49
|
metadata: {}
|
50
50
|
post_install_message:
|
@@ -53,17 +53,17 @@ require_paths:
|
|
53
53
|
- lib
|
54
54
|
required_ruby_version: !ruby/object:Gem::Requirement
|
55
55
|
requirements:
|
56
|
-
- -
|
56
|
+
- - ">="
|
57
57
|
- !ruby/object:Gem::Version
|
58
58
|
version: '0'
|
59
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
60
|
requirements:
|
61
|
-
- -
|
61
|
+
- - ">="
|
62
62
|
- !ruby/object:Gem::Version
|
63
63
|
version: '0'
|
64
64
|
requirements: []
|
65
65
|
rubyforge_project:
|
66
|
-
rubygems_version: 2.
|
66
|
+
rubygems_version: 2.4.5
|
67
67
|
signing_key:
|
68
68
|
specification_version: 4
|
69
69
|
summary: ActionView helper to render currently active links
|