bootstrap4_helper 0.0.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,106 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap4Helper
5
+ # @description
6
+ # - Builds a Accordion out of the collapse component of Bootstrap 4.
7
+ #
8
+ class Accordion < Component
9
+ CARD_METHODS = %i[
10
+ title
11
+ text
12
+ image_overlay
13
+ ].freeze
14
+
15
+ # @description
16
+ # -
17
+ #
18
+ # @param [ActionView] template
19
+ # @param [Hash] opts
20
+ #
21
+ def initialize(template, opts = {}, &block)
22
+ super(template)
23
+
24
+ @id = opts.fetch(:id, uuid)
25
+ @class = opts.fetch(:class, '')
26
+ @data = opts.fetch(:data, {})
27
+ @parent = opts.fetch(:parent, nil)
28
+ @target = @data.fetch(:target, uuid)
29
+ @content = block || proc { '' }
30
+ @card = Card.new(@template)
31
+ end
32
+
33
+ # @description
34
+ # - Builds a header component for the accordion, which is actually the header
35
+ # of a Card.
36
+ #
37
+ # @param [Hash] opts
38
+ # @return [String]
39
+ #
40
+ def header(opts = {}, &block)
41
+ @card.header(opts) do
42
+ content_tag(config(:accordion_header, :h5)) do
43
+ content_tag(
44
+ :a,
45
+ data: { toggle: 'collapse', target: "##{@target}" },
46
+ &block
47
+ )
48
+ end
49
+ end
50
+ end
51
+
52
+ # @description
53
+ # - Builds the body component for the accordion, which is actually the body
54
+ # of a Card.
55
+ #
56
+ # @note
57
+ # - The `@parent` gets used to set the parent element for the accordion. This
58
+ # gets used primarily in the `AccordionGroup`.
59
+ #
60
+ # @param [Hash] opts
61
+ # @return [String]
62
+ #
63
+ def body(opts = {}, &block)
64
+ data = { parent: "##{@parent}" } if @parent.present?
65
+
66
+ content_tag :div, id: @target, class: 'collapse', data: data do
67
+ @card.body(opts, &block)
68
+ end
69
+ end
70
+
71
+ # @description
72
+ # - Because Accordions are basically `Cards` with a wrapper, we might as
73
+ # well catch common `Card` methods and send them to the card object. No
74
+ # point in creating similar methods for `Accordions`.
75
+ #
76
+ # @param [Symbol|String] method
77
+ # @param [Mixed] args
78
+ #
79
+ def method_missing(method, *args, &block)
80
+ if CARD_METHODS.include?(method)
81
+ @card.send(method, *args, &block)
82
+ else
83
+ super
84
+ end
85
+ end
86
+
87
+ # @description
88
+ # -
89
+ #
90
+ def respond_to_missing?(method, include_private = false)
91
+ CARD_METHODS.include?(method) || super
92
+ end
93
+
94
+ # @description
95
+ # - Leaving off the the default `accordion` class because with only 1 accordion,
96
+ # there is no bottom.
97
+ #
98
+ # @return [String]
99
+ #
100
+ def to_s
101
+ content_tag :div, class: "card #{@class}", data: @data do
102
+ @content.call(self)
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,54 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap4Helper
5
+ # @description
6
+ # - Used to build groups of Accordions, that are all synced with each other.
7
+ #
8
+ class AccordionGroup < Component
9
+ # @description
10
+ # -
11
+ #
12
+ # @param [ActionView] template
13
+ # @param [NilClass|String|Symbol|Hash] opts
14
+ # @param [Hash]
15
+ #
16
+ def initialize(template, opts = {}, &block)
17
+ super(template)
18
+
19
+ @id = opts.fetch(:id, uuid)
20
+ @class = opts.fetch(:class, '')
21
+ @data = opts.fetch(:data, {})
22
+ @content = block || proc { '' }
23
+ end
24
+
25
+ # @description
26
+ # - Used to build a `Accordion` for the `AccordionGroup`.
27
+ #
28
+ # @param [Mixed] args
29
+ # @return [Accordion]
30
+ #
31
+ def accordion(*args, &block)
32
+ opts = *args
33
+
34
+ if opts.any? { |opt| opt.is_a?(Hash) }
35
+ opts.collect! { |opt| opt[:parent] = @id if opt.is_a?(Hash) }
36
+ else
37
+ opts << { parent: @id }
38
+ end
39
+
40
+ Accordion.new(self, *opts, &block)
41
+ end
42
+
43
+ # @description
44
+ # - Used to get the HTML markup of the `AccordionGroup`
45
+ #
46
+ # @return [String]
47
+ #
48
+ def to_s
49
+ content_tag :div, id: @id, class: "accordion #{@class}", data: @data do
50
+ @content.call(self)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,79 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap4Helper
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
+ # @return [String]
60
+ #
61
+ def to_s
62
+ content_tag :div, id: @id, class: container_class do
63
+ concat(@dismissible ? close_button : '')
64
+ @content.call(self)
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ # @description
71
+ # - Used to get the container classes.
72
+ #
73
+ # @return [String]
74
+ #
75
+ def container_class
76
+ "alert alert-#{@context} #{@class}"
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,46 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap4Helper
5
+ # @description
6
+ # - Creates Bootstrap badge components that can be used anywhere.
7
+ #
8
+ class Badge < Component
9
+ # @description
10
+ # -
11
+ #
12
+ # @param [ActionView] template
13
+ # @param [NilClass|String|Symbol|Hash] context_or_options.
14
+ # @param [Hash] opts
15
+ #
16
+ def initialize(template, context_or_options = nil, opts = {}, &block)
17
+ super(template)
18
+ @context, args = parse_arguments(context_or_options, opts)
19
+
20
+ @id = args.fetch(:id, nil)
21
+ @class = args.fetch(:class, '')
22
+ @content = block || proc { '' }
23
+ end
24
+
25
+ # @description
26
+ # -
27
+ #
28
+ def to_s
29
+ content_tag(config(:badge, :span), id: @id, class: container_class) { @content.call(self) }
30
+ end
31
+
32
+ private
33
+
34
+ # @description
35
+ # - Used to get the container classes.
36
+ #
37
+ # @return [String]
38
+ #
39
+ def container_class
40
+ string = 'badge '
41
+ string += @context == 'secondary' ? 'badge-secondary' : "badge-#{@context}"
42
+ string += " #{@class}"
43
+ string
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,129 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap4Helper
5
+ # @description
6
+ # - Used to build Bootstrap Card components. Cards are wildly used through Bootstrap 4.
7
+ #
8
+ class Card < Component
9
+ # @description
10
+ # - Used to initialize a new Card component.
11
+ #
12
+ # @param [ActionView] template
13
+ # @param [Hash] opts
14
+ # @return [Card]
15
+ #
16
+ def initialize(template, opts = {}, &block)
17
+ super(template)
18
+
19
+ @id = opts.fetch(:id, '')
20
+ @class = opts.fetch(:class, '')
21
+ @data = opts.fetch(:data, nil)
22
+ @content = block || proc { '' }
23
+ end
24
+
25
+ # @description
26
+ # - Builds the Header component.
27
+ #
28
+ # @param [Hash] args
29
+ # @return [String]
30
+ #
31
+ def header(args = {}, &block)
32
+ build_base_component :header, args, &block
33
+ end
34
+
35
+ # @description
36
+ # - Builds the Body component.
37
+ #
38
+ # @param [Hash] args
39
+ # @return [String]
40
+ #
41
+ def body(args = {}, &block)
42
+ build_base_component :body, args, &block
43
+ end
44
+
45
+ # @description
46
+ # - Builds the Footer component.
47
+ #
48
+ # @param [Hash] args
49
+ # @return [String]
50
+ #
51
+ def footer(args = {}, &block)
52
+ build_base_component :footer, args, &block
53
+ end
54
+
55
+ # @description
56
+ # - Builds a Title component.
57
+ #
58
+ # @param [Hash] args
59
+ # @return [String]
60
+ #
61
+ def title(args = {}, &block)
62
+ build_sub_component config(:card_title, :h5), :title, args, &block
63
+ end
64
+
65
+ # @description
66
+ # - Builds a Text component.
67
+ #
68
+ # @param [Hash] args
69
+ # @return [String]
70
+ #
71
+ def text(args = {}, &block)
72
+ build_sub_component config(:card_text, :p), :text, args, &block
73
+ end
74
+
75
+ # @description
76
+ # - Builds a Img Overlay component.
77
+ #
78
+ # @param [Hash] args
79
+ # @return [String]
80
+ #
81
+ def image_overlay(args = {}, &block)
82
+ build_base_component 'img-overlay', args, &block
83
+ end
84
+
85
+ # @description
86
+ # - Outputs the Object in its String representation.
87
+ #
88
+ def to_s
89
+ content_tag :div, id: @id, class: "card #{@class}", data: @data do
90
+ @content.call(self)
91
+ end
92
+ end
93
+
94
+ private
95
+
96
+ # @description
97
+ # - Used to build basic DIV components.
98
+ #
99
+ # @param [String] type
100
+ # @param [Mixed] args
101
+ # @return [String]
102
+ #
103
+ def build_base_component(type, args, &block)
104
+ build_sub_component :div, type, args, &block
105
+ end
106
+
107
+ # @description
108
+ # - Used to build various DOM components.
109
+ #
110
+ # @param [Symbol] tag
111
+ # @param [String] type
112
+ # @param [Mixed] args
113
+ # @return [String]
114
+ #
115
+ def build_sub_component(tag, type, args, &block)
116
+ id = args.fetch(:id, '')
117
+ klass = args.fetch(:class, '')
118
+ data = args.fetch(:data, {})
119
+
120
+ content_tag(
121
+ tag,
122
+ id: id,
123
+ class: "card-#{type} #{klass}",
124
+ data: data,
125
+ &block
126
+ )
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,18 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap4Helper
5
+ # @description
6
+ #
7
+ #
8
+ class CardColumn < CardGrouping
9
+ # @description
10
+ # -
11
+ #
12
+ def to_s
13
+ content_tag :div, id: @id, class: "card-columns #{@class}", data: @data do
14
+ @content.call(self)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap4Helper
5
+ # @description
6
+ #
7
+ #
8
+ class CardDeck < CardGrouping
9
+ # @description
10
+ # -
11
+ #
12
+ def to_s
13
+ content_tag :div, id: @id, class: "card-deck #{@class}", data: @data do
14
+ @content.call(self)
15
+ end
16
+ end
17
+ end
18
+ end