bootstrap-components-helpers 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,45 @@
1
+ ## Twitter Bootstrap Components Helper
2
+
3
+ Provides an `accordion` helper and a `tabs` helper
4
+
5
+ In your Gemfile:
6
+
7
+ gem 'bootstrap-components-helpers'
8
+
9
+ ### Accordion helper:
10
+
11
+ = accordion do |accordion|
12
+ = accordion.pane 'My first pane' do
13
+ = render partial: 'my_first_pane'
14
+ = accordion.pane 'My second pane' do
15
+ = render partial: 'my_second_pane'
16
+
17
+ `accordion` method options :
18
+
19
+ - `:accordion_id` : when you want more than one accordion on the same page.
20
+
21
+ `pane` method options :
22
+
23
+ - `:open` : when you want that pane to be open on load.
24
+
25
+ ### Tabs helper:
26
+
27
+ = tabs do |tabs|
28
+ - tabs.pane 'My first pane' do
29
+ = render partial: 'my_first_pane'
30
+ - tabs.pane 'My second pane' do
31
+ = render partial: 'my_second_pane'
32
+
33
+ `tabs` method options :
34
+
35
+ - `:direction` : to control the positioning of the tabs. Valid values are `below`, `left`, `right` and `above` (default is `above`).
36
+ - `:style` : `tabs` or `pills` (default is `tabs`).
37
+
38
+ `pane` method options :
39
+
40
+ - `:active` : when you want that pane to be active on load.
41
+
42
+ ### Contributors
43
+
44
+ - Graham Torn
45
+ - Caleb Adam Haye
@@ -0,0 +1,2 @@
1
+ require 'bootstrap-components-helpers/accordion_helper'
2
+ require 'bootstrap-components-helpers/tabs_helper'
@@ -0,0 +1,40 @@
1
+ module BootstrapComponentsHelpers
2
+ module AccordionHelper
3
+ def accordion opts = {}
4
+ opts[:accordion_id] ||= 'accordion'
5
+ builder = AccordionBuilder.new opts, self
6
+ content_tag :div, :class => 'accordion', :id => opts[:accordion_id] do
7
+ yield builder
8
+ end
9
+ end
10
+
11
+ class AccordionBuilder
12
+
13
+ attr_reader :parent
14
+ delegate :capture, :content_tag, :link_to, :to => :parent
15
+
16
+ def initialize opts, parent
17
+ @parent = parent
18
+ @opts = opts
19
+ end
20
+
21
+ def pane title, options = {}, &block
22
+ css_class = options[:open] ? 'in' : ''
23
+ content_tag :div, :class => 'accordion-group' do
24
+ heading = content_tag :div, :class => 'accordion-heading' do
25
+ link_to title, "##{title.parameterize}_pane", :class => 'accordion-toggle', :'data-toggle' => 'collapse',
26
+ :'data-parent' => "##{@opts[:accordion_id]}"
27
+ end
28
+ body = content_tag :div, :class => "accordion-body collapse #{css_class}", :id => "#{title.parameterize}_pane" do
29
+ content_tag :div, :class => 'accordion-inner' do
30
+ capture(&block)
31
+ end
32
+ end
33
+ heading + body
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ ActionView::Base.send :include, BootstrapComponentsHelpers::AccordionHelper
@@ -0,0 +1,54 @@
1
+ module BootstrapComponentsHelpers
2
+ module TabsHelper
3
+ def tabs opts = {}
4
+ opts[:direction] ||= 'above'
5
+ opts[:style] ||= 'tabs'
6
+ builder = TabsBuilder.new self
7
+ yield builder
8
+ tabs = content_tag(:ul, builder.pane_handles_html, :class => "nav nav-#{opts[:style]}")
9
+ contents = content_tag(:div, builder.pane_contents_html, :class => 'tab-content')
10
+ css_direction = "tabs-#{opts[:direction]}" unless opts[:direction] == 'above'
11
+ content_tag :div, :class => "tabbable #{css_direction}" do
12
+ opts[:direction] == 'below' ? (contents + tabs) : (tabs + contents)
13
+ end
14
+ end
15
+
16
+ class TabsBuilder
17
+
18
+ attr_reader :parent, :pane_contents, :pane_handles
19
+ delegate :capture, :content_tag, :to => :parent
20
+
21
+ def initialize parent
22
+ @parent = parent
23
+ @pane_handles = []
24
+ @pane_contents = []
25
+ end
26
+
27
+ def pane title, options = {}, &block
28
+ css_class = options[:active] ? 'active' : ''
29
+ link = content_tag(:a, title, :'data-toggle' => 'tab', :href => "##{title.parameterize}_tab")
30
+ @pane_handles << [link, css_class]
31
+ @pane_contents << [css_class, title, capture(&block)]
32
+ nil
33
+ end
34
+
35
+ def pane_handles_html
36
+ pane_handles.first[1] = 'active' unless pane_handles.detect {|ph| ph[1] == 'active'}
37
+ pane_handles.map do |link, css_class|
38
+ content_tag(:li, link, :class => css_class)
39
+ end.join("\n").html_safe
40
+ end
41
+
42
+ def pane_contents_html
43
+ pane_contents.first[0] = 'active' unless pane_contents.detect {|pc| pc[0] == 'active'}
44
+ pane_contents.map do |css_class, title, content|
45
+ content_tag :div, content, :class => "tab-pane #{css_class}", :id => "#{title.parameterize}_tab"
46
+ end.join("\n").html_safe
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
53
+
54
+ ActionView::Base.send :include, BootstrapComponentsHelpers::TabsHelper
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class AccordionHelperTest < ActionView::TestCase
4
+
5
+ include BootstrapComponentsHelpers::AccordionHelper
6
+
7
+ test "calling accordion method" do
8
+ accordion do |accordion|
9
+ accordion.pane 'My pane' do
10
+ 'inside the panee'
11
+ end
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,4 @@
1
+ require 'test/unit'
2
+ require 'action_view'
3
+ require 'action_view/template'
4
+ require 'bootstrap-components-helpers'
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class AccordionHelperTest < ActionView::TestCase
4
+
5
+ include BootstrapComponentsHelpers::TabsHelper
6
+
7
+ test "calling tabs method" do
8
+ tabs do |tabs|
9
+ tabs.pane 'My first pane' do
10
+ 'inside the pane'
11
+ end
12
+ end
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bootstrap-components-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ivan Schneider
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.0
30
+ description: View helpers that generate the proper markup for some Twitter Bootstrap
31
+ components
32
+ email: git@ivanschneider.fr
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - README.markdown
38
+ - lib/bootstrap-components-helpers/accordion_helper.rb
39
+ - lib/bootstrap-components-helpers/tabs_helper.rb
40
+ - lib/bootstrap-components-helpers.rb
41
+ - test/test_accordion_helper.rb
42
+ - test/test_helper.rb
43
+ - test/test_tabs_helper.rb
44
+ homepage: https://dbinsights.herokuapp.com
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.25
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: View helpers for Twitter Bootstrap components
68
+ test_files:
69
+ - test/test_accordion_helper.rb
70
+ - test/test_helper.rb
71
+ - test/test_tabs_helper.rb