diff --git a/CHANGELOG.md b/CHANGELOG.md index e6f5f2a66..2cfad8460 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,13 @@ Bugfixes: - Your contribution here! + - form-control-danger is replaced with is-invalid for bootstrap 4.0.0.beta3 + - form-control-feedback is replaced with invalid-feedback for bootstrap 4.0.0.beta3 + - help texts are rendered with tag instead of tag, i.e. like in bootstrap 4.0.0.beta3 Features: - Your contribution here! + - new `custom: true` option for radio buttons and check boxes according to bootstrap 4.0.0.beta3 - Allow HTML in help translations by using the '_html' suffix on the key - [@unikitty37](https://github.com/unikitty37) * [#325](https://github.com/bootstrap-ruby/rails-bootstrap-forms/pull/325): Support :prepend and :append for the `select` helper - [@donv](https://github.com/donv). diff --git a/README.md b/README.md index a6102cc70..f6a623606 100644 --- a/README.md +++ b/README.md @@ -477,6 +477,19 @@ The `layout` can be overridden per field: <% end %> ``` +### Custom Form Element Styles + +The `custom` option can be used to replace the browser default styles for check boxes and radio buttons with dedicated Bootstrap styled form elements. Here's an example: + +```erb +<%= bootstrap_form_for(@user) do |f| %> + <%= f.email_field :email %> + <%= f.password_field :password %> + <%= f.check_box :remember_me, custom: true %> + <%= f.submit "Log In" %> +<% end %> +``` + ## Validation & Errors ### Inline Errors @@ -488,8 +501,8 @@ div (field_with_errors), but this behavior is suppressed. Here's an example: ```html
- - + + can't be blank
``` diff --git a/lib/bootstrap_form/form_builder.rb b/lib/bootstrap_form/form_builder.rb index 74871db44..d69d88775 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -114,12 +114,23 @@ def time_zone_select_with_bootstrap(method, priority_zones = nil, options = {}, 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, :help, :inline) - check_box_options[:class] = ["form-check-input", check_box_options[:class]].compact.join(' ') + check_box_options = options.except(:label, :label_class, :help, :inline, :custom) + if options[:custom] + validation = nil + validation = "is-invalid" if has_error?(name) + check_box_options[:class] = ["custom-control-input", validation, check_box_options[:class]].compact.join(' ') + else + check_box_options[:class] = ["form-check-input", check_box_options[:class]].compact.join(' ') + end - html = check_box_without_bootstrap(name, check_box_options, checked_value, unchecked_value) + checkbox_html = check_box_without_bootstrap(name, check_box_options, checked_value, unchecked_value) label_content = block_given? ? capture(&block) : options[:label] - html.concat(" ").concat(label_content || (object && object.class.human_attribute_name(name)) || name.to_s.humanize) + label_description = label_content || (object && object.class.human_attribute_name(name)) || name.to_s.humanize + if options[:custom] + html = label_description + else + html = checkbox_html.concat(" ").concat(label_description) + end label_name = name # label's `for` attribute needs to match checkbox tag's id, @@ -130,15 +141,23 @@ def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_ "#{name}_#{checked_value.to_s.gsub(/\s/, "_").gsub(/[^-\w]/, "").downcase}" end - disabled_class = " disabled" if options[:disabled] - label_class = options[:label_class] + label_class = options[:label_class] - if options[:inline] - label_class = " #{label_class}" if label_class - label(label_name, html, class: "form-check-inline#{disabled_class}#{label_class}") - else - content_tag(:div, class: "form-check#{disabled_class}") do - label(label_name, html, class: ["form-check-label", label_class].compact.join(" ")) + if options[:custom] + div_class = ["custom-control", "custom-checkbox"] + div_class.append("custom-control-inline") if options[:inline] + content_tag(:div, class: div_class.compact.join(" ")) do + checkbox_html.concat(label(label_name, html, class: ["custom-control-label", label_class].compact.join(" "))) + end + else + disabled_class = " disabled" if options[:disabled] + if options[:inline] + label_class = " #{label_class}" if label_class + label(label_name, html, class: "form-check-inline#{disabled_class}#{label_class}") + else + content_tag(:div, class: "form-check#{disabled_class}") do + label(label_name, html, class: ["form-check-label", label_class].compact.join(" ")) + end end end end @@ -147,19 +166,33 @@ def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_ def radio_button_with_bootstrap(name, value, *args) options = args.extract_options!.symbolize_keys! - args << options.except(:label, :label_class, :help, :inline) - - html = radio_button_without_bootstrap(name, value, *args) + " " + options[:label] + radio_options = options.except(:label, :label_class, :help, :inline, :custom) + radio_options[:class] = ["custom-control-input", options[:class]].compact.join(' ') if options[:custom] + args << radio_options + radio_html = radio_button_without_bootstrap(name, value, *args) + if options[:custom] + html = options[:label] + else + html = radio_html.concat(" ").concat(options[:label]) + end disabled_class = " disabled" if options[:disabled] label_class = options[:label_class] - if options[:inline] - label_class = " #{label_class}" if label_class - label(name, html, class: "radio-inline#{disabled_class}#{label_class}", value: value) - else - content_tag(:div, class: "radio#{disabled_class}") do - label(name, html, value: value, class: label_class) + if options[:custom] + div_class = ["custom-control", "custom-radio"] + div_class.append("custom-control-inline") if options[:inline] + content_tag(:div, class: div_class.compact.join(" ")) do + radio_html.concat(label(name, html, value: value, class: ["custom-control-label", label_class].compact.join(" "))) + end + else + if options[:inline] + label_class = " #{label_class}" if label_class + label(name, html, class: "radio-inline#{disabled_class}#{label_class}", value: value) + else + content_tag(:div, class: "radio#{disabled_class}") do + label(name, html, value: value, class: label_class) + end end end end @@ -200,7 +233,6 @@ def form_group(*args, &block) options[:class] = ["form-group", options[:class]].compact.join(' ') options[:class] << " row" if get_group_layout(options[:layout]) == :horizontal - options[:class] << " #{error_class}" if has_error?(name) options[:class] << " #{feedback_class}" if options[:icon] content_tag(:div, options.except(:id, :label, :help, :icon, :label_col, :control_col, :layout)) do @@ -225,7 +257,7 @@ def form_group(*args, &block) def fields_for_with_bootstrap(record_name, record_object = nil, fields_options = {}, &block) fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options? fields_options[:layout] ||= options[:layout] - fields_options[:label_col] = fields_options[:label_col].present? ? "#{fields_options[:label_col]} #{label_class}" : options[:label_col] + fields_options[:label_col] = fields_options[:label_col].present? ? "#{fields_options[:label_col]}" : options[:label_col] fields_options[:control_col] ||= options[:control_col] fields_options[:inline_errors] ||= options[:inline_errors] fields_options[:label_errors] ||= options[:label_errors] @@ -264,14 +296,6 @@ def control_class "form-control" end - def label_class - "form-control-label" - end - - def error_class - "has-danger" - end - def feedback_class "has-feedback" end @@ -315,7 +339,7 @@ def form_group_builder(method, options, html_options = nil) css_options = html_options || options control_classes = css_options.delete(:control_class) { control_class } css_options[:class] = [control_classes, css_options[:class]].compact.join(" ") - css_options[:class] << " form-control-danger" if has_error?(method) + css_options[:class] << " is-invalid" if has_error?(method) options = convert_form_tag_options(method, options) if acts_like_form_tag @@ -373,7 +397,7 @@ def convert_form_tag_options(method, options = {}) def generate_label(id, name, options, custom_label_col, group_layout) options[:for] = id if acts_like_form_tag - classes = [options[:class], label_class] + classes = [options[:class]] classes << (custom_label_col || label_col) if get_group_layout(group_layout) == :horizontal unless options.delete(:skip_required) classes << "required" if required_attribute?(object, name) @@ -394,18 +418,20 @@ def generate_label(id, name, options, custom_label_col, group_layout) def generate_help(name, help_text) if has_error?(name) && inline_errors help_text = get_error_messages(name) - help_klass = 'form-control-feedback' + help_klass = 'invalid-feedback' + help_tag = :div end return if help_text == false help_klass ||= 'form-text text-muted' help_text ||= get_help_text_by_i18n_key(name) + help_tag ||= :small - content_tag(:span, help_text, class: help_klass) if help_text.present? + content_tag(help_tag, help_text, class: help_klass) if help_text.present? end def generate_icon(icon) - content_tag(:span, "", class: "glyphicon glyphicon-#{icon} form-control-feedback") + content_tag(:span, "", class: "glyphicon glyphicon-#{icon} invalid-feedback") end def get_error_messages(name) diff --git a/test/bootstrap_checkbox_test.rb b/test/bootstrap_checkbox_test.rb index da2df251c..78f192831 100644 --- a/test/bootstrap_checkbox_test.rb +++ b/test/bootstrap_checkbox_test.rb @@ -54,28 +54,28 @@ def setup test 'collection_check_boxes renders the form_group correctly' do collection = [Address.new(id: 1, street: 'Foobar')] - expected = %{
With a help!
} + expected = %{
With a help!
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, :id, :street, label: 'This is a checkbox collection', help: 'With a help!') end test 'collection_check_boxes renders multiple checkboxes correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, :id, :street) end test 'collection_check_boxes renders inline checkboxes correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, :id, :street, inline: true) end test 'collection_check_boxes renders with checked option correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, :id, :street, checked: 1) assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, :id, :street, checked: collection.first) @@ -83,7 +83,7 @@ def setup test 'collection_check_boxes renders with multiple checked options correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, :id, :street, checked: [1, 2]) assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, :id, :street, checked: collection) @@ -91,42 +91,42 @@ def setup test 'collection_check_boxes sanitizes values when generating label `for`' do collection = [Address.new(id: 1, street: 'Foo St')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, :street, :street) end test 'collection_check_boxes renders multiple checkboxes with labels defined by Proc :text_method correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, :id, Proc.new { |a| a.street.reverse }) end test 'collection_check_boxes renders multiple checkboxes with values defined by Proc :value_method correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, Proc.new { |a| "address_#{a.id}" }, :street) end test 'collection_check_boxes renders multiple checkboxes with labels defined by lambda :text_method correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, :id, lambda { |a| a.street.reverse }) end test 'collection_check_boxes renders multiple checkboxes with values defined by lambda :value_method correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, lambda { |a| "address_#{a.id}" }, :street) end test 'collection_check_boxes renders with checked option correctly with Proc :value_method' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, Proc.new { |a| "address_#{a.id}" }, :street, checked: "address_1") assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, Proc.new { |a| "address_#{a.id}" }, :street, checked: collection.first) @@ -134,11 +134,29 @@ def setup test 'collection_check_boxes renders with multiple checked options correctly with lambda :value_method' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, lambda { |a| "address_#{a.id}" }, :street, checked: ["address_1", "address_2"]) assert_equivalent_xml expected, @builder.collection_check_boxes(:misc, collection, lambda { |a| "address_#{a.id}" }, :street, checked: collection) end + test "check_box is wrapped correctly with custom option set" do + expected = %{
} + assert_equivalent_xml expected, @builder.check_box(:terms, {label: 'I agree to the terms', custom: true}) + end + + test "check_box is wrapped correctly with custom and inline options set" do + expected = %{
} + assert_equivalent_xml expected, @builder.check_box(:terms, {label: 'I agree to the terms', inline: true, custom: true}) + end + test "check_box is wrapped correctly with custom and disabled options set" do + expected = %{
} + assert_equivalent_xml expected, @builder.check_box(:terms, {label: 'I agree to the terms', disabled: true, custom: true}) + end + + test "check_box is wrapped correctly with custom, inline and disabled options set" do + expected = %{
} + assert_equivalent_xml expected, @builder.check_box(:terms, {label: 'I agree to the terms', inline: true, disabled: true, custom: true}) + end end diff --git a/test/bootstrap_fields_test.rb b/test/bootstrap_fields_test.rb index b7d9445c9..021c8a0e5 100644 --- a/test/bootstrap_fields_test.rb +++ b/test/bootstrap_fields_test.rb @@ -8,32 +8,32 @@ def setup end test "color fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.color_field(:misc) end test "date fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.date_field(:misc) end test "date time fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.datetime_field(:misc) end test "date time local fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.datetime_local_field(:misc) end test "email fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.email_field(:misc) end test "file fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.file_field(:misc) end @@ -43,58 +43,58 @@ def setup end test "month local fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.month_field(:misc) end test "number fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.number_field(:misc) end test "password fields are wrapped correctly" do - expected = %{
A good password should be at least six characters long
} + expected = %{
A good password should be at least six characters long
} assert_equivalent_xml expected, @builder.password_field(:password) end test "phone/telephone fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.phone_field(:misc) assert_equivalent_xml expected, @builder.telephone_field(:misc) end test "range fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.range_field(:misc) end test "search fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.search_field(:misc) end test "text areas are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.text_area(:comments) end test "text fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.text_field(:email) end test "time fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.time_field(:misc) end test "url fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.url_field(:misc) end test "week fields are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.week_field(:misc) end @@ -107,7 +107,7 @@ def setup end end - expected = %{
} + expected = %{
} assert_equivalent_xml expected, output end @@ -120,7 +120,7 @@ def setup end end - expected = %{
} + expected = %{
} assert_equivalent_xml expected, output end @@ -133,7 +133,7 @@ def setup end end - expected = %{
} + expected = %{
} assert_equivalent_xml expected, output end @@ -146,7 +146,7 @@ def setup end end - expected = %{
} + expected = %{
} assert_equivalent_xml expected, output end end diff --git a/test/bootstrap_form_group_test.rb b/test/bootstrap_form_group_test.rb index 5b2ac0acb..b41407e54 100644 --- a/test/bootstrap_form_group_test.rb +++ b/test/bootstrap_form_group_test.rb @@ -8,32 +8,32 @@ def setup end test "changing the label text via the label option parameter" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.text_field(:email, label: 'Email Address') end test "changing the label text via the html_options label hash" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.text_field(:email, label: {text: 'Email Address'}) end test "hiding a label" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.text_field(:email, hide_label: true) end test "adding a custom label class via the label_class parameter" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.text_field(:email, label_class: 'btn') end test "adding a custom label class via the html_options label hash" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.text_field(:email, label: {class: 'btn'}) end test "adding a custom label and changing the label text via the html_options label hash" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.text_field(:email, label: {class: 'btn', text: "Email Address"}) end @@ -43,22 +43,22 @@ def setup end test "preventing a label from having the required class" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.text_field(:email, skip_required: true) end test "adding prepend text" do - expected = %{
@
} + expected = %{
@
} assert_equivalent_xml expected, @builder.text_field(:email, prepend: '@') end test "adding append text" do - expected = %{
.00
} + expected = %{
.00
} assert_equivalent_xml expected, @builder.text_field(:email, append: '.00') end test "append and prepend button" do - prefix = %{
} + prefix = %{
} field = %{} button_prepend = %{} button_append = %{} @@ -73,22 +73,22 @@ def setup end test "adding both prepend and append text" do - expected = %{
$
.00
} + expected = %{
$
.00
} assert_equivalent_xml expected, @builder.text_field(:email, prepend: '$', append: '.00') end test "help messages for default forms" do - expected = %{
This is required
} + expected = %{
This is required
} assert_equivalent_xml expected, @builder.text_field(:email, help: 'This is required') end test "help messages for horizontal forms" do - expected = %{
This is required
} + expected = %{
This is required
} assert_equivalent_xml expected, @horizontal_builder.text_field(:email, help: "This is required") end test "help messages to look up I18n automatically" do - expected = %{
A good password should be at least six characters long
} + expected = %{
A good password should be at least six characters long
} assert_equivalent_xml expected, @builder.text_field(:password) end @@ -111,7 +111,7 @@ def setup end test "help messages to ignore translation when user disables help" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.text_field(:password, help: false) end @@ -120,7 +120,7 @@ def setup %{

Bar

}.html_safe end - expected = %{

Bar

} + expected = %{

Bar

} assert_equivalent_xml expected, output end @@ -138,7 +138,7 @@ def setup %{

Bar

}.html_safe end - expected = %{

Bar

} + expected = %{

Bar

} assert_equivalent_xml expected, output end @@ -165,7 +165,7 @@ def setup %{

Bar

}.html_safe end - expected = %{

Bar

} + expected = %{

Bar

} assert_equivalent_xml expected, output end @@ -177,12 +177,12 @@ def setup %{

Bar

}.html_safe end - expected = %{

Bar

} + expected = %{

Bar

can't be blank, is too short (minimum is 5 characters)
} assert_equivalent_xml expected, output end test "adds class to wrapped form_group by a field" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.search_field(:misc, wrapper_class: 'none-margin') end @@ -190,7 +190,7 @@ def setup @user.email = nil @user.valid? - expected = %{
} + expected = %{
can't be blank, is too short (minimum is 5 characters)
} assert_equivalent_xml expected, @builder.email_field(:email, wrapper_class: 'none-margin') end @@ -202,7 +202,7 @@ def setup f.text_field(:email, help: 'This is required', wrapper_class: 'none-margin') end - expected = %{
} + expected = %{
can't be blank, is too short (minimum is 5 characters)
} assert_equivalent_xml expected, output end @@ -225,7 +225,7 @@ def setup end test "adding an icon to a field" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.email_field(:misc, icon: 'ok') end @@ -237,17 +237,17 @@ def setup output = output + @horizontal_builder.text_field(:email) - expected = %{
Hallo
} + expected = %{
Hallo
} assert_equivalent_xml expected, output end test "adds data-attributes (or any other options) to wrapper" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.search_field(:misc, wrapper: { data: { foo: 'bar' } }) end test "passing options to a form control get passed through" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.text_field(:email, autofocus: true) end @@ -256,12 +256,12 @@ def setup nil end - expected = %{
} + expected = %{
} assert_equivalent_xml expected, output end test "custom form group layout option" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, bootstrap_form_for(@user, layout: :horizontal) { |f| f.email_field :email, layout: :inline } end @@ -284,7 +284,7 @@ def setup end test ":input_group_class should apply to input-group" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.email_field(:email, append: @builder.primary('Subscribe'), input_group_class: 'input-group-lg') end end diff --git a/test/bootstrap_form_test.rb b/test/bootstrap_form_test.rb index ff286ecc3..cf4fb5c60 100644 --- a/test/bootstrap_form_test.rb +++ b/test/bootstrap_form_test.rb @@ -18,12 +18,12 @@ def setup end test "horizontal-style forms" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, bootstrap_form_for(@user, layout: :horizontal) { |f| f.email_field :email } end test "existing styles aren't clobbered when specifying a form style" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, bootstrap_form_for(@user, layout: :horizontal, html: { class: "my-style" }) { |f| f.email_field :email } end @@ -33,12 +33,12 @@ def setup end test "bootstrap_form_tag acts like a form tag" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, bootstrap_form_tag(url: '/users') { |f| f.text_field :email, label: "Your Email" } end test "bootstrap_form_tag does not clobber custom options" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, bootstrap_form_tag(url: '/users') { |f| f.text_field :email, name: 'NAME', id: "ID" } end @@ -56,7 +56,7 @@ def setup @user.email = nil @user.valid? - expected = %{
} + expected = %{
} assert_equivalent_xml expected, bootstrap_form_for(@user, label_errors: true) { |f| f.text_field :email } end @@ -64,7 +64,7 @@ def setup @user.email = nil @user.valid? - expected = %{
can't be blank, is too short (minimum is 5 characters)
} + expected = %{
can't be blank, is too short (minimum is 5 characters)
} assert_equivalent_xml expected, bootstrap_form_for(@user, label_errors: true, inline_errors: true) { |f| f.text_field :email } end @@ -74,7 +74,7 @@ def setup @user.email = nil @user.valid? - expected = %{
can't be blank, is too short (minimum is 5 characters)
} + expected = %{
can't be blank, is too short (minimum is 5 characters)
} assert_equivalent_xml expected, bootstrap_form_for(@user, label_errors: true, inline_errors: true) { |f| f.text_field :email } I18n.backend.store_translations(:en, {activerecord: {attributes: {user: {email: nil}}}}) @@ -147,7 +147,7 @@ def setup end test "custom label width for horizontal forms" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, bootstrap_form_for(@user, layout: :horizontal) { |f| f.email_field :email, label_col: 'col-sm-1' } end @@ -157,7 +157,7 @@ def setup end test "custom input width for horizontal forms" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, bootstrap_form_for(@user, layout: :horizontal) { |f| f.email_field :email, control_col: 'col-sm-5' } end @@ -169,7 +169,7 @@ def setup f.text_field(:email, help: 'This is required') end - expected = %{
} + expected = %{
can't be blank, is too short (minimum is 5 characters)
} assert_equivalent_xml expected, output end @@ -181,7 +181,7 @@ def setup f.text_field(:email, help: 'This is required') end - expected = %{
} + expected = %{
can't be blank, is too short (minimum is 5 characters)
} assert_equivalent_xml expected, output end @@ -193,7 +193,7 @@ def setup f.text_field(:email, help: 'This is required') end - expected = %{
This is required
} + expected = %{
This is required
} assert_equivalent_xml expected, output end @@ -204,7 +204,7 @@ def setup f.text_field(:email) end - expected = %{
This is useful help
} + expected = %{
This is useful help
} assert_equivalent_xml expected, output I18n.backend.store_translations(:en, {activerecord: {help: {user: {email_html: nil}}}}) @@ -212,7 +212,7 @@ def setup test "allows the form object to be nil" do builder = BootstrapForm::FormBuilder.new :other_model, nil, self, {} - expected = %{
} + expected = %{
} assert_equivalent_xml expected, builder.text_field(:email) end diff --git a/test/bootstrap_other_components_test.rb b/test/bootstrap_other_components_test.rb index 0933c56f2..ea92ae609 100644 --- a/test/bootstrap_other_components_test.rb +++ b/test/bootstrap_other_components_test.rb @@ -10,7 +10,7 @@ def setup test "static control" do output = @horizontal_builder.static_control :email - expected = %{

steve@example.com

} + expected = %{

steve@example.com

} assert_equivalent_xml expected, output end @@ -19,7 +19,7 @@ def setup "this is a test" end - expected = %{

this is a test

} + expected = %{

this is a test

} assert_equivalent_xml expected, output end @@ -28,7 +28,7 @@ def setup "Custom Control" end - expected = %{

Custom Control

} + expected = %{

Custom Control

} assert_equivalent_xml expected, output end @@ -37,7 +37,7 @@ def setup "this is a test" end - expected = %{
this is a test
} + expected = %{
this is a test
} assert_equivalent_xml expected, output end @@ -46,7 +46,7 @@ def setup "this is a test" end - expected = %{
this is a test
} + expected = %{
this is a test
} assert_equivalent_xml expected, output end @@ -55,7 +55,7 @@ def setup "Custom Control" end - expected = %{
Custom Control
} + expected = %{
Custom Control
} assert_equivalent_xml expected, output end diff --git a/test/bootstrap_radio_button_test.rb b/test/bootstrap_radio_button_test.rb index 2ba720da1..cd4ca68b1 100644 --- a/test/bootstrap_radio_button_test.rb +++ b/test/bootstrap_radio_button_test.rb @@ -39,86 +39,104 @@ def setup test 'collection_radio_buttons renders the form_group correctly' do collection = [Address.new(id: 1, street: 'Foobar')] - expected = %{
With a help!
} + expected = %{
With a help!
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, :id, :street, label: 'This is a radio button collection', help: 'With a help!') end test 'collection_radio_buttons renders multiple radios correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, :id, :street) end test 'collection_radio_buttons renders inline radios correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, :id, :street, inline: true) end test 'collection_radio_buttons renders with checked option correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, :id, :street, checked: 1) end test 'collection_radio_buttons renders label defined by Proc correctly' do collection = [Address.new(id: 1, street: 'Foobar')] - expected = %{
With a help!
} + expected = %{
With a help!
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, :id, Proc.new { |a| a.street.reverse }, label: 'This is a radio button collection', help: 'With a help!') end test 'collection_radio_buttons renders value defined by Proc correctly' do collection = [Address.new(id: 1, street: 'Foobar')] - expected = %{
With a help!
} + expected = %{
With a help!
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, Proc.new { |a| "address_#{a.id}" }, :street, label: 'This is a radio button collection', help: 'With a help!') end test 'collection_radio_buttons renders multiple radios with label defined by Proc correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, :id, Proc.new { |a| a.street.reverse }) end test 'collection_radio_buttons renders multiple radios with value defined by Proc correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, Proc.new { |a| "address_#{a.id}" }, :street) end test 'collection_radio_buttons renders label defined by lambda correctly' do collection = [Address.new(id: 1, street: 'Foobar')] - expected = %{
With a help!
} + expected = %{
With a help!
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, :id, lambda { |a| a.street.reverse }, label: 'This is a radio button collection', help: 'With a help!') end test 'collection_radio_buttons renders value defined by lambda correctly' do collection = [Address.new(id: 1, street: 'Foobar')] - expected = %{
With a help!
} + expected = %{
With a help!
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, lambda { |a| "address_#{a.id}" }, :street, label: 'This is a radio button collection', help: 'With a help!') end test 'collection_radio_buttons renders multiple radios with label defined by lambda correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, :id, lambda { |a| a.street.reverse }) end test 'collection_radio_buttons renders multiple radios with value defined by lambda correctly' do collection = [Address.new(id: 1, street: 'Foo'), Address.new(id: 2, street: 'Bar')] - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_radio_buttons(:misc, collection, lambda { |a| "address_#{a.id}" }, :street) end + test "radio_button is wrapped correctly with custom option set" do + expected = %{
} + assert_equivalent_xml expected, @builder.radio_button(:misc, '1', {label: 'This is a radio button', custom: true}) + end + + test "radio_button is wrapped correctly with custom and inline options set" do + expected = %{
} + assert_equivalent_xml expected, @builder.radio_button(:misc, '1', {label: 'This is a radio button', inline: true, custom: true}) + end + + test "radio_button is wrapped correctly with custom and disabled options set" do + expected = %{
} + assert_equivalent_xml expected, @builder.radio_button(:misc, '1', {label: 'This is a radio button', disabled: true, custom: true}) + end + test "radio_button is wrapped correctly with custom, inline and disabled options set" do + expected = %{
} + assert_equivalent_xml expected, @builder.radio_button(:misc, '1', {label: 'This is a radio button', inline: true, disabled: true, custom: true}) + end end diff --git a/test/bootstrap_selects_test.rb b/test/bootstrap_selects_test.rb index 929130a82..a701a289b 100644 --- a/test/bootstrap_selects_test.rb +++ b/test/bootstrap_selects_test.rb @@ -8,34 +8,34 @@ def setup end test "time zone selects are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.time_zone_select(:misc) end test "selects are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.select(:status, [['activated', 1], ['blocked', 2]]) end test "bootstrap_specific options are handled correctly" do - expected = %{
Help!
} + expected = %{
Help!
} assert_equivalent_xml expected, @builder.select(:status, [['activated', 1], ['blocked', 2]], label: "My Status Label", help: "Help!" ) end test "selects with options are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.select(:status, [['activated', 1], ['blocked', 2]], prompt: "Please Select") end test "selects with both options and html_options are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.select(:status, [['activated', 1], ['blocked', 2]], { prompt: "Please Select" }, class: "my-select") end test 'selects with addons are wrapped correctly' do expected = <<-HTML.strip_heredoc
- +
Before
} + expected = %{
} select = @builder.select(:status) do content_tag(:option) { 'Option 1' } + content_tag(:option) { 'Option 2' } @@ -61,99 +61,99 @@ def setup end test "selects render labels properly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.select(:status, [['activated', 1], ['blocked', 2]], label: "User Status") end test "collection_selects are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_select(:status, [], :id, :name) end test "collection_selects with options are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_select(:status, [], :id, :name, prompt: "Please Select") end test "collection_selects with options and html_options are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.collection_select(:status, [], :id, :name, { prompt: "Please Select" }, class: "my-select") end test "grouped_collection_selects are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.grouped_collection_select(:status, [], :last, :first, :to_s, :to_s) end test "grouped_collection_selects with options are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.grouped_collection_select(:status, [], :last, :first, :to_s, :to_s, prompt: "Please Select") end test "grouped_collection_selects with options and html_options are wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.grouped_collection_select(:status, [], :last, :first, :to_s, :to_s, { prompt: "Please Select" }, class: "my-select") end test "date selects are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3)) do - expected = %{
\n\n\n
} + expected = %{
\n\n\n
} assert_equivalent_xml expected, @builder.date_select(:misc) end end test "date selects with options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3)) do - expected = %{
\n\n\n
} + expected = %{
\n\n\n
} assert_equivalent_xml expected, @builder.date_select(:misc, include_blank: true) end end test "date selects with options and html_options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3)) do - expected = %{
\n\n\n
} + expected = %{
\n\n\n
} assert_equivalent_xml expected, @builder.date_select(:misc, { include_blank: true }, class: "my-date-select") end end test "time selects are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do - expected = %{
\n\n\n\n : \n
} + expected = %{
\n\n\n\n : \n
} assert_equivalent_xml expected, @builder.time_select(:misc) end end test "time selects with options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do - expected = %{
\n\n\n\n : \n
} + expected = %{
\n\n\n\n : \n
} assert_equivalent_xml expected, @builder.time_select(:misc, include_blank: true) end end test "time selects with options and html_options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do - expected = %{
\n\n\n\n : \n
} + expected = %{
\n\n\n\n : \n
} assert_equivalent_xml expected, @builder.time_select(:misc, { include_blank: true }, class: "my-time-select") end end test "datetime selects are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do - expected = %{
\n\n\n — \n : \n
} + expected = %{
\n\n\n — \n : \n
} assert_equivalent_xml expected, @builder.datetime_select(:misc) end end test "datetime selects with options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do - expected = %{
\n\n\n — \n : \n
} + expected = %{
\n\n\n — \n : \n
} assert_equivalent_xml expected, @builder.datetime_select(:misc, include_blank: true) end end test "datetime selects with options and html_options are wrapped correctly" do Timecop.freeze(Time.utc(2012, 2, 3, 12, 0, 0)) do - expected = %{
\n\n\n — \n : \n
} + expected = %{
\n\n\n — \n : \n
} assert_equivalent_xml expected, @builder.datetime_select(:misc, { include_blank: true }, class: "my-datetime-select") end end diff --git a/test/special_form_class_models_test.rb b/test/special_form_class_models_test.rb index cc88f836a..5c3e428e0 100644 --- a/test/special_form_class_models_test.rb +++ b/test/special_form_class_models_test.rb @@ -14,7 +14,7 @@ def user_klass.model_name @horizontal_builder = BootstrapForm::FormBuilder.new(:user, @user, self, {layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10"}) I18n.backend.store_translations(:en, {activerecord: {help: {user: {password: "A good password should be at least six characters long"}}}}) - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.date_field(:misc) end @@ -24,7 +24,7 @@ def user_klass.model_name @horizontal_builder = BootstrapForm::FormBuilder.new(:user, @user, self, {layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10"}) I18n.backend.store_translations(:en, {activerecord: {help: {user: {password: "A good password should be at least six characters long"}}}}) - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.date_field(:misc) end @@ -36,7 +36,7 @@ def user_klass.model_name @horizontal_builder = BootstrapForm::FormBuilder.new(:user, @user, self, {layout: :horizontal, label_col: "col-sm-2", control_col: "col-sm-10"}) I18n.backend.store_translations(:en, {activerecord: {help: {faux_user: {password: "A good password should be at least six characters long"}}}}) - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.date_field(:misc) end