public
Last active

  • Download Gist
accordion_ruby.rb
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
#!/usr/bin/env ruby
 
# gem install colored
require "colored"
 
# The idea of this class is to encapsulate the creation of complex html elements such
# as the bootstrap carousel found here
# http://twitter.github.io/bootstrap/javascript.html#collapse
#
# This is a ruby prototype before implementing the idea in rails.
# This prototype is working fine now.
class AccordionRuby
 
def initialize(opts = {})
@parent_id = opts.fetch(:parent_id).red
 
puts <<-EOF
<div class="accordion" id="#{@parent_id}"> <!-- first line -->
#{yield self}
</div> <!-- last line -->
EOF
 
end
 
def element(opts = {}, &html_content)
target_id = opts.fetch(:target_id).blue
title = opts.fetch(:title).green
 
@build_buffer ||= ""
@build_buffer += <<-EOF
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="##{@parent_id}" href="##{target_id}">
#{title}
</a>
</div>
 
<div id="#{target_id}" class="accordion-body collapse in">
<div class="accordion-inner">
#{html_content.call.magenta}
</div>
</div>
</div>
EOF
 
end
end
 
AccordionRuby.new(parent_id: "front-page-accordion") do |accordion|
 
accordion.element(title: "My first title link", target_id: "collapseFirstRow") do
"<p>This is the body content of the first accordion row</p>"
end
 
accordion.element(title: "My second title link", target_id: "collapseSecondRow") do
"<p>Another row. This content appears after having clicked on the corresponding button like element</p>"
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.