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,80 +1,62 @@
1
- # @root
2
- #
3
- #
4
- module Bootstrap3Helper
5
- # @description
6
- # - The Alert helper is meant to help you rapidly build Bootstrap Alert
1
+ module Bootstrap3Helper # :nodoc:
2
+ # The Alert helper is meant to help you rapidly build Bootstrap Alert
7
3
  # components quickly and easily. The dissmiss button is optional.
8
4
  #
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
5
  class Alert < Component
20
- # @description
21
- # - Used to generate Bootstrap alert components quickly.
6
+ # Used to generate Bootstrap alert components quickly.
22
7
  #
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
8
+ # @param [ActionView] template Template in which your are binding too.
9
+ # @param [NilClass|String|Symbol|Hash] context_or_options Bootstrap class context, or options hash.
10
+ # @param [Hash] opts
11
+ # @option opts [String] :id The ID of the element
12
+ # @option opts [String] :class Custom class for the component.
33
13
  # @return [Alert]
34
14
  #
35
15
  def initialize(template, context_or_options = nil, opts = {}, &block)
36
16
  super(template)
37
- @context, args = parse_arguments(context_or_options, opts)
38
17
 
39
- @id = args.fetch(:id, nil)
40
- @class = args.fetch(:class, '')
41
- @dismissible = args.fetch(:dismissible, false)
42
- @content = block || proc { '' }
18
+ @context, args = parse_context_or_options(context_or_options, opts)
19
+ @id = args.fetch(:id, nil)
20
+ @class = args.fetch(:class, '')
21
+ @dismissible = args.fetch(:dismissible, false)
22
+ @content = block || proc { '' }
43
23
  end
44
24
 
45
- # @description
46
- # - The dissmiss button, if the element has one.
25
+ # The dissmiss button, if the element has one.
47
26
  #
48
27
  # @return [String]
49
28
  #
50
29
  def close_button
51
- content_tag(:button, class: 'close', data: { dismiss: 'alert' }, aria: { label: 'Close' }) do
30
+ content_tag(
31
+ :button,
32
+ class: 'close',
33
+ data: { dismiss: 'alert' },
34
+ aria: { label: 'Close' }
35
+ ) do
52
36
  content_tag(:span, aria: { hidden: true }) { '&times;'.html_safe }
53
37
  end
54
38
  end
55
39
 
56
- # @description
57
- # - Used to render out the Alert component.
40
+ # Used to render out the Alert component.
58
41
  #
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.
42
+ # @note Concat needs to be used to add the content of the button to the output
43
+ # buffer. For some reason, if though the block returns a String, trying to
44
+ # add that string to the dismiss button string, returns with the dismiss
45
+ # button missing. Was forced to caoncat the button to the current output
46
+ # buffer in order to get it to persist after the block call.
65
47
  #
66
48
  # @return [String]
67
49
  #
68
50
  def to_s
69
51
  content_tag :div, id: @id, class: container_class do
70
- concat(@dismissible ? close_button : '') + @content.call
52
+ concat(@dismissible ? close_button : '')
53
+ @content.call(self)
71
54
  end
72
55
  end
73
56
 
74
57
  private
75
58
 
76
- # @description
77
- # - Used to get the container classes.
59
+ # Used to get the container classes.
78
60
  #
79
61
  # @return [String]
80
62
  #
@@ -1,47 +1,37 @@
1
- # @root
2
- #
3
- #
4
- module Bootstrap3Helper
5
- # @description
6
- # - Used to generate Bootstrap callout component quickly.
1
+ module Bootstrap3Helper # :nodoc:
2
+ # Used to generate Bootstrap callout component quickly.
7
3
  #
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
4
  #
20
5
  class Callout < Component
6
+ # @param [ActionView] template Template in which your are binding too.
7
+ # @param [NilClass|String|Symbol|Hash] context_or_options Bootstrap class context, or options hash.
8
+ # @param [Hash] opts
9
+ # @option opts [String] :id The ID of the element
10
+ # @option opts [String] :class Custom class for the component.
11
+ # @return [Callout]
12
+ #
21
13
  def initialize(template, context_or_options = nil, opts = {}, &block)
22
14
  super(template)
23
- @context, args = parse_arguments(context_or_options, opts)
15
+ @context, args = parse_context_or_options(context_or_options, opts)
24
16
 
25
- @id = args.fetch(:id, nil)
26
- @class = args.fetch(:class, '')
17
+ @id = args.fetch(:id, nil)
18
+ @class = args.fetch(:class, '')
27
19
  @content = block || proc { '' }
28
20
  end
29
21
 
30
- # @description
31
- # - Returns a string representation of the component.
22
+ # Returns a string representation of the component.
32
23
  #
33
24
  # @return [String]
34
25
  #
35
26
  def to_s
36
27
  content_tag :div, id: @id, class: container_class do
37
- @content.call
28
+ @content.call(self)
38
29
  end
39
30
  end
40
31
 
41
32
  private
42
33
 
43
- # @description
44
- # - Used to get the container classes.
34
+ # Used to get the container classes.
45
35
  #
46
36
  # @return [String]
47
37
  #
@@ -1,35 +1,27 @@
1
- # @root
2
- #
3
- #
4
- module Bootstrap3Helper
5
- # @description
6
- # - This super class is meant to contain commonly used methods that
1
+ module Bootstrap3Helper # :nodoc
2
+ # This super class is meant to contain commonly used methods that
7
3
  # all sub classes can leverage.
8
4
  #
9
- # @note
10
- # - Every component that inherits from this class, needs to call the parent
11
- # initialization method! In order to properly render erb blocks within the
12
- # proper context, we need the template. The only way to get this, is to pass
13
- # in the template.
5
+ # @note Every component that inherits from this class, needs to call the parent
6
+ # initialization method! In order to properly render erb blocks within the
7
+ # proper context, we need the template. The only way to get this, is to pass
8
+ # in the template.
14
9
  #
15
- # @note
16
- # - the `context` mentioned above, refers to the context of `@template` and
17
- # not to be confused with `@context` that can be found in the sub classes.
18
- # `@context` refers to the Bootstrap class context of the component.
10
+ # @note The `context` mentioned above, refers to the context of `@template` and
11
+ # not to be confused with `@context` that can be found in the sub classes.
12
+ # `@context` refers to the Bootstrap class context of the component.
19
13
  #
20
14
  class Component
21
- # @description
22
- # - Used to ensure that the helpers always have the propert context for
15
+ # Used to ensure that the helpers always have the propert context for
23
16
  # rendering and bindings.
24
17
  #
25
- # @param [Class] template - the context of the bindings
18
+ # @param [ActionView] template the context of the bindings
26
19
  #
27
20
  def initialize(template)
28
21
  @template = template
29
22
  end
30
23
 
31
- # @description
32
- # - Used to pass all context of content_tag to the template. This ensures
24
+ # Used to pass all context of content_tag to the template. This ensures
33
25
  # proper template binding of variables and methods!
34
26
  #
35
27
  # @return [String]
@@ -50,38 +42,67 @@ module Bootstrap3Helper
50
42
  )
51
43
  end
52
44
 
53
- # @description
54
- # - Used to pass all concat references to the template. This ensures proper
45
+ # Used to pass all concat references to the template. This ensures proper
55
46
  # binding. Concat adds a String to the template Output buffer. Useful when
56
47
  # trying to add a String with no block.
57
48
  #
58
- # @params [String] text
49
+ # @param [String] text
59
50
  #
60
51
  def concat(text)
61
52
  @template.concat(text)
62
53
  end
63
54
 
64
- # @description
65
- # - Used to parse method arguments. If the first argument is
55
+ # Used to parse method arguments. If the first argument is
66
56
  # a Hash, then it is assumed that the user left off the bootstrap
67
57
  # contectual class. So we will assign it to `default` and
68
58
  # return the Hash to be used as options.
69
59
  #
70
- # @params [Hash|NilClass|String|Symbol] *args
60
+ # @param [Hash|NilClass|String|Symbol] args
71
61
  # @return [Array]
72
62
  #
