Skip to content

Instantly share code, notes, and snippets.

@calebhaye
Forked from gt-sdi/README.markdown
Last active December 14, 2015 15:09
Show Gist options
  • Save calebhaye/5105757 to your computer and use it in GitHub Desktop.
Save calebhaye/5105757 to your computer and use it in GitHub Desktop.

Twitter Bootstrap Components Helper

Provides an accordion helper and a tabs helper

In your Gemfile:

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

Accordion helper:

= 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.

pane method options :

  • :open : when you want that pane to be open on load.

Tabs helper:

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

tabs method options :

  • :active : when you want that pane to be active on load.

  • :direction : to control the positioning of the tabs. Valid values are below, left, right and above (default is above).

  • :style : tabs or pills (default is tabs).

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, :content_tag, :link_to, :to => :parent
def initialize opts, parent
@parent = parent
@opts = opts
end
def pane title, options = {}, &block
css_class = options[:open] ? 'in' : ''
content_tag :div, :class => 'accordion-group' do
heading = content_tag :div, :class => 'accordion-heading' do
link_to title, "##{title.parameterize}_pane", :class => 'accordion-toggle', :'data-toggle' => 'collapse',
:'data-parent' => "##{@opts[:accordion_id]}"
end
body = content_tag :div, :class => "accordion-body collapse #{css_class}", :id => "#{title.parameterize}_pane" do
content_tag :div, :class => 'accordion-inner' do
capture(&block)
end
end
heading + body
end
end
end
end
end
ActionView::Base.send :include, BootstrapComponentsHelpers::AccordionHelper
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. Modified by Graham to specify open accordion pane right on pane'
s.version = '0.0.2'
s.platform = Gem::Platform::RUBY
s.files = ['bootstrap-components-helpers.rb', 'accordion_helper.rb', 'tabs_helper.rb']
s.require_path = '.'
s.authors = ['Ivan Schneider', 'Graham Torn']
s.email = '[email protected]'
s.homepage = 'https://dbinsights.herokuapp.com'
# s.test_file = '_spec.rb'
# s.add_development_dependency('rspec', ["~> 2.8"])
end
require 'accordion_helper'
require 'tabs_helper'
module BootstrapComponentsHelpers
module TabsHelper
def tabs opts = {}
opts[:direction] ||= 'above'
opts[:style] ||= 'tabs'
builder = TabsBuilder.new self
yield builder
tabs = content_tag(:ul, builder.pane_handles.join("\n").html_safe, :class => "nav nav-#{opts[:style]}")
contents = content_tag(:div, builder.pane_contents.join("\n").html_safe, :class => 'tab-content')
css_direction = "tabs-#{opts[:direction]}" unless opts[:direction] == 'above'
content_tag :div, :class => "tabbable #{css_direction}" do
if opts[:direction] == 'below'
contents + tabs
else
tabs + contents
end
end
end
class TabsBuilder
attr_reader :parent, :pane_contents, :pane_handles
delegate :capture, :content_tag, :to => :parent
def initialize parent
@first = true
@parent = parent
@pane_handles = []
@pane_contents = []
end
def pane title, options = {}, &block
css_class = options[:active] ? 'active' : ''
@first = false if @first
link = content_tag(:a, title, :'data-toggle' => 'tab', :href => "##{title.parameterize}_tab")
@pane_handles << content_tag(:li, link, :class => css_class)
@pane_contents << (content_tag :div, :class => "tab-pane #{css_class}", :id => "#{title.parameterize}_tab" do
capture(&block)
end)
nil
end
end
end
end
ActionView::Base.send :include, BootstrapComponentsHelpers::TabsHelper
@MattMSumner
Copy link

This is perfect. Thanks for adding the active option to panes. Delicious!

@changwu
Copy link

changwu commented Mar 13, 2013

hi, I don't know why only display the second accordion when i use erb ?? can u help me ?? thanks.
<%= accordion do |aaa|
aaa.pane 'My first pane' do
render 'a'
end
aaa.pane 'My second pane' do
render 'b'
end
end
%>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment