Skip to content

Commit 00b5ad2

Browse files
authored
Fixed Calling << to an ActiveModel::Errors deprecation warning (bootstrap-ruby#690)
* Fixed `Calling << to an ActiveModel::Errors` deprecation warning * Disable rubocop Metrics/AbcSize around get_error_messages method * Added test case for belongs_to association error displaying. * Remove the need for addresses_controller and route
1 parent 0fef041 commit 00b5ad2

13 files changed

+35
-14
lines changed

lib/bootstrap_form/components/validation.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,20 @@ def generate_error(name)
6565
content_tag(help_tag, help_text, class: help_klass)
6666
end
6767

68+
# rubocop:disable Metrics/AbcSize
6869
def get_error_messages(name)
69-
messages = object.errors[name]
7070
object.class.try(:reflections)&.each do |association_name, a|
7171
next unless a.is_a?(ActiveRecord::Reflection::BelongsToReflection)
7272
next unless a.foreign_key == name.to_s
7373

74-
messages << object.errors[association_name]
74+
object.errors[association_name].each do |error|
75+
object.errors.add(name, error)
76+
end
7577
end
76-
messages.join(", ")
78+
79+
object.errors[name].join(", ")
7780
end
81+
# rubocop:enable Metrics/AbcSize
7882
end
7983
end
8084
end

test/bootstrap_checkbox_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "./test_helper"
1+
require_relative "test_helper"
22

33
class BootstrapCheckboxTest < ActionView::TestCase
44
include BootstrapForm::ActionViewExtensions::FormHelper

test/bootstrap_configuration_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "./test_helper"
1+
require_relative "test_helper"
22

33
class BootstrapConfigurationTest < ActionView::TestCase
44
test "has default form attributes" do

test/bootstrap_fields_test.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "./test_helper"
1+
require_relative "test_helper"
22

33
class BootstrapFieldsTest < ActionView::TestCase
44
include BootstrapForm::ActionViewExtensions::FormHelper
@@ -100,6 +100,22 @@ class BootstrapFieldsTest < ActionView::TestCase
100100
assert_equivalent_html expected, bootstrap_form_for(@user) { |f| f.file_field(:misc) }
101101
end
102102

103+
test "errors are correctly displayed for belongs_to assoication fields" do
104+
@address.valid?
105+
106+
expected = <<~HTML
107+
<form accept-charset="UTF-8" action="/users" class="new_address" id="new_address" method="post">
108+
#{'<input name="utf8" type="hidden" value="&#x2713;"/>' unless ::Rails::VERSION::STRING >= '6'}
109+
<div class="mb-3">
110+
<label class="form-label required" for="address_user_id">User</label>
111+
<input aria-required="true" class="form-control is-invalid" id="address_user_id" name="address[user_id]" required="required" type="text"/>
112+
<div class="invalid-feedback">must exist</div>
113+
</div>
114+
</form>
115+
HTML
116+
assert_equivalent_html expected, bootstrap_form_for(@address, url: users_path) { |f| f.text_field(:user_id) }
117+
end
118+
103119
test "hidden fields are supported" do
104120
expected = <<~HTML
105121
<input #{autocomplete_attr} id="user_misc" name="user[misc]" type="hidden" />

test/bootstrap_form_group_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "./test_helper"
1+
require_relative "test_helper"
22

33
class BootstrapFormGroupTest < ActionView::TestCase
44
include BootstrapForm::ActionViewExtensions::FormHelper

test/bootstrap_form_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "./test_helper"
1+
require_relative "test_helper"
22

33
class BootstrapFormTest < ActionView::TestCase
44
include BootstrapForm::ActionViewExtensions::FormHelper

test/bootstrap_other_components_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "./test_helper"
1+
require_relative "test_helper"
22

33
class BootstrapOtherComponentsTest < ActionView::TestCase
44
include BootstrapForm::ActionViewExtensions::FormHelper

test/bootstrap_radio_button_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "./test_helper"
1+
require_relative "test_helper"
22

33
class BootstrapRadioButtonTest < ActionView::TestCase
44
include BootstrapForm::ActionViewExtensions::FormHelper

test/bootstrap_rich_text_area_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "./test_helper"
1+
require_relative "test_helper"
22
require "minitest/mock"
33

44
if Rails::VERSION::STRING > "6"

test/bootstrap_selects_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "./test_helper"
1+
require_relative "test_helper"
22

33
class BootstrapSelectsTest < ActionView::TestCase
44
include BootstrapForm::ActionViewExtensions::FormHelper

0 commit comments

Comments
 (0)