73
- def parse_arguments(*args)
63
+ def parse_context_or_options(*args)
64
+ parse_arguments(*args, 'default')
65
+ end
66
+
67
+ # Used to parse method arguments. If the first argument is
68
+ # a Hash, then it is assumed that the user left off the tag
69
+ # element. So we will assign it to <tt>NilClass</tt> and
70
+ # return the Hash to be used as options.
71
+ #
72
+ # @param [Hash|NilClass|String|Symbol] args
73
+ # @return [Array]
74
+ #
75
+ def parse_tag_or_options(*args)
76
+ parse_arguments(*args, nil)
77
+ end
78
+
79
+ # Used to parse method arguments. If the first argument is
80
+ # a Hash, then it is assumed that the user left off the bootstrap
81
+ # contectual class. So we will assign it to `default` and
82
+ # return the Hash to be used as options.
83
+ #
84
+ # @overload parse_arguments(param_or_options, options, default)
85
+ # @param [NilClass|Hash|Symbol|String] param_or_options
86
+ # @param [Hash] options
87
+ # @param [NilClass|String|Symbol] default
88
+ #
89
+ # @overload parse_arguments(options, default)
90
+ # @param [Hash] options
91
+ # @param [NilClass|String|Symbol] default
92
+ #
93
+ # @return [Array]
94
+ #
95
+ def parse_arguments(*args, default)
74
96
  first, second = *args
75
97
  case first
76
98
  when Hash, NilClass
77
- ['default', first || second]
99
+ [default, first || second]
78
100
  when Symbol, String
79
101
  [first, second]
80
102
  end
81
103
  end
82
104
 
83
- # @description
84
- # - Used to generate a (hopefully) unique ID for DOM elements. Used as a
105
+ # Used to generate a (hopefully) unique ID for DOM elements. Used as a
85
106
  # fallback if the user doesn't specify one.
86
107
  #
87
108
  # @return [String]
@@ -89,5 +110,25 @@ module Bootstrap3Helper
89
110
  def uuid
90
111
  (0...10).map { rand(65..90).chr }.join
91
112
  end
113
+
114
+ # Used to get config settings inside of components quicker.
115
+ #
116
+ # @param [Symbol|String|Hash] setting
117
+ # @return [Mixed]
118
+ #
119
+ def config(setting, fallback)
120
+ object = Bootstrap3Helper.config
121
+
122
+ value = (
123
+ case setting
124
+ when Hash
125
+ object.send(setting.keys[0])[setting.values[0]] if object.send(setting.keys[0])
126
+ when Symbol, String
127
+ object.send(setting) if object.respond_to?(setting)
128
+ end
129
+ )
130
+
131
+ value || fallback
132
+ end
92
133
  end
93
134
  end
@@ -0,0 +1,36 @@
1
+ # @root
2
+ #
3
+ #
4
+ module Bootstrap3Helper
5
+ # @description
6
+ #
7
+ #
8
+ class Configuration
9
+ DEFAULT_SETTINGS = {
10
+ autoload_in_all_views: true,
11
+ accodions: {
12
+ header: :div,
13
+ body: :div,
14
+ footer: :div,
15
+ title: :h4
16
+ },
17
+ panels: {
18
+ header: :div,
19
+ body: :div,
20
+ footer: :div,
21
+ title: :h3
22
+ }
23
+ }.freeze
24
+
25
+ attr_accessor(*DEFAULT_SETTINGS.keys)
26
+
27
+ # Class constructor
28
+ #
29
+ # @param [Hash] _args
30
+ # @return [ClassName]
31
+ #
32
+ def initialize(_args = {})
33
+ @autoload_in_all_views = true
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ module Bootstrap3Helper # :nodoc:
2
+ # Naming convention used as to not pollute views where the module is
3
+ # included. @config is a common instance variable name. We don't want
4
+ # to risk overriding another developers variable.
5
+ #
6
+ @_bs3h_config = Configuration.new
7
+
8
+ class << self
9
+ # Simple interface for exposing the configuration object.
10
+ #
11
+ # @return [Bootstrap5Helper::Configuration]
12
+ #
13
+ def config
14
+ yield @_bs3h_config if block_given?
15
+
16
+ @_bs3h_config
17
+ end
18
+ end
19
+ end
@@ -1,102 +1,155 @@
1
- # @root
2
- #
3
- #
4
- module Bootstrap3Helper
5
- # @description
6
- # - Used to rapidly build Bootstrap Panel Components.
1
+ module Bootstrap3Helper # :nodoc:
2
+ # Used to rapidly build Bootstrap Panel Components.
7
3
  #
