simple_form 2.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of simple_form might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +77 -33
- data/MIT-LICENSE +1 -1
- data/README.md +387 -187
- data/lib/generators/simple_form/install_generator.rb +4 -4
- data/lib/generators/simple_form/templates/README +3 -4
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +45 -22
- data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +128 -24
- data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +87 -6
- data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
- data/lib/simple_form/action_view_extensions/builder.rb +2 -319
- data/lib/simple_form/action_view_extensions/form_helper.rb +8 -11
- data/lib/simple_form/components/errors.rb +28 -2
- data/lib/simple_form/components/hints.rb +8 -3
- data/lib/simple_form/components/html5.rb +6 -3
- data/lib/simple_form/components/label_input.rb +20 -2
- data/lib/simple_form/components/labels.rb +14 -10
- data/lib/simple_form/components/maxlength.rb +2 -9
- data/lib/simple_form/components/min_max.rb +1 -1
- data/lib/simple_form/components/pattern.rb +3 -3
- data/lib/simple_form/components/placeholders.rb +2 -2
- data/lib/simple_form/components/readonly.rb +1 -1
- data/lib/simple_form/components.rb +1 -1
- data/lib/simple_form/error_notification.rb +2 -2
- data/lib/simple_form/form_builder.rb +262 -107
- data/lib/simple_form/helpers.rb +6 -6
- data/lib/simple_form/inputs/base.rb +37 -16
- data/lib/simple_form/inputs/block_input.rb +2 -2
- data/lib/simple_form/inputs/boolean_input.rb +33 -18
- data/lib/simple_form/inputs/collection_input.rb +34 -13
- data/lib/simple_form/inputs/collection_radio_buttons_input.rb +7 -12
- data/lib/simple_form/inputs/collection_select_input.rb +4 -2
- data/lib/simple_form/inputs/date_time_input.rb +23 -9
- data/lib/simple_form/inputs/file_input.rb +4 -2
- data/lib/simple_form/inputs/grouped_collection_select_input.rb +15 -3
- data/lib/simple_form/inputs/hidden_input.rb +4 -2
- data/lib/simple_form/inputs/numeric_input.rb +3 -8
- data/lib/simple_form/inputs/password_input.rb +4 -3
- data/lib/simple_form/inputs/priority_input.rb +4 -2
- data/lib/simple_form/inputs/range_input.rb +1 -1
- data/lib/simple_form/inputs/string_input.rb +4 -3
- data/lib/simple_form/inputs/text_input.rb +4 -2
- data/lib/simple_form/railtie.rb +14 -0
- data/lib/simple_form/tags.rb +68 -0
- data/lib/simple_form/version.rb +1 -1
- data/lib/simple_form/wrappers/builder.rb +11 -35
- data/lib/simple_form/wrappers/leaf.rb +28 -0
- data/lib/simple_form/wrappers/many.rb +7 -7
- data/lib/simple_form/wrappers/root.rb +2 -2
- data/lib/simple_form/wrappers/single.rb +5 -3
- data/lib/simple_form/wrappers.rb +2 -1
- data/lib/simple_form.rb +99 -52
- data/test/action_view_extensions/builder_test.rb +113 -127
- data/test/action_view_extensions/form_helper_test.rb +58 -30
- data/test/components/label_test.rb +83 -83
- data/test/form_builder/association_test.rb +96 -61
- data/test/form_builder/button_test.rb +14 -14
- data/test/form_builder/error_notification_test.rb +9 -9
- data/test/form_builder/error_test.rb +159 -34
- data/test/form_builder/general_test.rb +176 -121
- data/test/form_builder/hint_test.rb +43 -37
- data/test/form_builder/input_field_test.rb +99 -52
- data/test/form_builder/label_test.rb +67 -15
- data/test/form_builder/wrapper_test.rb +157 -41
- data/test/generators/simple_form_generator_test.rb +4 -4
- data/test/inputs/boolean_input_test.rb +92 -24
- data/test/inputs/collection_check_boxes_input_test.rb +150 -71
- data/test/inputs/collection_radio_buttons_input_test.rb +213 -113
- data/test/inputs/collection_select_input_test.rb +221 -85
- data/test/inputs/datetime_input_test.rb +125 -47
- data/test/inputs/disabled_test.rb +25 -25
- data/test/inputs/discovery_test.rb +60 -10
- data/test/inputs/file_input_test.rb +3 -3
- data/test/inputs/general_test.rb +48 -32
- data/test/inputs/grouped_collection_select_input_test.rb +76 -27
- data/test/inputs/hidden_input_test.rb +6 -5
- data/test/inputs/numeric_input_test.rb +46 -46
- data/test/inputs/priority_input_test.rb +21 -15
- data/test/inputs/readonly_test.rb +31 -31
- data/test/inputs/required_test.rb +30 -18
- data/test/inputs/string_input_test.rb +53 -52
- data/test/inputs/text_input_test.rb +15 -8
- data/test/simple_form_test.rb +8 -0
- data/test/support/discovery_inputs.rb +32 -2
- data/test/support/misc_helpers.rb +130 -29
- data/test/support/mock_controller.rb +6 -6
- data/test/support/models.rb +125 -71
- data/test/test_helper.rb +28 -35
- metadata +17 -29
- data/lib/simple_form/action_view_extensions/builder.rb.orig +0 -247
- data/lib/simple_form/core_ext/hash.rb +0 -16
- data/lib/simple_form/form_builder.rb.orig +0 -486
- data/lib/simple_form/version.rb.orig +0 -7
@@ -8,7 +8,7 @@ class AssociationTest < ActionView::TestCase
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
test 'builder
|
11
|
+
test 'builder does not allow creating an association input when no object exists' do
|
12
12
|
assert_raise ArgumentError do
|
13
13
|
with_association_for :post, :author
|
14
14
|
end
|
@@ -25,7 +25,7 @@ class AssociationTest < ActionView::TestCase
|
|
25
25
|
test 'builder association forwards collection to simple_fields_for' do
|
26
26
|
calls = 0
|
27
27
|
simple_form_for @user do |f|
|
28
|
-
f.association :company, :
|
28
|
+
f.association :company, collection: Company.all do |c|
|
29
29
|
calls += 1
|
30
30
|
end
|
31
31
|
end
|
@@ -34,125 +34,160 @@ class AssociationTest < ActionView::TestCase
|
|
34
34
|
end
|
35
35
|
|
36
36
|
test 'builder association marks input as required based on both association and attribute' do
|
37
|
-
swap SimpleForm, :
|
38
|
-
with_association_for @validating_user, :company, :
|
37
|
+
swap SimpleForm, required_by_default: false do
|
38
|
+
with_association_for @validating_user, :company, collection: []
|
39
39
|
assert_select 'label.required'
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
43
|
test 'builder preloads collection association' do
|
44
|
-
value = @user.tags =
|
45
|
-
value.
|
44
|
+
value = @user.tags = MiniTest::Mock.new
|
45
|
+
value.expect(:to_a, value)
|
46
46
|
|
47
47
|
with_association_for @user, :tags
|
48
48
|
assert_select 'form select.select#user_tag_ids'
|
49
|
-
assert_select 'form select option[value=1]', 'Tag 1'
|
50
|
-
assert_select 'form select option[value=2]', 'Tag 2'
|
51
|
-
assert_select 'form select option[value=3]', 'Tag 3'
|
49
|
+
assert_select 'form select option[value="1"]', 'Tag 1'
|
50
|
+
assert_select 'form select option[value="2"]', 'Tag 2'
|
51
|
+
assert_select 'form select option[value="3"]', 'Tag 3'
|
52
|
+
|
53
|
+
value.verify
|
52
54
|
end
|
53
55
|
|
54
56
|
test 'builder does not preload collection association if preload is false' do
|
55
|
-
value = @user.tags =
|
56
|
-
value.
|
57
|
+
value = @user.tags = MiniTest::Mock.new
|
58
|
+
value.expect(:to_a, nil)
|
57
59
|
|
58
|
-
with_association_for @user, :tags, :
|
60
|
+
with_association_for @user, :tags, preload: false
|
59
61
|
assert_select 'form select.select#user_tag_ids'
|
60
|
-
assert_select 'form select option[value=1]', 'Tag 1'
|
61
|
-
assert_select 'form select option[value=2]', 'Tag 2'
|
62
|
-
assert_select 'form select option[value=3]', 'Tag 3'
|
62
|
+
assert_select 'form select option[value="1"]', 'Tag 1'
|
63
|
+
assert_select 'form select option[value="2"]', 'Tag 2'
|
64
|
+
assert_select 'form select option[value="3"]', 'Tag 3'
|
65
|
+
|
66
|
+
assert_raises MockExpectationError do
|
67
|
+
value.verify
|
68
|
+
end
|
63
69
|
end
|
64
70
|
|
65
71
|
test 'builder does not preload non-collection association' do
|
66
|
-
value = @user.company =
|
67
|
-
value.
|
72
|
+
value = @user.company = MiniTest::Mock.new
|
73
|
+
value.expect(:to_a, nil)
|
68
74
|
|
69
75
|
with_association_for @user, :company
|
70
76
|
assert_select 'form select.select#user_company_id'
|
71
|
-
assert_select 'form select option[value=1]', 'Company 1'
|
72
|
-
assert_select 'form select option[value=2]', 'Company 2'
|
73
|
-
assert_select 'form select option[value=3]', 'Company 3'
|
77
|
+
assert_select 'form select option[value="1"]', 'Company 1'
|
78
|
+
assert_select 'form select option[value="2"]', 'Company 2'
|
79
|
+
assert_select 'form select option[value="3"]', 'Company 3'
|
80
|
+
|
81
|
+
assert_raises MockExpectationError do
|
82
|
+
value.verify
|
83
|
+
end
|
74
84
|
end
|
75
85
|
|
76
86
|
# ASSOCIATIONS - BELONGS TO
|
77
87
|
test 'builder creates a select for belongs_to associations' do
|
78
88
|
with_association_for @user, :company
|
79
89
|
assert_select 'form select.select#user_company_id'
|
80
|
-
assert_select 'form select option[value=1]', 'Company 1'
|
81
|
-
assert_select 'form select option[value=2]', 'Company 2'
|
82
|
-
assert_select 'form select option[value=3]', 'Company 3'
|
90
|
+
assert_select 'form select option[value="1"]', 'Company 1'
|
91
|
+
assert_select 'form select option[value="2"]', 'Company 2'
|
92
|
+
assert_select 'form select option[value="3"]', 'Company 3'
|
83
93
|
end
|
84
94
|
|
85
95
|
test 'builder creates blank select if collection is nil' do
|
86
|
-
with_association_for @user, :company, :
|
96
|
+
with_association_for @user, :company, collection: nil
|
87
97
|
assert_select 'form select.select#user_company_id'
|
88
|
-
assert_no_select 'form select option[value=1]', 'Company 1'
|
98
|
+
assert_no_select 'form select option[value="1"]', 'Company 1'
|
89
99
|
end
|
90
100
|
|
91
101
|
test 'builder allows collection radio for belongs_to associations' do
|
92
|
-
with_association_for @user, :company, :
|
102
|
+
with_association_for @user, :company, as: :radio_buttons
|
93
103
|
assert_select 'form input.radio_buttons#user_company_id_1'
|
94
104
|
assert_select 'form input.radio_buttons#user_company_id_2'
|
95
105
|
assert_select 'form input.radio_buttons#user_company_id_3'
|
96
106
|
end
|
97
107
|
|
108
|
+
test 'builder allows collection to have a proc as a condition' do
|
109
|
+
with_association_for @user, :extra_special_company
|
110
|
+
assert_select 'form select.select#user_extra_special_company_id'
|
111
|
+
assert_select 'form select option[value="1"]'
|
112
|
+
assert_no_select 'form select option[value="2"]'
|
113
|
+
assert_no_select 'form select option[value="3"]'
|
114
|
+
end
|
115
|
+
|
116
|
+
test 'builder allows collection to have a scope' do
|
117
|
+
with_association_for @user, :special_pictures
|
118
|
+
assert_select 'form select.select#user_special_picture_ids'
|
119
|
+
assert_select 'form select option[value="3"]', '3'
|
120
|
+
assert_no_select 'form select option[value="1"]'
|
121
|
+
assert_no_select 'form select option[value="2"]'
|
122
|
+
end
|
123
|
+
|
98
124
|
test 'builder marks the record which already belongs to the user' do
|
99
125
|
@user.company_id = 2
|
100
|
-
with_association_for @user, :company, :
|
126
|
+
with_association_for @user, :company, as: :radio_buttons
|
101
127
|
assert_no_select 'form input.radio_buttons#user_company_id_1[checked=checked]'
|
102
128
|
assert_select 'form input.radio_buttons#user_company_id_2[checked=checked]'
|
103
129
|
assert_no_select 'form input.radio_buttons#user_company_id_3[checked=checked]'
|
104
130
|
end
|
105
131
|
|
106
132
|
# ASSOCIATIONS - FINDERS
|
107
|
-
test 'builder
|
133
|
+
test 'builder uses reflection conditions to find collection' do
|
108
134
|
with_association_for @user, :special_company
|
109
135
|
assert_select 'form select.select#user_special_company_id'
|
110
|
-
assert_select 'form select option[value=1]'
|
111
|
-
assert_no_select 'form select option[value=2]'
|
112
|
-
assert_no_select 'form select option[value=3]'
|
136
|
+
assert_select 'form select option[value="1"]'
|
137
|
+
assert_no_select 'form select option[value="2"]'
|
138
|
+
assert_no_select 'form select option[value="3"]'
|
113
139
|
end
|
114
140
|
|
115
|
-
test 'builder
|
116
|
-
with_association_for @user, :company, :
|
117
|
-
:
|
141
|
+
test 'builder allows overriding collection to association input' do
|
142
|
+
with_association_for @user, :company, include_blank: false,
|
143
|
+
collection: [Company.new(999, 'Teste')]
|
118
144
|
assert_select 'form select.select#user_company_id'
|
119
|
-
assert_no_select 'form select option[value=1]'
|
120
|
-
assert_select 'form select option[value=999]', 'Teste'
|
121
|
-
assert_select 'form select option', :
|
145
|
+
assert_no_select 'form select option[value="1"]'
|
146
|
+
assert_select 'form select option[value="999"]', 'Teste'
|
147
|
+
assert_select 'form select option', count: 1
|
122
148
|
end
|
123
149
|
|
124
150
|
# ASSOCIATIONS - has_*
|
125
151
|
test 'builder does not allow has_one associations' do
|
126
152
|
assert_raise ArgumentError do
|
127
|
-
with_association_for @user, :first_company, :
|
153
|
+
with_association_for @user, :first_company, as: :radio_buttons
|
128
154
|
end
|
129
155
|
end
|
130
156
|
|
157
|
+
test 'builder does not call order if the given association does not respond to it' do
|
158
|
+
with_association_for @user, :pictures
|
159
|
+
assert_select 'form select.select#user_picture_ids'
|
160
|
+
assert_select 'form select[multiple=multiple]'
|
161
|
+
assert_select 'form select option[value="1"]', 'Picture 1'
|
162
|
+
assert_select 'form select option[value="2"]', 'Picture 2'
|
163
|
+
assert_select 'form select option[value="3"]', 'Picture 3'
|
164
|
+
end
|
165
|
+
|
131
166
|
test 'builder creates a select with multiple options for collection associations' do
|
132
167
|
with_association_for @user, :tags
|
133
168
|
assert_select 'form select.select#user_tag_ids'
|
134
|
-
assert_select 'form select[multiple=multiple]
|
135
|
-
assert_select 'form select option[value=1]', 'Tag 1'
|
136
|
-
assert_select 'form select option[value=2]', 'Tag 2'
|
137
|
-
assert_select 'form select option[value=3]', 'Tag 3'
|
169
|
+
assert_select 'form select[multiple=multiple]'
|
170
|
+
assert_select 'form select option[value="1"]', 'Tag 1'
|
171
|
+
assert_select 'form select option[value="2"]', 'Tag 2'
|
172
|
+
assert_select 'form select option[value="3"]', 'Tag 3'
|
138
173
|
end
|
139
174
|
|
140
175
|
test 'builder allows size to be overwritten for collection associations' do
|
141
|
-
with_association_for @user, :tags, :
|
142
|
-
assert_select 'form select[multiple=multiple][size=10]'
|
176
|
+
with_association_for @user, :tags, input_html: { size: 10 }
|
177
|
+
assert_select 'form select[multiple=multiple][size="10"]'
|
143
178
|
end
|
144
179
|
|
145
180
|
test 'builder marks all selected records which already belongs to user' do
|
146
181
|
@user.tag_ids = [1, 2]
|
147
182
|
with_association_for @user, :tags
|
148
|
-
assert_select 'form select option[value=1][selected=selected]'
|
149
|
-
assert_select 'form select option[value=2][selected=selected]'
|
150
|
-
assert_no_select 'form select option[value=3][selected=selected]'
|
183
|
+
assert_select 'form select option[value="1"][selected=selected]'
|
184
|
+
assert_select 'form select option[value="2"][selected=selected]'
|
185
|
+
assert_no_select 'form select option[value="3"][selected=selected]'
|
151
186
|
end
|
152
187
|
|
153
188
|
test 'builder allows a collection of check boxes for collection associations' do
|
154
189
|
@user.tag_ids = [1, 2]
|
155
|
-
with_association_for @user, :tags, :
|
190
|
+
with_association_for @user, :tags, as: :check_boxes
|
156
191
|
assert_select 'form input#user_tag_ids_1[type=checkbox]'
|
157
192
|
assert_select 'form input#user_tag_ids_2[type=checkbox]'
|
158
193
|
assert_select 'form input#user_tag_ids_3[type=checkbox]'
|
@@ -160,27 +195,27 @@ class AssociationTest < ActionView::TestCase
|
|
160
195
|
|
161
196
|
test 'builder marks all selected records for collection boxes' do
|
162
197
|
@user.tag_ids = [1, 2]
|
163
|
-
with_association_for @user, :tags, :
|
164
|
-
assert_select 'form input[type=checkbox][value=1][checked=checked]'
|
165
|
-
assert_select 'form input[type=checkbox][value=2][checked=checked]'
|
166
|
-
assert_no_select 'form input[type=checkbox][value=3][checked=checked]'
|
198
|
+
with_association_for @user, :tags, as: :check_boxes
|
199
|
+
assert_select 'form input[type=checkbox][value="1"][checked=checked]'
|
200
|
+
assert_select 'form input[type=checkbox][value="2"][checked=checked]'
|
201
|
+
assert_no_select 'form input[type=checkbox][value="3"][checked=checked]'
|
167
202
|
end
|
168
203
|
|
169
204
|
test 'builder with collection support giving collection and item wrapper tags' do
|
170
|
-
with_association_for @user, :tags, :
|
171
|
-
:
|
205
|
+
with_association_for @user, :tags, as: :check_boxes,
|
206
|
+
collection_wrapper_tag: :ul, item_wrapper_tag: :li
|
172
207
|
|
173
|
-
assert_select 'form ul', :
|
174
|
-
assert_select 'form ul li', :
|
208
|
+
assert_select 'form ul', count: 1
|
209
|
+
assert_select 'form ul li', count: 3
|
175
210
|
end
|
176
211
|
|
177
|
-
test 'builder with collection support
|
178
|
-
options = { :
|
212
|
+
test 'builder with collection support does not change the options hash' do
|
213
|
+
options = { as: :check_boxes, collection_wrapper_tag: :ul, item_wrapper_tag: :li}
|
179
214
|
with_association_for @user, :tags, options
|
180
215
|
|
181
|
-
assert_select 'form ul', :
|
182
|
-
assert_select 'form ul li', :
|
183
|
-
assert_equal({ :
|
216
|
+
assert_select 'form ul', count: 1
|
217
|
+
assert_select 'form ul li', count: 3
|
218
|
+
assert_equal({ as: :check_boxes, collection_wrapper_tag: :ul, item_wrapper_tag: :li},
|
184
219
|
options)
|
185
220
|
end
|
186
221
|
end
|
@@ -8,33 +8,33 @@ class ButtonTest < ActionView::TestCase
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
test 'builder
|
11
|
+
test 'builder creates buttons' do
|
12
12
|
with_button_for :post, :submit
|
13
|
-
assert_select 'form input.button[type=submit][value=Save Post]'
|
13
|
+
assert_select 'form input.button[type=submit][value="Save Post"]'
|
14
14
|
end
|
15
15
|
|
16
|
-
test 'builder
|
17
|
-
with_button_for :post, :submit, :
|
18
|
-
assert_select 'form input.button.my_button[type=submit][value=Save Post]'
|
16
|
+
test 'builder creates buttons with options' do
|
17
|
+
with_button_for :post, :submit, class: 'my_button'
|
18
|
+
assert_select 'form input.button.my_button[type=submit][value="Save Post"]'
|
19
19
|
end
|
20
20
|
|
21
|
-
test 'builder
|
22
|
-
options = { :
|
21
|
+
test 'builder does not modify the options hash' do
|
22
|
+
options = { class: 'my_button' }
|
23
23
|
with_button_for :post, :submit, options
|
24
|
-
assert_select 'form input.button.my_button[type=submit][value=Save Post]'
|
25
|
-
assert_equal({ :
|
24
|
+
assert_select 'form input.button.my_button[type=submit][value="Save Post"]'
|
25
|
+
assert_equal({ class: 'my_button' }, options)
|
26
26
|
end
|
27
27
|
|
28
|
-
test 'builder
|
28
|
+
test 'builder creates buttons for records' do
|
29
29
|
@user.new_record!
|
30
30
|
with_button_for @user, :submit
|
31
|
-
assert_select 'form input.button[type=submit][value=Create User]'
|
31
|
+
assert_select 'form input.button[type=submit][value="Create User"]'
|
32
32
|
end
|
33
33
|
|
34
|
-
test "builder
|
35
|
-
swap SimpleForm, :
|
34
|
+
test "builder uses the default class from the configuration" do
|
35
|
+
swap SimpleForm, button_class: 'btn' do
|
36
36
|
with_button_for :post, :submit
|
37
|
-
assert_select 'form input.btn[type=submit][value=Save Post]'
|
37
|
+
assert_select 'form input.btn[type=submit][value="Save Post"]'
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -3,7 +3,7 @@ require 'test_helper'
|
|
3
3
|
|
4
4
|
# Tests for f.error_notification
|
5
5
|
class ErrorNotificationTest < ActionView::TestCase
|
6
|
-
def with_error_notification_for(object, options={}, &block)
|
6
|
+
def with_error_notification_for(object, options = {}, &block)
|
7
7
|
with_concat_form_for(object) do |f|
|
8
8
|
f.error_notification(options)
|
9
9
|
end
|
@@ -27,7 +27,7 @@ class ErrorNotificationTest < ActionView::TestCase
|
|
27
27
|
end
|
28
28
|
|
29
29
|
test 'error notification uses I18n based on model to generate the notification message' do
|
30
|
-
store_translations(:en, :
|
30
|
+
store_translations(:en, simple_form: { error_notification: { user:
|
31
31
|
'Alguns erros foram encontrados para o usuário:'
|
32
32
|
} }) do
|
33
33
|
with_error_notification_for @user
|
@@ -36,8 +36,8 @@ class ErrorNotificationTest < ActionView::TestCase
|
|
36
36
|
end
|
37
37
|
|
38
38
|
test 'error notification uses I18n fallbacking to default message' do
|
39
|
-
store_translations(:en, :
|
40
|
-
:
|
39
|
+
store_translations(:en, simple_form: { error_notification: {
|
40
|
+
default_message: 'Opa! Alguns erros foram encontrados, poderia verificar?'
|
41
41
|
} }) do
|
42
42
|
with_error_notification_for @user
|
43
43
|
assert_select 'p.error_notification', 'Opa! Alguns erros foram encontrados, poderia verificar?'
|
@@ -45,30 +45,30 @@ class ErrorNotificationTest < ActionView::TestCase
|
|
45
45
|
end
|
46
46
|
|
47
47
|
test 'error notification allows passing the notification message' do
|
48
|
-
with_error_notification_for @user, :
|
48
|
+
with_error_notification_for @user, message: 'Erro encontrado ao criar usuario'
|
49
49
|
assert_select 'p.error_notification', 'Erro encontrado ao criar usuario'
|
50
50
|
end
|
51
51
|
|
52
52
|
test 'error notification accepts other html options' do
|
53
|
-
with_error_notification_for @user, :
|
53
|
+
with_error_notification_for @user, id: 'user_error_message', class: 'form_error'
|
54
54
|
assert_select 'p#user_error_message.form_error.error_notification'
|
55
55
|
end
|
56
56
|
|
57
57
|
test 'error notification allows configuring the wrapper element' do
|
58
|
-
swap SimpleForm, :
|
58
|
+
swap SimpleForm, error_notification_tag: :div do
|
59
59
|
with_error_notification_for @user
|
60
60
|
assert_select 'div.error_notification'
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
64
|
test 'error notification can contain HTML tags' do
|
65
|
-
with_error_notification_for @user, :
|
65
|
+
with_error_notification_for @user, message: 'Erro encontrado ao criar <b>usuário</b>'
|
66
66
|
assert_select 'p.error_notification', 'Erro encontrado ao criar usuário'
|
67
67
|
assert_select 'p.error_notification b', 'usuário'
|
68
68
|
end
|
69
69
|
|
70
70
|
test 'error notification uses I18n based on model to generate the notification message and accepts HTML' do
|
71
|
-
store_translations(:en, :
|
71
|
+
store_translations(:en, simple_form: { error_notification: { user:
|
72
72
|
'Alguns erros foram encontrados para o <b>usuário</b>:'
|
73
73
|
} }) do
|
74
74
|
with_error_notification_for @user
|
@@ -14,100 +14,124 @@ class ErrorTest < ActionView::TestCase
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
test 'error
|
17
|
+
test 'error does not generate content for attribute without errors' do
|
18
18
|
with_error_for @user, :active
|
19
19
|
assert_no_select 'span.error'
|
20
20
|
end
|
21
21
|
|
22
|
-
test 'error
|
22
|
+
test 'error does not generate messages when object is not present' do
|
23
23
|
with_error_for :project, :name
|
24
24
|
assert_no_select 'span.error'
|
25
25
|
end
|
26
26
|
|
27
|
-
test "error
|
27
|
+
test "error does not generate messages when object doesn't respond to errors method" do
|
28
28
|
@user.instance_eval { undef errors }
|
29
29
|
with_error_for @user, :name
|
30
30
|
assert_no_select 'span.error'
|
31
31
|
end
|
32
32
|
|
33
|
-
test 'error
|
33
|
+
test 'error generates messages for attribute with single error' do
|
34
34
|
with_error_for @user, :name
|
35
|
-
assert_select 'span.error', "
|
35
|
+
assert_select 'span.error', "cannot be blank"
|
36
36
|
end
|
37
37
|
|
38
|
-
test 'error
|
39
|
-
swap SimpleForm, :
|
38
|
+
test 'error generates messages for attribute with one error when using first' do
|
39
|
+
swap SimpleForm, error_method: :first do
|
40
40
|
with_error_for @user, :age
|
41
41
|
assert_select 'span.error', 'is not a number'
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
test 'error
|
46
|
-
swap SimpleForm, :
|
45
|
+
test 'error generates messages for attribute with several errors when using to_sentence' do
|
46
|
+
swap SimpleForm, error_method: :to_sentence do
|
47
47
|
with_error_for @user, :age
|
48
48
|
assert_select 'span.error', 'is not a number and must be greater than 18'
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
test 'error
|
53
|
-
with_error_for @user, :name, :
|
52
|
+
test 'error is able to pass html options' do
|
53
|
+
with_error_for @user, :name, id: 'error', class: 'yay'
|
54
54
|
assert_select 'span#error.error.yay'
|
55
55
|
end
|
56
56
|
|
57
|
-
test 'error
|
58
|
-
options = { :
|
57
|
+
test 'error does not modify the options hash' do
|
58
|
+
options = { id: 'error', class: 'yay' }
|
59
59
|
with_error_for @user, :name, options
|
60
60
|
assert_select 'span#error.error.yay'
|
61
|
-
assert_equal({ :
|
61
|
+
assert_equal({ id: 'error', class: 'yay' }, options)
|
62
62
|
end
|
63
63
|
|
64
|
-
test 'error
|
65
|
-
with_error_for @user, :company_id, :
|
66
|
-
:
|
64
|
+
test 'error finds errors on attribute and association' do
|
65
|
+
with_error_for @user, :company_id, as: :select,
|
66
|
+
error_method: :to_sentence, reflection: Association.new(Company, :company, {})
|
67
67
|
assert_select 'span.error', 'must be valid and company must be present'
|
68
68
|
end
|
69
69
|
|
70
|
-
test 'error
|
70
|
+
test 'error generates an error tag with a clean HTML' do
|
71
71
|
with_error_for @user, :name
|
72
72
|
assert_no_select 'span.error[error_html]'
|
73
73
|
end
|
74
74
|
|
75
|
-
test 'error
|
76
|
-
with_error_for @user, :name, :
|
75
|
+
test 'error generates an error tag with a clean HTML when errors options are present' do
|
76
|
+
with_error_for @user, :name, error_tag: :p, error_prefix: 'Name', error_method: :first
|
77
77
|
assert_no_select 'p.error[error_html]'
|
78
78
|
assert_no_select 'p.error[error_tag]'
|
79
79
|
assert_no_select 'p.error[error_prefix]'
|
80
80
|
assert_no_select 'p.error[error_method]'
|
81
81
|
end
|
82
82
|
|
83
|
-
test 'error
|
84
|
-
with_error_for @user, :name, :
|
85
|
-
|
83
|
+
test 'error escapes error prefix text' do
|
84
|
+
with_error_for @user, :name, error_prefix: '<b>Name</b>'
|
85
|
+
assert_no_select 'span.error b'
|
86
|
+
end
|
87
|
+
|
88
|
+
test 'error escapes error text' do
|
89
|
+
@user.errors.add(:action, 'must not contain <b>markup</b>')
|
90
|
+
|
91
|
+
with_error_for @user, :action
|
92
|
+
|
93
|
+
assert_select 'span.error'
|
94
|
+
assert_no_select 'span.error b', 'markup'
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
test 'error generates an error message with raw HTML tags' do
|
99
|
+
with_error_for @user, :name, error_prefix: '<b>Name</b>'.html_safe
|
100
|
+
assert_select 'span.error', "Name cannot be blank"
|
86
101
|
assert_select 'span.error b', "Name"
|
87
102
|
end
|
88
103
|
|
89
104
|
# FULL ERRORS
|
90
105
|
|
91
|
-
test 'full error
|
106
|
+
test 'full error generates a full error tag for the attribute' do
|
92
107
|
with_full_error_for @user, :name
|
93
|
-
assert_select 'span.error', "Super User Name!
|
108
|
+
assert_select 'span.error', "Super User Name! cannot be blank"
|
94
109
|
end
|
95
110
|
|
96
|
-
test 'full error
|
111
|
+
test 'full error generates a full error tag with a clean HTML' do
|
97
112
|
with_full_error_for @user, :name
|
98
113
|
assert_no_select 'span.error[error_html]'
|
99
114
|
end
|
100
115
|
|
101
|
-
test 'full error
|
102
|
-
with_full_error_for @user, :name, :
|
103
|
-
assert_select 'span.error#name_error', "Your name
|
116
|
+
test 'full error allows passing options to full error tag' do
|
117
|
+
with_full_error_for @user, :name, id: 'name_error', error_prefix: "Your name"
|
118
|
+
assert_select 'span.error#name_error', "Your name cannot be blank"
|
104
119
|
end
|
105
120
|
|
106
|
-
test 'full error
|
107
|
-
options = { :
|
121
|
+
test 'full error does not modify the options hash' do
|
122
|
+
options = { id: 'name_error' }
|
108
123
|
with_full_error_for @user, :name, options
|
109
|
-
assert_select 'span.error#name_error', "Super User Name!
|
110
|
-
assert_equal({ :
|
124
|
+
assert_select 'span.error#name_error', "Super User Name! cannot be blank"
|
125
|
+
assert_equal({ id: 'name_error' }, options)
|
126
|
+
end
|
127
|
+
|
128
|
+
test 'full error escapes error text' do
|
129
|
+
@user.errors.add(:action, 'must not contain <b>markup</b>')
|
130
|
+
|
131
|
+
with_full_error_for @user, :action
|
132
|
+
|
133
|
+
assert_select 'span.error'
|
134
|
+
assert_no_select 'span.error b', 'markup'
|
111
135
|
end
|
112
136
|
|
113
137
|
# CUSTOM WRAPPERS
|
@@ -115,7 +139,108 @@ class ErrorTest < ActionView::TestCase
|
|
115
139
|
test 'error with custom wrappers works' do
|
116
140
|
swap_wrapper do
|
117
141
|
with_error_for @user, :name
|
118
|
-
assert_select 'span.omg_error', "
|
142
|
+
assert_select 'span.omg_error', "cannot be blank"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
# FULL_ERROR_WRAPPER
|
147
|
+
|
148
|
+
test 'full error finds errors on association' do
|
149
|
+
swap_wrapper :default, custom_wrapper_with_full_error do
|
150
|
+
with_form_for @user, :company_id, as: :select
|
151
|
+
assert_select 'span.error', 'Company must be valid'
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
test 'full error finds errors on association with reflection' do
|
156
|
+
swap_wrapper :default, custom_wrapper_with_full_error do
|
157
|
+
with_form_for @user, :company_id, as: :select,
|
158
|
+
reflection: Association.new(Company, :company, {})
|
159
|
+
assert_select 'span.error', 'Company must be valid'
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
test 'full error can be disabled' do
|
164
|
+
swap_wrapper :default, custom_wrapper_with_full_error do
|
165
|
+
with_form_for @user, :company_id, as: :select, full_error: false
|
166
|
+
assert_no_select 'span.error'
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
test 'full error can be disabled setting error to false' do
|
171
|
+
swap_wrapper :default, custom_wrapper_with_full_error do
|
172
|
+
with_form_for @user, :company_id, as: :select, error: false
|
173
|
+
assert_no_select 'span.error'
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
# CUSTOM ERRORS
|
178
|
+
|
179
|
+
test 'input with custom error works' do
|
180
|
+
error_text = "Super User Name! cannot be blank"
|
181
|
+
with_form_for @user, :name, error: error_text
|
182
|
+
|
183
|
+
assert_select 'span.error', error_text
|
184
|
+
end
|
185
|
+
|
186
|
+
test 'input with error option as true does not use custom error' do
|
187
|
+
with_form_for @user, :name, error: true
|
188
|
+
|
189
|
+
assert_select 'span.error', "cannot be blank"
|
190
|
+
end
|
191
|
+
|
192
|
+
test 'input with custom error does not generate the error if there is no error on the attribute' do
|
193
|
+
with_form_for @user, :active, error: "Super User Active! cannot be blank"
|
194
|
+
|
195
|
+
assert_no_select 'span.error'
|
196
|
+
end
|
197
|
+
|
198
|
+
test 'input with custom error works when using full_error component' do
|
199
|
+
swap_wrapper :default, custom_wrapper_with_full_error do
|
200
|
+
error_text = "Super User Name! cannot be blank"
|
201
|
+
with_form_for @user, :name, error: error_text
|
202
|
+
|
203
|
+
assert_select 'span.error', error_text
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
test 'input with custom error escapes the error text' do
|
208
|
+
with_form_for @user, :name, error: 'error must not contain <b>markup</b>'
|
209
|
+
|
210
|
+
assert_select 'span.error'
|
211
|
+
assert_no_select 'span.error b', 'markup'
|
212
|
+
end
|
213
|
+
|
214
|
+
test 'input with custom error does not escape the error text if it is safe' do
|
215
|
+
with_form_for @user, :name, error: 'error must contain <b>markup</b>'.html_safe
|
216
|
+
|
217
|
+
assert_select 'span.error'
|
218
|
+
assert_select 'span.error b', 'markup'
|
219
|
+
end
|
220
|
+
|
221
|
+
test 'input with custom error escapes the error text using full_error component' do
|
222
|
+
swap_wrapper :default, custom_wrapper_with_full_error do
|
223
|
+
with_form_for @user, :name, error: 'error must not contain <b>markup</b>'
|
224
|
+
|
225
|
+
assert_select 'span.error'
|
226
|
+
assert_no_select 'span.error b', 'markup'
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
test 'input with custom error does not escape the error text if it is safe using full_error component' do
|
231
|
+
swap_wrapper :default, custom_wrapper_with_full_error do
|
232
|
+
with_form_for @user, :name, error: 'error must contain <b>markup</b>'.html_safe
|
233
|
+
|
234
|
+
assert_select 'span.error'
|
235
|
+
assert_select 'span.error b', 'markup'
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
test 'input with custom error when using full_error component does not generate the error if there is no error on the attribute' do
|
240
|
+
swap_wrapper :default, custom_wrapper_with_full_error do
|
241
|
+
with_form_for @user, :active, error: "Super User Active! can't be blank"
|
242
|
+
|
243
|
+
assert_no_select 'span.error'
|
119
244
|
end
|
120
245
|
end
|
121
246
|
end
|