link_to_active_state 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md
CHANGED
@@ -49,6 +49,11 @@ Or a proc:
|
|
49
49
|
link_to "Account", account_path, active_on: lambda { request.fullpath == account_path }
|
50
50
|
```
|
51
51
|
|
52
|
+
Any other value will cause it to be matched against the URL supplied to the link_to helper:
|
53
|
+
```ruby
|
54
|
+
link_to "Account", account_path, active_on: true
|
55
|
+
```
|
56
|
+
|
52
57
|
### Custom options
|
53
58
|
|
54
59
|
By default the class "active" will be added to the existing classes of the link. However you can specify your own:
|
@@ -12,17 +12,19 @@ module LinkToActiveState
|
|
12
12
|
def link_to_with_active_state(*args, &block)
|
13
13
|
options = {}
|
14
14
|
|
15
|
-
html_options = if block_given?
|
16
|
-
args.second
|
15
|
+
link_options, html_options = if block_given?
|
16
|
+
[args.first, args.second]
|
17
17
|
else
|
18
|
-
args[2]
|
18
|
+
[args[1], args[2]]
|
19
19
|
end
|
20
20
|
|
21
|
+
link_options ||= {}
|
22
|
+
|
21
23
|
if html_options.present? && html_options[:active_on].present?
|
22
24
|
active_on = html_options.delete(:active_on)
|
23
25
|
active_state = html_options.delete(:active_state) || "active"
|
24
26
|
|
25
|
-
if is_active?(active_on)
|
27
|
+
if is_active?(active_on, link_options)
|
26
28
|
case active_state
|
27
29
|
when Proc
|
28
30
|
options = options.merge(active_state.call(html_options))
|
@@ -47,7 +49,7 @@ module LinkToActiveState
|
|
47
49
|
end
|
48
50
|
|
49
51
|
private
|
50
|
-
def is_active?(active_on)
|
52
|
+
def is_active?(active_on, link_options = {})
|
51
53
|
case active_on
|
52
54
|
when String
|
53
55
|
request.fullpath == active_on
|
@@ -57,6 +59,10 @@ module LinkToActiveState
|
|
57
59
|
request.fullpath =~ active_on
|
58
60
|
when Proc
|
59
61
|
active_on.arity == 1 ? active_on.call(request) : active_on.call
|
62
|
+
else
|
63
|
+
# Anything else we'll take as a true argument, and match the link's URL
|
64
|
+
url = url_for(link_options)
|
65
|
+
request.fullpath == url
|
60
66
|
end
|
61
67
|
end
|
62
68
|
|
@@ -43,13 +43,18 @@ describe LinkToActiveState::ViewHelpers::UrlHelper do
|
|
43
43
|
end
|
44
44
|
|
45
45
|
context "when the current request path matches" do
|
46
|
-
|
47
46
|
it "adds an active state" do
|
48
47
|
request.stub!(:fullpath).and_return("/")
|
49
48
|
lt = helper.link_to "Home", "/", :active_on => "/"
|
50
49
|
lt.should match(/class=\"active\"/i)
|
51
50
|
end
|
52
51
|
|
52
|
+
it "uses the link's URL by default" do
|
53
|
+
request.stub!(:fullpath).and_return("/")
|
54
|
+
lt = helper.link_to "Home", "/", :active_on => true
|
55
|
+
lt.should match(/class=\"active\"/i)
|
56
|
+
end
|
57
|
+
|
53
58
|
it "encloses link in an element with active state if active_wrapper is true" do
|
54
59
|
request.stub!(:fullpath).and_return("/")
|
55
60
|
lt = helper.link_to "Home", "/", :active_on => "/", :active_wrapper => :li
|