link_to_active_state 1.0.4 → 1.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +1 -3
- data/README.md +3 -2
- data/lib/link_to_active_state/version.rb +1 -1
- data/lib/link_to_active_state/view_helpers/url_helper.rb +21 -14
- data/link_to_active_state.gemspec +3 -3
- data/spec/link_to_active_state_spec.rb +45 -19
- metadata +18 -40
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6235f67887d012b32ed65cc5639c1d4235600e9572e3cf70a864caa4b300a83a
|
4
|
+
data.tar.gz: 47dd042be4c6b327b5aaa4f2c620ffc40d5eb713633ec8525cf83a6ec852151d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7be4375b7dd4fc5166fdc933e8d24c165f0b04f1c3d5c49883754a0436b1d9406c6dd85bf96e04d834a0290eff4cb540e9638e6296348fcc40fe0e23cf56e9f1
|
7
|
+
data.tar.gz: 0eba6b78e4d563f9c59d910371a779858a89f70f3ef6a1a89038ec50343f18383cc18faae87033c2109f6c9f83af96048ed7a8ba05a53da686ff2d2fc0f4a0ea
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# LinkToActiveState
|
2
2
|
|
3
|
-
[![Build Status](https://travis-ci.org/robotmay/link_to_active_state.png?branch=master)](https://travis-ci.org/robotmay/link_to_active_state)
|
3
|
+
[![Build Status](https://travis-ci.org/robotmay/link_to_active_state.png?branch=master)](https://travis-ci.org/robotmay/link_to_active_state)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/link_to_active_state.svg)](http://badge.fury.io/rb/link_to_active_state)
|
4
5
|
|
5
6
|
A simple gem to implement active states on links using the standard Rails `link_to` helper.
|
6
7
|
This can be helpful in navigation lists or buttons to give them a class when the current URL matches a condition on the link helper.
|
@@ -13,7 +14,7 @@ Add this line to your application's Gemfile:
|
|
13
14
|
|
14
15
|
Or install from the repository:
|
15
16
|
|
16
|
-
gem 'link_to_active_state', git: 'git://github.com/robotmay/link_to_active_state.git', tag: '
|
17
|
+
gem 'link_to_active_state', git: 'git://github.com/robotmay/link_to_active_state.git', tag: 'v1.0.5'
|
17
18
|
|
18
19
|
And then execute:
|
19
20
|
|
@@ -1,43 +1,44 @@
|
|
1
1
|
require 'action_view'
|
2
2
|
require 'link_to_active_state'
|
3
|
-
require 'pry'
|
4
3
|
|
5
4
|
module LinkToActiveState
|
6
5
|
module ViewHelpers
|
7
6
|
module UrlHelper
|
8
7
|
def self.included(base)
|
9
|
-
base.send(:
|
8
|
+
base.send(:alias_method, :link_to_without_active_state, :link_to)
|
9
|
+
base.send(:alias_method, :link_to, :link_to_with_active_state)
|
10
10
|
end
|
11
11
|
|
12
12
|
def link_to_with_active_state(*args, &block)
|
13
|
-
options = {}
|
14
|
-
|
15
13
|
link_options, html_options = if block_given?
|
16
14
|
[args.first, args.second]
|
17
15
|
else
|
18
16
|
[args[1], args[2]]
|
19
17
|
end
|
20
18
|
|
19
|
+
options = {}
|
21
20
|
link_options ||= {}
|
21
|
+
html_options ||= {}
|
22
|
+
wrapper_options = html_options.delete(:active_wrapper_options) || {}
|
22
23
|
|
23
24
|
if html_options.present? && html_options[:active_on].present?
|
24
25
|
active_on = html_options.delete(:active_on)
|
25
26
|
active_state = html_options.delete(:active_state) || "active"
|
26
27
|
|
27
|
-
if is_active?(active_on, link_options)
|
28
|
+
if is_active?(active_on, link_options, html_options)
|
28
29
|
case active_state
|
29
30
|
when Proc
|
30
31
|
options = options.merge(active_state.call(html_options))
|
32
|
+
wrapper_options = wrapper_options.merge(active_state.call(html_options))
|
31
33
|
when String
|
32
34
|
options[:class] = merge_class(html_options[:class], active_state)
|
35
|
+
wrapper_options[:class] = merge_class(wrapper_options[:class], active_state)
|
33
36
|
end
|
34
37
|
end
|
35
38
|
end
|
36
39
|
|
37
40
|
if html_options.present? && html_options[:active_wrapper]
|
38
41
|
element_or_proc = html_options.delete(:active_wrapper)
|
39
|
-
wrapper_options = html_options.delete(:active_wrapper_options) || {}
|
40
|
-
wrapper_options = wrapper_options.merge(options)
|
41
42
|
|
42
43
|
render_with_wrapper(element_or_proc, wrapper_options) do
|
43
44
|
link_to_without_active_state(*args, &block)
|
@@ -49,20 +50,26 @@ module LinkToActiveState
|
|
49
50
|
end
|
50
51
|
|
51
52
|
private
|
52
|
-
def is_active?(active_on, link_options = {})
|
53
|
+
def is_active?(active_on, link_options = {}, html_options = {})
|
54
|
+
if html_options.present? && html_options[:ignore_query]
|
55
|
+
path = request.fullpath.gsub( /\?.*/, "" )
|
56
|
+
else
|
57
|
+
path = request.fullpath
|
58
|
+
end
|
59
|
+
|
53
60
|
case active_on
|
54
61
|
when String
|
55
|
-
|
62
|
+
path == active_on
|
56
63
|
when Array
|
57
|
-
active_on.include?(
|
64
|
+
active_on.include?(path)
|
58
65
|
when Regexp
|
59
|
-
|
66
|
+
path =~ active_on
|
60
67
|
when Proc
|
61
68
|
active_on.arity == 1 ? active_on.call(request) : active_on.call
|
62
69
|
else
|
63
|
-
# Anything else we'll take as a true argument, and match the link's URL
|
70
|
+
# Anything else we'll take as a true argument, and match the link's URL (including any query string)
|
64
71
|
url = url_for(link_options)
|
65
|
-
|
72
|
+
path == url
|
66
73
|
end
|
67
74
|
end
|
68
75
|
|
@@ -70,7 +77,7 @@ module LinkToActiveState
|
|
70
77
|
original ||= ""
|
71
78
|
[original, new].delete_if(&:blank?).join(" ")
|
72
79
|
end
|
73
|
-
|
80
|
+
|
74
81
|
def render_with_wrapper(element_or_proc, wrapper_options, &block)
|
75
82
|
content = block.call
|
76
83
|
|
@@ -7,10 +7,11 @@ Gem::Specification.new do |gem|
|
|
7
7
|
gem.name = "link_to_active_state"
|
8
8
|
gem.version = LinkToActiveState::VERSION
|
9
9
|
gem.authors = ["Robert May"]
|
10
|
-
gem.email = ["
|
10
|
+
gem.email = ["rob@afternoonrobot.co.uk"]
|
11
11
|
gem.description = %q{A simple gem to implement active states on links using the standard Rails `link_to` helper.}
|
12
12
|
gem.summary = %q{Active states for links using the Rails link_to helper.}
|
13
|
-
gem.homepage = ""
|
13
|
+
gem.homepage = "https://github.com/robotmay/link_to_active_state"
|
14
|
+
gem.licenses = ["MIT"]
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($/)
|
16
17
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
@@ -19,5 +20,4 @@ Gem::Specification.new do |gem|
|
|
19
20
|
|
20
21
|
gem.add_development_dependency "rails", [">= 3.2.11"]
|
21
22
|
gem.add_development_dependency "rspec", ["~> 2.13.0"]
|
22
|
-
gem.add_development_dependency "pry", [">= 0.9.12"]
|
23
23
|
end
|
@@ -8,7 +8,20 @@ module App
|
|
8
8
|
end
|
9
9
|
|
10
10
|
describe LinkToActiveState::ViewHelpers::UrlHelper do
|
11
|
-
let(:helper) { App::Helper.new }
|
11
|
+
let(:helper) { App::Helper.new(ActionView::LookupContext.new([Pathname.new(__dir__).to_s]), {}, nil) }
|
12
|
+
|
13
|
+
let(:request) do
|
14
|
+
class Request
|
15
|
+
def fullpath
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Request.new
|
20
|
+
end
|
21
|
+
|
22
|
+
before(:each) do
|
23
|
+
helper.stub!(:request).and_return(request)
|
24
|
+
end
|
12
25
|
|
13
26
|
context "integration with ActionView" do
|
14
27
|
it "aliases the original link_to helper" do
|
@@ -29,19 +42,6 @@ describe LinkToActiveState::ViewHelpers::UrlHelper do
|
|
29
42
|
end
|
30
43
|
|
31
44
|
context "active states on links" do
|
32
|
-
let(:request) do
|
33
|
-
class Request
|
34
|
-
def fullpath
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
Request.new
|
39
|
-
end
|
40
|
-
|
41
|
-
before(:each) do
|
42
|
-
helper.stub!(:request).and_return(request)
|
43
|
-
end
|
44
|
-
|
45
45
|
context "when the current request path matches" do
|
46
46
|
it "adds an active state" do
|
47
47
|
request.stub!(:fullpath).and_return("/")
|
@@ -66,6 +66,18 @@ describe LinkToActiveState::ViewHelpers::UrlHelper do
|
|
66
66
|
lt = helper.link_to "Home", "/", :active_on => "/"
|
67
67
|
lt.should_not match(/li class=\"active\"/i)
|
68
68
|
end
|
69
|
+
|
70
|
+
it "matches in spite of query string if ignore query is true" do
|
71
|
+
request.stub!(:fullpath).and_return("/wobble?test=true")
|
72
|
+
lt = helper.link_to "Madness", "/wobble", :active_on => true, :ignore_query => true
|
73
|
+
lt.should match(/class=\"active\"/i)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "doesn't match in spite of query string if ignore query is not specified" do
|
77
|
+
request.stub!(:fullpath).and_return("/wobble?test=true")
|
78
|
+
lt = helper.link_to "Madness", "/wobble", :active_on => true
|
79
|
+
lt.should_not match(/class=\"active\"/i)
|
80
|
+
end
|
69
81
|
end
|
70
82
|
|
71
83
|
context "when the current request doesn't match" do
|
@@ -82,24 +94,38 @@ describe LinkToActiveState::ViewHelpers::UrlHelper do
|
|
82
94
|
end
|
83
95
|
end
|
84
96
|
|
85
|
-
describe "wrapper element
|
97
|
+
describe "wrapper element" do
|
86
98
|
it "supports options for the wrapper element" do
|
87
|
-
li = helper.link_to "Home", "/",
|
99
|
+
li = helper.link_to "Home", "/",
|
100
|
+
:class => "wobble",
|
88
101
|
:active_on => "/",
|
89
102
|
:active_wrapper => :li,
|
90
|
-
:active_wrapper_options => { :class => "
|
91
|
-
li.should match(/class=\"
|
103
|
+
:active_wrapper_options => { :class => "wibble" }
|
104
|
+
li.should match(/class=\"wibble\"/i)
|
92
105
|
end
|
93
106
|
|
94
107
|
it "supports a proc as the wrapper element" do
|
95
108
|
request.stub!(:fullpath).and_return("/")
|
96
109
|
li = helper.link_to "Home", "/",
|
110
|
+
:class => "wobble",
|
97
111
|
:active_on => "/",
|
98
112
|
:active_wrapper => proc { |link, wrapper_options|
|
99
113
|
"<li class=\"#{wrapper_options[:class]}\">#{link}</li>"
|
100
114
|
}
|
101
115
|
li.should match(/<li class=\"active\">/i)
|
102
|
-
li.should match(/<a href/i)
|
116
|
+
li.should match(/<a class=\"wobble\" href/i)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "supports options on proc wrapper elements" do
|
120
|
+
request.stub!(:fullpath).and_return("/")
|
121
|
+
li = helper.link_to "Home", "/",
|
122
|
+
:class => "wobble",
|
123
|
+
:active_on => "/",
|
124
|
+
:active_wrapper_options => { :class => "wibble" },
|
125
|
+
:active_wrapper => proc { |link, wrapper_options|
|
126
|
+
"<li class=\"#{wrapper_options[:class]}\">#{link}</li>"
|
127
|
+
}
|
128
|
+
li.should match(/<li class=\"wibble active\">/i)
|
103
129
|
end
|
104
130
|
end
|
105
131
|
end
|
metadata
CHANGED
@@ -1,75 +1,54 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: link_to_active_state
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.9
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Robert May
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2021-02-21 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rails
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: 3.2.11
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 3.2.11
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rspec
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- - ~>
|
31
|
+
- - "~>"
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: 2.13.0
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- - ~>
|
38
|
+
- - "~>"
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: 2.13.0
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
|
-
name: pry
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
|
-
requirements:
|
51
|
-
- - ! '>='
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: 0.9.12
|
54
|
-
type: :development
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
|
-
requirements:
|
59
|
-
- - ! '>='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: 0.9.12
|
62
41
|
description: A simple gem to implement active states on links using the standard Rails
|
63
42
|
`link_to` helper.
|
64
43
|
email:
|
65
|
-
-
|
44
|
+
- rob@afternoonrobot.co.uk
|
66
45
|
executables: []
|
67
46
|
extensions: []
|
68
47
|
extra_rdoc_files: []
|
69
48
|
files:
|
70
|
-
- .gitignore
|
71
|
-
- .public_cert.pem
|
72
|
-
- .travis.yml
|
49
|
+
- ".gitignore"
|
50
|
+
- ".public_cert.pem"
|
51
|
+
- ".travis.yml"
|
73
52
|
- Gemfile
|
74
53
|
- LICENSE.txt
|
75
54
|
- README.md
|
@@ -82,29 +61,28 @@ files:
|
|
82
61
|
- spec/link_to_active_state/view_helpers_spec.rb
|
83
62
|
- spec/link_to_active_state_spec.rb
|
84
63
|
- spec/spec_helper.rb
|
85
|
-
homepage:
|
86
|
-
licenses:
|
64
|
+
homepage: https://github.com/robotmay/link_to_active_state
|
65
|
+
licenses:
|
66
|
+
- MIT
|
67
|
+
metadata: {}
|
87
68
|
post_install_message:
|
88
69
|
rdoc_options: []
|
89
70
|
require_paths:
|
90
71
|
- lib
|
91
72
|
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
-
none: false
|
93
73
|
requirements:
|
94
|
-
- -
|
74
|
+
- - ">="
|
95
75
|
- !ruby/object:Gem::Version
|
96
76
|
version: '0'
|
97
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
78
|
requirements:
|
100
|
-
- -
|
79
|
+
- - ">="
|
101
80
|
- !ruby/object:Gem::Version
|
102
81
|
version: '0'
|
103
82
|
requirements: []
|
104
|
-
|
105
|
-
rubygems_version: 1.8.23
|
83
|
+
rubygems_version: 3.0.3
|
106
84
|
signing_key:
|
107
|
-
specification_version:
|
85
|
+
specification_version: 4
|
108
86
|
summary: Active states for links using the Rails link_to helper.
|
109
87
|
test_files:
|
110
88
|
- spec/link_to_active_state/view_helpers_spec.rb
|