selected_links 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +20 -14
- data/lib/selected_links.rb +3 -0
- data/lib/selected_links/link.rb +5 -18
- data/lib/selected_links/version.rb +1 -1
- data/spec/selected_links/link_spec.rb +116 -50
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a5ed9140ea4244863719311f15966c9d3d6de9e
|
4
|
+
data.tar.gz: fef8f4a290d2a7ffe36f6a511577653ece8495dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff25ad7a255154b401d91f053632ad2060adc9fb7bc2f90536ac7ab90ee4b3bd815bce47cb3545b0a96b419f08f17b83fd249d183ae41e978ecb541303ae25bc
|
7
|
+
data.tar.gz: 90b8d44449e0e41c3ae381674f090b48752af05b4fb629a426ec30e833c40aba0c2631aac39e26f29176ce7b8dcbaaf7cefd8cd1ad15522cf0e819031fefbd3d
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
## v0.0.3
|
2
|
+
|
3
|
+
* Added ability to change the default class name for selected links
|
4
|
+
* Added ability to pass in class name on a link to link basis
|
5
|
+
* Changed the default class name to _active_
|
6
|
+
|
1
7
|
## v0.0.2
|
2
8
|
|
3
9
|
* Added fallback behaviour support via initializer setting
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/kainage/selected_links.png)](https://travis-ci.org/kainage/selected_links)
|
4
4
|
|
5
|
-
Adds a link helper to ActionView::Base to that adds a class of
|
5
|
+
Adds a link helper to ActionView::Base to that adds a class of _active_
|
6
6
|
to the link when matched to a pattern, usually a url.
|
7
7
|
|
8
8
|
## Installation
|
@@ -21,27 +21,26 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
## Configuration
|
23
23
|
|
24
|
-
Usage is the same as link_to and takes
|
24
|
+
Usage is the same as link_to and takes 3 optional arguments ```:matcher```, ```:source``` and ```class_name```.
|
25
25
|
|
26
|
-
|
26
|
+
You can override the default behaviour in an initilizer file:
|
27
27
|
|
28
28
|
```ruby
|
29
29
|
SelectedLinks.setup do |config|
|
30
|
-
|
31
|
-
|
32
|
-
```
|
33
|
-
|
34
|
-
You can also change the fallback behaviour to check the name if the matcher fails
|
30
|
+
# Change the global default source that the matcher looks to check against
|
31
|
+
config.default_source = 'request.path'
|
35
32
|
|
36
|
-
|
37
|
-
SelectedLinks.setup do |config|
|
33
|
+
# Change the fallback behaviour to match against the name of the link if the matcher fails
|
38
34
|
config.fallback_to_name = false
|
35
|
+
|
36
|
+
# Set the global default class name that is added to the link when a match is found
|
37
|
+
config.default_class_name = 'active'
|
39
38
|
end
|
40
39
|
```
|
41
40
|
|
42
|
-
###
|
41
|
+
### Usage
|
43
42
|
|
44
|
-
To make this link have a class of
|
43
|
+
To make this link have a class of _active_ when the url is at the top level:
|
45
44
|
|
46
45
|
```
|
47
46
|
<%= selectable_link_to 'Home', root_url, :matcher => '\/\z' %>
|
@@ -61,7 +60,8 @@ Blocks still work and this will do the same thing as the previous example:
|
|
61
60
|
<% end %>
|
62
61
|
```
|
63
62
|
|
64
|
-
Without a matcher option and NOT in the block form, this will look for _about_
|
63
|
+
Without a matcher option and NOT in the block form, this will look for _about_
|
64
|
+
in the source if the option ```fallback_to_name``` is ```true```:
|
65
65
|
|
66
66
|
```
|
67
67
|
<%= selectable_link_to 'ABOUT', about_url %>
|
@@ -71,7 +71,7 @@ You can, of course, add this to links with other clases on them:
|
|
71
71
|
|
72
72
|
```
|
73
73
|
<%= selectable_link_to 'ABOUT', about_url, :class => 'nav' %>
|
74
|
-
# => <a ... class="nav
|
74
|
+
# => <a ... class="nav avtive">ABOUT</a>
|
75
75
|
```
|
76
76
|
|
77
77
|
To override the source per link, just add a ```:source``` argument:
|
@@ -80,6 +80,12 @@ To override the source per link, just add a ```:source``` argument:
|
|
80
80
|
<%= selectable_link_to 'ABOUT', about_url, :source => request.url %>
|
81
81
|
```
|
82
82
|
|
83
|
+
To override the default class name added per link add ```:class_name``` argument:
|
84
|
+
|
85
|
+
```
|
86
|
+
<%= selectable_link_to 'ABOUT', about_url, :class_name => 'highlight' %>
|
87
|
+
```
|
88
|
+
|
83
89
|
## Contributing
|
84
90
|
|
85
91
|
1. Fork it
|
data/lib/selected_links.rb
CHANGED
data/lib/selected_links/link.rb
CHANGED
@@ -5,14 +5,13 @@ module SelectedLinks
|
|
5
5
|
|
6
6
|
def initialize(*args, &block)
|
7
7
|
@name, @options, @html_options = parse_args(block_given?, *args)
|
8
|
-
@matcher
|
9
|
-
@source
|
8
|
+
@matcher = @html_options.delete(:matcher)
|
9
|
+
@source = @html_options.delete(:source)
|
10
|
+
@class_name = @html_options.delete(:class_name)
|
10
11
|
end
|
11
12
|
|
12
13
|
def generate
|
13
14
|
merge_classes if is_match?
|
14
|
-
cleanup
|
15
|
-
|
16
15
|
self
|
17
16
|
end
|
18
17
|
|
@@ -27,7 +26,8 @@ module SelectedLinks
|
|
27
26
|
end
|
28
27
|
|
29
28
|
def merge_classes
|
30
|
-
|
29
|
+
name = @class_name || SelectedLinks.default_class_name
|
30
|
+
@html_options.merge!({class: name}) { |key, old_v, new_v| [old_v, new_v].join(' ') }
|
31
31
|
end
|
32
32
|
|
33
33
|
def is_match?
|
@@ -45,18 +45,5 @@ module SelectedLinks
|
|
45
45
|
return false unless matcher # nil.to_s returns ""
|
46
46
|
source =~ /#{matcher}/i
|
47
47
|
end
|
48
|
-
|
49
|
-
def cleanup
|
50
|
-
remove_matcher
|
51
|
-
remove_source
|
52
|
-
end
|
53
|
-
|
54
|
-
def remove_matcher
|
55
|
-
@html_options.delete(:matcher)
|
56
|
-
end
|
57
|
-
|
58
|
-
def remove_source
|
59
|
-
@html_options.delete(:source)
|
60
|
-
end
|
61
48
|
end
|
62
49
|
end
|
@@ -15,11 +15,6 @@ describe SelectedLinks::Link do
|
|
15
15
|
it "should have options" do
|
16
16
|
@link.options.should eq 'options'
|
17
17
|
end
|
18
|
-
|
19
|
-
it "should have have html_options of a hash with two items" do
|
20
|
-
@link.html_options.should be_a(Hash)
|
21
|
-
@link.html_options.size.should eq 2
|
22
|
-
end
|
23
18
|
end
|
24
19
|
|
25
20
|
context "with a block" do
|
@@ -37,11 +32,6 @@ describe SelectedLinks::Link do
|
|
37
32
|
it "should have options" do
|
38
33
|
@link.options.should eq 'options'
|
39
34
|
end
|
40
|
-
|
41
|
-
it "should have have html_options of a hash with two items" do
|
42
|
-
@link.html_options.should be_a(Hash)
|
43
|
-
@link.html_options.size.should eq 2
|
44
|
-
end
|
45
35
|
end
|
46
36
|
end
|
47
37
|
end
|
@@ -52,22 +42,6 @@ describe SelectedLinks::Link do
|
|
52
42
|
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'foo')
|
53
43
|
end
|
54
44
|
|
55
|
-
it "should remove source option" do
|
56
|
-
@link.send(:remove_source)
|
57
|
-
@link.html_options.has_key?(:source).should be_false
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should remove matcher option" do
|
61
|
-
@link.send(:remove_matcher)
|
62
|
-
@link.html_options.has_key?(:matcher).should be_false
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should remove both source and matcher options" do
|
66
|
-
@link.send(:cleanup)
|
67
|
-
@link.html_options.has_key?(:source).should be_false
|
68
|
-
@link.html_options.has_key?(:matcher).should be_false
|
69
|
-
end
|
70
|
-
|
71
45
|
describe "url_match?" do
|
72
46
|
it "should return true when url matches given strig" do
|
73
47
|
@link.send(:url_match?, '/hello', 'hello').should be_true
|
@@ -120,44 +94,136 @@ describe SelectedLinks::Link do
|
|
120
94
|
end
|
121
95
|
|
122
96
|
describe "fully constructed link" do
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
97
|
+
describe "with default class name" do
|
98
|
+
context "with a matcher link" do
|
99
|
+
it "should have a class of active added" do
|
100
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'foo').generate
|
101
|
+
@link.html_options[:class].should =~ /active/
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should not have a class of active if there was not a match" do
|
105
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'bar').generate
|
106
|
+
@link.html_options[:class].should_not =~ /active/
|
107
|
+
end
|
127
108
|
end
|
128
109
|
|
129
|
-
|
130
|
-
|
131
|
-
|
110
|
+
context "with a named link" do
|
111
|
+
it "should have a class of active added" do
|
112
|
+
@link = SelectedLinks::Link.new('Foo', 'options', :source => '/foo').generate
|
113
|
+
@link.html_options[:class].should =~ /active/
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should not have a class of active if there was not a match" do
|
117
|
+
@link = SelectedLinks::Link.new('Bar', 'options', :source => '/foo').generate
|
118
|
+
@link.html_options[:class].should_not =~ /active/
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "with an existing class" do
|
123
|
+
it "should have a class of active added" do
|
124
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'foo', :class => 'nav').generate
|
125
|
+
@link.html_options[:class].should =~ /active/
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should not have a class of active if there was not a match" do
|
129
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'bar', :class => 'nav').generate
|
130
|
+
@link.html_options[:class].should_not =~ /active/
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'should have a space between the existing classes and the active class' do
|
134
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'foo', :class => 'nav').generate
|
135
|
+
@link.html_options[:class].should == 'nav active'
|
136
|
+
end
|
132
137
|
end
|
133
138
|
end
|
134
139
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
140
|
+
describe "with a passed in class name" do
|
141
|
+
context "with a matcher link" do
|
142
|
+
it "should have a class of passed added" do
|
143
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'foo', :class_name => 'passed').generate
|
144
|
+
@link.html_options[:class].should =~ /passed/
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should not have a class of passed if there was not a match" do
|
148
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'bar', :class_name => 'passed').generate
|
149
|
+
@link.html_options[:class].should_not =~ /passed/
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
context "with a named link" do
|
154
|
+
it "should have a class of passed added" do
|
155
|
+
@link = SelectedLinks::Link.new('Foo', 'options', :source => '/foo', :class_name => 'passed').generate
|
156
|
+
@link.html_options[:class].should =~ /passed/
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should not have a class of passed if there was not a match" do
|
160
|
+
@link = SelectedLinks::Link.new('Bar', 'options', :source => '/foo', :class_name => 'passed').generate
|
161
|
+
@link.html_options[:class].should_not =~ /passed/
|
162
|
+
end
|
139
163
|
end
|
140
164
|
|
141
|
-
|
142
|
-
|
143
|
-
|
165
|
+
context "with an existing class" do
|
166
|
+
it "should have a class of passed added" do
|
167
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'foo', :class_name => 'passed', :class => 'nav').generate
|
168
|
+
@link.html_options[:class].should =~ /passed/
|
169
|
+
end
|
170
|
+
|
171
|
+
it "should not have a class of passed if there was not a match" do
|
172
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'bar', :class_name => 'passed', :class => 'nav').generate
|
173
|
+
@link.html_options[:class].should_not =~ /passed/
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'should have a space between the existing classes and the passed class' do
|
177
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'foo', :class_name => 'passed', :class => 'nav').generate
|
178
|
+
@link.html_options[:class].should == 'nav passed'
|
179
|
+
end
|
144
180
|
end
|
145
181
|
end
|
146
182
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
183
|
+
describe "with a overridden default class name" do
|
184
|
+
before :each do
|
185
|
+
SelectedLinks.default_class_name = 'selected'
|
186
|
+
end
|
187
|
+
|
188
|
+
context "with a matcher link" do
|
189
|
+
it "should have a class of selected added" do
|
190
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'foo').generate
|
191
|
+
@link.html_options[:class].should =~ /selected/
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should not have a class of selected if there was not a match" do
|
195
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'bar').generate
|
196
|
+
@link.html_options[:class].should_not =~ /selected/
|
197
|
+
end
|
151
198
|
end
|
152
199
|
|
153
|
-
|
154
|
-
|
155
|
-
|
200
|
+
context "with a named link" do
|
201
|
+
it "should have a class of selected added" do
|
202
|
+
@link = SelectedLinks::Link.new('Foo', 'options', :source => '/foo').generate
|
203
|
+
@link.html_options[:class].should =~ /selected/
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should not have a class of selected if there was not a match" do
|
207
|
+
@link = SelectedLinks::Link.new('Bar', 'options', :source => '/foo').generate
|
208
|
+
@link.html_options[:class].should_not =~ /selected/
|
209
|
+
end
|
156
210
|
end
|
157
211
|
|
158
|
-
|
159
|
-
|
160
|
-
|
212
|
+
context "with an existing class" do
|
213
|
+
it "should have a class of selected added" do
|
214
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'foo', :class => 'nav').generate
|
215
|
+
@link.html_options[:class].should =~ /selected/
|
216
|
+
end
|
217
|
+
|
218
|
+
it "should not have a class of selected if there was not a match" do
|
219
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'bar', :class => 'nav').generate
|
220
|
+
@link.html_options[:class].should_not =~ /selected/
|
221
|
+
end
|
222
|
+
|
223
|
+
it 'should have a space between the existing classes and the selected class' do
|
224
|
+
@link = SelectedLinks::Link.new('Name', 'options', :source => '/foo', :matcher => 'foo', :class => 'nav').generate
|
225
|
+
@link.html_options[:class].should == 'nav selected'
|
226
|
+
end
|
161
227
|
end
|
162
228
|
end
|
163
229
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: selected_links
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kainage
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
109
109
|
version: '0'
|
110
110
|
requirements: []
|
111
111
|
rubyforge_project:
|
112
|
-
rubygems_version: 2.
|
112
|
+
rubygems_version: 2.1.11
|
113
113
|
signing_key:
|
114
114
|
specification_version: 4
|
115
115
|
summary: Adds a link helper to ActionView to that adds a class of 'selected' when
|