Skip to content

Commit 4141a61

Browse files
author
Russell Jones
committed
big change: helper 'tabs_for' now requires a block
1 parent 92aebfa commit 4141a61

File tree

2 files changed

+21
-43
lines changed

2 files changed

+21
-43
lines changed

README

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ Maybe you will find them useful as well.
1313
These include ... (just one helper for now)
1414

1515

16-
TabsRenderer
16+
TabsHelper
1717
----------------------------------------------------------------
1818

1919
This helper simplifies the code required to use JQuery UIs Tabs plugin.
2020

21-
There are 3 steps to rendering a tab ... initialize, create and render.
21+
Example usage ...
2222

23-
<% tabs = TabsRenderer.new(self) %>
24-
<% tabs.create('tab_one', 'Tab 1') do %>
23+
<% tabs_for do |tab| %>
24+
<% tab.create('tab_one', 'Tab 1') do %>
2525
# ... insert tab contents
2626
<% end %>
27-
<% tabs.create('tab_two', 'Tab 2') do %>
27+
<% tab.create('tab_two', 'Tab 2') do %>
2828
# ... insert tab contents
2929
<% end %>
30-
<%= tabs.render %>
30+
<% end %>
3131

3232
The above will generate this HTML in your view:
3333

@@ -49,43 +49,21 @@ Tabs will be rendered in the order you create them.
4949
You can easily render a tab conditionally by appending your condition to the end of
5050
the 'create' block as such ...
5151

52-
<% tabs.create('profile_tab', 'Your Profile') do %>
52+
<% tab.create('profile_tab', 'Your Profile') do %>
5353
# ... insert tab contents
5454
<% end unless @current_user.nil? %>
5555

5656
You can pass HTML options to either the parent DIV or any individual tab's
5757
DIV as you like ...
5858

59-
<% tabs = TabsRenderer.new(self, :class = 'flora') %>
60-
<% tabs.create('tab_one', 'Tab 1', :style => 'background: #FFF') do %>
59+
<% tabs_for(:class => 'zippy') do |tab| %>
60+
<% tab.create('tab_one', 'Tab 1', :style => 'background: #FFF') do %>
6161
# ... insert tab contents
6262
<% end %>
63-
<%= tabs.render %>
63+
<% end %>
6464

6565
The default DOM ID for the parent div is ... id="tabs" ... unless you pass in an HTML
6666
option with a different value.
6767

68-
Also optional, you can now call TabsRenderer.new with a block parameter, and create
69-
the tabs that way.
7068

71-
<% tabs = TabsRenderer.new(self) do |tab| %>
72-
<% tab.create('tab_one', 'Tab 1') do %>
73-
# ... insert tab contents
74-
<% end %>
75-
<% tab.create('tab_two', 'Tab 2') do %>
76-
# ... insert tab contents
77-
<% end %>
78-
<% end %>
79-
<%= tabs.render %>
80-
81-
##########################################################################
82-
Now works as:
83-
<% tabs_for do |tab| %>
84-
<% tab.create('tab_one', 'Tab 1') do %>
85-
# ... insert tab contents
86-
<% end %>
87-
<% tab.create('tab_two', 'Tab 2') do %>
88-
# ... insert tab contents
89-
<% end %>
90-
<% end %>
9169

helpers/tabs_helper.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,51 @@
11
module TabsHelper
22

3-
def tabs_for( *args, &proc )
3+
def tabs_for( *options, &block )
44
raise ArgumentError, "Missing block" unless block_given?
55

6-
tabs = TabsHelper::TabsRenderer.new( &proc )
6+
tabs = TabsHelper::TabsRenderer.new( *options, &block )
77
tabs_html = tabs.render
8-
concat tabs_html, proc.binding
8+
concat tabs_html, block.binding
99
end
1010

1111
class TabsRenderer
1212

13-
def initialize( options={}, &proc )
13+
def initialize( options={}, &block )
1414
raise ArgumentError, "Missing block" unless block_given?
1515

16-
@template = eval( 'self', proc.binding )
16+
@template = eval( 'self', block.binding )
1717
@options = options
1818
@tabs = []
1919

2020
yield self
2121
end
2222

23-
def create(tab_id, tab_text, options={}, &block)
23+
def create( tab_id, tab_text, options={}, &block )
2424
raise "Block needed for TabsRenderer#CREATE" unless block_given?
25-
@tabs << [tab_id, tab_text, options, block]
25+
@tabs << [ tab_id, tab_text, options, block ]
2626
end
2727

2828
def render
29-
content_tag(:div, (render_tabs + render_bodies), {:id => :tabs}.merge(@options))
29+
content_tag( :div, ( render_tabs + render_bodies ), { :id => :tabs }.merge( @options ) )
3030
end
3131

3232
private # ---------------------------------------------------------------------------
3333

3434
def render_tabs
3535
content_tag :ul do
3636
@tabs.collect do |tab|
37-
content_tag(:li, link_to(content_tag(:span, tab[1]), "##{tab[0]}") )
37+
content_tag( :li, link_to( content_tag( :span, tab[1] ), "##{tab[0]}" ) )
3838
end
3939
end
4040
end
4141

4242
def render_bodies
4343
@tabs.collect do |tab|
44-
content_tag(:div, capture(&tab[3]), tab[2].merge(:id => tab[0]))
44+
content_tag( :div, capture( &tab[3] ), tab[2].merge( :id => tab[0] ) )
4545
end.to_s
4646
end
4747

48-
def method_missing(*args, &block)
48+
def method_missing( *args, &block )
4949
@template.send( *args, &block )
5050
end
5151

0 commit comments

Comments
 (0)