simple_form 3.0.1 → 4.0.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +149 -25
- data/MIT-LICENSE +1 -1
- data/README.md +441 -107
- data/lib/generators/simple_form/install_generator.rb +4 -3
- data/lib/generators/simple_form/templates/README +4 -5
- data/lib/generators/simple_form/templates/_form.html.erb +2 -0
- data/lib/generators/simple_form/templates/_form.html.haml +2 -0
- data/lib/generators/simple_form/templates/_form.html.slim +1 -0
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +48 -11
- data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +417 -23
- data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +101 -5
- data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
- data/lib/simple_form/action_view_extensions/builder.rb +2 -0
- data/lib/simple_form/action_view_extensions/form_helper.rb +10 -3
- data/lib/simple_form/components/errors.rb +38 -3
- data/lib/simple_form/components/hints.rb +3 -2
- data/lib/simple_form/components/html5.rb +16 -5
- data/lib/simple_form/components/label_input.rb +21 -2
- data/lib/simple_form/components/labels.rb +22 -11
- data/lib/simple_form/components/maxlength.rb +9 -5
- data/lib/simple_form/components/min_max.rb +2 -1
- data/lib/simple_form/components/minlength.rb +38 -0
- data/lib/simple_form/components/pattern.rb +2 -1
- data/lib/simple_form/components/placeholders.rb +4 -3
- data/lib/simple_form/components/readonly.rb +2 -1
- data/lib/simple_form/components.rb +2 -0
- data/lib/simple_form/error_notification.rb +1 -0
- data/lib/simple_form/form_builder.rb +193 -80
- data/lib/simple_form/helpers/autofocus.rb +1 -0
- data/lib/simple_form/helpers/disabled.rb +1 -0
- data/lib/simple_form/helpers/readonly.rb +1 -0
- data/lib/simple_form/helpers/required.rb +1 -0
- data/lib/simple_form/helpers/validators.rb +2 -1
- data/lib/simple_form/helpers.rb +6 -5
- data/lib/simple_form/i18n_cache.rb +1 -0
- data/lib/simple_form/inputs/base.rb +57 -14
- data/lib/simple_form/inputs/block_input.rb +2 -1
- data/lib/simple_form/inputs/boolean_input.rb +39 -16
- data/lib/simple_form/inputs/collection_check_boxes_input.rb +2 -1
- data/lib/simple_form/inputs/collection_input.rb +37 -14
- data/lib/simple_form/inputs/collection_radio_buttons_input.rb +9 -13
- data/lib/simple_form/inputs/collection_select_input.rb +5 -2
- data/lib/simple_form/inputs/date_time_input.rb +24 -9
- data/lib/simple_form/inputs/file_input.rb +5 -2
- data/lib/simple_form/inputs/grouped_collection_select_input.rb +16 -3
- data/lib/simple_form/inputs/hidden_input.rb +5 -2
- data/lib/simple_form/inputs/numeric_input.rb +6 -4
- data/lib/simple_form/inputs/password_input.rb +6 -3
- data/lib/simple_form/inputs/priority_input.rb +5 -2
- data/lib/simple_form/inputs/range_input.rb +2 -1
- data/lib/simple_form/inputs/string_input.rb +6 -3
- data/lib/simple_form/inputs/text_input.rb +6 -3
- data/lib/simple_form/inputs.rb +1 -0
- data/lib/simple_form/map_type.rb +1 -0
- data/lib/simple_form/railtie.rb +8 -0
- data/lib/simple_form/tags.rb +7 -0
- data/lib/simple_form/version.rb +2 -1
- data/lib/simple_form/wrappers/builder.rb +7 -6
- data/lib/simple_form/wrappers/leaf.rb +29 -0
- data/lib/simple_form/wrappers/many.rb +7 -6
- data/lib/simple_form/wrappers/root.rb +3 -1
- data/lib/simple_form/wrappers/single.rb +7 -4
- data/lib/simple_form/wrappers.rb +2 -0
- data/lib/simple_form.rb +117 -18
- data/test/action_view_extensions/builder_test.rb +42 -41
- data/test/action_view_extensions/form_helper_test.rb +36 -16
- data/test/components/custom_components_test.rb +62 -0
- data/test/components/label_test.rb +70 -41
- data/test/form_builder/association_test.rb +79 -37
- data/test/form_builder/button_test.rb +11 -10
- data/test/form_builder/error_notification_test.rb +2 -1
- data/test/form_builder/error_test.rb +173 -24
- data/test/form_builder/general_test.rb +171 -73
- data/test/form_builder/hint_test.rb +24 -18
- data/test/form_builder/input_field_test.rb +132 -56
- data/test/form_builder/label_test.rb +68 -13
- data/test/form_builder/wrapper_test.rb +190 -22
- data/test/generators/simple_form_generator_test.rb +8 -7
- data/test/inputs/boolean_input_test.rb +89 -6
- data/test/inputs/collection_check_boxes_input_test.rb +109 -25
- data/test/inputs/collection_radio_buttons_input_test.rb +168 -54
- data/test/inputs/collection_select_input_test.rb +183 -77
- data/test/inputs/datetime_input_test.rb +131 -50
- data/test/inputs/disabled_test.rb +16 -15
- data/test/inputs/discovery_test.rb +58 -6
- data/test/inputs/file_input_test.rb +3 -2
- data/test/inputs/general_test.rb +23 -22
- data/test/inputs/grouped_collection_select_input_test.rb +54 -17
- data/test/inputs/hidden_input_test.rb +5 -4
- data/test/inputs/numeric_input_test.rb +48 -44
- data/test/inputs/priority_input_test.rb +23 -14
- data/test/inputs/readonly_test.rb +20 -19
- data/test/inputs/required_test.rb +58 -13
- data/test/inputs/string_input_test.rb +62 -33
- data/test/inputs/text_input_test.rb +20 -7
- data/test/simple_form_test.rb +9 -0
- data/test/support/discovery_inputs.rb +33 -2
- data/test/support/misc_helpers.rb +113 -5
- data/test/support/mock_controller.rb +7 -1
- data/test/support/models.rb +137 -37
- data/test/test_helper.rb +15 -1
- metadata +25 -33
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
1
2
|
# encoding: UTF-8
|
|
2
3
|
require 'test_helper'
|
|
3
4
|
|
|
@@ -8,7 +9,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
8
9
|
end
|
|
9
10
|
end
|
|
10
11
|
|
|
11
|
-
test 'nested simple fields
|
|
12
|
+
test 'nested simple fields yields an instance of FormBuilder' do
|
|
12
13
|
simple_form_for :user do |f|
|
|
13
14
|
f.simple_fields_for :posts do |posts_form|
|
|
14
15
|
assert posts_form.instance_of?(SimpleForm::FormBuilder)
|
|
@@ -22,7 +23,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
22
23
|
end
|
|
23
24
|
end
|
|
24
25
|
|
|
25
|
-
test 'builder
|
|
26
|
+
test 'builder works without controller' do
|
|
26
27
|
stub_any_instance ActionView::TestCase, :controller, nil do
|
|
27
28
|
simple_form_for @user do |f|
|
|
28
29
|
assert f.input(:name)
|
|
@@ -30,7 +31,13 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
30
31
|
end
|
|
31
32
|
end
|
|
32
33
|
|
|
33
|
-
test 'builder
|
|
34
|
+
test 'builder works with decorated object responsive to #to_model' do
|
|
35
|
+
assert_nothing_raised do
|
|
36
|
+
with_form_for @decorated_user, :name
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
test 'builder input allows a block to configure input' do
|
|
34
41
|
with_form_for @user, :name do
|
|
35
42
|
text_field_tag :foo, :bar, id: :cool
|
|
36
43
|
end
|
|
@@ -38,7 +45,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
38
45
|
assert_select 'input#cool'
|
|
39
46
|
end
|
|
40
47
|
|
|
41
|
-
test 'builder
|
|
48
|
+
test 'builder allows adding custom input mappings for default input types' do
|
|
42
49
|
swap SimpleForm, input_mappings: { /count$/ => :integer } do
|
|
43
50
|
with_form_for @user, :post_count
|
|
44
51
|
assert_no_select 'form input#user_post_count.string'
|
|
@@ -46,15 +53,26 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
46
53
|
end
|
|
47
54
|
end
|
|
48
55
|
|
|
49
|
-
test 'builder
|
|
50
|
-
swap SimpleForm,
|
|
56
|
+
test 'builder does not override custom input mappings for custom collection' do
|
|
57
|
+
swap SimpleForm, input_mappings: { /gender$/ => :check_boxes } do
|
|
58
|
+
with_concat_form_for @user do |f|
|
|
59
|
+
f.input :gender, collection: %i[male female]
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
assert_no_select 'select option', 'Male'
|
|
63
|
+
assert_select 'input[type=checkbox][value=male]'
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
test 'builder allows to skip input_type class' do
|
|
68
|
+
swap SimpleForm, generate_additional_classes_for: %i[label wrapper] do
|
|
51
69
|
with_form_for @user, :post_count
|
|
52
70
|
assert_no_select "form input#user_post_count.integer"
|
|
53
71
|
assert_select "form input#user_post_count"
|
|
54
72
|
end
|
|
55
73
|
end
|
|
56
74
|
|
|
57
|
-
test 'builder
|
|
75
|
+
test 'builder allows to add additional classes only for wrapper' do
|
|
58
76
|
swap SimpleForm, generate_additional_classes_for: [:wrapper] do
|
|
59
77
|
with_form_for @user, :post_count
|
|
60
78
|
assert_no_select "form input#user_post_count.string"
|
|
@@ -63,7 +81,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
63
81
|
end
|
|
64
82
|
end
|
|
65
83
|
|
|
66
|
-
test 'builder
|
|
84
|
+
test 'builder allows adding custom input mappings for integer input types' do
|
|
67
85
|
swap SimpleForm, input_mappings: { /lock_version/ => :hidden } do
|
|
68
86
|
with_form_for @user, :lock_version
|
|
69
87
|
assert_no_select 'form input#user_lock_version.integer'
|
|
@@ -101,187 +119,267 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
101
119
|
end
|
|
102
120
|
|
|
103
121
|
# INPUT TYPES
|
|
104
|
-
test 'builder
|
|
122
|
+
test 'builder generates text fields for string columns' do
|
|
105
123
|
with_form_for @user, :name
|
|
106
124
|
assert_select 'form input#user_name.string'
|
|
107
125
|
end
|
|
108
126
|
|
|
109
|
-
test 'builder
|
|
127
|
+
test 'builder generates text areas for text columns' do
|
|
110
128
|
with_form_for @user, :description
|
|
129
|
+
assert_no_select 'form input#user_description.string'
|
|
130
|
+
assert_select 'form textarea#user_description.text'
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
test 'builder generates text areas for text columns when hinted' do
|
|
134
|
+
with_form_for @user, :description, as: :text
|
|
135
|
+
assert_no_select 'form input#user_description.string'
|
|
111
136
|
assert_select 'form textarea#user_description.text'
|
|
112
137
|
end
|
|
113
138
|
|
|
114
|
-
test 'builder
|
|
139
|
+
test 'builder generates text field for text columns when hinted' do
|
|
140
|
+
with_form_for @user, :description, as: :string
|
|
141
|
+
assert_no_select 'form textarea#user_description.text'
|
|
142
|
+
assert_select 'form input#user_description.string'
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
test 'builder generates text areas for hstore columns' do
|
|
146
|
+
with_form_for @user, :hstore
|
|
147
|
+
assert_no_select 'form input#user_hstore.string'
|
|
148
|
+
assert_select 'form textarea#user_hstore.text'
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
test 'builder generates text areas for json columns' do
|
|
152
|
+
with_form_for @user, :json
|
|
153
|
+
assert_no_select 'form input#user_json.string'
|
|
154
|
+
assert_select 'form textarea#user_json.text'
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
test 'builder generates text areas for jsonb columns' do
|
|
158
|
+
with_form_for @user, :jsonb
|
|
159
|
+
assert_no_select 'form input#user_jsonb.string'
|
|
160
|
+
assert_select 'form textarea#user_jsonb.text'
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
test 'builder generates a checkbox for boolean columns' do
|
|
115
164
|
with_form_for @user, :active
|
|
116
165
|
assert_select 'form input[type=checkbox]#user_active.boolean'
|
|
117
166
|
end
|
|
118
167
|
|
|
119
|
-
test 'builder
|
|
168
|
+
test 'builder uses integer text field for integer columns' do
|
|
120
169
|
with_form_for @user, :age
|
|
121
170
|
assert_select 'form input#user_age.numeric.integer'
|
|
122
171
|
end
|
|
123
172
|
|
|
124
|
-
test 'builder
|
|
173
|
+
test 'builder generates decimal text field for decimal columns' do
|
|
125
174
|
with_form_for @user, :credit_limit
|
|
126
175
|
assert_select 'form input#user_credit_limit.numeric.decimal'
|
|
127
176
|
end
|
|
128
177
|
|
|
129
|
-
test 'builder
|
|
178
|
+
test 'builder generates uuid fields for uuid columns' do
|
|
179
|
+
with_form_for @user, :uuid
|
|
180
|
+
if defined? ActiveModel::Type
|
|
181
|
+
assert_select 'form input#user_uuid.string.string'
|
|
182
|
+
else
|
|
183
|
+
assert_select 'form input#user_uuid.string.uuid'
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
test 'builder generates string fields for citext columns' do
|
|
188
|
+
with_form_for @user, :citext
|
|
189
|
+
assert_select 'form input#user_citext.string'
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
test 'builder generates password fields for columns that matches password' do
|
|
130
193
|
with_form_for @user, :password
|
|
131
194
|
assert_select 'form input#user_password.password'
|
|
132
195
|
end
|
|
133
196
|
|
|
134
|
-
test 'builder
|
|
197
|
+
test 'builder generates country fields for columns that matches country' do
|
|
135
198
|
with_form_for @user, :residence_country
|
|
136
199
|
assert_select 'form select#user_residence_country.country'
|
|
137
200
|
end
|
|
138
201
|
|
|
139
|
-
test 'builder
|
|
202
|
+
test 'builder generates time_zone fields for columns that matches time_zone' do
|
|
140
203
|
with_form_for @user, :time_zone
|
|
141
204
|
assert_select 'form select#user_time_zone.time_zone'
|
|
142
205
|
end
|
|
143
206
|
|
|
144
|
-
test 'builder
|
|
207
|
+
test 'builder generates email fields for columns that matches email' do
|
|
145
208
|
with_form_for @user, :email
|
|
146
209
|
assert_select 'form input#user_email.string.email'
|
|
147
210
|
end
|
|
148
211
|
|
|
149
|
-
test 'builder
|
|
212
|
+
test 'builder generates tel fields for columns that matches phone' do
|
|
150
213
|
with_form_for @user, :phone_number
|
|
151
214
|
assert_select 'form input#user_phone_number.string.tel'
|
|
152
215
|
end
|
|
153
216
|
|
|
154
|
-
test 'builder
|
|
217
|
+
test 'builder generates url fields for columns that matches url' do
|
|
155
218
|
with_form_for @user, :url
|
|
156
219
|
assert_select 'form input#user_url.string.url'
|
|
157
220
|
end
|
|
158
221
|
|
|
159
|
-
test 'builder
|
|
222
|
+
test 'builder generates date select for date columns' do
|
|
160
223
|
with_form_for @user, :born_at
|
|
161
224
|
assert_select 'form select#user_born_at_1i.date'
|
|
162
225
|
end
|
|
163
226
|
|
|
164
|
-
test 'builder
|
|
227
|
+
test 'builder generates time select for time columns' do
|
|
165
228
|
with_form_for @user, :delivery_time
|
|
166
229
|
assert_select 'form select#user_delivery_time_4i.time'
|
|
167
230
|
end
|
|
168
231
|
|
|
169
|
-
test 'builder
|
|
232
|
+
test 'builder generates datetime select for datetime columns' do
|
|
170
233
|
with_form_for @user, :created_at
|
|
171
234
|
assert_select 'form select#user_created_at_1i.datetime'
|
|
172
235
|
end
|
|
173
236
|
|
|
174
|
-
test 'builder
|
|
237
|
+
test 'builder generates datetime select for timestamp columns' do
|
|
175
238
|
with_form_for @user, :updated_at
|
|
176
239
|
assert_select 'form select#user_updated_at_1i.datetime'
|
|
177
240
|
end
|
|
178
241
|
|
|
179
|
-
test 'builder
|
|
242
|
+
test 'builder generates file for file columns' do
|
|
180
243
|
@user.avatar = MiniTest::Mock.new
|
|
181
244
|
@user.avatar.expect(:public_filename, true)
|
|
245
|
+
@user.avatar.expect(:!, false)
|
|
246
|
+
|
|
247
|
+
with_form_for @user, :avatar
|
|
248
|
+
assert_select 'form input#user_avatar.file'
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
test 'builder generates file for activestorage entries' do
|
|
252
|
+
@user.avatar = MiniTest::Mock.new
|
|
253
|
+
@user.avatar.expect(:attached?, false)
|
|
254
|
+
@user.avatar.expect(:!, false)
|
|
182
255
|
|
|
183
256
|
with_form_for @user, :avatar
|
|
184
257
|
assert_select 'form input#user_avatar.file'
|
|
185
258
|
end
|
|
186
259
|
|
|
187
|
-
test 'builder
|
|
260
|
+
test 'builder generates file for attributes that are real db columns but have file methods' do
|
|
188
261
|
@user.home_picture = MiniTest::Mock.new
|
|
189
262
|
@user.home_picture.expect(:mounted_as, true)
|
|
263
|
+
@user.home_picture.expect(:!, false)
|
|
190
264
|
|
|
191
265
|
with_form_for @user, :home_picture
|
|
192
266
|
assert_select 'form input#user_home_picture.file'
|
|
193
267
|
end
|
|
194
268
|
|
|
195
|
-
test 'build
|
|
269
|
+
test 'build generates select if a collection is given' do
|
|
196
270
|
with_form_for @user, :age, collection: 1..60
|
|
197
271
|
assert_select 'form select#user_age.select'
|
|
198
272
|
end
|
|
199
273
|
|
|
200
|
-
test 'builder
|
|
274
|
+
test 'builder allows overriding default input type for text' do
|
|
201
275
|
with_form_for @user, :name, as: :text
|
|
202
276
|
assert_no_select 'form input#user_name'
|
|
203
277
|
assert_select 'form textarea#user_name.text'
|
|
278
|
+
end
|
|
204
279
|
|
|
280
|
+
test 'builder allows overriding default input type for radio_buttons' do
|
|
205
281
|
with_form_for @user, :active, as: :radio_buttons
|
|
206
282
|
assert_no_select 'form input[type=checkbox]'
|
|
207
283
|
assert_select 'form input.radio_buttons[type=radio]', count: 2
|
|
284
|
+
end
|
|
208
285
|
|
|
286
|
+
test 'builder allows overriding default input type for string' do
|
|
209
287
|
with_form_for @user, :born_at, as: :string
|
|
210
288
|
assert_no_select 'form select'
|
|
211
289
|
assert_select 'form input#user_born_at.string'
|
|
212
290
|
end
|
|
213
291
|
|
|
214
292
|
# COMMON OPTIONS
|
|
215
|
-
test
|
|
216
|
-
|
|
293
|
+
# Remove this test when SimpleForm.form_class is removed in 4.x
|
|
294
|
+
test 'builder adds chosen form class' do
|
|
295
|
+
ActiveSupport::Deprecation.silence do
|
|
296
|
+
swap SimpleForm, form_class: :my_custom_class do
|
|
297
|
+
with_form_for @user, :name
|
|
298
|
+
assert_select 'form.my_custom_class'
|
|
299
|
+
end
|
|
300
|
+
end
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
# Remove this test when SimpleForm.form_class is removed in 4.x
|
|
304
|
+
test 'builder adds chosen form class and default form class' do
|
|
305
|
+
ActiveSupport::Deprecation.silence do
|
|
306
|
+
swap SimpleForm, form_class: "my_custom_class", default_form_class: "my_default_class" do
|
|
307
|
+
with_form_for @user, :name
|
|
308
|
+
assert_select 'form.my_custom_class.my_default_class'
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
test 'builder adds default form class' do
|
|
314
|
+
swap SimpleForm, default_form_class: "default_class" do
|
|
217
315
|
with_form_for @user, :name
|
|
218
|
-
assert_select 'form.
|
|
316
|
+
assert_select 'form.default_class'
|
|
219
317
|
end
|
|
220
318
|
end
|
|
221
319
|
|
|
222
|
-
test 'builder
|
|
320
|
+
test 'builder allows passing options to input' do
|
|
223
321
|
with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
|
|
224
322
|
assert_select 'form input#my_input.my_input.string'
|
|
225
323
|
end
|
|
226
324
|
|
|
227
|
-
test 'builder
|
|
325
|
+
test 'builder does not propagate input options to wrapper' do
|
|
228
326
|
with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
|
|
229
327
|
assert_no_select 'form div.input.my_input.string'
|
|
230
328
|
assert_select 'form input#my_input.my_input.string'
|
|
231
329
|
end
|
|
232
330
|
|
|
233
|
-
test 'builder
|
|
234
|
-
swap_wrapper :default,
|
|
331
|
+
test 'builder does not propagate input options to wrapper with custom wrapper' do
|
|
332
|
+
swap_wrapper :default, custom_wrapper_with_wrapped_input do
|
|
235
333
|
with_form_for @user, :name, input_html: { class: 'my_input' }
|
|
236
334
|
assert_no_select 'form div.input.my_input'
|
|
237
335
|
assert_select 'form input.my_input.string'
|
|
238
336
|
end
|
|
239
337
|
end
|
|
240
338
|
|
|
241
|
-
test 'builder
|
|
242
|
-
swap_wrapper :default,
|
|
339
|
+
test 'builder does not propagate label options to wrapper with custom wrapper' do
|
|
340
|
+
swap_wrapper :default, custom_wrapper_with_wrapped_label do
|
|
243
341
|
with_form_for @user, :name, label_html: { class: 'my_label' }
|
|
244
342
|
assert_no_select 'form div.label.my_label'
|
|
245
343
|
assert_select 'form label.my_label.string'
|
|
246
344
|
end
|
|
247
345
|
end
|
|
248
346
|
|
|
249
|
-
test 'builder
|
|
347
|
+
test 'builder generates an input with label' do
|
|
250
348
|
with_form_for @user, :name
|
|
251
349
|
assert_select 'form label.string[for=user_name]', /Name/
|
|
252
350
|
end
|
|
253
351
|
|
|
254
|
-
test 'builder
|
|
352
|
+
test 'builder is able to disable the label for an input' do
|
|
255
353
|
with_form_for @user, :name, label: false
|
|
256
354
|
assert_no_select 'form label'
|
|
257
355
|
end
|
|
258
356
|
|
|
259
|
-
test 'builder
|
|
357
|
+
test 'builder is able to disable the label for an input and return a html safe string' do
|
|
260
358
|
with_form_for @user, :name, label: false, wrapper: custom_wrapper_with_wrapped_label_input
|
|
261
359
|
assert_select 'form input#user_name'
|
|
262
360
|
end
|
|
263
361
|
|
|
264
|
-
test 'builder
|
|
362
|
+
test 'builder uses custom label' do
|
|
265
363
|
with_form_for @user, :name, label: 'Yay!'
|
|
266
364
|
assert_select 'form label', /Yay!/
|
|
267
365
|
end
|
|
268
366
|
|
|
269
|
-
test 'builder
|
|
367
|
+
test 'builder passes options to label' do
|
|
270
368
|
with_form_for @user, :name, label_html: { id: "cool" }
|
|
271
369
|
assert_select 'form label#cool', /Name/
|
|
272
370
|
end
|
|
273
371
|
|
|
274
|
-
test 'builder
|
|
372
|
+
test 'builder does not generate hints for an input' do
|
|
275
373
|
with_form_for @user, :name
|
|
276
374
|
assert_no_select 'span.hint'
|
|
277
375
|
end
|
|
278
376
|
|
|
279
|
-
test 'builder
|
|
377
|
+
test 'builder is able to add a hint for an input' do
|
|
280
378
|
with_form_for @user, :name, hint: 'test'
|
|
281
379
|
assert_select 'span.hint', 'test'
|
|
282
380
|
end
|
|
283
381
|
|
|
284
|
-
test 'builder
|
|
382
|
+
test 'builder is able to disable a hint even if it exists in i18n' do
|
|
285
383
|
store_translations(:en, simple_form: { hints: { name: 'Hint test' } }) do
|
|
286
384
|
stub_any_instance(SimpleForm::Inputs::Base, :hint, -> { raise 'Never' }) do
|
|
287
385
|
with_form_for @user, :name, hint: false
|
|
@@ -290,32 +388,32 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
290
388
|
end
|
|
291
389
|
end
|
|
292
390
|
|
|
293
|
-
test 'builder
|
|
391
|
+
test 'builder passes options to hint' do
|
|
294
392
|
with_form_for @user, :name, hint: 'test', hint_html: { id: "cool" }
|
|
295
393
|
assert_select 'span.hint#cool', 'test'
|
|
296
394
|
end
|
|
297
395
|
|
|
298
|
-
test 'builder
|
|
396
|
+
test 'builder generates errors for attribute without errors' do
|
|
299
397
|
with_form_for @user, :credit_limit
|
|
300
398
|
assert_no_select 'span.errors'
|
|
301
399
|
end
|
|
302
400
|
|
|
303
|
-
test 'builder
|
|
401
|
+
test 'builder generates errors for attribute with errors' do
|
|
304
402
|
with_form_for @user, :name
|
|
305
|
-
assert_select 'span.error', "
|
|
403
|
+
assert_select 'span.error', "cannot be blank"
|
|
306
404
|
end
|
|
307
405
|
|
|
308
|
-
test 'builder
|
|
406
|
+
test 'builder is able to disable showing errors for an input' do
|
|
309
407
|
with_form_for @user, :name, error: false
|
|
310
408
|
assert_no_select 'span.error'
|
|
311
409
|
end
|
|
312
410
|
|
|
313
|
-
test 'builder
|
|
411
|
+
test 'builder passes options to errors' do
|
|
314
412
|
with_form_for @user, :name, error_html: { id: "cool" }
|
|
315
|
-
assert_select 'span.error#cool', "
|
|
413
|
+
assert_select 'span.error#cool', "cannot be blank"
|
|
316
414
|
end
|
|
317
415
|
|
|
318
|
-
test 'placeholder
|
|
416
|
+
test 'placeholder does not be generated when set to false' do
|
|
319
417
|
store_translations(:en, simple_form: { placeholders: { user: {
|
|
320
418
|
name: 'Name goes here'
|
|
321
419
|
} } }) do
|
|
@@ -325,31 +423,31 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
325
423
|
end
|
|
326
424
|
|
|
327
425
|
# DEFAULT OPTIONS
|
|
328
|
-
[
|
|
329
|
-
test "builder
|
|
426
|
+
%i[input input_field].each do |method|
|
|
427
|
+
test "builder receives a default argument and pass it to the inputs when calling '#{method}'" do
|
|
330
428
|
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
331
|
-
f.
|
|
429
|
+
f.public_send(method, :name)
|
|
332
430
|
end
|
|
333
431
|
assert_select 'input.default_class'
|
|
334
432
|
end
|
|
335
433
|
|
|
336
|
-
test "builder
|
|
434
|
+
test "builder receives a default argument and pass it to the inputs without changing the defaults when calling '#{method}'" do
|
|
337
435
|
with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
|
|
338
|
-
concat(f.
|
|
339
|
-
concat(f.
|
|
436
|
+
concat(f.public_send(method, :name))
|
|
437
|
+
concat(f.public_send(method, :credit_limit))
|
|
340
438
|
end
|
|
341
439
|
|
|
342
440
|
assert_select "input.string.default_class[name='user[name]']"
|
|
343
441
|
assert_no_select "input.string[name='user[credit_limit]']"
|
|
344
442
|
end
|
|
345
443
|
|
|
346
|
-
test "builder
|
|
444
|
+
test "builder receives a default argument and pass it to the inputs and nested form when calling '#{method}'" do
|
|
347
445
|
@user.company = Company.new(1, 'Empresa')
|
|
348
446
|
|
|
349
447
|
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
350
|
-
concat(f.
|
|
448
|
+
concat(f.public_send(method, :name))
|
|
351
449
|
concat(f.simple_fields_for(:company) do |company_form|
|
|
352
|
-
concat(company_form.
|
|
450
|
+
concat(company_form.public_send(method, :name))
|
|
353
451
|
end)
|
|
354
452
|
end
|
|
355
453
|
|
|
@@ -358,28 +456,28 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
358
456
|
end
|
|
359
457
|
end
|
|
360
458
|
|
|
361
|
-
test "builder
|
|
459
|
+
test "builder receives a default argument and pass it to the inputs when calling 'input', respecting the specific options" do
|
|
362
460
|
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
363
461
|
f.input :name, input_html: { id: 'specific_id' }
|
|
364
462
|
end
|
|
365
463
|
assert_select 'input.default_class#specific_id'
|
|
366
464
|
end
|
|
367
465
|
|
|
368
|
-
test "builder
|
|
466
|
+
test "builder receives a default argument and pass it to the inputs when calling 'input_field', respecting the specific options" do
|
|
369
467
|
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
370
468
|
f.input_field :name, id: 'specific_id'
|
|
371
469
|
end
|
|
372
470
|
assert_select 'input.default_class#specific_id'
|
|
373
471
|
end
|
|
374
472
|
|
|
375
|
-
test "builder
|
|
473
|
+
test "builder receives a default argument and pass it to the inputs when calling 'input', overwriting the defaults with specific options" do
|
|
376
474
|
with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
|
|
377
475
|
f.input :name, input_html: { id: 'specific_id' }
|
|
378
476
|
end
|
|
379
477
|
assert_select 'input.default_class#specific_id'
|
|
380
478
|
end
|
|
381
479
|
|
|
382
|
-
test "builder
|
|
480
|
+
test "builder receives a default argument and pass it to the inputs when calling 'input_field', overwriting the defaults with specific options" do
|
|
383
481
|
with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
|
|
384
482
|
f.input_field :name, id: 'specific_id'
|
|
385
483
|
end
|
|
@@ -387,24 +485,24 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
387
485
|
end
|
|
388
486
|
|
|
389
487
|
# WITHOUT OBJECT
|
|
390
|
-
test 'builder
|
|
488
|
+
test 'builder generates properly when object is not present' do
|
|
391
489
|
with_form_for :project, :name
|
|
392
490
|
assert_select 'form input.string#project_name'
|
|
393
491
|
end
|
|
394
492
|
|
|
395
|
-
test 'builder
|
|
493
|
+
test 'builder generates password fields based on attribute name when object is not present' do
|
|
396
494
|
with_form_for :project, :password_confirmation
|
|
397
495
|
assert_select 'form input[type=password].password#project_password_confirmation'
|
|
398
496
|
end
|
|
399
497
|
|
|
400
|
-
test 'builder
|
|
498
|
+
test 'builder generates text fields by default for all attributes when object is not present' do
|
|
401
499
|
with_form_for :project, :created_at
|
|
402
500
|
assert_select 'form input.string#project_created_at'
|
|
403
501
|
with_form_for :project, :budget
|
|
404
502
|
assert_select 'form input.string#project_budget'
|
|
405
503
|
end
|
|
406
504
|
|
|
407
|
-
test 'builder
|
|
505
|
+
test 'builder allows overriding input type when object is not present' do
|
|
408
506
|
with_form_for :project, :created_at, as: :datetime
|
|
409
507
|
assert_select 'form select.datetime#project_created_at_1i'
|
|
410
508
|
with_form_for :project, :budget, as: :decimal
|
|
@@ -412,24 +510,24 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
412
510
|
end
|
|
413
511
|
|
|
414
512
|
# CUSTOM FORM BUILDER
|
|
415
|
-
test 'custom builder
|
|
513
|
+
test 'custom builder inherits mappings' do
|
|
416
514
|
with_custom_form_for @user, :email
|
|
417
515
|
assert_select 'form input[type=email]#user_email.custom'
|
|
418
516
|
end
|
|
419
517
|
|
|
420
|
-
test 'form with CustomMapTypeFormBuilder
|
|
518
|
+
test 'form with CustomMapTypeFormBuilder uses custom map type builder' do
|
|
421
519
|
with_concat_custom_mapping_form_for(:user) do |user|
|
|
422
520
|
assert user.instance_of?(CustomMapTypeFormBuilder)
|
|
423
521
|
end
|
|
424
522
|
end
|
|
425
523
|
|
|
426
|
-
test 'form with CustomMapTypeFormBuilder
|
|
524
|
+
test 'form with CustomMapTypeFormBuilder uses custom mapping' do
|
|
427
525
|
with_concat_custom_mapping_form_for(:user) do |user|
|
|
428
526
|
assert_equal SimpleForm::Inputs::StringInput, user.class.mappings[:custom_type]
|
|
429
527
|
end
|
|
430
528
|
end
|
|
431
529
|
|
|
432
|
-
test 'form without CustomMapTypeFormBuilder
|
|
530
|
+
test 'form without CustomMapTypeFormBuilder does not use custom mapping' do
|
|
433
531
|
with_concat_form_for(:user) do |user|
|
|
434
532
|
assert_nil user.class.mappings[:custom_type]
|
|
435
533
|
end
|