8
- # <code>
9
- # <%= panel_helper class: 'panel-primary' do |p| %>
10
- # <%= p.header { "Some Title" }
11
- # <%= p.body class: 'custom-class', id: 'custom-id' do %>
12
- # //HTML or Ruby code here...
13
- # <% end %>
14
- # <%= p.footer do %>
15
- # //HTML or Ruby
16
- # <% end %>
17
- # <% end %>
18
- # </code>
19
4
  #
20
5
  class Panel < Component
21
- # @description
22
- # - Creates a new Panel object.
6
+ # Creates a new Panel object.
23
7
  #
24
- # @param [Class] template - Template in which your are binding too.
25
- # @param [NilClass|String|Symbol|Hash] - Bootstrap class context, or options hash.
8
+ # @param [ActionView] template - Template in which your are binding too.
9
+ # @param [NilClass|String|Symbol|Hash] context_or_options - Bootstrap class context, or options hash.
26
10
  # @param [Hash] opts
27
- # <code>
28
- # opts = {
29
- # id: [String|NilClass] - The ID, if you want one, for the element.
30
- # class: [String|NilClass] - Custom class for the element.
31
- # }
32
- # </code>
11
+ # @option opts [String] :id - The ID of the element
12
+ # @option opts [String] :class - Custom class for the component.
13
+ # @option opts [Hash] :data - Any data attributes for the element.
14
+ # @option opts [Symbol] :config_type - Used to change config from Panel to Accordion.
33
15
  # @return [Panel]
34
16
  #
35
- def initialize(template, context_or_options = nil, opts = {})
17
+ def initialize(template, context_or_options = nil, opts = {}, &block)
36
18
  super(template)
37
- @context, args = parse_arguments(context_or_options, opts)
19
+ @context, args = parse_context_or_options(context_or_options, opts)
38
20
 
39
- @id = args.fetch(:id, '')
40
- @class = args.fetch(:class, '')
41
- @data = args.fetch(:data, nil)
21
+ @id = args.fetch(:id, '')
22
+ @class = args.fetch(:class, '')
23
+ @data = args.fetch(:data, nil)
24
+ @config_type = args.fetch(:config_type, :panels)
25
+ @content = block || proc { '' }
42
26
  end
43
27
 
44
- # @description
45
- # - Used to generate the header component for the panel.
28
+ # Used to generate the header component for the panel.
46
29
  #
47
- # @param [Hash] args
30
+ # @param [Symbol|String|Hash|NilClass] tag_or_options
31
+ # @param [Hash] opts
32
+ # @option opts [String] :id
33
+ # @option opts [String] :class
34
+ # @option opts [Hash] :data
35
+ # @option opts [Hash] :aria
36
+ # @return [String]
48
37
  #
49
- def header(args = {})
50
- id = args.fetch(:id, '')
38
+ def header(tag_or_options = nil, opts = {}, &block)
39
+ tag, args = parse_tag_or_options(tag_or_options, opts)
40
+
41
+ id = args.fetch(:id, nil)
51
42
  klass = args.fetch(:class, '')
52
- @header = content_tag(:div, id: id, class: 'panel-heading ' + klass) do
53
- content_tag(:h3, class: 'panel-title') { yield if block_given? }
54
- end
43
+ data = args.fetch(:data, {})
44
+ aria = args.fetch(:aria, {})
45
+
46
+ content_tag(
47
+ tag || config({ @config_type => :header }, :div),
48
+ id: id,
49
+ class: "panel-heading #{klass}",
50
+ data: data,
51
+ aria: aria,
52
+ &block
53
+ )
55
54
  end
56
55
 
57
- # @description
58
- # - Used to generate the body component for the panel.
56
+ # Builds a title component for the panel.
59
57
  #
60
- # @param [Hash] args
58
+ # @param [Symbol|String|Hash|NilClass] tag_or_options
59
+ # @param [Hash] opts
60
+ # @option opts [String] :id
61
+ # @option opts [String] :class
62
+ # @option opts [Hash] :data
63
+ # @option opts [Hash] :aria
64
+ # @return [String]
61
65
  #
62
- def body(args = {})
63
- id = args.fetch(:id, '')
66
+ def title(tag_or_options = nil, opts = {}, &block)
67
+ tag, args = parse_tag_or_options(tag_or_options, opts)
68
+
69
+ id = args.fetch(:id, nil)
64
70
  klass = args.fetch(:class, '')
65
- @body = content_tag(:div, id: id, class: 'panel-body ' + klass) do
66
- yield if block_given?
67
- end
71
+ data = args.fetch(:data, {})
72
+ aria = args.fetch(:aria, {})
73
+
74
+ content_tag(
75
+ tag || config({ @config_type => :title }, :h3),
76
+ id: id,
77
+ class: "panel-title #{klass}",
78
+ data: data,
79
+ aria: aria,
80
+ &block
81
+ )
68
82
  end
69
83
 
70
- # @description
71
- # - Used to generate the footer component for the panel.
84
+ # Used to generate the body component for the panel.
72
85
  #
73
- # @param [Hash] args
86
+ # @param [Symbol|String|Hash|NilClass] tag_or_options
87
+ # @param [Hash] opts
88
+ # @option opts [String] :id
89
+ # @option opts [String] :class
90
+ # @option opts [Hash] :data
91
+ # @option opts [Hash] :aria
92
+ # @return [String]
74
93
  #
75
- def footer(args = {})
76
- id = args.fetch(:id, '')
94
+ def body(tag_or_options = nil, opts = {}, &block)
95
+ tag, args = parse_tag_or_options(tag_or_options, opts)
96
+
97
+ id = args.fetch(:id, nil)
77
98
  klass = args.fetch(:class, '')
78
- @footer = content_tag(:div, id: id, class: 'panel-footer ' + klass) do
79
- yield if block_given?
80
- end
99
+ data = args.fetch(:data, {})
100
+ aria = args.fetch(:aria, {})
101
+
102
+ content_tag(
103
+ tag || config({ @config_type => :body }, :div),
104
+ id: id,
105
+ class: "panel-body #{klass}",
106
+ data: data,
107
+ aria: aria,
108
+ &block
109
+ )
110
+ end
111
+
112
+ # Used to generate the footer component for the panel.
113
+ #
114
+ # @param [Symbol|String|Hash|NilClass] tag_or_options
115
+ # @param [Hash] opts
116
+ # @option opts [String] :id
117
+ # @option opts [String] :class
118
+ # @option opts [Hash] :data
119
+ # @option opts [Hash] :aria
120
+ # @return [String]
121
+ #
122
+ def footer(tag_or_options = nil, opts = {}, &block)
123
+ tag, args = parse_tag_or_options(tag_or_options, opts)
124
+
125
+ id = args.fetch(:id, nil)
126
+ klass = args.fetch(:class, '')
127
+ data = args.fetch(:data, {})
128
+ aria = args.fetch(:aria, {})
129
+
130
+ content_tag(
131
+ tag || config({ @config_type => :footer }, :div),
132
+ id: id,
133
+ class: "panel-footer #{klass}",
134
+ data: data,
135
+ aria: aria,
136
+ &block
137
+ )
81
138
  end
82
139
 
83
- # @description
84
- # - Used to render the html for the entire panel object.
140
+ # Used to render the html for the entire panel object.
85
141
  #
86
142
  # @return [String]
87
143
  #
88
144
  def to_s
89
- content = content_tag :div, id: @id, class: container_classes, data: @data do
90
- @header + @body + @footer
145
+ content_tag :div, id: @id, class: container_classes, data: @data do
146
+ @content.call(self)
91
147
  end
92
-
93
- content
94
148
  end
95
149
 
96
150
  private
97
151
 
98
- # @description
99
- # - Used to get the container css classes.
152
+ # Used to get the container css classes.
100
153
  #
101
154
  # @return [String]
102
155
  #