Skip to content

Commit cebabce

Browse files
committed
new hash format
1 parent 38081ae commit cebabce

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

README.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ active_link_to 'Users', '/users'
2020
This is exactly the same as:
2121

2222
```ruby
23-
active_link_to 'Users', '/users', :active => :inclusive
23+
active_link_to 'Users', '/users', active: :inclusive
2424
# => <a href="/users" class="active">Users</a>
2525
```
2626

@@ -44,72 +44,72 @@ nodes. Most commonly used for 'home' links.
4444

4545
```ruby
4646
# For URL: /users will be active
47-
active_link_to 'Users', users_path, :active => :exclusive
47+
active_link_to 'Users', users_path, active: :exclusive
4848
# => <a href="/users" class="active">Users</a>
4949
```
5050

5151
```ruby
5252
# But for URL: /users/123 it will not be active
53-
active_link_to 'Users', users_path, :active => :exclusive
53+
active_link_to 'Users', users_path, active: :exclusive
5454
# => <a href="/users">Users</a>
5555
```
5656

5757
If we need to set link to be active based on some regular expression, we can do
5858
that as well. Let's try to activate links urls of which begin with 'use':
5959

6060
```ruby
61-
active_link_to 'Users', users_path, :active => /^\/use/
61+
active_link_to 'Users', users_path, active: /^\/use/
6262
```
6363

6464
If we need to set link to be active based on an exact match, we can do
6565
that as well:
6666

6767
```ruby
68-
active_link_to 'Users', users_path, :active => :exact
68+
active_link_to 'Users', users_path, active: :exact
6969
```
7070

7171
What if we need to mark link active for all URLs that match a particular controller,
7272
or action, or both? Or any number of those at the same time? Sure, why not:
7373

7474
```ruby
7575
# For matching multiple controllers and actions:
76-
active_link_to 'User Edit', edit_user_path(@user), :active => [['people', 'news'], ['show', 'edit']]
76+
active_link_to 'User Edit', edit_user_path(@user), active: [['people', 'news'], ['show', 'edit']]
7777

7878
# for matching all actions under given controllers:
79-
active_link_to 'User Edit', edit_user_path(@user), :active => [['people', 'news'], []]
79+
active_link_to 'User Edit', edit_user_path(@user), active: [['people', 'news'], []]
8080

8181
# for matching all controllers for a particular action
82-
active_link_to 'User Edit', edit_user_path(@user), :active => [[], ['edit']]
82+
active_link_to 'User Edit', edit_user_path(@user), active: [[], ['edit']]
8383
```
8484

8585
Sometimes it should be as easy as giving link true or false value:
8686

8787
```ruby
88-
active_link_to 'Users', users_path, :active => true
88+
active_link_to 'Users', users_path, active: true
8989
```
9090

9191
## More Options
9292
You can specify active and inactive css classes for links:
9393

9494
```ruby
95-
active_link_to 'Users', users_path, :class_active => 'enabled'
95+
active_link_to 'Users', users_path, class_active: 'enabled'
9696
# => <a href="/users" class="enabled">Users</a>
9797

98-
active_link_to 'News', news_path, :class_inactive => 'disabled'
98+
active_link_to 'News', news_path, class_inactive: 'disabled'
9999
# => <a href="/news" class="disabled">News</a>
100100
```
101101

102102
Sometimes you want to replace link tag with a span if it's active:
103103

104104
```ruby
105-
active_link_to 'Users', users_path, :active_disable => true
105+
active_link_to 'Users', users_path, active_disable: true
106106
# => <span class="active">Users</span>
107107
```
108108

109109
If you are constructing navigation menu it might be helpful to wrap links in another tag, like `<li>`:
110110

111111
```ruby
112-
active_link_to 'Users', users_path, :wrap_tag => :li
112+
active_link_to 'Users', users_path, wrap_tag: :li
113113
# => <li class="active"><a href="/users" class="active">Users</a></li>
114114
```
115115

@@ -126,7 +126,7 @@ is_active_link?(users_path, :inclusive)
126126
`active_link_to_class` will return the css class:
127127

128128
```
129-
active_link_to_class(users_path, :active => :inclusive)
129+
active_link_to_class(users_path, active: :inclusive)
130130
# => 'active'
131131
```
132132

Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ Rake::TestTask.new(:test) do |test|
88
test.verbose = true
99
end
1010

11-
task :default => :test
11+
task default: :test

lib/active_link_to/active_link_to.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module ActiveLinkTo
77
# :disable_active => Boolean
88
# :wrap_tag => Symbol
99
# Example usage:
10-
# active_link_to('/users', :class_active => 'enabled')
11-
# active_link_to(users_path, :active => :exclusive, :wrap_tag => :li)
10+
# active_link_to('/users', class_active: 'enabled')
11+
# active_link_to(users_path, active: :exclusive, wrap_tag: :li)
1212
def active_link_to(*args, &block)
1313
if block_given?
1414
name = capture(&block)
@@ -44,12 +44,12 @@ def active_link_to(*args, &block)
4444
link_to(name, url, link_options)
4545
end
4646

47-
wrap_tag ? content_tag(wrap_tag, link, :class => (css_class if css_class.present?)) : link
47+
wrap_tag ? content_tag(wrap_tag, link, class: (css_class if css_class.present?)) : link
4848
end
4949

