Skip to content

Commit

Permalink
Refactor to use newly merged separate files and general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
simmerz committed Feb 18, 2019
1 parent 0865a25 commit c3c1893
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 45 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
### New features

* Your contribution here!
* [#514](https://github.com/bootstrap-ruby/bootstrap_form/pull/514): Add support for BS 4.2 switches - [@simmerz](https://github.com/simmerz)
* [#508] Support `rich_text_area` AKA the Trix editor on Rails 6+.
* [#518] Move all inputs to separate, more maintainable files.
* [#514](https://github.com/bootstrap-ruby/bootstrap_form/pull/514): Add support for BS 4.2 switches - [@simmerz](https://github.com/simmerz)

### Bugfixes

Expand Down
100 changes: 56 additions & 44 deletions lib/bootstrap_form/inputs/check_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,75 @@ module CheckBox
extend ActiveSupport::Concern
include Base

# rubocop:disable Metrics/BlockLength
included do
def check_box_with_bootstrap(name, options={}, checked_value="1", unchecked_value="0", &block)
options = options.symbolize_keys!
check_box_options = options.except(:label, :label_class, :error_message, :help,
check_box_options = options.except(:class, :label, :label_class, :error_message, :help,
:inline, :custom, :hide_label, :skip_label, :wrapper_class)
check_box_classes = [check_box_options[:class]]
check_box_classes << "position-static" if options[:skip_label] || options[:hide_label]
check_box_classes << "is-invalid" if has_error?(name)
check_box_options[:class] = check_box_classes(name, options)

label_classes = [options[:label_class]]
label_classes << hide_class if options[:hide_label]

if options[:custom]
check_box_options[:class] = (["custom-control-input"] + check_box_classes).compact.join(" ")
wrapper_class = ["custom-control", "custom-checkbox"]
wrapper_class.append("custom-control-inline") if layout_inline?(options[:inline])
label_class = label_classes.prepend("custom-control-label").compact.join(" ")
else
check_box_options[:class] = (["form-check-input"] + check_box_classes).compact.join(" ")
wrapper_class = ["form-check"]
wrapper_class.append("form-check-inline") if layout_inline?(options[:inline])
label_class = label_classes.prepend("form-check-label").compact.join(" ")
content_tag(:div, class: check_box_wrapper_class(options)) do
html = check_box_without_bootstrap(name, check_box_options, checked_value, unchecked_value)
html.concat(check_box_label(name, options, checked_value, &block)) unless options[:skip_label]
html.concat(generate_error(name)) if options[:error_message]
html
end
end

checkbox_html = check_box_without_bootstrap(name, check_box_options, checked_value, unchecked_value)
label_content = block_given? ? capture(&block) : options[:label]
label_description = label_content || (object && object.class.human_attribute_name(name)) || name.to_s.humanize
bootstrap_alias :check_box
end

label_name = name
# label's `for` attribute needs to match checkbox tag's id,
# IE sanitized value, IE
# https://github.com/rails/rails/blob/5-0-stable/actionview/lib/action_view/helpers/tags/base.rb#L123-L125
if options[:multiple]
label_name =
"#{name}_#{checked_value.to_s.gsub(/\s/, '_').gsub(/[^-[[:word:]]]/, '').mb_chars.downcase}"
end
private

def check_box_label(name, options, checked_value, &block)
content = block_given? ? capture(&block) : options[:label]
description = content || (object && object.class.human_attribute_name(name)) || name.to_s.humanize
label_name = if options[:multiple]
check_box_value(name, checked_value)
else
name
end
label_options = { class: check_box_label_class(options) }
label_options[:for] = options[:id] if options[:id].present?
label(label_name, description, label_options)
end

label_options = { class: label_class }
label_options[:for] = options[:id] if options[:id].present?
def check_box_value(name, value)
# label's `for` attribute needs to match checkbox tag's id,
# IE sanitized value, IE
# https://github.com/rails/rails/blob/5-0-stable/actionview/lib/action_view/helpers/tags/base.rb#L123-L125
"#{name}_#{value.to_s.gsub(/\s/, '_').gsub(/[^-[[:word:]]]/, '').mb_chars.downcase}"
end

wrapper_class.append(options[:wrapper_class]) if options[:wrapper_class]
def check_box_classes(name, options)
classes = [options[:class]]
classes << (options[:custom] ? "custom-control-input" : "form-check-input")
classes << "is-invalid" if has_error?(name)
classes << "position-static" if options[:skip_label] || options[:hide_label]
classes.flatten.compact
end

content_tag(:div, class: wrapper_class.compact.join(" ")) do
html = if options[:skip_label]
checkbox_html
else
checkbox_html.concat(label(label_name, label_description, label_options))
end
html.concat(generate_error(name)) if options[:error_message]
html
end
end
def check_box_label_class(options)
classes = []
classes << (options[:custom] ? "custom-control-label" : "form-check-label")
classes << options[:label_class]
classes << hide_class if options[:hide_label]
classes.flatten.compact
end

bootstrap_alias :check_box
def check_box_wrapper_class(options)
classes = []
if options[:custom]
classes << "custom-control"
classes << (options[:custom] == :switch ? "custom-switch" : "custom-checkbox")
classes << "custom-control-inline" if layout_inline?(options[:inline])
else
classes << "form-check"
classes << "form-check-inline" if layout_inline?(options[:inline])
end
classes << options[:wrapper_class] if options[:wrapper_class].present?
classes.flatten.compact
end
# rubocop:enable Metrics/BlockLength
end
end
end

0 comments on commit c3c1893

Please # to comment.