Skip to content

Commit 38081ae

Browse files
committed
added memoization
1 parent 3442451 commit 38081ae

File tree

5 files changed

+86
-69
lines changed

5 files changed

+86
-69
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2009-14 Oleg Khabarov, The Working Group Inc.
1+
Copyright (c) 2009-17 Oleg Khabarov
22

33
Permission is hereby granted, free of charge, to any person obtaining
44
a copy of this software and associated documentation files (the

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,4 @@ active_link_to_class(users_path, :active => :inclusive)
132132

133133
### Copyright
134134

135-
Copyright (c) 2009-15 Oleg Khabarov, The Working Group Inc. See LICENSE for details.
135+
Copyright (c) 2009-17 Oleg Khabarov. See LICENSE for details.

lib/active_link_to/active_link_to.rb

+26-23
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,32 @@ def active_link_to_class(url, options = {})
7474
# is_active_link?('/root', ['users', ['show', 'edit']])
7575
#
7676
def is_active_link?(url, condition = nil)
77-
original_url = url
78-
url = URI::parse(url).path
79-
case condition
80-
when :inclusive, nil
81-
!request.fullpath.match(/^#{Regexp.escape(url).chomp('/')}(\/.*|\?.*)?$/).blank?
82-
when :exclusive
83-
!request.fullpath.match(/^#{Regexp.escape(url)}\/?(\?.*)?$/).blank?
84-
when :exact
85-
request.fullpath == original_url
86-
when Regexp
87-
!request.fullpath.match(condition).blank?
88-
when Array
89-
controllers = [*condition[0]]
90-
actions = [*condition[1]]
91-
(controllers.blank? || controllers.member?(params[:controller])) &&
92-
(actions.blank? || actions.member?(params[:action]))
93-
when TrueClass
94-
true
95-
when FalseClass
96-
false
97-
when Hash
98-
condition.all? do |key, value|
99-
params[key].to_s == value.to_s
77+
@is_active_link ||= {}
78+
@is_active_link[[url, condition]] ||= begin
79+
original_url = url
80+
url = URI::parse(url).path
81+
case condition
82+
when :inclusive, nil
83+
!request.fullpath.match(/^#{Regexp.escape(url).chomp('/')}(\/.*|\?.*)?$/).blank?
84+
when :exclusive
85+
!request.fullpath.match(/^#{Regexp.escape(url)}\/?(\?.*)?$/).blank?
86+
when :exact
87+
request.fullpath == original_url
88+
when Regexp
89+
!request.fullpath.match(condition).blank?
90+
when Array
91+
controllers = [*condition[0]]
92+
actions = [*condition[1]]
93+
(controllers.blank? || controllers.member?(params[:controller])) &&
94+
(actions.blank? || actions.member?(params[:action]))
95+
when TrueClass
96+
true
97+
when FalseClass
98+
false
99+
when Hash
100+
condition.all? do |key, value|
101+
params[key].to_s == value.to_s
102+
end
100103
end
101104
end
102105
end

test/active_link_to_test.rb

+37-29
Original file line numberDiff line numberDiff line change
@@ -8,82 +8,82 @@ def test_is_active_link_booleans_test
88
end
99

1010
def test_is_active_link_symbol_inclusive
11-
request.fullpath = '/root'
11+
set_fullpath('/root')
1212
assert is_active_link?('/root', :inclusive)
1313

14-
request.fullpath = '/root?param=test'
14+
set_fullpath('/root?param=test')
1515
assert is_active_link?('/root', :inclusive)
1616

17-
request.fullpath = '/root/child/sub-child'
17+
set_fullpath('/root/child/sub-child')
1818
assert is_active_link?('/root', :inclusive)
1919

20-
request.fullpath = '/other'
20+
set_fullpath('/other')
2121
refute is_active_link?('/root', :inclusive)
2222
end
2323

2424
def test_is_active_link_symbol_inclusive_implied
25-
request.fullpath = '/root/child/sub-child'
25+
set_fullpath('/root/child/sub-child')
2626
assert is_active_link?('/root')
2727
end
2828

2929
def test_is_active_link_symbol_inclusive_similar_path
30-
request.fullpath = '/root/abc'
30+
set_fullpath('/root/abc')
3131
refute is_active_link?('/root/a', :inclusive)
3232
end
3333

3434
def test_is_active_link_symbol_inclusive_with_last_slash
35-
request.fullpath = '/root/abc'
35+
set_fullpath('/root/abc')
3636
assert is_active_link?('/root/')
3737
end
3838

3939
def test_is_active_link_symbol_inclusive_with_last_slash_and_similar_path
40-
request.fullpath = '/root_path'
40+
set_fullpath('/root_path')
4141
refute is_active_link?('/root/')
4242
end
4343

4444
def test_is_active_link_symbol_inclusive_with_link_params
45-
request.fullpath = '/root?param=test'
45+
set_fullpath('/root?param=test')
4646
assert is_active_link?('/root?attr=example')
4747
end
4848

4949
def test_is_active_link_symbol_exclusive
50-
request.fullpath = '/root'
50+
set_fullpath('/root')
5151
assert is_active_link?('/root', :exclusive)
5252

53-
request.fullpath = '/root?param=test'
53+
set_fullpath('/root?param=test')
5454
assert is_active_link?('/root', :exclusive)
5555

56-
request.fullpath = '/root/child'
56+
set_fullpath('/root/child')
5757
refute is_active_link?('/root', :exclusive)
5858
end
5959

6060
def test_is_active_link_symbol_exclusive_with_link_params
61-
request.fullpath = '/root?param=test'
61+
set_fullpath('/root?param=test')
6262
assert is_active_link?('/root?attr=example', :exclusive)
6363
end
6464

6565
def test_is_active_link_symbol_exact
66-
request.fullpath = '/root?param=test'
66+
set_fullpath('/root?param=test')
6767
assert is_active_link?('/root?param=test', :exact)
6868

69-
request.fullpath = '/root?param=test'
69+
set_fullpath('/root?param=test')
7070
refute is_active_link?('/root?param=exact', :exact)
7171

72-
request.fullpath = '/root'
72+
set_fullpath('/root')
7373
refute is_active_link?('/root?param=test', :exact)
7474

75-
request.fullpath = '/root?param=test'
75+
set_fullpath('/root?param=test')
7676
refute is_active_link?('/root', :exact)
7777
end
7878

7979
def test_is_active_link_regex
80-
request.fullpath = '/root'
80+
set_fullpath('/root')
8181
assert is_active_link?('/', /^\/root/)
8282

83-
request.fullpath = '/root/child'
83+
set_fullpath('/root/child')
8484
assert is_active_link?('/', /^\/r/)
8585

86-
request.fullpath = '/other'
86+
set_fullpath('/other')
8787
refute is_active_link?('/', /^\/r/)
8888
end
8989

@@ -120,8 +120,16 @@ def test_is_active_link_hash
120120
assert is_active_link?('/', {:b => 2})
121121
end
122122

123+
def test_is_active_link_with_memoization
124+
set_fullpath('/')
125+
assert is_active_link?('/', :exclusive)
126+
127+
set_fullpath('/other', false)
128+
assert is_active_link?('/', :exclusive)
129+
end
130+
123131
def test_active_link_to_class
124-
request.fullpath = '/root'
132+
set_fullpath('/root')
125133
assert_equal 'active', active_link_to_class('/root')
126134
assert_equal 'on', active_link_to_class('/root', :class_active => 'on')
127135

@@ -130,7 +138,7 @@ def test_active_link_to_class
130138
end
131139

132140
def test_active_link_to
133-
request.fullpath = '/root'
141+
set_fullpath('/root')
134142
link = active_link_to('label', '/root')
135143
assert_html link, 'a.active[href="/root"]', 'label'
136144

@@ -139,7 +147,7 @@ def test_active_link_to
139147
end
140148

141149
def test_active_link_to_with_existing_class
142-
request.fullpath = '/root'
150+
set_fullpath('/root')
143151
link = active_link_to('label', '/root', :class => 'current')
144152
assert_html link, 'a.current.active[href="/root"]', 'label'
145153

@@ -148,7 +156,7 @@ def test_active_link_to_with_existing_class
148156
end
149157

150158
def test_active_link_to_with_custom_classes
151-
request.fullpath = '/root'
159+
set_fullpath('/root')
152160
link = active_link_to('label', '/root', :class_active => 'on')
153161
assert_html link, 'a.on[href="/root"]', 'label'
154162

@@ -157,7 +165,7 @@ def test_active_link_to_with_custom_classes
157165
end
158166

159167
def test_active_link_to_with_wrap_tag
160-
request.fullpath = '/root'
168+
set_fullpath('/root')
161169
link = active_link_to('label', '/root', :wrap_tag => :li)
162170
assert_html link, 'li.active a.active[href="/root"]', 'label'
163171

@@ -169,21 +177,21 @@ def test_active_link_to_with_wrap_tag
169177
end
170178

171179
def test_active_link_to_with_active_disable
172-
request.fullpath = '/root'
180+
set_fullpath('/root')
173181
link = active_link_to('label', '/root', :active_disable => true)
174182
assert_html link, 'span.active', 'label'
175183
end
176184

177185
def test_should_not_modify_passed_params
178-
request.fullpath = '/root'
186+
set_fullpath('/root')
179187
params = { :class => 'testing', :active => :inclusive }
180188
out = active_link_to 'label', '/root', params
181189
assert_html out, 'a.testing.active[href="/root"]', 'label'
182190
assert_equal ({:class => 'testing', :active => :inclusive }), params
183191
end
184192

185193
def test_no_empty_class_attribute
186-
request.fullpath = '/root'
194+
set_fullpath('/root')
187195
link = active_link_to('label', '/root', :wrap_tag => :li)
188196
assert_html link, 'li.active a.active[href="/root"]', 'label'
189197

@@ -192,7 +200,7 @@ def test_no_empty_class_attribute
192200
end
193201

194202
def test_active_link_to_with_url
195-
request.fullpath = '/root'
203+
set_fullpath('/root')
196204
link = active_link_to('label', 'http://example.com/root')
197205
assert_html link, 'a.active[href="http://example.com/root"]', 'label'
198206
end

test/test_helper.rb

+21-15
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,38 @@
44
require 'action_view'
55
require 'active_link_to'
66

7-
# need this to simulate requests that drive active_link_helper
8-
module FakeRequest
9-
class Request
10-
attr_accessor :fullpath
11-
end
12-
def request
13-
@request ||= Request.new
14-
end
15-
def params
16-
@params ||= {}
17-
end
18-
end
7+
class MiniTest::Test
198

20-
ActiveLinkTo.send :include, FakeRequest
9+
# need this to simulate requests that drive active_link_helper
10+
module FakeRequest
11+
class Request
12+
attr_accessor :fullpath
13+
end
14+
def request
15+
@request ||= Request.new
16+
end
17+
def params
18+
@params ||= {}
19+
end
20+
end
2121

22-
class MiniTest::Test
22+
ActiveLinkTo.send :include, FakeRequest
2323

2424
include ActionView::Helpers::UrlHelper
2525
include ActionView::Helpers::TagHelper
2626
include ActiveLinkTo
2727

28+
def set_fullpath(path, purge_cache = true)
29+
request.fullpath = path
30+
if purge_cache && defined?(@is_active_link)
31+
remove_instance_variable(:@is_active_link)
32+
end
33+
end
34+
2835
def assert_html(html, selector, value = nil)
2936
doc = Nokogiri::HTML(html)
3037
element = doc.at_css(selector)
3138
assert element, "No element found at: `#{selector}`"
3239
assert_equal value, element.text if value
3340
end
34-
3541
end

0 commit comments

Comments
 (0)