Skip to content

@benjamin-thomas /accordion_rails.rb
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.