bootstrap3_helper 1.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,66 +1,60 @@
1
- # @description
2
- # - Root Object
3
- #
4
- module Bootstrap3Helper
1
+ module Bootstrap3Helper # :nodoc:
5
2
  class Tabs
6
- # @description
7
- # - Used to rapidly generated Bootstrap Tabs Content Components.
3
+ # Used to rapidly generated Bootstrap Tabs Content Components.
4
+ #
8
5
  #
9
6
  class Content < Component
10
- # @description
11
- # - Creates a new Tabs::Menu object.
7
+ # Creates a new Tabs::Menu object.
12
8
  #
13
- # @param [Class] template - Template in which your are binding too.
9
+ # @param [ActionView] template Template in which your are binding too.
14
10
  # @param [Hash] args
15
- # <code>
16
- # args = {
17
- # id: [String|nilClass] - The ID, if you want one, for the parent container
18
- # class: [String|nilClass] - Custom class for the parent container
19
- # }
20
- # </code>
11
+ # @option args [String] :id The ID, if you want one, for the parent container.
12
+ # @option args [String] :class Custom class for the parent container.
13
+ # @option args [Hash] :data Any data attributes you want on the parent element.
21
14
  #
22
- def initialize(template, args = {})
15
+ def initialize(template, args = {}, &block)
23
16
  super(template)
24
17
 
25
- @id = args.fetch(:id, nil)
26
- @class = args.fetch(:class, '')
27
- @items = []
18
+ @id = args.fetch(:id, nil)
19
+ @class = args.fetch(:class, '')
20
+ @data = args.fetch(:data, {})
21
+ @content = block || proc { '' }
28
22
  end
29
23
 
30
- # @description
31
- # - Adds a new tabe-pane item to the object.
24
+ # Adds a new tabe pane item to the object.
32
25
  #
33
26
  # @param [String|Symbol] name - Used to link to the nav menu item.
34
27
  # @param [Hash] args
35
- # <code>
36
- # args = {
37
- # class: [String|nilClass] - Custom class for the div element
38
- # data: [Hash] - Any data attributes you wish to assign to div element.
39
- # }
40
- # </code>
28
+ # @option args [String] :class Custom class for the pane.
29
+ # @option args [Hash] :data Any data attributes you want on the pane element.
30
+ # @yieldreturn [String]
41
31
  #
42
- def item(name, args = {})
43
- data = args.fetch(:data, nil)
44
- klass = args.fetch(:class, '')
32
+ def pane(name, args = {})
33
+ data = args.fetch(:data, nil)
34
+ klass = args.fetch(:class, '')
35
+ active = klass.include? 'active'
45
36
 
46
- content = content_tag :div, id: name, class: 'tab-pane ' + klass, data: data, role: 'tabpanel' do
37
+ content_tag(
38
+ :div,
39
+ id: name,
40
+ class: "tab-pane fade #{active ? 'in' : ''} #{klass}",
41
+ aria: { hidden: active },
42
+ data: data,
43
+ role: 'tabpanel',
44
+ tabindex: -1
45
+ ) do
47
46
  yield if block_given?
48
47
  end
49
-
50
- @items.push(content)
51
48
  end
52
49
 
53
- # @description
54
- # - Used to render out the object as HTML
50
+ # Used to render out the object as HTML
55
51
  #
56
52
  # @return [String]
57
53
  #
58
54
  def to_s
59
- html = content_tag :div, id: @id, class: 'tab-content' + @class do
60
- @items.collect(&:to_s).join.html_safe
55
+ content_tag :div, id: @id, class: 'tab-content' + @class do
56
+ @content.call(self)
61
57
  end
62
-
63
- html
64
58
  end
65
59
  end
66
60
  end
@@ -1,76 +1,84 @@
1
- # @description
2
- # - Root Object
3
- #
4
- module Bootstrap3Helper
1
+ module Bootstrap3Helper # :nodoc:
5
2
  class Tabs
6
- # @description
7
- # - Used to rapidly build dropdown menus for Bootstrap tabs.
3
+ # Used to rapidly build dropdown menus for Bootstrap tabs.
8
4
  #
