public
Last active

  • Download Gist
accordion_rails.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.