simple_active_link_to 1.0.3 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -8
- data/lib/simple_active_link_to/simple_active_link_to.rb +40 -32
- data/lib/simple_active_link_to/version.rb +1 -1
- data/lib/simple_active_link_to.rb +0 -1
- metadata +87 -14
- data/.github/workflows/gem.yml +0 -28
- data/.gitignore +0 -2
- data/Gemfile +0 -10
- data/Rakefile +0 -13
- data/simple_active_link_to.gemspec +0 -22
- data/test/gemfiles/5.0.gemfile +0 -12
- data/test/gemfiles/5.1.gemfile +0 -12
- data/test/gemfiles/5.2.gemfile +0 -12
- data/test/gemfiles/6.0.gemfile +0 -12
- data/test/simple_active_link_to_test.rb +0 -220
- data/test/test_helper.rb +0 -55
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b562f2384f992bd5a60fe58b437dadb2f2913a3c6086fd4d7299081fb1577d38
|
4
|
+
data.tar.gz: f858baef43ba051f5ed83930ea79879b7ef1f58f0550caf7822b5fb36b0fe21c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9d17ac58827a31b29f574686e4899046bb58a6008e13c1ff1cd8981421a75ab5a0df58b185ba130bf33de60bce2ea7d73f7d2237e6a76396abd7cbea684cd72
|
7
|
+
data.tar.gz: a40af6bcf2e777900cfb19ea93c4f6757fc1a6022b9c07f5c579fa2f2a46cabae592f4e08ca00537f5f0c71586b5c1721821914ef5c435177170d2dc8da819be
|
data/README.md
CHANGED
@@ -1,18 +1,15 @@
|
|
1
|
-
# simple_active_link_to
|
1
|
+
# simple_active_link_to [![Build Rails < 7](https://github.com/frullah/simple_active_link_to/actions/workflows/test-rails-below-7.yml/badge.svg?branch=master)](https://github.com/frullah/simple_active_link_to/actions/workflows/test-rails-below-7.yml) [![Build Rails >= 7](https://github.com/frullah/simple_active_link_to/actions/workflows/test-rails-7.yml/badge.svg?branch=master)](https://github.com/frullah/simple_active_link_to/actions/workflows/test-rails-7.yml) [![Gem Version](https://img.shields.io/gem/v/simple_active_link_to.svg?style=flat)](http://rubygems.org/gems/simple_active_link_to)
|
2
2
|
|
3
|
-
`simple_active_link_to` is a wrapper for [link_to](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to),
|
3
|
+
`simple_active_link_to` is a wrapper for [link_to](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to), with active state by adding an extra css class `active` by default.
|
4
4
|
|
5
|
-
This
|
5
|
+
This gem works like [active_link_to](https://github.com/comfy/active_link_to), but with additional ```active_disable``` option and without wrap tag.
|
6
6
|
|
7
|
-
|
8
|
-
[![Gem Downloads](https://img.shields.io/gem/dt/simple_active_link_to.svg?style=flat)](http://rubygems.org/gems/simple_active_link_to)
|
9
|
-
|
10
|
-
Tested with ruby >= 2.5.x <= 2.7.x and rails >= 5.0 <= 6.0
|
7
|
+
Tested with ruby 2.5 to 3.0 and rails 5.0 to 6.1, and rails 7.0
|
11
8
|
|
12
9
|
## Installation
|
13
10
|
add `gem 'simple_active_link_to'` to Gemfile and run `bundle install`.
|
14
11
|
|
15
|
-
or
|
12
|
+
or if you want to add with bundle command
|
16
13
|
|
17
14
|
`bundle add simple_active_link_to`
|
18
15
|
|
@@ -125,6 +122,13 @@ simple_active_link_to 'Users', users_path, active_disable: true
|
|
125
122
|
# => <span class="active">Users</span>
|
126
123
|
```
|
127
124
|
|
125
|
+
or you want to append it with hash (`#`) at the end of url to make the link does not reload the page when clicked
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
simple_active_link_to 'Users', users_path, active_disable: :hash
|
129
|
+
# => <a href="/users#" class="active">Users</a>
|
130
|
+
```
|
131
|
+
|
128
132
|
## Helper Methods
|
129
133
|
You may directly use methods that `simple_active_link_to` relies on.
|
130
134
|
|
@@ -141,3 +145,7 @@ is_active_link?(users_path, :inclusive)
|
|
141
145
|
active_link_to_class(users_path, active: :inclusive)
|
142
146
|
# => 'active'
|
143
147
|
```
|
148
|
+
|
149
|
+
## Contributing
|
150
|
+
|
151
|
+
Bug reports and pull requests are welcome.
|
@@ -12,44 +12,45 @@ module SimpleActiveLinkTo
|
|
12
12
|
# :active => Boolean | Symbol | Regex | Controller/Action Pair
|
13
13
|
# :class_active => String
|
14
14
|
# :class_inactive => String
|
15
|
-
# :active_disable => Boolean
|
15
|
+
# :active_disable => Boolean | :hash
|
16
16
|
# Example usage:
|
17
17
|
# simple_active_link_to('/users', class_active: 'enabled')
|
18
|
-
def simple_active_link_to(name = nil,
|
18
|
+
def simple_active_link_to(name = nil, url_options = nil, html_options = nil, &block)
|
19
19
|
if block_given?
|
20
|
-
html_options =
|
21
|
-
|
20
|
+
html_options = url_options
|
21
|
+
url_options = name
|
22
22
|
name = capture(&block)
|
23
|
-
else
|
24
|
-
html_options = html_options
|
25
|
-
options ||= {}
|
26
23
|
end
|
27
24
|
|
28
|
-
html_options ||= {}
|
29
25
|
link_options = {}
|
30
26
|
active_options = {}
|
31
|
-
html_options.
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
27
|
+
if html_options.is_a?(Hash)
|
28
|
+
html_options.each do |k, v|
|
29
|
+
if ACTIVE_OPTIONS.include?(k)
|
30
|
+
active_options[k] = v
|
31
|
+
else
|
32
|
+
link_options[k] = v
|
33
|
+
end
|
36
34
|
end
|
37
35
|
end
|
38
36
|
|
39
|
-
url = url_for(
|
37
|
+
url = url_for(url_options)
|
40
38
|
|
41
|
-
|
42
|
-
|
43
|
-
link_options[:class] = "#{
|
39
|
+
activated = is_active_link?(url, active_options[:active])
|
40
|
+
state_class = link_state_class(activated, active_options)
|
41
|
+
link_options[:class] = "#{link_options[:class]} #{state_class}".strip
|
44
42
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
43
|
+
if activated
|
44
|
+
link_options[:'aria-current'] = 'page'
|
45
|
+
case active_options[:active_disable]
|
46
|
+
when true
|
47
|
+
return content_tag(:span, name, link_options)
|
48
|
+
when :hash
|
49
|
+
url += "#"
|
50
|
+
end
|
52
51
|
end
|
52
|
+
|
53
|
+
link_to(name, url, link_options)
|
53
54
|
end
|
54
55
|
|
55
56
|
# Returns css class name. Takes the link's URL and its params
|
@@ -57,7 +58,11 @@ module SimpleActiveLinkTo
|
|
57
58
|
# active_link_to_class('/root', class_active: 'on', class_inactive: 'off')
|
58
59
|
#
|
59
60
|
def active_link_to_class(url, options = {})
|
60
|
-
|
61
|
+
link_state_class(is_active_link?(url, options[:active]), options)
|
62
|
+
end
|
63
|
+
|
64
|
+
private def link_state_class(activated, options = {})
|
65
|
+
if activated
|
61
66
|
options[:class_active] || 'active'
|
62
67
|
else
|
63
68
|
options[:class_inactive] || ''
|
@@ -80,18 +85,21 @@ module SimpleActiveLinkTo
|
|
80
85
|
#
|
81
86
|
def is_active_link?(url, condition = nil)
|
82
87
|
@is_active_link ||= {}
|
83
|
-
@is_active_link[
|
88
|
+
@is_active_link[url] ||= {}
|
89
|
+
@is_active_link[url][condition] ||= begin
|
84
90
|
case condition
|
85
91
|
when :exclusive, :inclusive, nil
|
86
92
|
url_path = url.split('#').first.split('?').first
|
87
|
-
url_string = URI.
|
88
|
-
request_uri = URI.
|
93
|
+
url_string = URI::DEFAULT_PARSER.unescape(url_path).force_encoding(Encoding::BINARY)
|
94
|
+
request_uri = URI::DEFAULT_PARSER.unescape(request.path).force_encoding(Encoding::BINARY)
|
89
95
|
|
90
|
-
if
|
91
|
-
|
92
|
-
|
96
|
+
if url_string == request_uri
|
97
|
+
true
|
98
|
+
elsif condition != :exclusive
|
93
99
|
closing = url_string.end_with?('/') ? '' : '/'
|
94
|
-
|
100
|
+
request_uri.start_with?(url_string + closing)
|
101
|
+
else
|
102
|
+
false
|
95
103
|
end
|
96
104
|
when :exact
|
97
105
|
request.original_fullpath == url
|
metadata
CHANGED
@@ -1,15 +1,99 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_active_link_to
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fajarullah
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
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: nokogiri
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rack-test
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: benchmark
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: benchmark-ips
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
13
97
|
- !ruby/object:Gem::Dependency
|
14
98
|
name: actionpack
|
15
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -47,22 +131,11 @@ executables: []
|
|
47
131
|
extensions: []
|
48
132
|
extra_rdoc_files: []
|
49
133
|
files:
|
50
|
-
- ".github/workflows/gem.yml"
|
51
|
-
- ".gitignore"
|
52
|
-
- Gemfile
|
53
134
|
- LICENSE
|
54
135
|
- README.md
|
55
|
-
- Rakefile
|
56
136
|
- lib/simple_active_link_to.rb
|
57
137
|
- lib/simple_active_link_to/simple_active_link_to.rb
|
58
138
|
- lib/simple_active_link_to/version.rb
|
59
|
-
- simple_active_link_to.gemspec
|
60
|
-
- test/gemfiles/5.0.gemfile
|
61
|
-
- test/gemfiles/5.1.gemfile
|
62
|
-
- test/gemfiles/5.2.gemfile
|
63
|
-
- test/gemfiles/6.0.gemfile
|
64
|
-
- test/simple_active_link_to_test.rb
|
65
|
-
- test/test_helper.rb
|
66
139
|
homepage: http://github.com/frullah/simple_active_link_to
|
67
140
|
licenses:
|
68
141
|
- MIT
|
@@ -82,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
155
|
- !ruby/object:Gem::Version
|
83
156
|
version: '0'
|
84
157
|
requirements: []
|
85
|
-
rubygems_version: 3.
|
158
|
+
rubygems_version: 3.2.22
|
86
159
|
signing_key:
|
87
160
|
specification_version: 4
|
88
161
|
summary: ActionView helper to render currently active links
|
data/.github/workflows/gem.yml
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
name: Ruby Gem
|
2
|
-
|
3
|
-
on:
|
4
|
-
push:
|
5
|
-
branches: [ master ]
|
6
|
-
pull_request:
|
7
|
-
branches: [ master ]
|
8
|
-
|
9
|
-
jobs:
|
10
|
-
tests:
|
11
|
-
name: Tests
|
12
|
-
runs-on: ubuntu-latest
|
13
|
-
strategy:
|
14
|
-
matrix:
|
15
|
-
ruby: [2.5, 2.6, 2.7]
|
16
|
-
rails: ['5.0', '5.1', '5.2', '6.0']
|
17
|
-
steps:
|
18
|
-
- uses: actions/checkout@v2
|
19
|
-
- name: Set up Ruby 2.5
|
20
|
-
uses: ruby/setup-ruby@v1
|
21
|
-
with:
|
22
|
-
ruby-version: ${{ matrix.ruby }}
|
23
|
-
- name: Bundle install
|
24
|
-
run: bundle install
|
25
|
-
env:
|
26
|
-
BUNDLE_GEMFILE: "test/gemfiles/${{ matrix.rails }}.gemfile"
|
27
|
-
- run: bundle exec rake
|
28
|
-
|
data/.gitignore
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'rake/testtask'
|
5
|
-
require 'bundler/setup'
|
6
|
-
|
7
|
-
Rake::TestTask.new(:test) do |test|
|
8
|
-
test.libs << 'lib' << 'test'
|
9
|
-
test.pattern = 'test/**/*_test.rb'
|
10
|
-
test.verbose = true
|
11
|
-
end
|
12
|
-
|
13
|
-
task default: :test
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
$:.unshift File.expand_path('lib', __dir__)
|
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', '>= 5.0'
|
21
|
-
s.add_dependency 'activesupport', '>= 5.0'
|
22
|
-
end
|
data/test/gemfiles/5.0.gemfile
DELETED
data/test/gemfiles/5.1.gemfile
DELETED
data/test/gemfiles/5.2.gemfile
DELETED
data/test/gemfiles/6.0.gemfile
DELETED
@@ -1,220 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'test_helper'
|
4
|
-
|
5
|
-
class SimpleActiveLinkToTest < MiniTest::Test
|
6
|
-
|
7
|
-
def test_is_active_link_booleans_test
|
8
|
-
assert is_active_link?('/', true)
|
9
|
-
refute is_active_link?('/', false)
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_is_active_link_symbol_inclusive
|
13
|
-
set_path('/root')
|
14
|
-
assert is_active_link?('/root', :inclusive)
|
15
|
-
|
16
|
-
set_path('/root?param=test')
|
17
|
-
assert is_active_link?('/root', :inclusive)
|
18
|
-
|
19
|
-
set_path('/root/child/sub-child')
|
20
|
-
assert is_active_link?('/root', :inclusive)
|
21
|
-
|
22
|
-
set_path('/other')
|
23
|
-
refute is_active_link?('/root', :inclusive)
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_is_active_link_symbol_inclusive_implied
|
27
|
-
set_path('/root/child/sub-child')
|
28
|
-
assert is_active_link?('/root')
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_is_active_link_symbol_inclusive_similar_path
|
32
|
-
set_path('/root/abc')
|
33
|
-
refute is_active_link?('/root/a', :inclusive)
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_is_active_link_symbol_inclusive_with_last_slash
|
37
|
-
set_path('/root/abc')
|
38
|
-
assert is_active_link?('/root/')
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_is_active_link_symbol_inclusive_with_last_slash_and_similar_path
|
42
|
-
set_path('/root_path')
|
43
|
-
refute is_active_link?('/root/')
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_is_active_link_symbol_inclusive_with_link_params
|
47
|
-
set_path('/root?param=test')
|
48
|
-
assert is_active_link?('/root?attr=example')
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_is_active_link_symbol_exclusive
|
52
|
-
set_path('/root')
|
53
|
-
assert is_active_link?('/root', :exclusive)
|
54
|
-
|
55
|
-
set_path('/root?param=test')
|
56
|
-
assert is_active_link?('/root', :exclusive)
|
57
|
-
|
58
|
-
set_path('/root/child')
|
59
|
-
refute is_active_link?('/root', :exclusive)
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_is_active_link_symbol_exclusive_with_link_params
|
63
|
-
set_path('/root?param=test')
|
64
|
-
assert is_active_link?('/root?attr=example', :exclusive)
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_is_active_link_symbol_exact
|
68
|
-
set_path('/root?param=test')
|
69
|
-
assert is_active_link?('/root?param=test', :exact)
|
70
|
-
|
71
|
-
set_path('/root?param=test')
|
72
|
-
refute is_active_link?('/root?param=exact', :exact)
|
73
|
-
|
74
|
-
set_path('/root')
|
75
|
-
refute is_active_link?('/root?param=test', :exact)
|
76
|
-
|
77
|
-
set_path('/root?param=test')
|
78
|
-
refute is_active_link?('/root', :exact)
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_is_active_link_regex
|
82
|
-
set_path('/root')
|
83
|
-
assert is_active_link?('/', /^\/root/)
|
84
|
-
|
85
|
-
set_path('/root/child')
|
86
|
-
assert is_active_link?('/', /^\/r/)
|
87
|
-
|
88
|
-
set_path('/other')
|
89
|
-
refute is_active_link?('/', /^\/r/)
|
90
|
-
end
|
91
|
-
|
92
|
-
def test_is_active_link_array
|
93
|
-
params[:controller], params[:action] = 'controller', 'action'
|
94
|
-
|
95
|
-
assert is_active_link?('/', [['controller'], ['action']])
|
96
|
-
assert is_active_link?('/', [['controller'], ['action', 'action_b']])
|
97
|
-
assert is_active_link?('/', [['controller', 'controller_b'], ['action']])
|
98
|
-
assert is_active_link?('/', [['controller', 'controller_b'], ['action', 'action_b']])
|
99
|
-
assert is_active_link?('/', ['controller', 'action'])
|
100
|
-
assert is_active_link?('/', ['controller', ['action', 'action_b']])
|
101
|
-
assert is_active_link?('/', [['controller', 'controller_b'], 'action'])
|
102
|
-
|
103
|
-
refute is_active_link?('/', ['controller_a', 'action'])
|
104
|
-
refute is_active_link?('/', ['controller', 'action_a'])
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_is_active_link_array_with_hash
|
108
|
-
params[:controller], params[:action] = 'controller', 'action'
|
109
|
-
|
110
|
-
assert is_active_link?('/', [controller: :action])
|
111
|
-
assert is_active_link?('/', ['controller' => 'action'])
|
112
|
-
|
113
|
-
refute is_active_link?('/', [controller_b: :action])
|
114
|
-
refute is_active_link?('/', [controller: :action_b])
|
115
|
-
refute is_active_link?('/', [controller_b: :action_b])
|
116
|
-
|
117
|
-
params[:controller], params[:action] = 'controller_b', 'action_b'
|
118
|
-
|
119
|
-
assert is_active_link?('/', [controller: :action, controller_b: :action_b])
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_is_active_link_hash
|
123
|
-
params[:a] = 1
|
124
|
-
|
125
|
-
assert is_active_link?('/', {a: 1})
|
126
|
-
assert is_active_link?('/', {a: 1, b: nil})
|
127
|
-
|
128
|
-
refute is_active_link?('/', {a: 1, b: 2})
|
129
|
-
refute is_active_link?('/', {a: 2})
|
130
|
-
|
131
|
-
params[:b] = 2
|
132
|
-
|
133
|
-
assert is_active_link?('/', {a: 1, b: 2})
|
134
|
-
assert is_active_link?('/', {a: 1, b: 2, c: nil})
|
135
|
-
|
136
|
-
assert is_active_link?('/', {a: 1})
|
137
|
-
assert is_active_link?('/', {b: 2})
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_is_active_link_with_anchor
|
141
|
-
set_path('/foo')
|
142
|
-
assert is_active_link?('/foo#anchor', :exclusive)
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_is_active_link_with_memoization
|
146
|
-
set_path('/')
|
147
|
-
assert is_active_link?('/', :exclusive)
|
148
|
-
|
149
|
-
set_path('/other', false)
|
150
|
-
assert is_active_link?('/', :exclusive)
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_active_link_to_class
|
154
|
-
set_path('/root')
|
155
|
-
assert_equal 'active', active_link_to_class('/root')
|
156
|
-
assert_equal 'on', active_link_to_class('/root', class_active: 'on')
|
157
|
-
|
158
|
-
assert_equal '', active_link_to_class('/other')
|
159
|
-
assert_equal 'off', active_link_to_class('/other', class_inactive: 'off')
|
160
|
-
end
|
161
|
-
|
162
|
-
def test_active_link_to
|
163
|
-
set_path('/root')
|
164
|
-
link = simple_active_link_to('label', '/root')
|
165
|
-
assert_html link, 'a.active[href="/root"]', 'label'
|
166
|
-
|
167
|
-
link = simple_active_link_to('label', '/other')
|
168
|
-
assert_html link, 'a[href="/other"]', 'label'
|
169
|
-
end
|
170
|
-
|
171
|
-
def test_active_link_to_with_existing_class
|
172
|
-
set_path('/root')
|
173
|
-
link = simple_active_link_to('label', '/root', class: 'current')
|
174
|
-
assert_html link, 'a.current.active[href="/root"]', 'label'
|
175
|
-
|
176
|
-
link = simple_active_link_to('label', '/other', class: 'current')
|
177
|
-
assert_html link, 'a.current[href="/other"]', 'label'
|
178
|
-
end
|
179
|
-
|
180
|
-
def test_active_link_to_with_custom_classes
|
181
|
-
set_path('/root')
|
182
|
-
link = simple_active_link_to('label', '/root', class_active: 'on')
|
183
|
-
assert_html link, 'a.on[href="/root"]', 'label'
|
184
|
-
|
185
|
-
link = simple_active_link_to('label', '/other', class_inactive: 'off')
|
186
|
-
assert_html link, 'a.off[href="/other"]', 'label'
|
187
|
-
end
|
188
|
-
|
189
|
-
def test_active_link_to_with_active_disable
|
190
|
-
set_path('/root')
|
191
|
-
link = simple_active_link_to('label', '/root', active_disable: true)
|
192
|
-
assert_html link, 'span.active', 'label'
|
193
|
-
end
|
194
|
-
|
195
|
-
def test_should_not_modify_passed_params
|
196
|
-
set_path('/root')
|
197
|
-
params = {class: 'testing', active: :inclusive}
|
198
|
-
out = simple_active_link_to 'label', '/root', params
|
199
|
-
assert_html out, 'a.testing.active[href="/root"]', 'label'
|
200
|
-
assert_equal ({class: 'testing', active: :inclusive }), params
|
201
|
-
end
|
202
|
-
|
203
|
-
def test_active_link_to_with_aria
|
204
|
-
set_path('/root')
|
205
|
-
link = simple_active_link_to('label', '/root')
|
206
|
-
assert_html link, 'a.active[href="/root"][aria-current="page"]', 'label'
|
207
|
-
end
|
208
|
-
|
209
|
-
def test_active_link_to_with_utf8
|
210
|
-
set_path('/äöü')
|
211
|
-
link = simple_active_link_to('label', '/äöü')
|
212
|
-
assert_html link, 'a.active[href="/äöü"]', 'label'
|
213
|
-
end
|
214
|
-
|
215
|
-
def test_active_link_to_with_block
|
216
|
-
set_path('/root')
|
217
|
-
link = simple_active_link_to('/root') { 'label' }
|
218
|
-
assert_html link, 'a.active[href="/root"]', 'label'
|
219
|
-
end
|
220
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'bundler/setup'
|
4
|
-
require 'minitest/autorun'
|
5
|
-
require 'uri'
|
6
|
-
require 'action_view'
|
7
|
-
require 'simple_active_link_to'
|
8
|
-
|
9
|
-
class MiniTest::Test
|
10
|
-
# need this to simulate requests that drive active_link_helper
|
11
|
-
module FakeRequest
|
12
|
-
class Request
|
13
|
-
attr_accessor :original_fullpath
|
14
|
-
|
15
|
-
def path
|
16
|
-
return original_fullpath unless original_fullpath&.include?('?')
|
17
|
-
|
18
|
-
@path ||= original_fullpath.split('?').first
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def request
|
23
|
-
@request ||= Request.new
|
24
|
-
end
|
25
|
-
|
26
|
-
def params
|
27
|
-
@params ||= {}
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
module FakeCapture
|
32
|
-
def capture
|
33
|
-
yield
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
SimpleActiveLinkTo.include FakeRequest
|
38
|
-
SimpleActiveLinkTo.include FakeCapture
|
39
|
-
|
40
|
-
include ActionView::Helpers::UrlHelper
|
41
|
-
include ActionView::Helpers::TagHelper
|
42
|
-
include SimpleActiveLinkTo
|
43
|
-
|
44
|
-
def set_path(path, purge_cache = true)
|
45
|
-
request.original_fullpath = path
|
46
|
-
remove_instance_variable(:@is_active_link) if purge_cache && defined?(@is_active_link)
|
47
|
-
end
|
48
|
-
|
49
|
-
def assert_html(html, selector, value = nil)
|
50
|
-
doc = Nokogiri::HTML(html)
|
51
|
-
element = doc.at_css(selector)
|
52
|
-
assert element, "No element found at: `#{selector}`"
|
53
|
-
assert_equal value, element.text if value
|
54
|
-
end
|
55
|
-
end
|