Skip to content

Instantly share code, notes, and snippets.

@benjamin-thomas
Created June 7, 2013 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benjamin-thomas/5732422 to your computer and use it in GitHub Desktop.
Save benjamin-thomas/5732422 to your computer and use it in GitHub Desktop.
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment