Created
June 7, 2013 21:06
-
-
Save benjamin-thomas/5732422 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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