bootstrap-view_helpers 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +33 -0
  5. data/app/assets/javascripts/bootstrap/view_helpers/application.js +13 -0
  6. data/app/assets/stylesheets/bootstrap/view_helpers/application.css +15 -0
  7. data/app/helpers/bootstrap/view_helpers/accordion_helper.rb +9 -0
  8. data/app/helpers/bootstrap/view_helpers/alert_helper.rb +12 -0
  9. data/app/helpers/bootstrap/view_helpers/application_helper.rb +6 -0
  10. data/app/helpers/bootstrap/view_helpers/badge_helper.rb +18 -0
  11. data/app/helpers/bootstrap/view_helpers/button_helper.rb +28 -0
  12. data/app/helpers/bootstrap/view_helpers/card_helper.rb +19 -0
  13. data/app/helpers/bootstrap/view_helpers/modal_helper.rb +20 -0
  14. data/lib/bootstrap/view_helpers/component.rb +40 -0
  15. data/lib/bootstrap/view_helpers/components/accordion/button.rb +54 -0
  16. data/lib/bootstrap/view_helpers/components/accordion/card.rb +49 -0
  17. data/lib/bootstrap/view_helpers/components/accordion.rb +45 -0
  18. data/lib/bootstrap/view_helpers/components/alert.rb +67 -0
  19. data/lib/bootstrap/view_helpers/components/badge.rb +33 -0
  20. data/lib/bootstrap/view_helpers/components/button/button_type_not_found_error.rb +13 -0
  21. data/lib/bootstrap/view_helpers/components/button/checkbox.rb +38 -0
  22. data/lib/bootstrap/view_helpers/components/button/close.rb +43 -0
  23. data/lib/bootstrap/view_helpers/components/button/link.rb +37 -0
  24. data/lib/bootstrap/view_helpers/components/button/radio.rb +38 -0
  25. data/lib/bootstrap/view_helpers/components/button.rb +87 -0
  26. data/lib/bootstrap/view_helpers/components/card.rb +85 -0
  27. data/lib/bootstrap/view_helpers/components/modal/body.rb +25 -0
  28. data/lib/bootstrap/view_helpers/components/modal/footer.rb +35 -0
  29. data/lib/bootstrap/view_helpers/components/modal/header.rb +48 -0
  30. data/lib/bootstrap/view_helpers/components/modal.rb +96 -0
  31. data/lib/bootstrap/view_helpers/components/style_not_found_error.rb +11 -0
  32. data/lib/bootstrap/view_helpers/contextual_classes.rb +22 -0
  33. data/lib/bootstrap/view_helpers/engine.rb +7 -0
  34. data/lib/bootstrap/view_helpers/version.rb +5 -0
  35. data/lib/bootstrap/view_helpers.rb +15 -0
  36. data/lib/tasks/bootstrap/view_helpers_tasks.rake +4 -0
  37. metadata +107 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1c253237b727ef60ed1c592ae2244045ea115fc6
