Skip to content

Instantly share code, notes, and snippets.

@benjamin-thomas
Last active December 18, 2015 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjamin-thomas/5723324 to your computer and use it in GitHub Desktop.
Save benjamin-thomas/5723324 to your computer and use it in GitHub Desktop.
class AccordionRails
=begin
This should be called inside an erb template (currently not working)
<%= AccordionRails.new(parent_id: "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 %>
=end
def initialize(opts = {})
@parent_id = opts.fetch(:parent_id)
<<-EOF.html_safe
<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)
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}">
#{title}
</a>
</div>
<div id="#{target_id}" class="accordion-body collapse in">
<div class="accordion-inner">
#{content}
</div>
</div>
</div>
EOF
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment