public
Last active — forked from isc/README.markdown

A micro gem providing an accordion view helper that generates the proper markup for Twitter Bootstrap

  • Download Gist
README.markdown
Markdown

In your Gemfile:

gem 'bootstrap-components-helpers', :git => 'git://gist.github.com/2117187.git'

In your views:

= accordion do |accordion|
  = accordion.pane 'My first pane' do
    = render partial: 'my_first_pane'
  = accordion.pane 'My second pane' do
    = render partial: 'my_second_pane'

accordion method options :

  • :accordion_id : when you want more than one accordion on the same page
  • :open : when you want the first pane to be open on load
bootstrap-components-helpers.gemspec
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Gem::Specification.new do |s|
s.name = 'bootstrap-components-helpers'
s.summary = 'Accordion view helper for Twitter Bootstrap'
s.description = 'A micro gem providing an accordion view helper that generates the proper markup for Twitter Bootstrap'
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY
 
s.files = ['bootstrap-components-helpers.rb']
s.require_path = '.'
 
s.author = 'Ivan Schneider'
s.email = 'git@ivanschneider.fr'
s.homepage = 'https://dbinsights.herokuapp.com'
 
# s.test_file = '_spec.rb'
# s.add_development_dependency('rspec', ["~> 2.8"])
end
bootstrap-components-helpers.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
require 'builder'
module BootstrapComponentsHelpers
module AccordionHelper
def accordion opts = {}
opts[:accordion_id] ||= 'accordion'
builder = AccordionBuilder.new opts, self
content_tag :div, :class => 'accordion', :id => opts[:accordion_id] do
yield builder
end
end
 
class AccordionBuilder
 
attr_reader :parent
delegate :capture, :to => :parent
 
def initialize opts, parent
@first = true
@parent = parent
@opts = opts
end
 
def pane title, &block
b = Builder::XmlMarkup.new
b.div :class => 'accordion-group' do
b.div :class => 'accordion-heading' do
b.a title, :class => 'accordion-toggle', :'data-toggle' => 'collapse',
:'data-parent' => "##{@opts[:accordion_id]}", :href => "##{title.parameterize}_pane"
end
css_class = (@first && @opts[:open]) ? 'in' : ''
@first = false
b.div :class => "accordion-body collapse #{css_class}", :id => "#{title.parameterize}_pane" do
b.div :class => 'accordion-inner' do
b << capture(&block)
end
end
end.html_safe
end
end
end
end
 
ActionView::Base.send :include, BootstrapComponentsHelpers::AccordionHelper

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.