4
+ data.tar.gz: b09831d5980813e6ad41bbbf368a760b116f5242
5
+ SHA512:
6
+ metadata.gz: d703e3ad58db741cd6925bb66b483ca4941a069242aa2bc70aea152ec544ff6bb3ec423a3412d71a0bd7988167b88c503e8bb33fbd154ff2652078bde0c5ef3e
7
+ data.tar.gz: 3b93b3acea48e1fce4da8ac8fb1a33957826aa9de6f3a845d1e720bcc6cbb2296d6b080a9b9de9258621fff7f35984a7d797d86b43e29453b1aa4ea3bbe006e5
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Rodrigo Castro
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.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = BootstrapViewHelpers
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,33 @@
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 = 'BootstrapViewHelpers'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ Bundler::GemHelper.install_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
+ task default: :test
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,9 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module AccordionHelper
4
+ def bs_accordion(options = {}, &block)
5
+ Components::Accordion.new(self, options, &block).to_html
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module AlertHelper
4
+ def bs_alert(title_or_options = nil, options = {}, &block)
5
+ options = block.present? ? title_or_options : options
6
+ options[:content] = block.present? ? nil : title_or_options
7
+
8
+ Components::Alert.new(self, options || {}, &block).to_html
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module ApplicationHelper
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,18 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module BadgeHelper
4
+ def bs_badge(label_or_options = {}, options = {}, &block)
5
+ options = label_or_options if block_given?
6
+ options[:label] = label_or_options unless block_given?
7
+ Components::Badge.new(self, options, &block).to_html
8
+ end
9
+
10
+ def bs_pill(label_or_options = {}, options = {}, &block)
11
+ options = label_or_options if block_given?
12
+ options[:label] = label_or_options unless block_given?
13
+ options[:pill] = true
14
+ Components::Badge.new(self, options, &block).to_html
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module ButtonHelper
4
+ def bs_button(label, options = {}, &block)
5
+ options[:label] = label
6
+ Components::Button.build(self, options, &block).to_html
7
+ button_tag
8
+ end
9
+
10
+ def bs_link_to(name = nil, path_opts = nil, opts = nil, &block)
11
+ options, path_opts, name = path_opts, name, block if block_given?
12
+ options ||= opts || {}
13
+ options = options.merge({ href: path_opts, label: name, type: :link })
14
+ Components::Button.build(self, options).to_html
15
+ end
16
+
17
+ def bs_checkbox_button(label, options = {}, &block)
18
+ options = options.merge(label: label, type: :checkbox)
19
+ Components::Button.build(self, options, &block).to_html
20
+ end
21
+
22
+ def bs_radio_button(label, options = {}, &block)
23
+ options = options.merge(label: label, type: :radio)
24
+ Components::Button.build(self, options, &block).to_html
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module CardHelper
4
+ def bs_card(title_or_options = nil, options = {}, &block)
5
+ options = title_or_options if block && title_or_options.is_a?(Hash)
6
+ options[:title] = title_or_options if title_or_options.is_a?(String)
7
+
8
+ Components::Card.new(self, options || {}, &block).to_html
9
+ end
10
+
11
+ def bs_card_header(title_or_options = nil, options = {}, &block)
12
+ options = title_or_options if block && title_or_options.is_a?(Hash)
13
+ options[:title] = title_or_options if title_or_options.is_a?(String)
14
+
15
+ Components::Card.new(self, options || {}, &block).header
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module ModalHelper
4
+ def bs_modal(title_or_options = nil, opts = {}, &block)
5
+ options = title_or_options if block && title_or_options.is_a?(Hash)
6
+ options ||= opts || {}
7
+ options[:title] = title_or_options if title_or_options.is_a?(String)
8
+
9
+ Components::Modal.new(self, options || {}, &block).to_html
10
+ end
11
+
12
+ def bs_card_header(title_or_options = nil, options = {}, &block)
13
+ options = title_or_options if block && title_or_options.is_a?(Hash)
14
+ options[:title] = title_or_options if title_or_options.is_a?(String)
15
+
16
+ Components::Card.new(self, options || {}, &block).header
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,40 @@
1
+ require_relative 'components/style_not_found_error'
2
+ require_relative 'contextual_classes'
3
+
4
+ module Bootstrap
5
+ module ViewHelpers
6
+ class Component
7
+ attr_reader :view, :options, :block, :style
8
+
9
+ def initialize(view_context, options, &block)
10
+ @view = view_context
11
+ parse_options(options)
12
+ @block = block
13
+ end
14
+
15
+ def to_html; end
16
+
17
+ protected
18
+
19
+ delegate :content_tag, :safe_join, :concat, :capture, :link_to,
20
+ :button_tag, to: :view
21
+
22
+ def defaults
23
+ { style: ContextualClasses::PRIMARY }
24
+ end
25
+
26
+ def assign_and_validate_style
27
+ style = options.delete(:style) || defaults[:style]
28
+ return @style = style if ContextualClasses.valid?(style)
29
+ raise Components::StyleNotFoundError, style
30
+ end
31
+
32
+ def parse_options(options)
33
+ @options = options
34
+ assign_and_validate_style
35
+ end
36
+ end
37
+
38
+ module Components; end
39
+ end
40
+ end
@@ -0,0 +1,54 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Accordion < Component
5
+ class Button < Bootstrap::ViewHelpers::Components::Button
6
+ protected
7
+
8
+ def inject_aria_attributes
9
+ options[:aria] ||= {}
10
+ options[:aria][:expanded] = expanded?
11
+ options[:aria][:controls] = target
12
+ end
13
+
14
+ def inject_data_attributes
15
+ options[:data] ||= {}
16
+ options[:data][:toggle] = 'collapse'
17
+ options[:data][:target] = "##{target}"
18
+ end
19
+
20
+ def inject_class_name_to_options
21
+ super
22
+ options[:class] << ' btn-block text-left py-2 px-3'
23
+ end
24
+
25
+ def html_options
26
+ options.merge({ type: :button })
27
+ end
28
+
29
+ def assign_and_validate_style
30
+ @style = ContextualClasses::LINK
31
+ end
32
+
33
+ def target
34
+ @target ||= options.delete(:target)
35
+ end
36
+
37
+ # rubocop:disable Style/DoubleNegation
38
+ def expanded?
39
+ @expanded ||= !!options.delete(:expanded)
40
+ end
41
+ # rubocop:enable Style/DoubleNegation
42
+
43
+ # rubocop:disable Style/DoubleNegation
44
+ def parse_options(options)
45
+ super
46
+ @target = options.delete(:target)
47
+ @expanded = !!options.delete(:expanded)
48
+ end
49
+ # rubocop:enable Style/DoubleNegation
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,49 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Accordion < Component
5
+ class Card < Bootstrap::ViewHelpers::Components::Card
6
+ def header(_ = nil, _ = {})
7
+ content_tag(:div, class: "card-header p-0 #{options[:class]}",
8
+ id: header_id) do
9
+ title_tag(options[:title])
10
+ end
11
+ end
12
+
13
+ def body(_ = nil, &_block)
14
+ content_tag(:div, collapse_options) do
15
+ content_tag(:div, class: body_class(options)) { yield }
16
+ end
17
+ end
18
+
19
+ protected
20
+
21
+ attr_reader :id
22
+
23
+ def title_tag(title)
24
+ content_tag(:h5, class: header_options[:class]) do
25
+ Button.new(view, { label: title, target: accordion_item_id,
26
+ expanded: options[:expanded] }).to_html
27
+ end
28
+ end
29
+
30
+ def header_id
31
+ return @header_id if defined? @header_id
32
+ random_id = "header_#{SecureRandom.urlsafe_base64}"
33
+ @header_id = options[:header_id] || random_id
34
+ end
35
+
36
+ def accordion_item_id
37
+ @id ||= options[:id] || "group_#{SecureRandom.urlsafe_base64}"
38
+ end
39
+
40
+ def collapse_options
41
+ { class: "collapse #{'show' if options[:expanded]}",
42
+ id: accordion_item_id, data: { parent: "##{options[:parent]}" },
43
+ aria: { labelled_by: header_id } }
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,45 @@
1
+ require_relative 'button'
2
+ require_relative 'card'
3
+ require_relative 'accordion/button'
4
+ require_relative 'accordion/card'
5
+
6
+ module Bootstrap
7
+ module ViewHelpers
8
+ module Components
9
+ class Accordion < Component
10
+ def to_html
11
+ content_tag(:div, options.merge({ id: accordion_id })) do
12
+ block.call(self)
13
+ end
14
+ end
15
+
16
+ def group(title, options = {}, &block)
17
+ options[:title] = title
18
+ options[:body] = true
19
+ options[:parent] = accordion_id
20
+ Card.new(view, options, &block).to_html
21
+ end
22
+
23
+ protected
24
+
25
+ def accordion_id
26
+ @id ||= options[:id] || "accordion_#{random_uid}"
27
+ end
28
+
29
+ def random_uid
30
+ SecureRandom.urlsafe_base64
31
+ end
32
+
33
+ def assign_and_validate_style
34
+ @style = ContextualClasses::LINK
35
+ end
36
+
37
+ def inject_aria_attributes(opts)
38
+ opts[:aria] ||= {}
39
+ opts[:aria][:controls] = ''
40
+ opts[:aria][:expanded] = false
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,67 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Alert < Component
5
+ def to_html
6
+ content_tag(:div, options) do
7
+ concat(options[:content]) if options[:content]
8
+ concat close_button
9
+ block.call(self) if block.present?
10
+ end
11
+ end
12
+
13
+ def alert_link_to(name = nil, opts = nil, html_options = nil, &block)
14
+ link_to(name, opts, html_options.merge({ class: 'alert-link' }),
15
+ &block)
16
+ end
17
+
18
+ # Defaults to false
19
+ def dismissible?
20
+ options[:dismissible]
21
+ end
22
+
23
+ # Defaults to true
24
+ def fade?
25
+ return true if options[:fade].nil?
26
+ options[:fade]
27
+ end
28
+
29
+ protected
30
+
31
+ def defaults
32
+ { style: ContextualClasses::SECONDARY }
33
+ end
34
+
35
+ def parse_options(_)
36
+ super
37
+ inject_class_name
38
+ options[:role] = 'alert'
39
+ options[:style] = options[:html_style]
40
+ end
41
+
42
+ def inject_class_name
43
+ class_name = "alert alert-#{style} "
44
+ class_name << 'alert-dismissible ' if dismissible?
45
+ class_name << 'fade show ' if fade?
46
+ class_name << options.delete(:class) if options[:class]
47
+ options[:class] = class_name
48
+ end
49
+
50
+ def container_options
51
+ default = { class: "alert alert-#{style}#{' fade show' if fade?} " }
52
+ default[:class] << 'alert-dismissible ' if dismissible?
53
+ default[:class] << options.delete(:class) if options[:class]
54
+ default.merge(options)
55
+ end
56
+
57
+ def close_button
58
+ return unless dismissible?
59
+ content_tag(:button, class: 'close', data: { dismiss: 'alert' },
60
+ aria: { label: 'Close' }) do
61
+ content_tag(:span, '×', aria: { hidden: true })
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,33 @@
1
+ require_relative '../contextual_classes'
2
+
3
+ module Bootstrap
4
+ module ViewHelpers
5
+ module Components
6
+ class Badge < Component
7
+ def to_html
8
+ content_tag(:span, options) { options[:label] || block.call }
9
+ end
10
+
11
+ # Defaults to true
12
+ def fade?
13
+ return true if options[:fade].nil?
14
+ options[:fade]
15
+ end
16
+
17
+ protected
18
+
19
+ def parse_options(opts)
20
+ super
21
+ inject_class_name
22
+ end
23
+
24
+ def inject_class_name
25
+ pill = options[:pill] ? 'badge-pill' : ''
26
+ class_name = "badge badge-#{style} #{pill} "
27
+ class_name << options[:class] if options[:class].present?
28
+ options[:class] = class_name
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,13 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Button < Component
5
+ class ButtonTypeNotFoundError < StandardError
6
+ def initialize(type)
7
+ super("Button type \"#{type}\" does not exist")
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Button < Component
5
+ class Checkbox < Button
6
+ def to_html
7
+ input_options = input_html_options
8
+ content_tag(:label, html_options) do
9
+ content_tag(:input, label, input_options)
10
+ end
11
+ end
12
+
13
+ protected
14
+
15
+ attr_reader :checked, :input_options
16
+
17
+ def inject_class_name_to_options
18
+ super
19
+ options[:class] << ' disabled' if options[:disabled]
20
+ end
21
+
22
+ def html_options
23
+ @checked ||= options.delete(:checked)
24
+ options
25
+ end
26
+
27
+ def input_html_options
28
+ input_options = options.slice!(:id, :name)
29
+ @options, input_options = input_options, options
30
+ @input_options ||= { id: SecureRandom.hex, checked: checked,
31
+ type: 'checkbox', autocomplete: 'off' }
32
+ .merge(input_options)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,43 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Button < Component
5
+ class Close < Button
6
+ def to_html
7
+ button_tag(html_options) do
8
+ content_tag(:span, '×', aria: { hidden: true })
9
+ end
10
+ end
11
+
12
+ protected
13
+
14
+ attr_reader :dismiss
15
+
16
+ def verify_disabled_link
17
+ return unless options[:disabled]
18
+ options[:tabindex] = '-1'
19
+ end
20
+
21
+ def inject_data_attributes
22
+ super
23
+ @dismiss = options.delete(:dismiss) || 'modal'
24
+ end
25
+
26
+ def inject_class_name_to_options
27
+ options[:class] ||= ''
28
+ options[:class] << ' close'
29
+ end
30
+
31
+ def inject_aria_attributes
32
+ options[:aria] ||= {}
33
+ options[:aria][:label] = 'Fechar'
34
+ end
35
+
36
+ def html_options
37
+ options.merge({ type: :button, data: { dismiss: dismiss } })
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,37 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Button < Component
5
+ class Link < Button
6
+ def to_html
7
+ verify_disabled_link
8
+ link_to(label, href, html_options)
9
+ end
10
+
11
+ protected
12
+
13
+ attr_reader :href
14
+
15
+ def verify_disabled_link
16
+ return unless options[:disabled]
17
+ options[:tabindex] = '-1'
18
+ end
19
+
20
+ def inject_class_name_to_options
21
+ super
22
+ options[:class] << ' disabled' if options[:disabled]
23
+ end
24
+
25
+ def html_options
26
+ options.merge({ role: :button })
27
+ end
28
+
29
+ def inject_additional_attributes
30
+ super
31
+ @href = options.delete(:href)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Button < Component
5
+ class Radio < Button
6
+ def to_html
7
+ input_options = input_html_options
8
+ content_tag(:label, html_options) do
9
+ content_tag(:input, label, input_options)
10
+ end
11
+ end
12
+
13
+ protected
14
+
15
+ attr_reader :checked, :input_options
16
+
17
+ def inject_class_name_to_options
18
+ super
19
+ options[:class] << ' disabled' if options[:disabled]
20
+ end
21
+
22
+ def html_options
23
+ @checked ||= options.delete(:checked)
24
+ options
25
+ end
26
+
27
+ def input_html_options
28
+ input_options = options.slice!(:id, :name)
29
+ @options, input_options = input_options, options
30
+ @input_options ||= { id: SecureRandom.hex, checked: checked,
31
+ name: 'options', type: 'radio',
32
+ autocomplete: 'off' }.merge(input_options)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,87 @@
1
+ require_relative '../contextual_classes'
2
+ require_relative 'button/button_type_not_found_error'
3
+ require_relative 'button/checkbox'
4
+ require_relative 'button/close'
5
+ require_relative 'button/link'
6
+ require_relative 'button/radio'
7
+
8
+ module Bootstrap
9
+ module ViewHelpers
10
+ module Components
11
+ class Button < Component
12
+ TYPES = {
13
+ button: Button,
14
+ checkbox: Button::Checkbox,
15
+ close: Button::Close,
16
+ link: Button::Link,
17
+ radio: Button::Radio
18
+ }
19
+
20
+ class << self
21
+ def build(view_context, options, &block)
22
+ type = options.delete(:type) || defaults[:type]
23
+ options[:type] = options[:html_type]
24
+ unless TYPES.keys.include? type
25
+ raise ButtonTypeNotFoundError, type
26
+ end
27
+ TYPES[type].new(view_context, options, &block)
28
+ end
29
+
30
+ protected
31
+
32
+ def defaults
33
+ { type: :button, style: ContextualClasses::PRIMARY }
34
+ end
35
+ end
36
+
37
+ def to_html
38
+ content_tag(:button, label, html_options)
39
+ end
40
+
41
+ protected
42
+
43
+ attr_reader :label
44
+
45
+ def defaults
46
+ Button.send(:defaults)
47
+ end
48
+
49
+ def inject_additional_attributes
50
+ inject_class_name_to_options
51
+ inject_aria_attributes
52
+ inject_data_attributes
53
+ @label = options.delete(:label)
54
+ end
55
+
56
+ def inject_class_name_to_options
57
+ options[:class] ||= ''
58
+ outline = options[:outline] ? 'outline-' : ''
59
+ active = options[:active] ? ' active' : ''
60
+ options[:class] << " btn btn-#{outline}#{style}#{active}"
61
+ end
62
+
63
+ # rubocop:disable Style/DoubleNegation
64
+ def inject_aria_attributes
65
+ options[:aria] ||= {}
66
+ options[:aria][:disabled] = true if options[:disabled]
67
+ options[:aria][:pressed] = !!options[:active]
68
+ end
69
+ # rubocop:enable Style/DoubleNegation
70
+
71
+ def inject_data_attributes
72
+ options[:data] ||= {}
73
+ options[:data][:toggle] = 'button' if options.delete(:toggle)
74
+ end
75
+
76
+ def html_options
77
+ { type: :submit }.merge(options)
78
+ end
79
+
80
+ def parse_options(_)
81
+ super
82
+ inject_additional_attributes
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,85 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Card < Component
5
+ def to_html
6
+ content_tag(:div, class: container_options[:class]) do
7
+ concat(header) if options[:title]
8
+ concat(body(&block)) if options[:body]
9
+ block.call(self) if block.present? && !options[:body]
10
+ end
11
+ end
12
+
13
+ def header(title_or_opts = nil, opts = {})
14
+ opts = title_or_opts if title_or_opts.is_a? Hash
15
+ opts[:title] = title_or_opts if title_or_opts.is_a? String
16
+ return if no_header?(opts[:title], block_given?)
17
+ content_tag(:div, class: "card-header #{opts[:class]}") do
18
+ yield if block_given?
19
+ concat(title_tag(opts[:title])) unless block_given?
20
+ end
21
+ end
22
+
23
+ def body(content_or_options = nil)
24
+ opts = content_or_options.is_a?(Hash) ? content_or_options : {}
25
+ content = content_or_options.is_a?(String) ? content_or_options : nil
26
+ content_tag(:div, class: body_class(opts)) do
27
+ yield if block_given?
28
+ concat(content) if content.present?
29
+ end
30
+ end
31
+
32
+ def footer(content = nil, opts = {})
33
+ opts, content = content, nil if content.is_a? Hash
34
+ content_tag(:div, class: "card-footer #{opts[:class]}") do
35
+ yield if block_given?
36
+ concat(content) unless block_given? || content.present?
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def parse_options(options)
43
+ super(options)
44
+ options[:body] = {} if options[:body].is_a?(TrueClass)
45
+ end
46
+
47
+ def assign_and_validate_style; end
48
+
49
+ def no_header?(title, has_block)
50
+ title.blank? && options[:title].blank? && !has_block
51
+ end
52
+
53
+ def title_tag(title)
54
+ title ||= options[:title]
55
+ return title unless header_options[:tag]
56
+ content_tag(header_options[:tag], title,
57
+ class: header_options[:class])
58
+ end
59
+
60
+ def container_options
61
+ default = { class: 'card ' }
62
+ default[:class] << options.delete(:class).to_s
63
+ default.merge(options)
64
+ end
65
+
66
+ def header_options
67
+ default = { class: 'my-0', tag: :h5 }
68
+ default.merge(options[:header] || {})
69
+ end
70
+
71
+ def body_class(opts)
72
+ classes = ['card-body']
73
+ classes << (options[:body] || {}).delete(:class)
74
+ classes << (opts || {}).delete(:class)
75
+ classes.compact.join(' ')
76
+ end
77
+
78
+ def footer_options
79
+ default = { class: 'card-footer' }
80
+ default.merge(options[:footer] || {})
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,25 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Modal < Component
5
+ class Body < Component
6
+ def to_html
7
+ content_tag(:div, options) { block.call(self) if block.present? }
8
+ end
9
+
10
+ protected
11
+
12
+ def inject_class_name_to_options
13
+ options[:class] = "modal-body #{options[:class]}"
14
+ options[:class].strip!
15
+ end
16
+
17
+ def parse_options(options)
18
+ super
19
+ inject_class_name_to_options
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Modal < Component
5
+ class Footer < Component
6
+ def to_html
7
+ content_tag(:div, options) { block.call(self) if block.present? }
8
+ end
9
+
10
+ def close_button(label, close_options = {})
11
+ close_options = close_button_default_options.merge(close_options)
12
+ close_options[:label] = label
13
+ Button.new(view, close_options).to_html
14
+ end
15
+
16
+ protected
17
+
18
+ def close_button_default_options
19
+ { data: { dismiss: 'modal' }, style: :secondary, type: :button }
20
+ end
21
+
22
+ def inject_class_name_to_options
23
+ options[:class] = "modal-footer #{options[:class]}"
24
+ options[:class].strip!
25
+ end
26
+
27
+ def parse_options(options)
28
+ super
29
+ inject_class_name_to_options
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,48 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class Modal < Component
5
+ class Header < Component
6
+ def to_html
7
+ content_tag(:div, class: 'modal-header') do
8
+ concat(block.call(self)) if block.present?
9
+ concat title if block.blank?
10
+ concat close_button if dismiss
11
+ end
12
+ end
13
+
14
+ def title(title = nil)
15
+ title ||= title_text
16
+ return title unless title_options[:tag]
17
+ content_tag(title_options[:tag], title,
18
+ class: title_options[:class])
19
+ end
20
+
21
+ def close_button
22
+ Button::Close.new(view, {}).to_html
23
+ end
24
+
25
+ protected
26
+
27
+ attr_reader :title_text, :dismiss
28
+
29
+ def title_options
30
+ { tag: :h5, class: 'modal-title' }
31
+ end
32
+
33
+ def inject_class_name_to_options
34
+ options[:class] = "modal-header #{options[:class]}"
35
+ options[:class].strip!
36
+ end
37
+
38
+ def parse_options(options)
39
+ super
40
+ @title_text = options.delete(:title)
41
+ @dismiss = options.delete(:dismiss)
42
+ inject_class_name_to_options
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,96 @@
1
+ require_relative 'button'
2
+ require_relative 'button/close'
3
+ require_relative 'modal/body'
4
+ require_relative 'modal/footer'
5
+ require_relative 'modal/header'
6
+
7
+ module Bootstrap
8
+ module ViewHelpers
9
+ module Components
10
+ class Modal < Component
11
+ def to_html
12
+ content_tag(:div, modal_options) do
13
+ content_tag(:div, dialog_options) do
14
+ content_tag(:div, content_options) { content_to_html(&block) }
15
+ end
16
+ end
17
+ end
18
+
19
+ def header(title_or_options = nil, header_options = {}, &block)
20
+ header_options = title_or_options || {} if block.present?
21
+ title_or_options = nil if block.blank?
22
+ header_options[:title] ||= title_or_options || title_text
23
+ header_options[:dismiss] ||= dismiss
24
+ Header.new(view, header_options, &block).to_html
25
+ end
26
+
27
+ def body(body_options = {}, &block)
28
+ Body.new(view, body_options, &block).to_html
29
+ end
30
+
31
+ def footer(footer_options = {}, &block)
32
+ Footer.new(view, footer_options, &block).to_html
33
+ end
34
+
35
+ protected
36
+
37
+ attr_reader :title_text, :centered, :dismiss, :fade, :show, :size
38
+
39
+ def content_to_html(&block)
40
+ capture do
41
+ concat(header) if title_text
42
+ concat(body(&block)) if render_body?
43
+ yield(self) if block.present? && !render_body?
44
+ end
45
+ end
46
+
47
+ def random_uid
48
+ SecureRandom.urlsafe_base64
49
+ end
50
+
51
+ def dialog_options
52
+ class_name = "modal-dialog#{' modal-dialog-centered' if centered}"
53
+ { class: class_name, role: 'document' }
54
+ end
55
+
56
+ def content_options
57
+ { class: 'modal-content' }
58
+ end
59
+
60
+ def modal_options
61
+ defaults = { id: "modal_#{random_uid}", tabindex: -1, role: 'dialog' }
62
+ defaults.merge(options)
63
+ end
64
+
65
+ def header_options
66
+ { tag: :h5, class: 'modal-title' }
67
+ end
68
+
69
+ def render_body?
70
+ @render_body ||= options.delete(:body)
71
+ end
72
+
73
+ def inject_class_name_to_options
74
+ options[:class] = "#{options[:class]} modal#{fade}#{show}"
75
+ options[:class] << " modal-#{size}" if size.present?
76
+ options[:class].strip!
77
+ end
78
+
79
+ def parse_html_options
80
+ @centered = options.delete(:centered)
81
+ @dismiss = options.delete(:dismiss)
82
+ @fade = ' fade' if options[:fade].nil? || options.delete(:fade)
83
+ @show = ' show' if options.delete(:show)
84
+ @size = options.delete(:size)
85
+ end
86
+
87
+ def parse_options(options)
88
+ super
89
+ @title_text = options.delete(:title)
90
+ parse_html_options
91
+ inject_class_name_to_options
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,11 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ module Components
4
+ class StyleNotFoundError < StandardError
5
+ def initialize(message)
6
+ super("Contextual style \"#{message}\" does not exist")
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,22 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ class ContextualClasses
4
+ PRIMARY = 'primary'.freeze
5
+ SECONDARY = 'secondary'.freeze
6
+ SUCCESS = 'success'.freeze
7
+ INFO = 'info'.freeze
8
+ WARNING = 'warning'.freeze
9
+ DANGER = 'danger'.freeze
10
+ LIGHT = 'light'.freeze
11
+ DARK = 'dark'.freeze
12
+ LINK = 'link'.freeze
13
+
14
+ ALL = [PRIMARY, SECONDARY, SUCCESS, INFO, WARNING, DANGER, LIGHT, DARK,
15
+ LINK]
16
+
17
+ def self.valid?(style)
18
+ ALL.include?(style.to_s.freeze)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace Bootstrap::ViewHelpers
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Bootstrap
2
+ module ViewHelpers
3
+ VERSION = '0.0.2'.freeze
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'securerandom'
2
+ require 'bootstrap/view_helpers/engine'
3
+
4
+ require 'bootstrap/view_helpers/component'
5
+ require 'bootstrap/view_helpers/components/accordion'
6
+ require 'bootstrap/view_helpers/components/alert'
7
+ require 'bootstrap/view_helpers/components/badge'
8
+ require 'bootstrap/view_helpers/components/button'
9
+ require 'bootstrap/view_helpers/components/card'
10
+ require 'bootstrap/view_helpers/components/modal'
11
+
12
+ module Bootstrap
13
+ module ViewHelpers
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :bootstrap-view_helpers do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bootstrap-view_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Rodrigo Castro Azevedo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.52'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.52'
41
+ description: 'PAFA: Description of BootstrapViewHelpers.'
42
+ email:
43
+ - rod.c.azevedo@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.rdoc
50
+ - Rakefile
51
+ - app/assets/javascripts/bootstrap/view_helpers/application.js
52
+ - app/assets/stylesheets/bootstrap/view_helpers/application.css
53
+ - app/helpers/bootstrap/view_helpers/accordion_helper.rb
54
+ - app/helpers/bootstrap/view_helpers/alert_helper.rb
55
+ - app/helpers/bootstrap/view_helpers/application_helper.rb
56
+ - app/helpers/bootstrap/view_helpers/badge_helper.rb
57
+ - app/helpers/bootstrap/view_helpers/button_helper.rb
58
+ - app/helpers/bootstrap/view_helpers/card_helper.rb
59
+ - app/helpers/bootstrap/view_helpers/modal_helper.rb
60
+ - lib/bootstrap/view_helpers.rb
61
+ - lib/bootstrap/view_helpers/component.rb
62
+ - lib/bootstrap/view_helpers/components/accordion.rb
63
+ - lib/bootstrap/view_helpers/components/accordion/button.rb
64
+ - lib/bootstrap/view_helpers/components/accordion/card.rb
65
+ - lib/bootstrap/view_helpers/components/alert.rb
66
+ - lib/bootstrap/view_helpers/components/badge.rb
67
+ - lib/bootstrap/view_helpers/components/button.rb
68
+ - lib/bootstrap/view_helpers/components/button/button_type_not_found_error.rb
69
+ - lib/bootstrap/view_helpers/components/button/checkbox.rb
70
+ - lib/bootstrap/view_helpers/components/button/close.rb
71
+ - lib/bootstrap/view_helpers/components/button/link.rb
72
+ - lib/bootstrap/view_helpers/components/button/radio.rb
73
+ - lib/bootstrap/view_helpers/components/card.rb
74
+ - lib/bootstrap/view_helpers/components/modal.rb
75
+ - lib/bootstrap/view_helpers/components/modal/body.rb
76
+ - lib/bootstrap/view_helpers/components/modal/footer.rb
77
+ - lib/bootstrap/view_helpers/components/modal/header.rb
78
+ - lib/bootstrap/view_helpers/components/style_not_found_error.rb
79
+ - lib/bootstrap/view_helpers/contextual_classes.rb
80
+ - lib/bootstrap/view_helpers/engine.rb
81
+ - lib/bootstrap/view_helpers/version.rb
82
+ - lib/tasks/bootstrap/view_helpers_tasks.rake
83
+ homepage: http://github.com
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.5.1
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: 'PAFA: Summary of BootstrapViewHelpers.'
107
+ test_files: []