Class: BootstrapComponentsHelpers::TabsHelper::TabsBuilder

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::OutputSafetyHelper
Defined in:
lib/bootstrap-components-helpers/tabs_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ TabsBuilder

Returns a new instance of TabsBuilder.



22
23
24
25
26
# File 'lib/bootstrap-components-helpers/tabs_helper.rb', line 22

def initialize parent
  @parent = parent
  @pane_handles = []
  @pane_contents = []
end

Instance Attribute Details

#pane_contentsObject (readonly)

Returns the value of attribute pane_contents.



19
20
21
# File 'lib/bootstrap-components-helpers/tabs_helper.rb', line 19

def pane_contents
  @pane_contents
end

#pane_handlesObject (readonly)

Returns the value of attribute pane_handles.



19
20
21
# File 'lib/bootstrap-components-helpers/tabs_helper.rb', line 19

def pane_handles
  @pane_handles
end

#parentObject (readonly)

Returns the value of attribute parent.



19
20
21
# File 'lib/bootstrap-components-helpers/tabs_helper.rb', line 19

def parent
  @parent
end

Instance Method Details

#pane(title, options = {}, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bootstrap-components-helpers/tabs_helper.rb', line 28

def pane title, options = {}, &block
  css_class = options[:active] ? 'active' : ''
  tab_id = options[:id] || default_tab_id(title)
  link = (:a, 'data-toggle': 'tab', href: "##{tab_id}") do
    if options[:icon]
      title = raw((:i, '', class: options[:icon].to_s) + ' ' + title)
    end
    title
  end
  content = capture(&block)
  if content.present?
    @pane_handles << [link, css_class]
    @pane_contents << [[css_class, options[:class]], title, tab_id, content]
  end
  nil
end

#pane_contents_htmlObject



53
54
55
56
57
58
59
60
61
# File 'lib/bootstrap-components-helpers/tabs_helper.rb', line 53

def pane_contents_html
  return if pane_contents.empty?
  pane_contents.first[0] << 'active' unless pane_contents.detect {|pc| pc[0].include? 'active'}
  pane_contents.map do |css_class, title, tab_id, content|
    css_class << 'in' if css_class.include?('active') && css_class.include?('fade')
    css_class = css_class.join(' ').strip
     :div, content, class: "tab-pane #{css_class}", id: tab_id.to_s
  end.join("\n").html_safe
end

#pane_handles_htmlObject



45
46
47
48
49
50
51
# File 'lib/bootstrap-components-helpers/tabs_helper.rb', line 45

def pane_handles_html
  return if pane_handles.empty?
  pane_handles.first[1] = 'active' unless pane_handles.detect {|ph| ph[1] == 'active'}
  pane_handles.map do |link, css_class|
    (:li, link, class: css_class)
  end.join("\n").html_safe
end