link_to_active_state 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -1,3 +1,3 @@
1
1
  module LinkToActiveState
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
@@ -33,11 +33,11 @@ module LinkToActiveState
33
33
  end
34
34
 
35
35
  if html_options.present? && html_options[:active_wrapper]
36
- element = html_options.delete(:active_wrapper)
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
- content_tag(element, wrapper_options) do
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
@@ -3,5 +3,7 @@ require 'action_view'
3
3
 
4
4
  module LinkToActiveState
5
5
  module ViewHelpers
6
+ module UrlHelper
7
+ end
6
8
  end
7
9
  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 => lambda { true }
24
- ltwas = helper.link_to_with_active_state "Test", "/", :active_on => lambda { true }
25
- ltwoas = helper.link_to_without_active_state "Test", "/", :active_on => lambda { true }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: link_to_active_state
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: