Skip to content

Commit 19d4e08

Browse files
author
Russell Jones
committed
Added a helper for working with jquery ui accordions, and removed the 2nd param for concat as its no longer required in new versions o rails.
1 parent a21b663 commit 19d4e08

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
== 03-07-2009
2+
3+
* big change: removed the 2nd param for concat, rails no longer requires the binding be passed.
4+
* added a helper for creating accordions
5+
16
== 09-04-2008
27

38
* big change: renamed classes, and helper 'tabs_for' now requires a block

README

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ These are some view helpers I use in Rails to better integrate JQuery UI in my s
1010

1111
Maybe you will find them useful as well.
1212

13-
These include ... (just one helper for now)
13+
These include: TabsHelper and AccordionsHelper
1414

1515

1616
TabsHelper
@@ -66,4 +66,16 @@ The default DOM ID for the parent div is ... id="tabs" ... unless you pass in an
6666
option with a different value.
6767

6868

69+
AccordionsHelper
70+
----------------------------------------------------------------
71+
72+
This helper simplifies the code required to use JQuery UIs Accordion plugin.
73+
74+
Usage is identical to the tabs helper.
75+
76+
<% accordions_for do |accordion| %>
77+
<% accordion.create("dom_id", "accordion_title") do %>
78+
# ... insert accordion contents
79+
<% end %>
80+
<% end %>
6981

helpers/accordions_helper.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
module AccordionsHelper
2+
3+
def accordions_for( *options, &block )
4+
raise ArgumentError, "Missing block" unless block_given?
5+
6+
accordions = AccordionsHelper::AccordionsRenderer.new( *options, &block )
7+
accordions_html = accordions.render
8+
concat accordions_html
9+
end
10+
11+
class AccordionsRenderer
12+
13+
def initialize( options={}, &block )
14+
raise ArgumentError, "Missing block" unless block_given?
15+
16+
@template = eval( 'self', block.binding )
17+
@options = options
18+
@accordions = []
19+
20+
yield self
21+
end
22+
23+
def create( accordion_id, accordion_text, options={}, &block )
24+
raise "Block needed for AccordionsRenderer#CREATE" unless block_given?
25+
@accordions << [ accordion_id, accordion_text, options, block ]
26+
end
27+
28+
def render
29+
content_tag :div, { :id => :accordions }.merge( @options ) do
30+
@accordions.collect do |accordion|
31+
content_tag :div, accordion[2].merge( :id => accordion[0] ) do
32+
accordion_head(accordion) + accordion_body(accordion)
33+
end
34+
end
35+
end
36+
end
37+
38+
private # ---------------------------------------------------------------------------
39+
40+
def accordion_head(accordion)
41+
content_tag :h3 do
42+
link_to accordion[1], '#'
43+
end
44+
end
45+
46+
def accordion_body(accordion)
47+
content_tag :div do
48+
capture( &accordion[3] )
49+
end
50+
end
51+
52+
def method_missing( *args, &block )
53+
@template.send( *args, &block )
54+
end
55+
56+
end
57+
58+
end

helpers/tabs_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def tabs_for( *options, &block )
55

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

1111
class TabsRenderer

0 commit comments

Comments
 (0)