Skip to content

Instantly share code, notes, and snippets.

@fareesh
Forked from isc/README.markdown
Created March 21, 2012 10:27
Show Gist options
  • Save fareesh/2146036 to your computer and use it in GitHub Desktop.
Save fareesh/2146036 to your computer and use it in GitHub Desktop.
A micro gem providing an accordion view helper that generates the proper markup for Twitter Bootstrap

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
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 = '[email protected]'
s.homepage = 'https://dbinsights.herokuapp.com'
# s.test_file = '_spec.rb'
# s.add_development_dependency('rspec', ["~> 2.8"])
end
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment