diff --git a/CHANGELOG.md b/CHANGELOG.md index 83b9ebea7..9ed6f469d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ Bugfixes: Features: - Your contribution here! + * [#325](https://github.com/bootstrap-ruby/rails-bootstrap-forms/pull/325): Support :prepend and :append for the `select` helper - [@donv](https://github.com/donv). + ## [2.6.0][] (2017-02-03) diff --git a/README.md b/README.md index c1404a130..136717838 100644 --- a/README.md +++ b/README.md @@ -55,13 +55,13 @@ This generates the following HTML: -
+
- + ``` @@ -209,7 +209,7 @@ This automatically adds the `has-feedback` class to the `form-group`: ```html
- +
@@ -227,7 +227,7 @@ You can also prepend and append buttons. Note: The buttons must contain the `btn` class to generate the correct markup. ```erb -<%= f.text_field :search, append: link_to("Go", "#", class: "btn btn-default") %> +<%= f.text_field :search, append: link_to("Go", "#", class: "btn btn-secondary") %> ``` To add a class to the input group wrapper, use `:input_group_class` option. @@ -249,7 +249,7 @@ Which produces the following output: ```erb
- +
``` @@ -264,6 +264,19 @@ Our select helper accepts the same arguments as the [default Rails helper](http: <%= f.select :product, [[1, "Apple"], [2, "Grape"]], { label: "Choose your favorite fruit:" }, { class: "selectpicker" } %> ``` +#### Collection select on an association + +Validations on foreign key columns used for assigning associations do not by +default know about validation errors on the association itself. This can be +changed by using the `error_key` option, which will look for errors using the +given key rather than the value being assigned to by the select: + +```erb +<%= bootstrap_form_for(@user) do |f| %> + <%= f.collection_select(:product_id, Product.all, :id, :name, error_key: :product) %> +<% end %> +``` + ### Checkboxes and Radios Checkboxes and radios should be placed inside of a `form_group` to render @@ -330,7 +343,7 @@ Here's the output: ```html
- +

test@email.com

@@ -355,7 +368,7 @@ this defining these selects as `inline-block` and a width of `auto`. ### Submit Buttons -The `btn btn-default` css classes are automatically added to your submit +The `btn btn-secondary` css classes are automatically added to your submit buttons. ```erb @@ -468,10 +481,10 @@ error will be displayed below the field. Rails normally wraps the fields in a 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 d89c96643..9d913a204 100644 --- a/lib/bootstrap_form/form_builder.rb +++ b/lib/bootstrap_form/form_builder.rb @@ -71,13 +71,17 @@ def file_field_with_bootstrap(name, options = {}) if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new("4.1.0") def select_with_bootstrap(method, choices = nil, options = {}, html_options = {}, &block) form_group_builder(method, options, html_options) do - select_without_bootstrap(method, choices, options, html_options, &block) + prepend_and_append_input(options) do + select_without_bootstrap(method, choices, options, html_options, &block) + end end end else def select_with_bootstrap(method, choices, options = {}, html_options = {}) form_group_builder(method, options, html_options) do - select_without_bootstrap(method, choices, options, html_options) + prepend_and_append_input(options) do + select_without_bootstrap(method, choices, options, html_options) + end end end end @@ -111,6 +115,7 @@ 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(' ') html = check_box_without_bootstrap(name, check_box_options, checked_value, unchecked_value) label_content = block_given? ? capture(&block) : options[:label] @@ -130,10 +135,10 @@ def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_ if options[:inline] label_class = " #{label_class}" if label_class - label(label_name, html, class: "checkbox-inline#{disabled_class}#{label_class}") + label(label_name, html, class: "form-check-inline#{disabled_class}#{label_class}") else - content_tag(:div, class: "checkbox#{disabled_class}") do - label(label_name, html, class: label_class) + content_tag(:div, class: "form-check#{disabled_class}") do + label(label_name, html, class: ["form-check-label", label_class].compact.join(" ")) end end end @@ -192,15 +197,17 @@ def radio_buttons_collection(*args) def form_group(*args, &block) options = args.extract_options! name = args.first + error_name = options.delete(:error_key) || name options[:class] = ["form-group", options[:class]].compact.join(' ') - options[:class] << " #{error_class}" if has_error?(name) + options[:class] << " row" if get_group_layout(options[:layout]) == :horizontal + options[:class] << " #{error_class}" if has_error?(error_name) options[:class] << " #{feedback_class}" if options[:icon] content_tag(:div, options.except(:id, :label, :help, :icon, :label_col, :control_col, :layout)) do - label = generate_label(options[:id], name, options[:label], options[:label_col], options[:layout]) if options[:label] + label = generate_label(options[:id], name, error_name, options[:label], options[:label_col], options[:layout]) if options[:label] control = capture(&block).to_s - control.concat(generate_help(name, options[:help]).to_s) + control.concat(generate_help(error_name, options[:help]).to_s) control.concat(generate_icon(options[:icon])) if options[:icon] if get_group_layout(options[:layout]) == :horizontal @@ -259,11 +266,11 @@ def control_class end def label_class - "control-label" + "form-control-label" end def error_class - "has-error" + "has-danger" end def feedback_class @@ -309,6 +316,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) options = convert_form_tag_options(method, options) if acts_like_form_tag @@ -318,6 +326,7 @@ def form_group_builder(method, options, html_options = nil) icon = options.delete(:icon) label_col = options.delete(:label_col) control_col = options.delete(:control_col) + error_key = options.delete(:error_key) layout = get_group_layout(options.delete(:layout)) form_group_options = { id: options[:id], @@ -325,6 +334,7 @@ def form_group_builder(method, options, html_options = nil) icon: icon, label_col: label_col, control_col: control_col, + error_key: error_key, layout: layout, class: wrapper_class } @@ -364,19 +374,19 @@ def convert_form_tag_options(method, options = {}) options end - def generate_label(id, name, options, custom_label_col, group_layout) + def generate_label(id, name, error_name, options, custom_label_col, group_layout) options[:for] = id if acts_like_form_tag classes = [options[:class], label_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) + classes << "required" if required_attribute?(object, error_name) end options[:class] = classes.compact.join(" ") - if label_errors && has_error?(name) - error_messages = get_error_messages(name) - label_text = (options[:text] || object.class.human_attribute_name(name)).to_s.concat(" #{error_messages}") + if label_errors && has_error?(error_name) + error_messages = get_error_messages(error_name) + label_text = (options[:text] || object.class.human_attribute_name(error_name)).to_s.concat(" #{error_messages}") label(name, label_text, options.except(:text)) else label(name, options[:text], options.except(:text)) @@ -385,12 +395,16 @@ def generate_label(id, name, options, custom_label_col, group_layout) end def generate_help(name, help_text) - help_text = get_error_messages(name) if has_error?(name) && inline_errors - return if help_text === false + if has_error?(name) && inline_errors + help_text = get_error_messages(name) + help_klass = 'form-control-feedback' + end + return if help_text == false + help_klass ||= 'form-text text-muted' help_text ||= get_help_text_by_i18n_key(name) - content_tag(:span, help_text, class: 'help-block') if help_text.present? + content_tag(:span, help_text, class: help_klass) if help_text.present? end def generate_icon(icon) diff --git a/lib/bootstrap_form/helper.rb b/lib/bootstrap_form/helper.rb index ee73c9016..6788588e8 100644 --- a/lib/bootstrap_form/helper.rb +++ b/lib/bootstrap_form/helper.rb @@ -10,15 +10,8 @@ def bootstrap_form_for(object, options = {}, &block) options[:html] ||= {} options[:html][:role] ||= 'form' - layout = case options[:layout] - when :inline - "form-inline" - when :horizontal - "form-horizontal" - end - - if layout - options[:html][:class] = [options[:html][:class], layout].compact.join(" ") + if options[:layout] == :inline + options[:html][:class] = [options[:html][:class], "form-inline"].compact.join(" ") end temporarily_disable_field_error_proc do diff --git a/lib/bootstrap_form/helpers/bootstrap.rb b/lib/bootstrap_form/helpers/bootstrap.rb index 988b16280..58b31d76e 100644 --- a/lib/bootstrap_form/helpers/bootstrap.rb +++ b/lib/bootstrap_form/helpers/bootstrap.rb @@ -2,7 +2,7 @@ module BootstrapForm module Helpers module Bootstrap def submit(name = nil, options = {}) - options.reverse_merge! class: 'btn btn-default' + options.reverse_merge! class: 'btn btn-secondary' super(name, options) end diff --git a/test/bootstrap_checkbox_test.rb b/test/bootstrap_checkbox_test.rb index 0d2832690..da2df251c 100644 --- a/test/bootstrap_checkbox_test.rb +++ b/test/bootstrap_checkbox_test.rb @@ -8,74 +8,74 @@ def setup end test "check_box is wrapped correctly" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.check_box(:terms, label: 'I agree to the terms') end test "disabled check_box has proper wrapper classes" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.check_box(:terms, label: 'I agree to the terms', disabled: true) end test "check_box label allows html" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.check_box(:terms, label: %{I agree to the terms}.html_safe) end test "check_box accepts a block to define the label" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.check_box(:terms) { "I agree to the terms" } end test "check_box accepts a custom label class" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.check_box(:terms, label_class: 'btn') end test "check_box responds to checked_value and unchecked_value arguments" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.check_box(:terms, {label: 'I agree to the terms'}, 'yes', 'no') end test "inline checkboxes" do - expected = %{} + expected = %{} assert_equivalent_xml expected, @builder.check_box(:terms, label: 'I agree to the terms', inline: true) end test "disabled inline check_box" do - expected = %{} + expected = %{} assert_equivalent_xml expected, @builder.check_box(:terms, label: 'I agree to the terms', inline: true, disabled: true) end test "inline checkboxes with custom label class" do - expected = %{} + expected = %{} assert_equivalent_xml expected, @builder.check_box(:terms, inline: true, label_class: 'btn') end 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,7 +134,7 @@ 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) diff --git a/test/bootstrap_fields_test.rb b/test/bootstrap_fields_test.rb index 3eaec8ece..b7d9445c9 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 26a0a62fb..6c1f6d115 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,51 +43,51 @@ 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 = %{Click} + button = %{Click} suffix = %{
} after_button = prefix + field + button + suffix before_button = prefix + button + field + suffix both_button = prefix + button + field + button + suffix - button_src = link_to("Click", "#", class: "btn btn-default") + button_src = link_to("Click", "#", class: "btn btn-secondary") assert_equivalent_xml after_button, @builder.text_field(:email, append: button_src) assert_equivalent_xml before_button, @builder.text_field(:email, prepend: button_src) assert_equivalent_xml both_button, @builder.text_field(:email, append: button_src, prepend: button_src) 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 @@ -110,7 +110,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 @@ -119,7 +119,7 @@ def setup %{

Bar

}.html_safe end - expected = %{

Bar

} + expected = %{

Bar

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

Bar

}.html_safe end - expected = %{

Bar

} + expected = %{

Bar

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

Bar

}.html_safe end - expected = %{

Bar

} + expected = %{

Bar

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

Bar

}.html_safe end - expected = %{

Bar

} + expected = %{

Bar

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

Bar

}.html_safe end - expected = %{

Bar

} + expected = %{

Bar

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

Bar

}.html_safe end - expected = %{

Bar

} + expected = %{

Bar

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

Bar

}.html_safe end - expected = %{

Bar

can't be blank, is too short (minimum is 5 characters)
} + expected = %{

Bar

} 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 @@ -189,7 +189,7 @@ def setup @user.email = nil @user.valid? - expected = %{
can't be blank, is too short (minimum is 5 characters)
} + expected = %{
} assert_equivalent_xml expected, @builder.email_field(:email, wrapper_class: 'none-margin') end @@ -201,7 +201,7 @@ def setup f.text_field(:email, help: 'This is required', wrapper_class: 'none-margin') end - expected = %{
can't be blank, is too short (minimum is 5 characters)
} + expected = %{
} assert_equivalent_xml expected, output end @@ -210,7 +210,7 @@ def setup @horizontal_builder.submit end - expected = %{
} + expected = %{
} assert_equivalent_xml expected, output end @@ -219,12 +219,12 @@ def setup @horizontal_builder.submit end - expected = %{
} + expected = %{
} assert_equivalent_xml expected, output end test "adding an icon to a field" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, @builder.email_field(:misc, icon: 'ok') end @@ -236,17 +236,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 @@ -255,12 +255,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 @@ -270,7 +270,7 @@ def setup %{

Bar

}.html_safe end - expected = %{

Bar

} + expected = %{

Bar

} assert_equivalent_xml expected, output end @@ -278,12 +278,12 @@ def setup frozen_horizontal_builder = BootstrapForm::FormBuilder.new(:user, @user, self, { layout: :horizontal, label_col: "col-sm-3".freeze, control_col: "col-sm-9".freeze }) output = frozen_horizontal_builder.form_group { 'test' } - expected = %{
test
} + expected = %{
test
} assert_equivalent_xml expected, output 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 b2bcd5c32..9798f2ffc 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,17 +33,17 @@ 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 test "bootstrap_form_tag allows an empty name for checkboxes" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, bootstrap_form_tag(url: '/users') { |f| f.check_box :misc } end @@ -51,7 +51,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 @@ -59,7 +59,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 @@ -69,7 +69,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}}}}) @@ -142,17 +142,17 @@ 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 test "offset for form group without label respects label width for horizontal forms" do - expected = %{
} + expected = %{
} assert_equivalent_xml expected, bootstrap_form_for(@user, layout: :horizontal, label_col: 'col-md-2', control_col: 'col-md-10') { |f| f.form_group { f.submit } } 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 @@ -164,7 +164,7 @@ def setup f.text_field(:email, help: 'This is required') end - expected = %{
can't be blank, is too short (minimum is 5 characters)
} + expected = %{
} assert_equivalent_xml expected, output end @@ -176,7 +176,7 @@ def setup f.text_field(:email, help: 'This is required') end - expected = %{
can't be blank, is too short (minimum is 5 characters)
} + expected = %{
} assert_equivalent_xml expected, output end @@ -188,13 +188,13 @@ 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 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 f175c94ed..0933c56f2 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,8 +37,8 @@ def setup "this is a test" end - expected = %{
this is a test
} - assert_equal expected, output + expected = %{
this is a test
} + assert_equivalent_xml expected, output end test "custom control doesn't require an actual attribute" do @@ -46,8 +46,8 @@ def setup "this is a test" end - expected = %{
this is a test
} - assert_equal expected, output + expected = %{
this is a test
} + assert_equivalent_xml expected, output end test "custom control doesn't require a name" do @@ -55,17 +55,17 @@ def setup "Custom Control" end - expected = %{
Custom Control
} - assert_equal expected, output + expected = %{
Custom Control
} + assert_equivalent_xml expected, output end test "submit button defaults to rails action name" do - expected = %{} + expected = %{} assert_equivalent_xml expected, @builder.submit end test "submit button uses default button classes" do - expected = %{} + expected = %{} assert_equivalent_xml expected, @builder.submit("Submit Form") end diff --git a/test/bootstrap_radio_button_test.rb b/test/bootstrap_radio_button_test.rb index b420c1e01..2ba720da1 100644 --- a/test/bootstrap_radio_button_test.rb +++ b/test/bootstrap_radio_button_test.rb @@ -39,84 +39,84 @@ 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 diff --git a/test/bootstrap_selects_test.rb b/test/bootstrap_selects_test.rb index 80846bcd1..673ab6afd 100644 --- a/test/bootstrap_selects_test.rb +++ b/test/bootstrap_selects_test.rb @@ -8,33 +8,50 @@ 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 + + After +
+
+ HTML + assert_equivalent_xml expected, @builder.select(:status, [['activated', 1], ['blocked', 2]], prepend: 'Before', append: 'After') + end + if Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new("4.1.0") test "selects with block use block as content" do - expected = %{
} + expected = %{
} select = @builder.select(:status) do content_tag(:option) { 'Option 1' } + content_tag(:option) { 'Option 2' } @@ -44,100 +61,108 @@ 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 + + test "collection_selects display errors correctly when error_key is set" do + @address = Address.new + @address.valid? + + expected = %{
can't be blank
} + assert_equivalent_xml expected, bootstrap_form_for(@address, url: "/addresses", label_errors: true, inline_errors: true) { |f| f.collection_select :user_id, [], :id, :email, error_key: :user } + end end diff --git a/test/dummy/app/models/address.rb b/test/dummy/app/models/address.rb index 2c0c20b88..e5759528d 100644 --- a/test/dummy/app/models/address.rb +++ b/test/dummy/app/models/address.rb @@ -1,3 +1,4 @@ class Address < ActiveRecord::Base belongs_to :user + validates :user, presence: true end diff --git a/test/special_form_class_models_test.rb b/test/special_form_class_models_test.rb index 2579c9a48..cc88f836a 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