CodeOfficer (owner)

Revisions

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