active_link_to 0.0.10 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +69 -94
- data/Rakefile +8 -37
- data/VERSION +1 -1
- data/active_link_to.gemspec +2 -2
- data/lib/active_link_to.rb +58 -125
- data/test/active_link_to_test.rb +132 -110
- metadata +2 -2
data/README.md
CHANGED
@@ -1,127 +1,102 @@
|
|
1
1
|
active\_link\_to
|
2
2
|
================
|
3
3
|
|
4
|
-
Creates a link tag of the given name using a URL created by the set of
|
5
|
-
options. Please see documentation for `link_to`, as `active_link_to` is
|
6
|
-
basically a wrapper for it. This method accepts an optional :active
|
7
|
-
parameter that dictates if the given link will have an extra css class
|
8
|
-
attached that marks it as 'active'.
|
4
|
+
Creates a link tag of the given name using a URL created by the set of options. Please see documentation for `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'.
|
9
5
|
|
10
6
|
## Install
|
11
|
-
### In Rails 3.*
|
12
7
|
|
13
|
-
|
8
|
+
When installing for Rails 3 applications add this to the Gemfile: `gem 'active_link_to'` and run `bundle install`.
|
14
9
|
|
15
|
-
|
16
|
-
|
17
|
-
and run `bundle install`
|
18
|
-
|
19
|
-
### In older Rails
|
20
|
-
|
21
|
-
$ sudo gem install active_link_to
|
22
|
-
|
23
|
-
Then you probably want to declare it in your environment.rb
|
10
|
+
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.
|
24
11
|
|
25
|
-
|
26
|
-
|
27
|
-
and freeze it (preferably):
|
28
|
-
|
29
|
-
$ rake gems:unpack GEM=active_link_to
|
30
|
-
|
31
|
-
### Super Simple Example
|
12
|
+
## Super Simple Example
|
32
13
|
Here's a link that will have class attached if it happens to be rendered
|
33
|
-
on page with path `/
|
14
|
+
on page with path `/users` or any child of that page like `/users/123`
|
34
15
|
|
35
|
-
active_link_to '
|
36
|
-
# => <a href="/
|
16
|
+
active_link_to 'Users', '/users'
|
17
|
+
# => <a href="/users" class="active">Users</a>
|
37
18
|
|
38
19
|
This is exactly the same as:
|
39
20
|
|
40
|
-
active_link_to '
|
41
|
-
# => <a href="/
|
21
|
+
active_link_to 'Users', '/users', :active => :inclusive
|
22
|
+
# => <a href="/users" class="active">Users</a>
|
42
23
|
|
43
|
-
|
44
|
-
Here's available options that can be
|
24
|
+
## Active Options
|
25
|
+
Here's available options that can be used as the `:active` value
|
45
26
|
|
46
|
-
*
|
47
|
-
|
48
|
-
*
|
49
|
-
|
50
|
-
allows it to be changed
|
51
|
-
* `:inactive_class => 'class_name'` - Opposite of the :active_class
|
52
|
-
By default it's blank. However you can change it to whatever you want.
|
53
|
-
* `:disable_link => Boolean` - When link is active, sometimes you
|
54
|
-
want to render span tag instead of the link. This parameter is for that.
|
27
|
+
* Boolean -> true | false
|
28
|
+
* Symbol -> :exclusive | :inclusive
|
29
|
+
* Regex -> /regex/
|
30
|
+
* Controller/Action Pair -> [[:controller], [:action\_a, :action\_b]]
|
55
31
|
|
56
|
-
|
32
|
+
## More Examples
|
57
33
|
Most of the functionality of `active_link_to` depends on the current
|
58
34
|
url. Specifically, `request.fullpath` value. We covered the basic example
|
59
35
|
already, so let's try something more fun.
|
60
36
|
|
61
37
|
We want to highlight the link that matches immediate url, and not the children
|
62
38
|
nodes as well
|
63
|
-
|
64
|
-
# For URL: /
|
65
|
-
active_link_to '
|
66
|
-
# => <a href="/
|
67
|
-
|
68
|
-
#
|
69
|
-
active_link_to '
|
70
|
-
# => <a href="/
|
71
|
-
|
39
|
+
|
40
|
+
# For URL: /users will be active
|
41
|
+
active_link_to 'Users', users_path, :active => :exclusive
|
42
|
+
# => <a href="/users" class="active">Users</a>
|
43
|
+
|
44
|
+
# But for URL: /users/123 it will not be active
|
45
|
+
active_link_to 'Users', users_path, :active => :exclusive
|
46
|
+
# => <a href="/users">Users</a>
|
47
|
+
|
72
48
|
If we need to set link to be active based on some regular expression, we can do
|
73
|
-
that as well. Let's try to activate links urls of which begin with '
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
# => <a href="/people" class="active">People</a>
|
78
|
-
|
79
|
-
# For URL: /aliens/9
|
80
|
-
active_link_to 'People', people_path, :active => { :when => /^peop/ }
|
81
|
-
# => <a href="/people">People</a>
|
82
|
-
|
49
|
+
that as well. Let's try to activate links urls of which begin with 'use':
|
50
|
+
|
51
|
+
active_link_to 'Users', users_path, :active => /^\/use/
|
52
|
+
|
83
53
|
What if we need to mark link active for all URLs that match a particular controller,
|
84
54
|
or action, or both? Or any number of those at the same time? Sure, why not:
|
85
|
-
|
86
|
-
# For URL: /people/56/edit
|
55
|
+
|
87
56
|
# For matching multiple controllers and actions:
|
88
|
-
active_link_to '
|
89
|
-
|
90
|
-
}
|
91
|
-
|
57
|
+
active_link_to 'User Edit', edit_user_path(@user), :active => [['people', 'news'], ['show', 'edit']]
|
58
|
+
|
92
59
|
# for matching all actions under given controllers:
|
93
|
-
active_link_to '
|
94
|
-
|
95
|
-
}
|
96
|
-
|
60
|
+
active_link_to 'User Edit', edit_user_path(@user), :active => [['people', 'news'], []]
|
61
|
+
|
97
62
|
# for matching all controllers for a particular action
|
98
|
-
active_link_to '
|
99
|
-
|
100
|
-
}
|
101
|
-
# => <a href="/people" class="active">People</a>
|
102
|
-
|
63
|
+
active_link_to 'User Edit', edit_user_path(@user), :active => [[], ['edit']]
|
64
|
+
|
103
65
|
Sometimes it should be easy as setting a true or false:
|
66
|
+
|
67
|
+
active_link_to 'Users', users_path, :active => true
|
68
|
+
|
69
|
+
## More Options
|
70
|
+
You can specify active and inactive css classes for links:
|
71
|
+
|
72
|
+
active_link_to 'Users', users_path, :class_active => 'enabled'
|
73
|
+
# => <a href="/users" class="enabled">Users</a>
|
74
|
+
|
75
|
+
active_link_to 'News', news_path, :class_inactive => 'disabled'
|
76
|
+
# => <a href="/news" class="disabled">News</a>
|
77
|
+
|
78
|
+
Sometimes you want to replace link with a span if it's active:
|
79
|
+
|
80
|
+
active_link_to 'Users', users_path, :disable_active => true
|
81
|
+
# => <span class="active">Users</span>
|
82
|
+
|
83
|
+
If you are constructing navigation links it's helpful to wrap links in another tag, like `<li>` maybe:
|
84
|
+
|
85
|
+
active_link_to 'Users', users_path, :wrap_link => :li
|
86
|
+
# => <li class="active"><a href="/users">Users</a></li>
|
87
|
+
|
88
|
+
## Helper Methods
|
89
|
+
You may directly use methods that `active_link_to` relies on.
|
104
90
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
#
|
114
|
-
active_link_to 'People', people_path, :active => { :active_link => 'awesome_selected' }
|
115
|
-
# => <a href="/people" class="awesome_selected">People</a>
|
116
|
-
|
117
|
-
active_link_to 'Aliens', aliens_path, :active => { :inactive_link => 'bummer_inactive' }
|
118
|
-
# => <a href="/aliens" class="bummer_inactive">Aliens</a>
|
119
|
-
|
120
|
-
Some weird people think that link should change to a span if it indicated current page:
|
121
|
-
|
122
|
-
# For URL: /people
|
123
|
-
active_link_to 'People', people_path, :active => { :disable_link => true }
|
124
|
-
# => <span class="active">People</span>
|
91
|
+
`is_active_link?` will return true or false based on the URL and value of the `:active` parameter:
|
92
|
+
|
93
|
+
is_active_link?(users_path, :inclusive)
|
94
|
+
# => true
|
95
|
+
|
96
|
+
`active_link_to_class` will return the css class:
|
97
|
+
|
98
|
+
active_link_to_class(users_path, :active => :inclusive)
|
99
|
+
# => 'active'
|
125
100
|
|
126
101
|
### Copyright
|
127
102
|
|
data/Rakefile
CHANGED
@@ -4,17 +4,16 @@ require 'rake'
|
|
4
4
|
begin
|
5
5
|
require 'jeweler'
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
|
-
gem.name
|
8
|
-
gem.summary
|
7
|
+
gem.name = 'active_link_to'
|
8
|
+
gem.summary = 'Marks currently active links'
|
9
9
|
gem.description = 'Extremely helpful 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'
|
10
|
-
gem.email
|
11
|
-
gem.homepage
|
12
|
-
gem.authors
|
10
|
+
gem.email = 'oleg@theworkinggroup.ca'
|
11
|
+
gem.homepage = 'http://github.com/twg/active_link_to'
|
12
|
+
gem.authors = ['Oleg Khabarov']
|
13
13
|
end
|
14
|
-
|
15
14
|
Jeweler::GemcutterTasks.new
|
16
15
|
rescue LoadError
|
17
|
-
puts "Jeweler (or a dependency) not available. Install it with:
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
17
|
end
|
19
18
|
|
20
19
|
require 'rake/testtask'
|
@@ -24,33 +23,5 @@ Rake::TestTask.new(:test) do |test|
|
|
24
23
|
test.verbose = true
|
25
24
|
end
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
Rcov::RcovTask.new do |test|
|
30
|
-
test.libs << 'test'
|
31
|
-
test.pattern = 'test/**/*_test.rb'
|
32
|
-
test.verbose = true
|
33
|
-
end
|
34
|
-
rescue LoadError
|
35
|
-
task :rcov do
|
36
|
-
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
task :test => :check_dependencies
|
41
|
-
|
42
|
-
task :default => :test
|
43
|
-
|
44
|
-
require 'rake/rdoctask'
|
45
|
-
Rake::RDocTask.new do |rdoc|
|
46
|
-
if File.exist?('VERSION')
|
47
|
-
version = File.read('VERSION')
|
48
|
-
else
|
49
|
-
version = ""
|
50
|
-
end
|
51
|
-
|
52
|
-
rdoc.rdoc_dir = 'rdoc'
|
53
|
-
rdoc.title = "active_link_to #{version}"
|
54
|
-
rdoc.rdoc_files.include('README*')
|
55
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
-
end
|
26
|
+
task :test => :check_dependencies
|
27
|
+
task :default => :test
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
1.0.0
|
data/active_link_to.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{active_link_to}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "1.0.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Oleg Khabarov"]
|
12
|
-
s.date = %q{2011-07-
|
12
|
+
s.date = %q{2011-07-22}
|
13
13
|
s.description = %q{Extremely helpful 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
|
s.email = %q{oleg@theworkinggroup.ca}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/active_link_to.rb
CHANGED
@@ -1,105 +1,15 @@
|
|
1
1
|
module ActiveLinkTo
|
2
2
|
|
3
|
-
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
# active_link_to 'People', '/people'
|
14
|
-
# # => <a href="/people" class="active">People</a>
|
15
|
-
#
|
16
|
-
# This is exactly the same as:
|
17
|
-
#
|
18
|
-
# active_link_to 'People', '/people', :active => { :when => :self }
|
19
|
-
# # => <a href="/people" class="active">People</a>
|
20
|
-
#
|
21
|
-
# === Options
|
22
|
-
# Here's available options that can be inserted into :active hash
|
23
|
-
# * <tt>:when => predefined symbol || Regexp || Array tuple of controller/actions || Boolean</tt> - This controls
|
24
|
-
# when link is considered to be 'active'
|
25
|
-
# * <tt>:active_class => 'class_name'</tt> - When link is 'active', css
|
26
|
-
# class of that link is set to the default 'active'. This parameter
|
27
|
-
# allows it to be changed
|
28
|
-
# * <tt>:inactive_class => 'class_name'</tt> - Opposite of the :active_class
|
29
|
-
# By default it's blank. However you can change it to whatever you want.
|
30
|
-
# * <tt>:disable_link => Boolean</tt> - When link is active, sometimes you
|
31
|
-
# want to render span tag instead of the link. This parameter is for that.
|
32
|
-
#
|
33
|
-
# === Examples
|
34
|
-
# Most of the functionality of active_link_helper depends on the current
|
35
|
-
# url. Specifically, request.request_uri value. We covered the basic example
|
36
|
-
# already, so let's try sometihng more fun
|
37
|
-
#
|
38
|
-
# We want to highlight the link that matches immidiate url, and not the children
|
39
|
-
# nodes as well
|
40
|
-
#
|
41
|
-
# # For URL: /people/24
|
42
|
-
# active_link_to 'People', people_path, :active => { :when => :self_only }
|
43
|
-
# # => <a href="/people">People</a>
|
44
|
-
#
|
45
|
-
# # For URL: /people
|
46
|
-
# active_link_to 'People', people_path, :active => { :when => :self_only }
|
47
|
-
# # => <a href="/people" class="active">People</a>
|
48
|
-
#
|
49
|
-
# If we need to set link to be active based on some regular expression, we can do
|
50
|
-
# that as well. Let's try to activate links urls of which begin with 'peop':
|
51
|
-
#
|
52
|
-
# # For URL: /people/44
|
53
|
-
# active_link_to 'People', people_path, :active => { :when => /^peop/ }
|
54
|
-
# # => <a href="/people" class="active">People</a>
|
55
|
-
#
|
56
|
-
# # For URL: /aliens/9
|
57
|
-
# active_link_to 'People', people_path, :active => { :when => /^peop/ }
|
58
|
-
# # => <a href="/people">People</a>
|
59
|
-
#
|
60
|
-
# What if we need to mark link active for all URLs that match a particular controller,
|
61
|
-
# or action, or both? Or any number of those at the same time? Sure, why not:
|
62
|
-
#
|
63
|
-
# # For URL: /people/56/edit
|
64
|
-
# # For matching multiple controllers and actions:
|
65
|
-
# active_link_to 'Person Edit', edit_person_path(@person), :active => {
|
66
|
-
# :when => [['people', 'aliens'], ['show', 'edit']]
|
67
|
-
# }
|
68
|
-
#
|
69
|
-
# # for matching all actions under given controllers:
|
70
|
-
# active_link_to 'Person Edit', edit_person_path(@person), :active => {
|
71
|
-
# :when => [['people', 'aliens'], []]
|
72
|
-
# }
|
73
|
-
#
|
74
|
-
# # for matching all controllers for a particular action
|
75
|
-
# active_link_to 'Person Edit', edit_person_path(@person), :active => {
|
76
|
-
# :when => [[], ['edit']]
|
77
|
-
# }
|
78
|
-
# # => <a href="/people" class="active">People</a>
|
79
|
-
#
|
80
|
-
# Sometimes it should be easy as setting a true or false:
|
81
|
-
#
|
82
|
-
# active_link_to 'People', people_path, :active => { :when => true }
|
83
|
-
# # => <a href="/people" class="active">People</a>
|
84
|
-
#
|
85
|
-
# active_link_to 'People', people_path, :active => { :when => false }
|
86
|
-
# # => <a href="/people">People</a>
|
87
|
-
#
|
88
|
-
# Lets see what happens when we push some css class modifier parameters
|
89
|
-
#
|
90
|
-
# # For URL: /people/12
|
91
|
-
# active_link_to 'People', people_path, :active => { :active_link => 'awesome_selected' }
|
92
|
-
# # => <a href="/people" class="awesome_selected">People</a>
|
93
|
-
#
|
94
|
-
# active_link_to 'Aliens', aliens_path, :active => { :inactive_link => 'bummer_inactive' }
|
95
|
-
# # => <a href="/aliens" class="bummer_inactive">Aliens</a>
|
96
|
-
#
|
97
|
-
# Some wierd people think that link should change to a span if it indicated current page:
|
98
|
-
#
|
99
|
-
# # For URL: /people
|
100
|
-
# active_link_to 'People', people_path, :active => { :disable_link => true }
|
101
|
-
# # => <span class="active">People</span>
|
102
|
-
#
|
3
|
+
|
4
|
+
# Wrapper around link_to. Accepts following params:
|
5
|
+
# :active => Boolean | Symbol | Regex | Controller/Action Pair
|
6
|
+
# :class_active => String
|
7
|
+
# :class_inactive => String
|
8
|
+
# :disable_active => Boolean
|
9
|
+
# :wrap_tag => Symbol
|
10
|
+
# Example usage:
|
11
|
+
# active_link_to('/users', :class_active => 'enabled')
|
12
|
+
# active_link_to(users_path, :active => :exclusive, :wrap_tag => :li)
|
103
13
|
def active_link_to(*args, &block)
|
104
14
|
if block_given?
|
105
15
|
name = capture(&block)
|
@@ -110,47 +20,70 @@ module ActiveLinkTo
|
|
110
20
|
options = args[1] || {}
|
111
21
|
html_options = args[2] || {}
|
112
22
|
end
|
23
|
+
url = url_for(options)
|
113
24
|
|
114
|
-
|
115
|
-
|
25
|
+
active_options = { }
|
26
|
+
link_options = { }
|
27
|
+
html_options.each do |k, v|
|
28
|
+
if [:active, :class_active, :class_inactive, :active_disable, :wrap_tag].member?(k)
|
29
|
+
active_options[k] = v
|
30
|
+
else
|
31
|
+
link_options[k] = v
|
32
|
+
end
|
33
|
+
end
|
116
34
|
|
117
|
-
|
118
|
-
|
119
|
-
css_class
|
35
|
+
css_class = link_options.delete(:class).to_s + ' '
|
36
|
+
css_class << active_link_to_class(url, active_options)
|
37
|
+
css_class.strip!
|
120
38
|
|
121
|
-
|
122
|
-
|
123
|
-
html_options[:class].blank? ? html_options.delete(:class) : html_options[:class].lstrip!
|
39
|
+
wrap_tag = active_options[:wrap_tag].present? ? active_options[:wrap_tag] : nil
|
40
|
+
link_options[:class] = css_class if css_class.present? && !wrap_tag
|
124
41
|
|
125
|
-
if
|
126
|
-
content_tag(:span, name,
|
42
|
+
link = if active_options[:active_disable] === true && is_active_link?(url, active_options[:active])
|
43
|
+
content_tag(:span, name, link_options)
|
127
44
|
else
|
128
|
-
link_to(name, url,
|
45
|
+
link_to(name, url, link_options)
|
129
46
|
end
|
47
|
+
|
48
|
+
wrap_tag ? content_tag(wrap_tag, link, :class => css_class) : link
|
130
49
|
end
|
131
50
|
|
132
|
-
# Returns css class name. Takes the link's URL and
|
133
|
-
|
134
|
-
|
135
|
-
|
51
|
+
# Returns css class name. Takes the link's URL and its params
|
52
|
+
# Example usage:
|
53
|
+
# active_link_to_class('/root', :class_active => 'on', :class_inactive => 'off')
|
54
|
+
#
|
55
|
+
def active_link_to_class(url, options = {})
|
56
|
+
if is_active_link?(url, options[:active])
|
57
|
+
options[:class_active] || 'active'
|
136
58
|
else
|
137
|
-
options[:
|
59
|
+
options[:class_inactive] || ''
|
138
60
|
end
|
139
61
|
end
|
140
62
|
|
141
|
-
# Returns true or false
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
63
|
+
# Returns true or false based on the provided path and condition
|
64
|
+
# Possible condition values are:
|
65
|
+
# Boolean -> true | false
|
66
|
+
# Symbol -> :exclusive | :inclusive
|
67
|
+
# Regex -> /regex/
|
68
|
+
# Controller/Action Pair -> [[:controller], [:action_a, :action_b]]
|
69
|
+
# Example usage:
|
70
|
+
# is_active_link?('/root', true)
|
71
|
+
# is_active_link?('/root', :exclusive)
|
72
|
+
# is_active_link?('/root', /^\/root/)
|
73
|
+
# is_active_link?('/root', ['users', ['show', 'edit']])
|
74
|
+
#
|
75
|
+
def is_active_link?(url, condition = nil)
|
76
|
+
url = url_for(url).sub(/\?.*/, '') # ignore GET params
|
77
|
+
case condition
|
78
|
+
when :inclusive, nil
|
146
79
|
!request.fullpath.match(/^#{Regexp.escape(url)}(\/.*|\?.*)?$/).blank?
|
147
|
-
when :
|
80
|
+
when :exclusive
|
148
81
|
!request.fullpath.match(/^#{Regexp.escape(url)}\/?(\?.*)?$/).blank?
|
149
82
|
when Regexp
|
150
|
-
!request.fullpath.match(
|
83
|
+
!request.fullpath.match(condition).blank?
|
151
84
|
when Array
|
152
|
-
controllers = [*
|
153
|
-
actions = [*
|
85
|
+
controllers = [*condition[0]]
|
86
|
+
actions = [*condition[1]]
|
154
87
|
(controllers.blank? || controllers.member?(params[:controller])) &&
|
155
88
|
(actions.blank? || actions.member?(params[:action]))
|
156
89
|
when TrueClass
|
data/test/active_link_to_test.rb
CHANGED
@@ -2,120 +2,142 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class ActiveLinkToTest < Test::Unit::TestCase
|
4
4
|
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
request.fullpath = '/
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
def
|
51
|
-
request.fullpath = '/test
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
request.fullpath = '/
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
request.fullpath = 'doesnotmatter'
|
86
|
-
out = active_link_to 'name', '/test', :active => { :when => true }
|
87
|
-
assert_equal '<a href="/test" class="active">name</a>', out
|
88
|
-
out = active_link_to 'name', '/test', :active => { :when => false }
|
89
|
-
assert_equal '<a href="/test">name</a>', out
|
90
|
-
end
|
91
|
-
|
92
|
-
def test_setting_active_class
|
93
|
-
request.fullpath = '/test'
|
94
|
-
out = active_link_to 'name', '/test', :active => { :active_class => 'new_active'}
|
95
|
-
assert_equal '<a href="/test" class="new_active">name</a>', out
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_setting_inactive_class
|
99
|
-
request.fullpath = '/test'
|
100
|
-
out = active_link_to 'name', '/not-test', :active => { :inactive_class => 'new_inactive'}
|
101
|
-
assert_equal '<a href="/not-test" class="new_inactive">name</a>', out
|
102
|
-
end
|
103
|
-
|
104
|
-
def test_transforming_to_span
|
105
|
-
request.fullpath = '/test'
|
106
|
-
out = active_link_to 'name', '/test', :active => { :disable_link => true }
|
107
|
-
assert_equal '<span class="active">name</span>', out
|
5
|
+
def test_is_active_link_booleans
|
6
|
+
assert is_active_link?('/', true)
|
7
|
+
assert !is_active_link?('/', false)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_is_active_link_symbol_inclusive
|
11
|
+
request.fullpath = '/root'
|
12
|
+
assert is_active_link?('/root', :inclusive)
|
13
|
+
|
14
|
+
request.fullpath = '/root?param=test'
|
15
|
+
assert is_active_link?('/root', :inclusive)
|
16
|
+
|
17
|
+
request.fullpath = '/root/child/sub-child'
|
18
|
+
assert is_active_link?('/root', :inclusive)
|
19
|
+
|
20
|
+
request.fullpath = '/other'
|
21
|
+
assert !is_active_link?('/root', :inclusive)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_is_active_link_symbol_inclusive_implied
|
25
|
+
request.fullpath = '/root/child/sub-child'
|
26
|
+
assert is_active_link?('/root')
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_is_active_link_symbol_inclusive_similar_path
|
30
|
+
request.fullpath = '/root/abc'
|
31
|
+
assert !is_active_link?('/root/a', :inclusive)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_is_active_link_symbol_inclusive_with_link_params
|
35
|
+
request.fullpath = '/root?param=test'
|
36
|
+
assert is_active_link?('/root?attr=example')
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_is_active_link_symbol_exclusive
|
40
|
+
request.fullpath = '/root'
|
41
|
+
assert is_active_link?('/root', :exclusive)
|
42
|
+
|
43
|
+
request.fullpath = '/root?param=test'
|
44
|
+
assert is_active_link?('/root', :exclusive)
|
45
|
+
|
46
|
+
request.fullpath = '/root/child'
|
47
|
+
assert !is_active_link?('/root', :exclusive)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_is_active_link_symbol_exclusive_with_link_params
|
51
|
+
request.fullpath = '/root?param=test'
|
52
|
+
assert is_active_link?('/root?attr=example', :exclusive)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_is_active_link_regex
|
56
|
+
request.fullpath = '/root'
|
57
|
+
assert is_active_link?('/', /^\/root/)
|
58
|
+
|
59
|
+
request.fullpath = '/root/child'
|
60
|
+
assert is_active_link?('/', /^\/r/)
|
61
|
+
|
62
|
+
request.fullpath = '/other'
|
63
|
+
assert !is_active_link?('/', /^\/r/)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_is_active_link_array
|
67
|
+
params[:controller], params[:action] = 'controller', 'action'
|
68
|
+
|
69
|
+
assert is_active_link?('/', [['controller'], ['action']])
|
70
|
+
assert is_active_link?('/', [['controller'], ['action', 'action_b']])
|
71
|
+
assert is_active_link?('/', [['controller', 'controller_b'], ['action']])
|
72
|
+
assert is_active_link?('/', [['controller', 'controller_b'], ['action', 'action_b']])
|
73
|
+
assert is_active_link?('/', ['controller', 'action'])
|
74
|
+
assert is_active_link?('/', ['controller', ['action', 'action_b']])
|
75
|
+
assert is_active_link?('/', [['controller', 'controller_b'], 'action'])
|
76
|
+
|
77
|
+
assert !is_active_link?('/', ['controller_a', 'action'])
|
78
|
+
assert !is_active_link?('/', ['controller', 'action_a'])
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_active_link_to_class
|
82
|
+
request.fullpath = '/root'
|
83
|
+
assert_equal 'active', active_link_to_class('/root')
|
84
|
+
assert_equal 'on', active_link_to_class('/root', :class_active => 'on')
|
108
85
|
|
109
|
-
|
110
|
-
assert_equal '
|
86
|
+
assert_equal '', active_link_to_class('/other')
|
87
|
+
assert_equal 'off', active_link_to_class('/other', :class_inactive => 'off')
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_active_link_to
|
91
|
+
request.fullpath = '/root'
|
92
|
+
link = active_link_to('label', '/root')
|
93
|
+
assert_equal '<a href="/root" class="active">label</a>', link
|
94
|
+
|
95
|
+
link = active_link_to('label', '/other')
|
96
|
+
assert_equal '<a href="/other">label</a>', link
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_active_link_to_with_existing_class
|
100
|
+
request.fullpath = '/root'
|
101
|
+
link = active_link_to('label', '/root', :class => 'current')
|
102
|
+
assert_equal '<a href="/root" class="current active">label</a>', link
|
103
|
+
|
104
|
+
link = active_link_to('label', '/other', :class => 'current')
|
105
|
+
assert_equal '<a href="/other" class="current">label</a>', link
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_active_link_to_with_custom_classes
|
109
|
+
request.fullpath = '/root'
|
110
|
+
link = active_link_to('label', '/root', :class_active => 'on')
|
111
|
+
assert_equal '<a href="/root" class="on">label</a>', link
|
112
|
+
|
113
|
+
link = active_link_to('label', '/other', :class_inactive => 'off')
|
114
|
+
assert_equal '<a href="/other" class="off">label</a>', link
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_active_link_to_with_wrap_tag
|
118
|
+
request.fullpath = '/root'
|
119
|
+
link = active_link_to('label', '/root', :wrap_tag => :li)
|
120
|
+
assert_equal '<li class="active"><a href="/root">label</a></li>', link
|
121
|
+
|
122
|
+
link = active_link_to('label', '/root', :wrap_tag => :li, :active_disable => true)
|
123
|
+
assert_equal '<li class="active"><span>label</span></li>', link
|
124
|
+
|
125
|
+
link = active_link_to('label', '/root', :wrap_tag => :li, :class => 'testing')
|
126
|
+
assert_equal '<li class="testing active"><a href="/root">label</a></li>', link
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_active_link_to_with_active_disable
|
130
|
+
request.fullpath = '/root'
|
131
|
+
link = active_link_to('label', '/root', :active_disable => true)
|
132
|
+
assert_equal '<span class="active">label</span>', link
|
111
133
|
end
|
112
134
|
|
113
135
|
def test_should_not_modify_passed_params
|
114
|
-
request.fullpath = '/
|
115
|
-
params = {:class => 'testing'}
|
116
|
-
out = active_link_to '
|
117
|
-
assert_equal '<a href="/
|
118
|
-
assert_equal ({:class => 'testing'}), params
|
136
|
+
request.fullpath = '/root'
|
137
|
+
params = { :class => 'testing', :active => :inclusive }
|
138
|
+
out = active_link_to 'label', '/root', params
|
139
|
+
assert_equal '<a href="/root" class="testing active">label</a>', out
|
140
|
+
assert_equal ({:class => 'testing', :active => :inclusive }), params
|
119
141
|
end
|
120
142
|
|
121
143
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_link_to
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-07-
|
12
|
+
date: 2011-07-22 00:00:00.000000000 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
description: Extremely helpful when you need to add some logic that figures out if
|