Class: BootstrapTabHelper::TabBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/bootstrap_tab_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, template, options = {}) ⇒ TabBuilder

Returns a new instance of TabBuilder.



10
11
12
13
# File 'app/helpers/bootstrap_tab_helper.rb', line 10

def initialize(index, template, options={})
  @default_index, @options, @template = index, options, template
  @tabs = {}
end

Instance Attribute Details

#default_indexObject

Returns the value of attribute default_index.



8
9
10
# File 'app/helpers/bootstrap_tab_helper.rb', line 8

def default_index
  @default_index
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'app/helpers/bootstrap_tab_helper.rb', line 8

def options
  @options
end

#tabsObject

Returns the value of attribute tabs.



8
9
10
# File 'app/helpers/bootstrap_tab_helper.rb', line 8

def tabs
  @tabs
end

#templateObject

Returns the value of attribute template.



8
9
10
# File 'app/helpers/bootstrap_tab_helper.rb', line 8

def template
  @template
end

Instance Method Details

#tab(label, options = {}, &proc) ⇒ Object

tab label and content of tabs

label - label of tab options -

disabled  - true, false default: false
clickable - true, false default: true
other options that can be accpted by li
pane_options -
  every option that can be accepted by div


24
25
26
27
28
# File 'app/helpers/bootstrap_tab_helper.rb', line 24

def tab(label, options={}, &proc)
  default_options = {disabled: false, clickable: true}
  pane_options = options.delete(:pane_options) || {}
  @tabs[label] = [options.reverse_merge!(default_options), pane_options, proc]
end

#to_sObject



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/bootstrap_tab_helper.rb', line 30

def to_s

  style = @options.delete(:style)
  inline = @options.delete(:inline)
  position = @options.delete(:position)
  #align = @options.delete(:align)
  stacked = @options.delete(:stacked)

  @options[:class] ||= ''
  @options[:class] << ' nav-stacked' if stacked
  #@options[:class] << " pull-#{align}" if align
  @options[:class] << ' nav nav-tabs' if style == 'tab'
  @options[:class] << ' nav nav-pills' if style == 'pill'

  wrap_options = {style: "display: #{inline ? 'inline-block' : 'block'};", class: " tabbable tabs-#{position}"}

  content_options = @options.delete(:content_options) || {}
  content_options[:class] ||= ''
  content_options[:class] << ' tab-content'

  @template.div(wrap_options) do
    tab_content = ''
    tab_content << @template.list(@options) do |li|
      @tabs.keys.each do |k|
        render_label(li, style,
                     k,
                     @tabs.keys.index(k) == @default_index.to_i,
                     "#{@options[:id]}-tab-#{@tabs.keys.index(k)}",
                     @tabs[k][0])
      end
    end

    tab_content << @template.div(content_options) do
      tab_panes = ''
      @tabs.values.each do |v|

        tab_panes << render_content(@tabs.values.index(v) == @default_index.to_i,
                                    "#{@options[:id]}-tab-#{@tabs.values.index(v)}",
                                    v[1],
                                    &v[2])

      end
      tab_panes.html_safe
    end

    tab_content.html_safe
  end

end