bootstrap3_helper 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7690ea3af6632902048f7f389e97a9c2317e007c6dab849b754fabe805a7357e
4
+ data.tar.gz: 2c8234630ce566f379f1d1a46d9ab7e47e958c3b1059df4aa41a0003a27700bc
5
+ SHA512:
6
+ metadata.gz: d54b3740ae4f7bd45adcea82506ebe65fae16dcca0837c42e83ada33480c67053acaee9112ffe7029283d2d4babe71d0cfbe9183e97b8d30e886527fd2cf70e5
7
+ data.tar.gz: 5e42f3ff987c0e32aa5f2b5dd94c744abc7d4afbadeb41ee17da5b1cfd64b5975a123aa4d3d135e9dd2871fa9abd1ee3ec7d6862cd41d53a8bc41ece2e9a6ea8
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Robert David
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,149 @@
1
+ # Bootstrap3Helper
2
+
3
+ This gem was designed to help you rapidly build common Bootstrap 3 Components. They where designed to very flexible and reduce a lot of boiler plate HTML.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bootstrap3_helper'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bootstrap3_helper
20
+
21
+ ## Usage
22
+
23
+ `Panel Helper`
24
+
25
+ ```erb
26
+ <%= panel_helper :primary do |p| %>
27
+ <%= p.header id: 'optional-id', class: 'any-optional-extra-classes' do %>
28
+ Some Header for your panel
29
+ <% end %>
30
+ <%= p.body id: 'optional-id', class: 'any-optional-extra-classes' do %>
31
+ All your custom HTML or Ruby. You can render blocks here, whatever
32
+ <% end%>
33
+ <% p.footer id: 'optional-id', class: 'any-optional-extra-classes' do %>
34
+ You don't need to have a footer but if you do, you can put your
35
+ HTML and Ruby in here as well.
36
+ <% end %>
37
+ end
38
+ ```
39
+
40
+ ---
41
+
42
+ `Alert Helper`
43
+
44
+ ```erb
45
+ <%= alert_helper :success { 'Some success message' } %>
46
+
47
+ <!-- or -->
48
+
49
+ <%= alert_helper :danger, dismissable: true do %>
50
+ HTML and/or Ruby
51
+ <% end %>
52
+ ```
53
+
54
+ ---
55
+
56
+ `Accordion Helper`
57
+
58
+ ```erb
59
+ <%= accordion_helper :primary, { class: 'optional-class', id: 'optional_id', collapse_id: 'optional_collapse_id', expanded: true } do |accordion| %>
60
+ <%= accordion.header do %>
61
+ <span class="something">This is the heading....</span>
62
+ <% end %>
63
+ <%= accordion.body do %>
64
+ <p>This is the body of the accordion....</p>
65
+ <% end %>
66
+ <% end %>
67
+ ```
68
+
69
+ **Note:** the accordion helper handles all the attributes and data attributes needed to sync up the javascript in order to give the component its functionality. You just worry about about the class or state of the component. The helper does the rest. But if you do want to control the collapse ID and syncing, `collapse_id` is the attribute you are looking for.
70
+
71
+ ---
72
+
73
+ `AccordionGroup Helper`
74
+
75
+ ```erb
76
+ <%= accordion_group_helper do |g| %>
77
+ <% g.accordion :primary do |accordion| %>
78
+ <%= accordion.header { "Accordion 1" } %>
79
+ <%= accordion.body do %>
80
+ <p>This is accordion 1</p>
81
+ <% end %>
82
+ <%end %>
83
+ <% g.accordion :info do |accordion| %>
84
+ <%= accordion.header { "Accordion 2" } %>
85
+ <%= accordion.body do %>
86
+ <p>This is accordion 2</p>
87
+ <% end %>
88
+ <% end %>
89
+ <% g.accordion :danger do |accordion| %>
90
+ <%= accordion.header { "Accordion 3" } %>
91
+ <%= accordion.body do %>
92
+ <p>This is accordion 3</p>
93
+ <% end %>
94
+ <%end %>
95
+ <% end %>
96
+ ```
97
+
98
+ **Note:** the accordion_group helper handles all the attributes and data attributes needed to sync up the javascript in order to give the component its functionality. You just worry about about the class or state of the individual accordions. The helper does the rest.
99
+
100
+ ---
101
+
102
+ `Panel Helper`
103
+
104
+ ```erb
105
+ <%= tabs_helper type: :pills do |menu, content| %>
106
+ <% menu.item(:testing1, class: 'active') { ' Testing 1' } %>
107
+ <% menu.item :testing2 %>
108
+ <% menu.item(:testing3) { ' Testing 3' } %>
109
+ <% menu.dropdown 'Testing Dropdown' do |dropdown| %>
110
+ <%= dropdown.item(:testing5 ) { 'Testing 5' } %>
111
+ <%= dropdown.item(:testing6 ) { 'Testing 6' } %>
112
+ <%= dropdown.item(:testing7 ) { 'Testing 7' } %>
113
+ <% end %>
114
+
115
+
116
+ <% content.item :testing1, class: 'active' do %>
117
+ Testing 1 content
118
+ <% end %>
119
+ <% content.item :testing2 do %>
120
+ Testing 2 content
121
+ <% end %>
122
+ <% content.item :testing3 do %>
123
+ Testing 3 content
124
+ <% end %>
125
+ <% content.item :testing5 do %>
126
+ Testing 5 content
127
+ <% end %>
128
+ <% content.item :testing6 do %>
129
+ Testing 6 content
130
+ <% end %>
131
+ <% content.item :testing7 do %>
132
+ Testing 7 content
133
+ <% end %>
134
+ <% end %>
135
+ ```
136
+
137
+ ---
138
+
139
+ ## Development
140
+
141
+ To install this gem onto your local machine, run `bundle exec rake install`.
142
+
143
+ ## Contributing
144
+
145
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rdavid369/bootstrap3-helper.
146
+
147
+ ## License
148
+
149
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Bootstrap3Helper'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,174 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap3Helper
5
+ # @description
6
+ # - Used to generate Bootstrap Accordion objects.
7
+ #
8
+ # <code>
9
+ # <%= accordion_helper :primary do |accordion| %>
10
+ # <%= accordion.header do %>
11
+ # <span class="something">This is the heading....</span>
12
+ # <% end %>
13
+ # <%= accordion.body do %>
14
+ # <p>This is the body of the accordion....</p>
15
+ # <% end %>
16
+ # <% end %>
17
+ # </code>
18
+ #
19
+ class Accordion < Component
20
+ # @description
21
+ # - Initlize a new accordion object. If this part of a parent element, i.e
22
+ # AccordionGroup, we need to keep track of the parent element id, so we can
23
+ # pass it down to the other components.
24
+ #
25
+ # @param [Class] template - Template in which your are binding too.
26
+ # @param [NilClass|String|Symbol|Hash] - Bootstrap class context, or options hash.
27
+ # @param [Hash] opts
28
+ # <code>
29
+ # args = {
30
+ # parent_id: [String]
31
+ # id: [String]
32
+ # class: [String]
33
+ # collapse_id: [String]
34
+ # expanded: Boolean
35
+ # }
36
+ # </code>
37
+ # @return [Accordion]
38
+ #
39
+ def initialize(template, context_or_options = nil, opts = {})
40
+ super(template)
41
+ @context, args = parse_arguments(context_or_options, opts)
42
+
43
+ @parent_id = args.fetch(:parent_id, nil)
44
+ @id = args.fetch(:id, nil)
45
+ @class = args.fetch(:class, '')
46
+ @collapse_id = args.fetch(:collapse_id, uuid)
47
+ @expanded = args.fetch(:expanded, false)
48
+ end
49
+
50
+ # @description
51
+ # - Creates the header element for the accordion
52
+ #
53
+ # @note
54
+ # - NilClass :to_s returns an empty String
55
+ #
56
+ # @params [Hash] args
57
+ # <code>
58
+ # args = {
59
+ # id: [String]
60
+ # class: [String]
61
+ # data: [Hash]
62
+ # }
63
+ # </code>
64
+ #
65
+ # @yields [Accordion] self
66
+ # @return [NilClass]
67
+ #
68
+ # rubocop:disable Metrics/MethodLength
69
+ def header(args = {})
70
+ id = args.fetch(:id, nil)
71
+ klass = args.fetch(:class, '')
72
+ data = args.fetch(:data, {})
73
+
74
+ data[:toggle] = 'collapse'
75
+ data[:parent] = "##{@parent_id}"
76
+
77
+ @header = content_tag :div, id: id, class: 'panel-heading ' + klass do
78
+ content_tag :h3, class: 'panel-title' do
79
+ content_tag :a, href: "##{@collapse_id}", data: data do
80
+ content = yield if block_given?
81
+ content.to_s.html_safe
82
+ end
83
+ end
84
+ end
85
+ end
86
+ # rubocop:enable Metrics/MethodLength
87
+
88
+ # @description
89
+ # - Creates the body element for the accordion.
90
+ #
91
+ # @note
92
+ # - NilClass :to_s returns an empty String
93
+ #
94
+ # @params [Hash] args
95
+ # <code>
96
+ # args = {
97
+ # id: [String]
98
+ # class: [String]
99
+ # data: [Hash]
100
+ # }
101
+ # </code>
102
+ #
103
+ # @yields [Accordion] self
104
+ # @return [nilClass]
105
+ #
106
+ #
107
+ def body(args = {})
108
+ klass = 'panel-collapse collapse '
109
+ data = args.fetch(:data, {})
110
+ klass += args.fetch(:class, '')
111
+ klass += ' in' if @expanded
112
+
113
+ @body = content_tag :div, id: @collapse_id, class: klass do
114
+ content_tag :div, class: 'panel-body', data: data do
115
+ content = yield if block_given?
116
+ content.to_s.html_safe
117
+ end
118
+ end
119
+ end
120
+
121
+ # @description
122
+ # - Creates the footer element for the accordion
123
+ #
124
+ # @params [Hash] args
125
+ # <code>
126
+ # args = {
127
+ # id: [String]
128
+ # class: [String]
129
+ # data: [Hash]
130
+ # }
131
+ # </code>
132
+ #
133
+ # @yields [Accordion] self
134
+ # @return [nilClass]
135
+ #
136
+ #
137
+ def footer(args = {})
138
+ id = args.fetch(:id, nil)
139
+ klass = args.fetch(:class, '')
140
+ data = args.fetch(:data, {})
141
+
142
+ @footer = content_tag :div, id: id, class: 'panel-footer ' + klass, data: data do
143
+ content = yield if block_given?
144
+ content.to_s.html_safe
145
+ end
146
+ end
147
+
148
+ # @description
149
+ # - The to string method here is what is responsible for rendering out the
150
+ # accordion element. As long as the main method is rendered out in the
151
+ # helper, you will get the entire accordion.
152
+ #
153
+ # @return [String]
154
+ #
155
+ def to_s
156
+ content = content_tag :div, id: @id, class: container_classes do
157
+ @header + @body + @footer
158
+ end
159
+
160
+ content
161
+ end
162
+
163
+ private
164
+
165
+ # @description
166
+ # - Used to get the container element classes
167
+ #
168
+ # @return [String]
169
+ #
170
+ def container_classes
171
+ "panel panel-#{@context} #{@class}"
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,88 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap3Helper
5
+ # @description
6
+ # - This class is used to general groups of accordions.
7
+ #
8
+ # <code>
9
+ # <%= accordion_group_helper do |group| %>
10
+ # <%= group.accordion :primary do |accordion| %>
11
+ # <%= accordion.header { "accordion 1" } %>
12
+ # <%= accordion.body do %>
13
+ # <p>This is accordion 1</p>
14
+ # <% end %>
15
+ # <% end %>
16
+ # <%= group.accordion :info do |accordion| %>
17
+ # <%= accordion.header { "accordion 2" } %>
18
+ # <%= accordion.body do %>
19
+ # <p>This is accordion 2</p>
20
+ # <% end %>
21
+ # <% end %>
22
+ # <%= group.accordion :danger do |accordion| %>
23
+ # <%= accordion.header { "accordion 3" } %>
24
+ # <%= accordion.body do %>
25
+ # <p>This is accordion 3</p>
26
+ # <% end %>
27
+ # <% end %>
28
+ # <% end %>
29
+ # </code>
30
+ #
31
+ class AccordionGroup < Component
32
+ # @description
33
+ # - Used to initialize the main object. This objects sole purpose is to
34
+ # generate a wrapper element with a distinct id and pass that id down to
35
+ # all the child elements.
36
+ #
37
+ def initialize(template, opts = {})
38
+ super(template)
39
+ @accordions = []
40
+ @id = opts.fetch(:id, uuid)
41
+ @class = opts.fetch(:class, '')
42
+ end
43
+
44
+ # @description
45
+ # - This method is the main method for generating individual accordions.
46
+ # This is where you would pass in the html attributes.
47
+ #
48
+ # @param [NilClass|String|Symbol|Hash] - Bootstrap class context, or options hash.
49
+ # @param [Hash] opts
50
+ # <code>
51
+ # args = {
52
+ # id: [String]
53
+ # class: [String]
54
+ # }
55
+ # </code>
56
+ #
57
+ # @yields [Accordion] accordion
58
+ # @return [nilClass]
59
+ #
60
+ def accordion(context_or_options = nil, opts = {})
61
+ if context_or_options.is_a?(Hash)
62
+ context_or_options[:parent_id] = @id
63
+ else
64
+ opts[:parent_id] = @id
65
+ end
66
+
67
+ accordion = Accordion.new(@template, context_or_options, opts)
68
+ yield accordion if block_given?
69
+
70
+ @accordions.push(accordion)
71
+ end
72
+
73
+ # @description
74
+ # - The to string method here is what is responsible for rendering out the
75
+ # entire accordion. As long as the main method is rendered out in the helper,
76
+ # you will get all the contents.
77
+ #
78
+ # @return [String]
79
+ #
80
+ def to_s
81
+ content = content_tag :div, id: @id, class: "panel-group #{@class}" do
82
+ @accordions.map(&:to_s).join.html_safe
83
+ end
84
+
85
+ content
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,88 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap3Helper
5
+ # @description
6
+ # - The Alert helper is meant to help you rapidly build Bootstrap Alert
7
+ # components quickly and easily. The dissmiss button is optional.
8
+ #
9
+ # <code>
10
+ # <%= alert_helper :warning, dismissable: true do %>
11
+ # <% if @model.errors.present? %>
12
+ # <p>Some kind of error</p>
13
+ # <% end %>
14
+ # <% end %>
15
+ #
16
+ # <%= alert_helper(:success, dismissible: true) { "Successful save"}
17
+ # </code>
18
+ #
19
+ class Alert < Component
20
+ # @description
21
+ # - Used to generate Bootstrap alert components quickly.
22
+ #
23
+ # @param [Class] template - Template in which your are binding too.
24
+ # @param [NilClass|String|Symbol|Hash] - Bootstrap class context, or options hash.
25
+ # @param [Hash] opts
26
+ # <code>
27
+ # opts = {
28
+ # id: [String] - ID of the alert box
29
+ # class: [String] - Additional classes for the alert box
30
+ # }
31
+ # </code>
32
+ # @param [Proc] &block
33
+ # @return [Alert]
34
+ #
35
+ def initialize(template, context_or_options = nil, opts = {}, &block)
36
+ super(template)
37
+ @context, args = parse_arguments(context_or_options, opts)
38
+
39
+ @id = args.fetch(:id, nil)
40
+ @class = args.fetch(:class, '')
41
+ @dismissible = args.fetch(:dismissible, false)
42
+ @content = block || proc { '' }
43
+ end
44
+
45
+ # @description
46
+ # - The dissmiss button, if the element has one.
47
+ #
48
+ # @return [String]
49
+ #
50
+ def close_button
51
+ content_tag(:button, class: 'close', data: { dismiss: 'alert' }, aria: { label: 'Close' }) do
52
+ content_tag(:span, aria: { hidden: true }) { '&times;'.html_safe }
53
+ end
54
+ end
55
+
56
+ # @description
57
+ # - Used to render out the Alert component.
58
+ #
59
+ # @note
60
+ # - concat needs to be used to add the content of the button to the output
61
+ # buffer. For some reason, if though the block returns a String, trying to
62
+ # add that string to the dismiss button string, returns with the dismiss
63
+ # button missing. Was forced to caoncat the button to the current output
64
+ # buffer in order to get it to persist after the block call.
65
+ #
66
+ # @return [String]
67
+ #
68
+ def to_s
69
+ content_tag :div, id: @id, class: container_class do
70
+ concat(@dismissible ? close_button : '') + @content.call
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ # @description
77
+ # - Used to get the container classes.
78
+ #
79
+ # @return [String]
80
+ #
81
+ def container_class
82
+ string = 'alert '
83
+ string += @context == 'default' ? 'alert-info' : "alert-#{@context}"
84
+ string += " #{@class}"
85
+ string
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,55 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap3Helper
5
+ # @description
6
+ # - Used to generate Bootstrap callout component quickly.
7
+ #
8
+ # @param [Class] template - Template in which your are binding too.
9
+ # @param [NilClass|String|Symbol|Hash] - Bootstrap class context, or options hash.
10
+ # @param [Hash] opts
11
+ # <code>
12
+ # opts = {
13
+ # id: [String] - ID of the alert box
14
+ # class: [String] - Additional classes for the alert box
15
+ # }
16
+ # </code>
17
+ # @param [Proc] &block
18
+ # @return [Callout]
19
+ #
20
+ class Callout < Component
21
+ def initialize(template, context_or_options = nil, opts = {}, &block)
22
+ super(template)
23
+ @context, args = parse_arguments(context_or_options, opts)
24
+
25
+ @id = args.fetch(:id, nil)
26
+ @class = args.fetch(:class, '')
27
+ @content = block || proc { '' }
28
+ end
29
+
30
+ # @description
31
+ # - Returns a string representation of the component.
32
+ #
33
+ # @return [String]
34
+ #
35
+ def to_s
36
+ content_tag :div, id: @id, class: container_class do
37
+ @content.call
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ # @description
44
+ # - Used to get the container classes.
45
+ #
46
+ # @return [String]
47
+ #
48
+ def container_class
49
+ string = 'callout '
50
+ string += "callout-#{@context}"
51
+ string += " #{@class}"
52
+ string
53
+ end
54
+ end
55
+ end