simple_active_link_to 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +16 -0
- data/Gemfile +10 -0
- data/LICENSE +21 -0
- data/README.md +149 -0
- data/Rakefile +11 -0
- data/lib/simple_active_link_to.rb +3 -0
- data/lib/simple_active_link_to/simple_active_link_to.rb +106 -0
- data/lib/simple_active_link_to/version.rb +3 -0
- data/simple_active_link_to-1.0.0.gem +0 -0
- data/simple_active_link_to.gemspec +22 -0
- data/test/gemfiles/5.0.gemfile +12 -0
- data/test/gemfiles/5.1.gemfile +12 -0
- data/test/gemfiles/5.2.gemfile +12 -0
- data/test/simple_active_link_to_test.rb +212 -0
- data/test/test_helper.rb +41 -0
- metadata +89 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c2fc2f2a0baac9582848c238f5bbfb336d0829226b53f24fe50c3ed59e70ac5b
|
4
|
+
data.tar.gz: 197483dc1ed0902110ed3549379db959e300850548becdf3eecc8fd29d824a32
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 481c153155351ffd7f0344d62e800913383d3fe58499baea5a016008d3ba1d49543148ab9a9040953d1ff92bc3bd988b7580a16c9dd4a5f1fa7824852368d470
|
7
|
+
data.tar.gz: 6442363c9c5a5f0c872886ab8eb24fcb4ab34db9950bfd604a08edc58a891dfe8d836d47b875ac31f7be89529e9befc56f287e2f4c601a808101373d54d233c6
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.3.7
|
4
|
+
- 2.4.4
|
5
|
+
- 2.5.1
|
6
|
+
- ruby-head
|
7
|
+
gemfile:
|
8
|
+
- test/gemfiles/5.0.gemfile
|
9
|
+
- test/gemfiles/5.1.gemfile
|
10
|
+
- test/gemfiles/5.2.gemfile
|
11
|
+
before_install:
|
12
|
+
- gem update --system
|
13
|
+
- gem update bundler
|
14
|
+
matrix:
|
15
|
+
allow_failures:
|
16
|
+
- rvm: ruby-head
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2009-17 Oleg Khabarov
|
2
|
+
Copyright (c) 2020 Fajarullah
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
# simple_active_link_to
|
2
|
+
|
3
|
+
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), `simple_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'.
|
4
|
+
|
5
|
+
[![Gem Version](https://img.shields.io/gem/v/simple_active_link_to.svg?style=flat)](http://rubygems.org/gems/simple_active_link_to)
|
6
|
+
[![Gem Downloads](https://img.shields.io/gem/dt/simple_active_link_to.svg?style=flat)](http://rubygems.org/gems/simple_active_link_to)
|
7
|
+
[![Build Status](https://img.shields.io/travis/comfy/simple_active_link_to.svg?style=flat)](https://travis-ci.org/comfy/simple_active_link_to)
|
8
|
+
[![Gitter](https://badges.gitter.im/comfy/comfortable-mexican-sofa.svg)](https://gitter.im/comfy/comfortable-mexican-sofa)
|
9
|
+
|
10
|
+
support ruby >= 2.4
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
add `gem 'simple_active_link_to'` to Gemfile and run `bundle install`.
|
14
|
+
|
15
|
+
or using `bundle add` command
|
16
|
+
|
17
|
+
`bundle add simple_active_link_to`
|
18
|
+
|
19
|
+
For older Rails apps add `config.gem 'simple_active_link_to'` in config/environment.rb and run `rake gems:install`. Or just checkout this repo into /vendor/plugins directory.
|
20
|
+
|
21
|
+
## Super Simple Example
|
22
|
+
Here's a link that will have a class attached if it happens to be rendered
|
23
|
+
on page with path `/users` or any child of that page, like `/users/123`
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
simple_active_link_to 'Users', '/users'
|
27
|
+
# => <a href="/users" class="active">Users</a>
|
28
|
+
```
|
29
|
+
|
30
|
+
This is exactly the same as:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
simple_active_link_to 'Users', '/users', active: :inclusive
|
34
|
+
# => <a href="/users" class="active">Users</a>
|
35
|
+
```
|
36
|
+
|
37
|
+
## Active Options
|
38
|
+
Here's a list of available options that can be used as the `:active` value
|
39
|
+
|
40
|
+
```
|
41
|
+
* Boolean -> true | false
|
42
|
+
* Symbol -> :exclusive | :inclusive | :exact
|
43
|
+
* Regex -> /regex/
|
44
|
+
* Controller/Action Pair -> [[:controller], [:action_a, :action_b]]
|
45
|
+
* Controller/Specific Action Pair -> [controller: :action_a, controller_b: :action_b]
|
46
|
+
* Hash -> { param_a: 1, param_b: 2 }
|
47
|
+
```
|
48
|
+
|
49
|
+
## More Examples
|
50
|
+
Most of the functionality of `simple_active_link_to` depends on the current
|
51
|
+
url. Specifically, `request.original_fullpath` value. We covered the basic example
|
52
|
+
already, so let's try something more fun.
|
53
|
+
|
54
|
+
We want to highlight a link that matches immediate url, but not the children
|
55
|
+
nodes. Most commonly used for 'home' links.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
# For URL: /users will be active
|
59
|
+
simple_active_link_to 'Users', users_path, active: :exclusive
|
60
|
+
# => <a href="/users" class="active">Users</a>
|
61
|
+
```
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
# But for URL: /users/123 it will not be active
|
65
|
+
simple_active_link_to 'Users', users_path, active: :exclusive
|
66
|
+
# => <a href="/users">Users</a>
|
67
|
+
```
|
68
|
+
|
69
|
+
If we need to set link to be active based on some regular expression, we can do
|
70
|
+
that as well. Let's try to activate links urls of which begin with 'use':
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
simple_active_link_to 'Users', users_path, active: /^\/use/
|
74
|
+
```
|
75
|
+
|
76
|
+
If we need to set link to be active based on an exact match, for example on
|
77
|
+
filter made via a query string, we can do that as well:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
simple_active_link_to 'Users', users_path(role_eq: 'admin'), active: :exact
|
81
|
+
```
|
82
|
+
|
83
|
+
What if we need to mark link active for all URLs that match a particular controller,
|
84
|
+
or action, or both? Or any number of those at the same time? Sure, why not:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
# For matching multiple controllers and actions:
|
88
|
+
simple_active_link_to 'User Edit', edit_user_path(@user), active: [['people', 'news'], ['show', 'edit']]
|
89
|
+
|
90
|
+
# For matching specific controllers and actions:
|
91
|
+
simple_active_link_to 'User Edit', edit_user_path(@user), active: [people: :show, news: :edit]
|
92
|
+
|
93
|
+
# for matching all actions under given controllers:
|
94
|
+
simple_active_link_to 'User Edit', edit_user_path(@user), active: [['people', 'news'], []]
|
95
|
+
|
96
|
+
# for matching all controllers for a particular action
|
97
|
+
simple_active_link_to 'User Edit', edit_user_path(@user), active: [[], ['edit']]
|
98
|
+
```
|
99
|
+
|
100
|
+
Sometimes it should be as easy as giving link true or false value:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
simple_active_link_to 'Users', users_path, active: true
|
104
|
+
```
|
105
|
+
|
106
|
+
If we need to set link to be active based on `params`, we can do that as well:
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
simple_active_link_to 'Admin users', users_path(role_eq: 'admin'), active: { role_eq: 'admin' }
|
110
|
+
```
|
111
|
+
|
112
|
+
## More Options
|
113
|
+
You can specify active and inactive css classes for links:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
simple_active_link_to 'Users', users_path, class_active: 'enabled'
|
117
|
+
# => <a href="/users" class="enabled">Users</a>
|
118
|
+
|
119
|
+
simple_active_link_to 'News', news_path, class_inactive: 'disabled'
|
120
|
+
# => <a href="/news" class="disabled">News</a>
|
121
|
+
```
|
122
|
+
|
123
|
+
Sometimes you want to replace link tag with a span if it's active:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
simple_active_link_to 'Users', users_path, active_disable: true
|
127
|
+
# => <span class="active">Users</span>
|
128
|
+
```
|
129
|
+
|
130
|
+
## Helper Methods
|
131
|
+
You may directly use methods that `simple_active_link_to` relies on.
|
132
|
+
|
133
|
+
`is_active_link?` will return true or false based on the URL and value of the `:active` parameter:
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
is_active_link?(users_path, :inclusive)
|
137
|
+
# => true
|
138
|
+
```
|
139
|
+
|
140
|
+
`active_link_to_class` will return the css class:
|
141
|
+
|
142
|
+
```
|
143
|
+
active_link_to_class(users_path, active: :inclusive)
|
144
|
+
# => 'active'
|
145
|
+
```
|
146
|
+
|
147
|
+
### Copyright
|
148
|
+
|
149
|
+
Copyright (c) 2009-18 Oleg Khabarov. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
module SimpleActiveLinkTo
|
2
|
+
# Wrapper around link_to. Accepts following params:
|
3
|
+
# :active => Boolean | Symbol | Regex | Controller/Action Pair
|
4
|
+
# :class_active => String
|
5
|
+
# :class_inactive => String
|
6
|
+
# :active_disable => Boolean
|
7
|
+
# :wrap_tag => Symbol
|
8
|
+
# Example usage:
|
9
|
+
# simple_active_link_to('/users', class_active: 'enabled')
|
10
|
+
# simple_active_link_to(users_path, active: :exclusive, wrap_tag: :li)
|
11
|
+
def simple_active_link_to(*args, &block)
|
12
|
+
name = block_given? ? capture(&block) : args.shift
|
13
|
+
options = args.shift || {}
|
14
|
+
html_options = args.shift || {}
|
15
|
+
|
16
|
+
url = url_for(options)
|
17
|
+
|
18
|
+
active_options = {}
|
19
|
+
link_options = {}
|
20
|
+
html_options.each do |k, v|
|
21
|
+
if %i[active class_active class_inactive active_disable].member?(k)
|
22
|
+
active_options[k] = v
|
23
|
+
else
|
24
|
+
link_options[k] = v
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
css_class = "#{link_options.delete(:class)} #{active_link_to_class(url, active_options)}"
|
29
|
+
css_class.strip!
|
30
|
+
link_options[:class] = css_class if css_class != ''
|
31
|
+
|
32
|
+
is_active = is_active_link?(url, active_options[:active])
|
33
|
+
link_options[:'aria-current'] = 'page' if is_active
|
34
|
+
|
35
|
+
if active_options[:active_disable] == true && is_active
|
36
|
+
content_tag(:span, name, link_options)
|
37
|
+
else
|
38
|
+
link_to(name, url, link_options)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns css class name. Takes the link's URL and its params
|
43
|
+
# Example usage:
|
44
|
+
# active_link_to_class('/root', class_active: 'on', class_inactive: 'off')
|
45
|
+
#
|
46
|
+
def active_link_to_class(url, options = {})
|
47
|
+
if is_active_link?(url, options[:active])
|
48
|
+
options[:class_active] || 'active'
|
49
|
+
else
|
50
|
+
options[:class_inactive] || ''
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns true or false based on the provided path and condition
|
55
|
+
# Possible condition values are:
|
56
|
+
# Boolean -> true | false
|
57
|
+
# Symbol -> :exclusive | :inclusive
|
58
|
+
# Regex -> /regex/
|
59
|
+
# Controller/Action Pair -> [[:controller], [:action_a, :action_b]]
|
60
|
+
#
|
61
|
+
# Example usage:
|
62
|
+
#
|
63
|
+
# is_active_link?('/root', true)
|
64
|
+
# is_active_link?('/root', :exclusive)
|
65
|
+
# is_active_link?('/root', /^\/root/)
|
66
|
+
# is_active_link?('/root', ['users', ['show', 'edit']])
|
67
|
+
#
|
68
|
+
def is_active_link?(url, condition = nil)
|
69
|
+
@is_active_link ||= {}
|
70
|
+
@is_active_link[[url, condition]] ||= begin
|
71
|
+
original_url = url
|
72
|
+
url = Addressable::URI.parse(url).path
|
73
|
+
path = request.original_fullpath
|
74
|
+
case condition
|
75
|
+
when :inclusive, nil
|
76
|
+
path.match?(%r{^#{Regexp.escape(url).chomp('/')}(/.*|\?.*)?$})
|
77
|
+
when :exclusive
|
78
|
+
path.match?(%r{^#{Regexp.escape(url)}/?(\?.*)?$})
|
79
|
+
when :exact
|
80
|
+
path == original_url
|
81
|
+
when Regexp
|
82
|
+
path.match?(condition)
|
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
|
+
controllers.any? do |controller, action|
|
89
|
+
params[:controller] == controller.to_s && params[:action] == action.to_s
|
90
|
+
end
|
91
|
+
when TrueClass
|
92
|
+
true
|
93
|
+
when FalseClass
|
94
|
+
false
|
95
|
+
when Hash
|
96
|
+
condition.all? do |key, value|
|
97
|
+
params[key].to_s == value.to_s
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
ActiveSupport.on_load :action_view do
|
105
|
+
include SimpleActiveLinkTo
|
106
|
+
end
|
Binary file
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
$:.unshift File.expand_path('../lib', __FILE__)
|
4
|
+
require 'simple_active_link_to/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "simple_active_link_to"
|
8
|
+
s.version = SimpleActiveLinkTo::VERSION
|
9
|
+
s.authors = ["Fajarullah"]
|
10
|
+
s.email = ["frullah12@gmail.com"]
|
11
|
+
s.homepage = "http://github.com/frullah/simple_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
|
+
s.license = "MIT"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
|
18
|
+
s.required_ruby_version = ">= 2.4.0"
|
19
|
+
|
20
|
+
s.add_dependency 'actionpack'
|
21
|
+
s.add_dependency 'addressable'
|
22
|
+
end
|
@@ -0,0 +1,212 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class SimpleActiveLinkToTest < MiniTest::Test
|
4
|
+
|
5
|
+
def test_is_active_link_booleans_test
|
6
|
+
assert is_active_link?('/', true)
|
7
|
+
refute is_active_link?('/', false)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_is_active_link_symbol_inclusive
|
11
|
+
set_path('/root')
|
12
|
+
assert is_active_link?('/root', :inclusive)
|
13
|
+
|
14
|
+
set_path('/root?param=test')
|
15
|
+
assert is_active_link?('/root', :inclusive)
|
16
|
+
|
17
|
+
set_path('/root/child/sub-child')
|
18
|
+
assert is_active_link?('/root', :inclusive)
|
19
|
+
|
20
|
+
set_path('/other')
|
21
|
+
refute is_active_link?('/root', :inclusive)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_is_active_link_symbol_inclusive_implied
|
25
|
+
set_path('/root/child/sub-child')
|
26
|
+
assert is_active_link?('/root')
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_is_active_link_symbol_inclusive_similar_path
|
30
|
+
set_path('/root/abc')
|
31
|
+
refute is_active_link?('/root/a', :inclusive)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_is_active_link_symbol_inclusive_with_last_slash
|
35
|
+
set_path('/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
|
+
set_path('/root_path')
|
41
|
+
refute is_active_link?('/root/')
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_is_active_link_symbol_inclusive_with_link_params
|
45
|
+
set_path('/root?param=test')
|
46
|
+
assert is_active_link?('/root?attr=example')
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_is_active_link_symbol_exclusive
|
50
|
+
set_path('/root')
|
51
|
+
assert is_active_link?('/root', :exclusive)
|
52
|
+
|
53
|
+
set_path('/root?param=test')
|
54
|
+
assert is_active_link?('/root', :exclusive)
|
55
|
+
|
56
|
+
set_path('/root/child')
|
57
|
+
refute is_active_link?('/root', :exclusive)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_is_active_link_symbol_exclusive_with_link_params
|
61
|
+
set_path('/root?param=test')
|
62
|
+
assert is_active_link?('/root?attr=example', :exclusive)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_is_active_link_symbol_exact
|
66
|
+
set_path('/root?param=test')
|
67
|
+
assert is_active_link?('/root?param=test', :exact)
|
68
|
+
|
69
|
+
set_path('/root?param=test')
|
70
|
+
refute is_active_link?('/root?param=exact', :exact)
|
71
|
+
|
72
|
+
set_path('/root')
|
73
|
+
refute is_active_link?('/root?param=test', :exact)
|
74
|
+
|
75
|
+
set_path('/root?param=test')
|
76
|
+
refute is_active_link?('/root', :exact)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_is_active_link_regex
|
80
|
+
set_path('/root')
|
81
|
+
assert is_active_link?('/', /^\/root/)
|
82
|
+
|
83
|
+
set_path('/root/child')
|
84
|
+
assert is_active_link?('/', /^\/r/)
|
85
|
+
|
86
|
+
set_path('/other')
|
87
|
+
refute is_active_link?('/', /^\/r/)
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_is_active_link_array
|
91
|
+
params[:controller], params[:action] = 'controller', 'action'
|
92
|
+
|
93
|
+
assert is_active_link?('/', [['controller'], ['action']])
|
94
|
+
assert is_active_link?('/', [['controller'], ['action', 'action_b']])
|
95
|
+
assert is_active_link?('/', [['controller', 'controller_b'], ['action']])
|
96
|
+
assert is_active_link?('/', [['controller', 'controller_b'], ['action', 'action_b']])
|
97
|
+
assert is_active_link?('/', ['controller', 'action'])
|
98
|
+
assert is_active_link?('/', ['controller', ['action', 'action_b']])
|
99
|
+
assert is_active_link?('/', [['controller', 'controller_b'], 'action'])
|
100
|
+
|
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])
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_is_active_link_hash
|
121
|
+
params[:a] = 1
|
122
|
+
|
123
|
+
assert is_active_link?('/', {a: 1})
|
124
|
+
assert is_active_link?('/', {a: 1, b: nil})
|
125
|
+
|
126
|
+
refute is_active_link?('/', {a: 1, b: 2})
|
127
|
+
refute is_active_link?('/', {a: 2})
|
128
|
+
|
129
|
+
params[:b] = 2
|
130
|
+
|
131
|
+
assert is_active_link?('/', {a: 1, b: 2})
|
132
|
+
assert is_active_link?('/', {a: 1, b: 2, c: nil})
|
133
|
+
|
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)
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_active_link_to_class
|
152
|
+
set_path('/root')
|
153
|
+
assert_equal 'active', active_link_to_class('/root')
|
154
|
+
assert_equal 'on', active_link_to_class('/root', class_active: 'on')
|
155
|
+
|
156
|
+
assert_equal '', active_link_to_class('/other')
|
157
|
+
assert_equal 'off', active_link_to_class('/other', class_inactive: 'off')
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_active_link_to
|
161
|
+
set_path('/root')
|
162
|
+
link = simple_active_link_to('label', '/root')
|
163
|
+
assert_html link, 'a.active[href="/root"]', 'label'
|
164
|
+
|
165
|
+
link = simple_active_link_to('label', '/other')
|
166
|
+
assert_html link, 'a[href="/other"]', 'label'
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_active_link_to_with_existing_class
|
170
|
+
set_path('/root')
|
171
|
+
link = simple_active_link_to('label', '/root', class: 'current')
|
172
|
+
assert_html link, 'a.current.active[href="/root"]', 'label'
|
173
|
+
|
174
|
+
link = simple_active_link_to('label', '/other', class: 'current')
|
175
|
+
assert_html link, 'a.current[href="/other"]', 'label'
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_active_link_to_with_custom_classes
|
179
|
+
set_path('/root')
|
180
|
+
link = simple_active_link_to('label', '/root', class_active: 'on')
|
181
|
+
assert_html link, 'a.on[href="/root"]', 'label'
|
182
|
+
|
183
|
+
link = simple_active_link_to('label', '/other', class_inactive: 'off')
|
184
|
+
assert_html link, 'a.off[href="/other"]', 'label'
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_active_link_to_with_active_disable
|
188
|
+
set_path('/root')
|
189
|
+
link = simple_active_link_to('label', '/root', active_disable: true)
|
190
|
+
assert_html link, 'span.active', 'label'
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_should_not_modify_passed_params
|
194
|
+
set_path('/root')
|
195
|
+
params = {class: 'testing', active: :inclusive}
|
196
|
+
out = simple_active_link_to 'label', '/root', params
|
197
|
+
assert_html out, 'a.testing.active[href="/root"]', 'label'
|
198
|
+
assert_equal ({class: 'testing', active: :inclusive }), params
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_active_link_to_with_aria
|
202
|
+
set_path('/root')
|
203
|
+
link = simple_active_link_to('label', '/root')
|
204
|
+
assert_html link, 'a.active[href="/root"][aria-current="page"]', 'label'
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_active_link_to_with_utf8
|
208
|
+
set_path('/äöü')
|
209
|
+
link = simple_active_link_to('label', '/äöü')
|
210
|
+
assert_html link, 'a.active[href="/äöü"]', 'label'
|
211
|
+
end
|
212
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'uri'
|
4
|
+
require 'action_view'
|
5
|
+
require 'simple_active_link_to'
|
6
|
+
|
7
|
+
class MiniTest::Test
|
8
|
+
|
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
|
21
|
+
|
22
|
+
SimpleActiveLinkTo.send :include, FakeRequest
|
23
|
+
|
24
|
+
include ActionView::Helpers::UrlHelper
|
25
|
+
include ActionView::Helpers::TagHelper
|
26
|
+
include SimpleActiveLinkTo
|
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
|
+
|
35
|
+
def assert_html(html, selector, value = nil)
|
36
|
+
doc = Nokogiri::HTML(html)
|
37
|
+
element = doc.at_css(selector)
|
38
|
+
assert element, "No element found at: `#{selector}`"
|
39
|
+
assert_equal value, element.text if value
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_active_link_to
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fajarullah
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: actionpack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
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'
|
41
|
+
description: Helpful method when you need to add some logic that figures out if the
|
42
|
+
link (or more often navigation item) is selected based on the current page or other
|
43
|
+
arbitrary condition
|
44
|
+
email:
|
45
|
+
- frullah12@gmail.com
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".gitignore"
|
51
|
+
- ".travis.yml"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- lib/simple_active_link_to.rb
|
57
|
+
- lib/simple_active_link_to/simple_active_link_to.rb
|
58
|
+
- lib/simple_active_link_to/version.rb
|
59
|
+
- simple_active_link_to-1.0.0.gem
|
60
|
+
- simple_active_link_to.gemspec
|
61
|
+
- test/gemfiles/5.0.gemfile
|
62
|
+
- test/gemfiles/5.1.gemfile
|
63
|
+
- test/gemfiles/5.2.gemfile
|
64
|
+
- test/simple_active_link_to_test.rb
|
65
|
+
- test/test_helper.rb
|
66
|
+
homepage: http://github.com/frullah/simple_active_link_to
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 2.4.0
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubygems_version: 3.1.2
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: ActionView helper to render currently active links
|
89
|
+
test_files: []
|