9
- # <code>
10
- # <% menu.dropdown 'Testing Dropdown' do |dropdown| %>
11
- # <%= dropdown.item(:testing5 ) { 'Testing 5' } %>
12
- # <%= dropdown.item(:testing6 ) { 'Testing 6' } %>
13
- # <%= dropdown.item(:testing7 ) { 'Testing 7' } %>
14
- # <% end %>
15
- # </code>
5
+ # @example Rendering a Dropdown menu component:
6
+ # <code>
7
+ # <%= menu.dropdown 'Testing Dropdown' do |dropdown| %>
8
+ # <%= dropdown.item(:testing5 ) { 'Testing 5' } %>
9
+ # <%= dropdown.item(:testing6 ) { 'Testing 6' } %>
10
+ # <%= dropdown.item(:testing7 ) { 'Testing 7' } %>
11
+ # <% end %>
12
+ # </code>
16
13
  #
17
14
  class Dropdown < Component
18
- # @description
19
- # - Creates a new Tabs::Dropdown object.
15
+ # Creates a new Tabs::Dropdown object.
20
16
  #
21
- # @param [Class] template - Template in which your are binding too.
17
+ # @param [ActionView] template Template in which your are binding too.
18
+ # @param [String] name
22
19
  #
23
- def initialize(template, name = '')
20
+ def initialize(template, name = '', &block)
24
21
  super(template)
25
22
 
26
- @name = name
27
- @items = []
23
+ @name = name
24
+ @content = block || proc { '' }
28
25
  end
29
26
 
30
- # @description
31
- # - Adds a new item to the dropdown object.
27
+ # rubocop:disable Metrics/MethodLength
28
+
29
+ # Adds a new item to the dropdown object.
32
30
  #
33
- # @note
34
- # - You can opt out of passing in a block and the li will use the name attribute
35
- # for the menu item.
31
+ # @note You can opt out of passing in a block and the li will use the name attribute
32
+ # for the menu item.
36
33
  #
37
34
  # @param [String|Symbol] name - Used to link nav li to tab-content
38
35
  # @param [Hash] args
39
- # <code>
40
- # args = {
41
- # id: [String|Symbol] - Custom ID for li element.
42
- # class: [String|nilClass] - Custom class for the li element
43
- # data: [Hash] - Any data attributes you wish to assign to li element.
44
- # }
45
- # </code>
36
+ # @option args [String] :id The ID, if you want one, for the li element.
37
+ # @option args [String] :class Custom class for the li element.
38
+ # @option args [Hash] :data Any data attributes you want on the li element.
39
+ # @yieldreturn [String]
46
40
  #
47
41
  def item(name, args = {})
48
- id = args.fetch(:id, nil)
49
- klass = args.fetch(:class, '')
50
- data = args.fetch(:data, nil)
42
+ id = args.fetch(:id, nil)
43
+ klass = args.fetch(:class, '')
44
+ data = args.fetch(:data, nil)
45
+ active = klass.include? 'active'
51
46
 
52
- li = content_tag :li, id: id, class: klass, data: data do
53
- content_tag :a, href: "##{name}", role: 'tab', data: { toggle: 'tab' }, aria: { controls: name, expanded: false } do
47
+ content_tag(
48
+ :li,
49
+ id: id,
50
+ class: klass,
51
+ data: data
52
+ ) do
53
+ content_tag(
54
+ :a,
55
+ href: "##{name}",
56
+ role: 'tab',
57
+ data: { toggle: 'tab' },
58
+ aria: { controls: name, expanded: active }
59
+ ) do
54
60
  block_given? ? yield : name.to_s.titleize
55
61
  end
56
62
  end
57
-
58
- @items.push(li)
59
63
  end
64
+ # rubocop:enable Metrics/MethodLength
60
65
 
61
- # @description
62
- # - Used to render out the object and get the HTML representation.
66
+ # Used to render out the object and get the HTML representation.
63
67
  #
64
68
  # @return [String]
65
69
  #
66
70
  def to_s
67
- content = content_tag :a, @name, href: '#', class: 'dropdown-toggle', data: { toggle: 'dropdown' }, aria: { expanded: false }
71
+ content = content_tag(
72
+ :a,
73
+ @name,
74
+ href: '#',
75
+ class: 'dropdown-toggle',
76
+ data: { toggle: 'dropdown' }, aria: { expanded: false }
77
+ )
68
78
 
69
- content += content_tag :ul, id: @id, class: 'dropdown-menu ' do
70
- @items.collect(&:to_s).join.html_safe
79
+ content + content_tag(:ul, id: @id, class: 'dropdown-menu ') do
80
+ @content.call(self)
71
81
  end
72
-
73
- content
74
82
  end
75
83
  end
76
84
  end
@@ -1,100 +1,99 @@
1
- # @description
2
- # - Root Object
3
- #
4
- module Bootstrap3Helper
1
+ module Bootstrap3Helper # :nodoc:
5
2
  class Tabs
6
- # @description
7
- # - Used to rapidly generated Bootstrap Tabs Menu Components.
3
+ # Used to rapidly generated Bootstrap Tabs Menu Components.
8
4
  #
9
- # <code>
10
- # <% menu.item(:testing3) { ' Testing 3' } %>
11
- # </code>
5
+ # @example Rendering out a Tabs::Menu component:
6
+ # <code>
7
+ # <% menu.item(:testing3) { ' Testing 3' } %>
8
+ # </code>
12
9
  #
13
10
  class Menu < Component
14
- # @description
15
- # - Creates a new Tabs::Menu object.
11
+ # Creates a new Tabs::Menu object.
16
12
  #
17
- # @param [Class] template - Template in which your are binding too.
13
+ # @param [ActionView] template - Template in which your are binding too.
18
14
  # @param [Hash] args
19
- # <code>
20
- # args = {
21
- # type: [String|Symbol] - Used to tell the helper which tab version - :tabs|:pills
22
- # id: [String|nilClass] - The ID, if you want one, for the parent container
23
- # class: [String|nilClass] - Custom class for the parent container
24
- # }
25
- # </code>
15
+ # @option args [Symbol] type Used to tell the helper which tab version :tabs|:pills
16
+ # @option args [String] id The ID, if you want one, for the parent container
17
+ # @option args [String] class Custom class for the parent container
26
18
  #
27
- def initialize(template, args = {})
19
+ def initialize(template, args = {}, &block)
28
20
  super(template)
29
21
 
30
- @id = args.fetch(:id, nil)
31
- @class = args.fetch(:class, '')
32
- @type = args.fetch(:type, :tabs)
33
- @items = []
22
+ @id = args.fetch(:id, nil)
23
+ @class = args.fetch(:class, '')
24
+ @type = args.fetch(:type, :tabs)
25
+ @content = block || proc { '' }
34
26
  end
35
27
 
36
- # @description
37
- # - Adds a new menu item to the object.
28
+ # rubocop:disable Metrics/MethodLength
29
+
30
+ # Adds a new menu item to the object.
38
31
  #
39
- # @note
40
- # - You can opt out of passing in a block and the li will use the name attribute
41
- # for the menu item.
32
+ # @note You can opt out of passing in a block and the li will use
33
+ # the name attribute for the menu item.
42
34
  #
43
- # @param [String|Symbol] name - Used to link nav li to tab-content
35
+ # @param [String|Symbol] name Used to link nav li to tab-content
44
36
  # @param [Hash] args
45
- # <code>
46
- # args = {
47
- # id: [String|Symbol] - Custom ID for li element.
48
- # class: [String|nilClass] - Custom class for the li element
49
- # data: [Hash] - Any data attributes you wish to assign to li element.
50
- # }
51
- # </code>
37
+ # @option args [String] :id
38
+ # @option args [String] :class
39
+ # @option args [Hash] :data
40
+ # @yieldreturn [String]
52
41
  #
53
42
  def item(name, args = {})
54
- id = args.fetch(:id, nil)
55
- data = args.fetch(:data, nil)
56
- klass = args.fetch(:class, '')
43
+ id = args.fetch(:id, nil)
44
+ data = args.fetch(:data, nil)
45
+ klass = args.fetch(:class, '')
46
+ active = klass.include? 'active'
57
47
 
58
- li = content_tag :li, id: id, class: klass, data: data, role: 'presentation' do
59
- content_tag :a, href: "##{name}", data: { toggle: 'tab' }, aria: { controls: '' } do
48
+ content_tag(
49
+ :li,
50
+ id: id,
51
+ class: klass,
52
+ data: data,
53
+ role: 'presentation'
54
+ ) do
55
+ content_tag(
56
+ :a,
57
+ href: "##{name}",
58
+ role: 'tab',
59
+ tabindex: -1,
60
+ data: { toggle: 'tab' },
61
+ aria: { controls: "##{name}", expanded: active, selected: active }
62
+ ) do
60
63
  block_given? ? yield : name.to_s.titleize
61
64
  end
62
65
  end
63
-
64
- @items.push(li)
65
66
  end
67
+ # rubocop:enable Metrics/MethodLength
66
68
 
67
- # @description
68
- # - Used to create menu items that are Dropdowns
69
+ # Used to create menu items that are Dropdowns
69
70
  #
70
- # @param [String|Symbol] name -
71
+ # @param [String|Symbol] name
72
+ # @param [Hash] args
73
+ # @option args [String] :id
74
+ # @option args [String] :class
75
+ # @option args [Hash] :data
76
+ # @yieldparam dropdown [Tabs::Dropdown]
71
77
  #
72
- def dropdown(name, args = {})
73
- id = args.fetch(:id, nil)
74
- data = args.fetch(:data, nil)
75
- klass = args.fetch(:class, '')
76
-
77
- dropdown = Tabs::Dropdown.new(@template, name)
78
- yield dropdown if block_given?
78
+ def dropdown(name, args = {}, &block)
79
+ id = args.fetch(:id, nil)
80
+ data = args.fetch(:data, nil)
81
+ klass = args.fetch(:class, '')
82
+ dropdown = Tabs::Dropdown.new(@template, name, &block)
79
83
 
80
- content = content_tag :li, id: id, class: 'dropdown' + klass, data: data do
81
- dropdown.to_s
84
+ content_tag :li, id: id, class: 'dropdown' + klass, data: data do
85
+ dropdown.to_s.html_safe
82
86
  end
83
-
84
- @items.push(content)
85
87
  end
86
88
 
87
- # @description
88
- # - Used to render out the contents of the menu.
89
+ # Used to render out the contents of the menu.
89
90
  #
90
91
  # @return [String]
91
92
  #
92
93
  def to_s
93
- html = content_tag :ul, id: @id, class: "nav nav-#{@type} " + @class, role: 'tablist' do
94
- @items.collect(&:to_s).join.html_safe
94
+ content_tag :ul, id: @id, class: "nav nav-#{@type} " + @class, role: 'tablist' do
95
+ @content.call(self)
95
96
  end
96
-
97
- html
98
97
  end
99
98
  end
100
99
  end
@@ -1,103 +1,89 @@
1
- # @root
2
- #
3
- #
4
- module Bootstrap3Helper
5
- # @description
6
- # - Used to rapidly generated Bootstrap Tabs Components.
1
+ module Bootstrap3Helper # :nodoc:
2
+ # Used to rapidly generated Bootstrap Tabs Components.
7
3
  #
8
- # @note
9
- # - On menu items - you can pass in either symbol or string for the link. If
10
- # you pass in a block, it will use the block for the title of the li. If no
11
- # block is present, then it will titleize the symbol or string.
4
+ # @note On menu items - you can pass in either symbol or string for the link. If
5
+ # you pass in a block, it will use the block for the title of the li. If no
6
+ # block is present, then it will titleize the symbol or string.
12
7
  #
13
- # Tabs::Menu will respond to <code>item</code> and <code>dropdown</code>
14
- # Each method will yield the corresponding component, either a Tabs::Menu
15
- # or a Tabs::Dropdown.
8
+ # Tabs::Menu will respond to <code>item</code> and <code>dropdown</code>
9
+ # Each method will yield the corresponding component, either a Tabs::Menu
10
+ # or a Tabs::Dropdown.
16
11
  #
17
- # <code>
18
- # <%= tabs_helper type: :pills do |menu, content| %>
19
- # <% menu.item(:testing1, class: 'active') { ' Testing 1' } %>
20
- # <% menu.item :testing2 %>
21
- # <% menu.item(:testing3) { ' Testing 3' } %>
22
- # <% menu.dropdown 'Testing Dropdown' do |dropdown| %>
23
- # <%= dropdown.item(:testing5 ) { 'Testing 5' } %>
24
- # <%= dropdown.item(:testing6 ) { 'Testing 6' } %>
25
- # <%= dropdown.item(:testing7 ) { 'Testing 7' } %>
26
- # <% end %>
12
+ # @example Rendering out a Bootstrap Tab components in a view:
13
+ # <%= tabs_helper type: :pills do |menu, content| %>
14
+ # <%= menu.item(:testing1, class: 'active') { ' Testing 1' } %>
15
+ # <%= menu.item :testing2 %>
16
+ # <%= menu.item(:testing3) { ' Testing 3' } %>
17
+ # <%= menu.dropdown 'Testing Dropdown' do |dropdown| %>
18
+ # <%= dropdown.item(:testing5 ) { 'Testing 5' } %>
19
+ # <%= dropdown.item(:testing6 ) { 'Testing 6' } %>
20
+ # <%= dropdown.item(:testing7 ) { 'Testing 7' } %>
21
+ # <% end %>
22
+ #
23
+ # <%= content.item :testing1, class: 'active' do %>
24
+ # Testing 1 content
25
+ # <% end %>
26
+ # <%= content.item :testing2 do %>
27
+ # Testing 2 content
28
+ # <% end %>
29
+ # <%= content.item :testing3 do %>
30
+ # Testing 3 content
31
+ # <% end %>
32
+ # <%= content.item :testing5 do %>
33
+ # Testing 5 content
34
+ # <% end %>
35
+ # <%= content.item :testing6 do %>
36
+ # Testing 6 content
37
+ # <% end %>
38
+ # <%= content.item :testing7 do %>
39
+ # Testing 7 content
40
+ # <% end %>
41
+ # <% end %>
27
42
  #
28
- # <% content.item :testing1, class: 'active' do %>
29
- # Testing 1 content
30
- # <% end %>
31
- # <% content.item :testing2 do %>
32
- # Testing 2 content
33
- # <% end %>
34
- # <% content.item :testing3 do %>
35
- # Testing 3 content
36
- # <% end %>
37
- # <% content.item :testing5 do %>
38
- # Testing 5 content
39
- # <% end %>
40
- # <% content.item :testing6 do %>
41
- # Testing 6 content
42
- # <% end %>
43
- # <% content.item :testing7 do %>
44
- # Testing 7 content
45
- # <% end %>
46
- # <% end %>
47
- # </code>
48
43
  class Tabs < Component
49
- # @description
50
- # - Creates a new Tabs object.
44
+ # Creates a new Tabs object.
51
45
  #
52
- # @param [Class] template - Template in which your are binding too.
46
+ # @param [ActionView] template - Template in which your are binding too.
53
47
  # @param [Hash] args
54
- # <code>
55
- # args = {
56
- # type: [String|Symbol] - Used to tell the helper which tab version - :tabs|:pills
57
- # id: [String|nilClass] - The ID, if you want one, for the parent container
58
- # class: [String|nilClass] - Custom class for the parent container
59
- # }
60
- # </code>
48
+ # @option args [String|Symbol] :type Used to tell the helper which tab version - :tabs|:pills
49
+ # @option args [String] :id The ID, if you want one, for the parent container
50
+ # @option args [String] :class Custom class for the parent container.
61
51
  #
62
- def initialize(template, args = {})
52
+ def initialize(template, args = {}, &block)
63
53
  super(template)
64
- @type = args.fetch(:type, :tabs)
65
- @id = args.fetch(:id, nil)
66
- @class = args.fetch(:class, '')
67
54
 
68
- @tab_menu = Tabs::Menu.new(@template, type: @type)
69
- @tab_content = Tabs::Content.new(@template)
55
+ @type = args.fetch(:type, :tabs)
56
+ @id = args.fetch(:id, nil)
57
+ @class = args.fetch(:class, '')
58
+ @content = block || proc { '' }
70
59
  end
71
60
 
72
- # @description
73
- # - Allows you access the Tabs::Menu object.
61
+ # Allows you access the Tabs::Menu object.
74
62
  #
63
+ # @see Tabs::Menu
75
64
  # @return [Tabs::Menu]
76
65
  #
77
- def menu
78
- @tab_menu
66
+ def menu(args = {}, &block)
67
+ Tabs::Menu.new(@template, args.merge(type: @type), &block)
79
68
  end
80
69
 
81
- # @description
82
- # - Allows you to access the Tabs::Content object
70
+ # Allows you to access the Tabs::Content object
83
71
  #
72
+ # @see Tabs::Content
84
73
  # @return [Tabs::Content]
85
74
  #
86
- def content
87
- @tab_content
75
+ def content(args = {}, &block)
76
+ Tabs::Content.new(@template, args, &block)
88
77
  end
89
78
 
90
- # @description
91
- # - Used to render out the HTML of the Tabs Object
79
+ # Used to render out the HTML of the Tabs Object
92
80
  #
93
81
  # @return [String]
94
82
  #
95
83
  def to_s
96
- html = content_tag :div, id: @id, class: @class do
97
- menu.to_s + content.to_s
84
+ content_tag :div, id: @id, class: @class do
85
+ @content.call(self)
98
86
  end
99
-
100
- html
101
87
  end
102
88
  end
103
89
  end
@@ -1,3 +1,3 @@
1
1
  module Bootstrap3Helper
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '3.0.0'.freeze
3
3
  end