Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
class AccordionRails
This should be called inside an erb template (currently not working)
<%= "front-page-accordion") do |accordion| %>
<%= accordion.element(template: self, title: "My first title link", target_id: "collapseFirstRow") do %>
<p>This is the body content of the first accordion row</p>
<% end %>
<%= accordion.element(template: self, title: "My second title link", target_id: "collapseSecondRow") do %>
<p>Second accordion element</p>
<% end %>
<% end %>
def initialize(opts = {})
@parent_id = opts.fetch(:parent_id)
<div class="accordion" id="#{@parent_id}"> <!-- first line -->
#{yield self}
</div> <!-- last line -->
def element(opts = {}, &html_content)
target_id = opts.fetch(:target_id)
title = opts.fetch(:title)
template = opts.fetch(:template)
content = template.capture(&html_content)
@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}">
<div id="#{target_id}" class="accordion-body collapse in">
<div class="accordion-inner">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment