link_to_active_state 1.0.2 → 1.0.3
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
@@ -88,6 +88,23 @@ Which will result in:
|
|
88
88
|
|
89
89
|
Note the li tag is included as part of the html generated by the link_to helper and should not be added separately.
|
90
90
|
|
91
|
+
You can also pass a proc to the wrapper option. This takes two arguments; the generated link and the options for the wrapper (which will include the active state class):
|
92
|
+
```ruby
|
93
|
+
link_to "Account", account_path,
|
94
|
+
active_on: account_path,
|
95
|
+
active_wrapper: proc { |link, wrapper_opts|
|
96
|
+
content_tag(:li, link, wrapper_opts)
|
97
|
+
}
|
98
|
+
```
|
99
|
+
|
100
|
+
And for either of the above methods you can supply HTML attributes for the wrapper:
|
101
|
+
```ruby
|
102
|
+
link_to "Account", account_path,
|
103
|
+
active_on: account_path,
|
104
|
+
active_wrapper: :li,
|
105
|
+
active_wrapper_options: { rel: "gallery" }
|
106
|
+
```
|
107
|
+
|
91
108
|
## Contributing
|
92
109
|
|
93
110
|
1. Fork it
|
@@ -33,11 +33,11 @@ module LinkToActiveState
|
|
33
33
|
end
|
34
34
|
|
35
35
|
if html_options.present? && html_options[:active_wrapper]
|
36
|
-
|
36
|
+
element_or_proc = html_options.delete(:active_wrapper)
|
37
37
|
wrapper_options = html_options.delete(:active_wrapper_options) || {}
|
38
38
|
wrapper_options = wrapper_options.merge(options)
|
39
39
|
|
40
|
-
|
40
|
+
render_with_wrapper(element_or_proc, wrapper_options) do
|
41
41
|
link_to_without_active_state(*args, &block)
|
42
42
|
end
|
43
43
|
else
|
@@ -64,6 +64,17 @@ module LinkToActiveState
|
|
64
64
|
original ||= ""
|
65
65
|
[original, new].delete_if(&:blank?).join(" ")
|
66
66
|
end
|
67
|
+
|
68
|
+
def render_with_wrapper(element_or_proc, wrapper_options, &block)
|
69
|
+
content = block.call
|
70
|
+
|
71
|
+
case element_or_proc
|
72
|
+
when Proc
|
73
|
+
element_or_proc.call(content, wrapper_options)
|
74
|
+
when Symbol || String
|
75
|
+
content_tag(element_or_proc, content, wrapper_options)
|
76
|
+
end
|
77
|
+
end
|
67
78
|
end
|
68
79
|
end
|
69
80
|
end
|
@@ -20,9 +20,9 @@ describe LinkToActiveState::ViewHelpers::UrlHelper do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
it "aliases link_to_with_active_state to link_to" do
|
23
|
-
lt = helper.link_to "Test", "/", :active_on =>
|
24
|
-
ltwas = helper.link_to_with_active_state "Test", "/", :active_on =>
|
25
|
-
ltwoas = helper.link_to_without_active_state "Test", "/", :active_on =>
|
23
|
+
lt = helper.link_to "Test", "/", :active_on => proc { true }
|
24
|
+
ltwas = helper.link_to_with_active_state "Test", "/", :active_on => proc { true }
|
25
|
+
ltwoas = helper.link_to_without_active_state "Test", "/", :active_on => proc { true }
|
26
26
|
lt.should eq(ltwas)
|
27
27
|
lt.should_not eq(ltwoas)
|
28
28
|
end
|
@@ -75,15 +75,27 @@ describe LinkToActiveState::ViewHelpers::UrlHelper do
|
|
75
75
|
lt = helper.link_to "Home", "/", :active_on => "/", :active_wrapper => :li
|
76
76
|
lt.should match(/<li>/i)
|
77
77
|
end
|
78
|
-
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "wrapper element support" do
|
79
81
|
it "supports options for the wrapper element" do
|
80
|
-
request.stub!(:fullpath).and_return("/wibble")
|
81
82
|
li = helper.link_to "Home", "/",
|
82
83
|
:active_on => "/",
|
83
84
|
:active_wrapper => :li,
|
84
85
|
:active_wrapper_options => { :class => "wobble" }
|
85
86
|
li.should match(/class=\"wobble\"/i)
|
86
87
|
end
|
88
|
+
|
89
|
+
it "supports a proc as the wrapper element" do
|
90
|
+
request.stub!(:fullpath).and_return("/")
|
91
|
+
li = helper.link_to "Home", "/",
|
92
|
+
:active_on => "/",
|
93
|
+
:active_wrapper => proc { |link, wrapper_options|
|
94
|
+
"<li class=\"#{wrapper_options[:class]}\">#{link}</li>"
|
95
|
+
}
|
96
|
+
li.should match(/<li class=\"active\">/i)
|
97
|
+
li.should match(/<a href/i)
|
98
|
+
end
|
87
99
|
end
|
88
100
|
end
|
89
101
|
end
|