5050
# Returns css class name. Takes the link's URL and its params
5151
# Example usage:
52-
# active_link_to_class('/root', :class_active => 'on', :class_inactive => 'off')
52+
# active_link_to_class('/root', class_active: 'on', class_inactive: 'off')
5353
#
5454
def active_link_to_class(url, options = {})
5555
if is_active_link?(url, options[:active])

test/active_link_to_test.rb

+22-22
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,19 @@ def test_is_active_link_array
105105
def test_is_active_link_hash
106106
params[:a] = 1
107107

108-
assert is_active_link?('/', {:a => 1})
109-
assert is_active_link?('/', {:a => 1, :b => nil})
108+
assert is_active_link?('/', {a: 1})
109+
assert is_active_link?('/', {a: 1, b: nil})
110110

111-
refute is_active_link?('/', {:a => 1, :b => 2})
112-
refute is_active_link?('/', {:a => 2})
111+
refute is_active_link?('/', {a: 1, b: 2})
112+
refute is_active_link?('/', {a: 2})
113113

114114
params[:b] = 2
115115

116-
assert is_active_link?('/', {:a => 1, :b => 2})
117-
assert is_active_link?('/', {:a => 1, :b => 2, :c => nil})
116+
assert is_active_link?('/', {a: 1, b: 2})
117+
assert is_active_link?('/', {a: 1, b: 2, c: nil})
118118

119-
assert is_active_link?('/', {:a => 1})
120-
assert is_active_link?('/', {:b => 2})
119+
assert is_active_link?('/', {a: 1})
120+
assert is_active_link?('/', {b: 2})
121121
end
122122

123123
def test_is_active_link_with_memoization
@@ -131,10 +131,10 @@ def test_is_active_link_with_memoization
131131
def test_active_link_to_class
132132
set_fullpath('/root')
133133
assert_equal 'active', active_link_to_class('/root')
134-
assert_equal 'on', active_link_to_class('/root', :class_active => 'on')
134+
assert_equal 'on', active_link_to_class('/root', class_active: 'on')
135135

136136
assert_equal '', active_link_to_class('/other')
137-
assert_equal 'off', active_link_to_class('/other', :class_inactive => 'off')
137+
assert_equal 'off', active_link_to_class('/other', class_inactive: 'off')
138138
end
139139

140140
def test_active_link_to
@@ -148,54 +148,54 @@ def test_active_link_to
148148

149149
def test_active_link_to_with_existing_class
150150
set_fullpath('/root')
151-
link = active_link_to('label', '/root', :class => 'current')
151+
link = active_link_to('label', '/root', class: 'current')
152152
assert_html link, 'a.current.active[href="/root"]', 'label'
153153

154-
link = active_link_to('label', '/other', :class => 'current')
154+
link = active_link_to('label', '/other', class: 'current')
155155
assert_html link, 'a.current[href="/other"]', 'label'
156156
end
157157

158158
def test_active_link_to_with_custom_classes
159159
set_fullpath('/root')
160-
link = active_link_to('label', '/root', :class_active => 'on')
160+
link = active_link_to('label', '/root', class_active: 'on')
161161
assert_html link, 'a.on[href="/root"]', 'label'
162162

163-
link = active_link_to('label', '/other', :class_inactive => 'off')
163+
link = active_link_to('label', '/other', class_inactive: 'off')
164164
assert_html link, 'a.off[href="/other"]', 'label'
165165
end
166166

167167
def test_active_link_to_with_wrap_tag
168168
set_fullpath('/root')
169-
link = active_link_to('label', '/root', :wrap_tag => :li)
169+
link = active_link_to('label', '/root', wrap_tag: :li)
170170
assert_html link, 'li.active a.active[href="/root"]', 'label'
171171

172-
link = active_link_to('label', '/root', :wrap_tag => :li, :active_disable => true)
172+
link = active_link_to('label', '/root', wrap_tag: :li, active_disable: true)
173173
assert_html link, 'li.active span.active', 'label'
174174

175-
link = active_link_to('label', '/root', :wrap_tag => :li, :class => 'testing')
175+
link = active_link_to('label', '/root', wrap_tag: :li, class: 'testing')
176176
assert_html link, 'li.testing.active a.testing.active[href="/root"]', 'label'
177177
end
178178

179179
def test_active_link_to_with_active_disable
180180
set_fullpath('/root')
181-
link = active_link_to('label', '/root', :active_disable => true)
181+
link = active_link_to('label', '/root', active_disable: true)
182182
assert_html link, 'span.active', 'label'
183183
end
184184

185185
def test_should_not_modify_passed_params
186186
set_fullpath('/root')
187-
params = { :class => 'testing', :active => :inclusive }
187+
params = {class: 'testing', active: :inclusive}
188188
out = active_link_to 'label', '/root', params
189189
assert_html out, 'a.testing.active[href="/root"]', 'label'
190-
assert_equal ({:class => 'testing', :active => :inclusive }), params
190+
assert_equal ({class: 'testing', active: :inclusive }), params
191191
end
192192

193193
def test_no_empty_class_attribute
194194
set_fullpath('/root')
195-
link = active_link_to('label', '/root', :wrap_tag => :li)
195+
link = active_link_to('label', '/root', wrap_tag: :li)
196196
assert_html link, 'li.active a.active[href="/root"]', 'label'
197197

198-
link = active_link_to('label', '/other', :wrap_tag => :li)
198+
link = active_link_to('label', '/other', wrap_tag: :li)
199199
assert_html link, 'li a[href="/other"]', 'label'
200200
end
201201

0 commit comments

Comments
 (0)