simple_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/.github/workflows/gem.yml +14 -10
- data/README.md +1 -1
- data/Rakefile +2 -0
- data/lib/simple_active_link_to.rb +3 -1
- data/lib/simple_active_link_to/simple_active_link_to.rb +48 -22
- data/lib/simple_active_link_to/version.rb +3 -1
- data/simple_active_link_to.gemspec +12 -12
- data/test/gemfiles/6.0.gemfile +12 -0
- data/test/simple_active_link_to_test.rb +8 -0
- data/test/test_helper.rb +19 -5
- metadata +8 -8
- data/.travis.yml +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4427909ec1eefd905a39cd23945f33ae5b999cbc19cb919eecf9e25ef4cf5e37
|
4
|
+
data.tar.gz: 84a29b27108c169427b4791a4c36ff2c141027604ebc6a62cd3372fa64362bb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f96594d2e4acf09083588e821e49636f633710eba41f59fe452e6268600eaa2449fe14c4bee126a19ab4618ae09ef863a7f9d6af9436804bd21ec6d5952c9537
|
7
|
+
data.tar.gz: af11845584d76188f7061228f0fb91db1b6a89a6c1c9c1837efdee20968fefda8cbd551fe68cdb83bfbe8278ff1d6fd560f7a3534c18d2a17cd7a4f8b6345ec2
|
data/.github/workflows/gem.yml
CHANGED
@@ -7,18 +7,22 @@ on:
|
|
7
7
|
branches: [ master ]
|
8
8
|
|
9
9
|
jobs:
|
10
|
-
|
11
|
-
name:
|
10
|
+
tests:
|
11
|
+
name: Tests
|
12
12
|
runs-on: ubuntu-latest
|
13
|
-
|
13
|
+
strategy:
|
14
|
+
matrix:
|
15
|
+
ruby: [2.5, 2.6, 2.7]
|
16
|
+
rails: ['5.0', '5.1', '5.2', '6.0']
|
14
17
|
steps:
|
15
18
|
- uses: actions/checkout@v2
|
16
|
-
- name: Set up Ruby 2.
|
17
|
-
uses:
|
19
|
+
- name: Set up Ruby 2.5
|
20
|
+
uses: ruby/setup-ruby@v1
|
18
21
|
with:
|
19
|
-
ruby-version:
|
20
|
-
|
21
|
-
- name: Install bundle gems
|
22
|
+
ruby-version: ${{ matrix.ruby }}
|
23
|
+
- name: Bundle install
|
22
24
|
run: bundle install
|
23
|
-
|
24
|
-
|
25
|
+
env:
|
26
|
+
BUNDLE_GEMFILE: "test/gemfiles/${{ matrix.rails }}.gemfile"
|
27
|
+
- run: bundle exec rake
|
28
|
+
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ This project is fork of [active_link_to](https://github.com/comfy/active_link_to
|
|
7
7
|
[![Gem Version](https://img.shields.io/gem/v/simple_active_link_to.svg?style=flat)](http://rubygems.org/gems/simple_active_link_to)
|
8
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
9
|
|
10
|
-
|
10
|
+
Tested with ruby >= 2.5.x <= 2.7.x and rails >= 5.0 <= 6.0
|
11
11
|
|
12
12
|
## Installation
|
13
13
|
add `gem 'simple_active_link_to'` to Gemfile and run `bundle install`.
|
data/Rakefile
CHANGED
@@ -1,5 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SimpleActiveLinkTo
|
2
|
-
ACTIVE_OPTIONS = %i[
|
4
|
+
ACTIVE_OPTIONS = %i[
|
5
|
+
active
|
6
|
+
class_active
|
7
|
+
class_inactive
|
8
|
+
active_disable
|
9
|
+
].freeze
|
3
10
|
|
4
11
|
# Wrapper around link_to. Accepts following params:
|
5
12
|
# :active => Boolean | Symbol | Regex | Controller/Action Pair
|
@@ -8,15 +15,32 @@ module SimpleActiveLinkTo
|
|
8
15
|
# :active_disable => Boolean
|
9
16
|
# Example usage:
|
10
17
|
# simple_active_link_to('/users', class_active: 'enabled')
|
11
|
-
def simple_active_link_to(
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
18
|
+
def simple_active_link_to(name = nil, options = nil, html_options = nil, &block)
|
19
|
+
if block_given?
|
20
|
+
html_options = options
|
21
|
+
options = name
|
22
|
+
name = capture(&block)
|
23
|
+
else
|
24
|
+
html_options = html_options
|
25
|
+
options ||= {}
|
26
|
+
end
|
16
27
|
|
17
|
-
|
18
|
-
|
19
|
-
|
28
|
+
html_options ||= {}
|
29
|
+
link_options = {}
|
30
|
+
active_options = {}
|
31
|
+
html_options.each do |k, v|
|
32
|
+
if ACTIVE_OPTIONS.include?(k)
|
33
|
+
active_options[k] = v
|
34
|
+
else
|
35
|
+
link_options[k] = v
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
url = url_for(options)
|
40
|
+
|
41
|
+
css_class = link_options[:class]
|
42
|
+
active_class = active_link_to_class(url, active_options)
|
43
|
+
link_options[:class] = "#{css_class} #{active_class}".strip
|
20
44
|
|
21
45
|
is_active = is_active_link?(url, active_options[:active])
|
22
46
|
link_options[:'aria-current'] = 'page' if is_active
|
@@ -57,18 +81,22 @@ module SimpleActiveLinkTo
|
|
57
81
|
def is_active_link?(url, condition = nil)
|
58
82
|
@is_active_link ||= {}
|
59
83
|
@is_active_link[[url, condition]] ||= begin
|
60
|
-
original_url = url
|
61
|
-
url = Addressable::URI.parse(url).path
|
62
|
-
path = request.original_fullpath
|
63
84
|
case condition
|
64
|
-
when :inclusive, nil
|
65
|
-
|
66
|
-
|
67
|
-
|
85
|
+
when :exclusive, :inclusive, nil
|
86
|
+
url_path = url.split('#').first.split('?').first
|
87
|
+
url_string = URI.parser.unescape(url_path).force_encoding(Encoding::BINARY)
|
88
|
+
request_uri = URI.parser.unescape(request.path).force_encoding(Encoding::BINARY)
|
89
|
+
|
90
|
+
if condition == :exclusive
|
91
|
+
url_string == request_uri
|
92
|
+
else
|
93
|
+
closing = url_string.end_with?('/') ? '' : '/'
|
94
|
+
url_string == request_uri || request_uri.start_with?(url_string + closing)
|
95
|
+
end
|
68
96
|
when :exact
|
69
|
-
|
97
|
+
request.original_fullpath == url
|
70
98
|
when Regexp
|
71
|
-
|
99
|
+
request.original_fullpath.match?(condition)
|
72
100
|
when Array
|
73
101
|
controllers = Array(condition[0])
|
74
102
|
actions = Array(condition[1])
|
@@ -77,10 +105,8 @@ module SimpleActiveLinkTo
|
|
77
105
|
controllers.any? do |controller, action|
|
78
106
|
params[:controller] == controller.to_s && params[:action] == action.to_s
|
79
107
|
end
|
80
|
-
when TrueClass
|
81
|
-
|
82
|
-
when FalseClass
|
83
|
-
false
|
108
|
+
when TrueClass, FalseClass
|
109
|
+
condition
|
84
110
|
when Hash
|
85
111
|
condition.all? do |key, value|
|
86
112
|
params[key].to_s == value.to_s
|
@@ -1,22 +1,22 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
$:.unshift File.expand_path('
|
3
|
+
$:.unshift File.expand_path('lib', __dir__)
|
4
4
|
require 'simple_active_link_to/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
7
|
+
s.name = 'simple_active_link_to'
|
8
8
|
s.version = SimpleActiveLinkTo::VERSION
|
9
|
-
s.authors = [
|
10
|
-
s.email = [
|
11
|
-
s.homepage =
|
12
|
-
s.summary =
|
13
|
-
s.description =
|
14
|
-
s.license =
|
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
15
|
|
16
16
|
s.files = `git ls-files`.split("\n")
|
17
17
|
|
18
|
-
s.required_ruby_version =
|
18
|
+
s.required_ruby_version = '>= 2.4.0'
|
19
19
|
|
20
|
-
s.add_dependency 'actionpack'
|
21
|
-
s.add_dependency '
|
20
|
+
s.add_dependency 'actionpack', '>= 5.0'
|
21
|
+
s.add_dependency 'activesupport', '>= 5.0'
|
22
22
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'test_helper'
|
2
4
|
|
3
5
|
class SimpleActiveLinkToTest < MiniTest::Test
|
@@ -209,4 +211,10 @@ class SimpleActiveLinkToTest < MiniTest::Test
|
|
209
211
|
link = simple_active_link_to('label', '/äöü')
|
210
212
|
assert_html link, 'a.active[href="/äöü"]', 'label'
|
211
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
|
212
220
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'bundler/setup'
|
2
4
|
require 'minitest/autorun'
|
3
5
|
require 'uri'
|
@@ -5,21 +7,35 @@ require 'action_view'
|
|
5
7
|
require 'simple_active_link_to'
|
6
8
|
|
7
9
|
class MiniTest::Test
|
8
|
-
|
9
10
|
# need this to simulate requests that drive active_link_helper
|
10
11
|
module FakeRequest
|
11
12
|
class Request
|
12
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
|
13
20
|
end
|
21
|
+
|
14
22
|
def request
|
15
23
|
@request ||= Request.new
|
16
24
|
end
|
25
|
+
|
17
26
|
def params
|
18
27
|
@params ||= {}
|
19
28
|
end
|
20
29
|
end
|
21
30
|
|
22
|
-
|
31
|
+
module FakeCapture
|
32
|
+
def capture
|
33
|
+
yield
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
SimpleActiveLinkTo.include FakeRequest
|
38
|
+
SimpleActiveLinkTo.include FakeCapture
|
23
39
|
|
24
40
|
include ActionView::Helpers::UrlHelper
|
25
41
|
include ActionView::Helpers::TagHelper
|
@@ -27,9 +43,7 @@ class MiniTest::Test
|
|
27
43
|
|
28
44
|
def set_path(path, purge_cache = true)
|
29
45
|
request.original_fullpath = path
|
30
|
-
if purge_cache && defined?(@is_active_link)
|
31
|
-
remove_instance_variable(:@is_active_link)
|
32
|
-
end
|
46
|
+
remove_instance_variable(:@is_active_link) if purge_cache && defined?(@is_active_link)
|
33
47
|
end
|
34
48
|
|
35
49
|
def assert_html(html, selector, value = nil)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_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
|
- Fajarullah
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-09-
|
11
|
+
date: 2020-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
19
|
+
version: '5.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
|
-
version: '0'
|
26
|
+
version: '5.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
33
|
+
version: '5.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
40
|
+
version: '5.0'
|
41
41
|
description: Helpful method when you need to add some logic that figures out if the
|
42
42
|
link (or more often navigation item) is selected based on the current page or other
|
43
43
|
arbitrary condition
|
@@ -49,7 +49,6 @@ extra_rdoc_files: []
|
|
49
49
|
files:
|
50
50
|
- ".github/workflows/gem.yml"
|
51
51
|
- ".gitignore"
|
52
|
-
- ".travis.yml"
|
53
52
|
- Gemfile
|
54
53
|
- LICENSE
|
55
54
|
- README.md
|
@@ -61,6 +60,7 @@ files:
|
|
61
60
|
- test/gemfiles/5.0.gemfile
|
62
61
|
- test/gemfiles/5.1.gemfile
|
63
62
|
- test/gemfiles/5.2.gemfile
|
63
|
+
- test/gemfiles/6.0.gemfile
|
64
64
|
- test/simple_active_link_to_test.rb
|
65
65
|
- test/test_helper.rb
|
66
66
|
homepage: http://github.com/frullah/simple_active_link_to
|
data/.travis.yml
DELETED
@@ -1,16 +0,0 @@
|
|
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
|