simple_form 5.1.0 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -0
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +64 -16
  5. data/lib/generators/simple_form/install_generator.rb +2 -2
  6. data/lib/generators/simple_form/templates/README +1 -1
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +1 -1
  8. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +126 -194
  9. data/lib/simple_form/components/label_input.rb +1 -1
  10. data/lib/simple_form/form_builder.rb +2 -6
  11. data/lib/simple_form/inputs/boolean_input.rb +6 -2
  12. data/lib/simple_form/inputs/grouped_collection_select_input.rb +1 -1
  13. data/lib/simple_form/inputs/priority_input.rb +16 -2
  14. data/lib/simple_form/railtie.rb +4 -0
  15. data/lib/simple_form/version.rb +1 -1
  16. data/lib/simple_form/wrappers/leaf.rb +1 -1
  17. data/lib/simple_form.rb +9 -5
  18. metadata +11 -83
  19. data/test/action_view_extensions/builder_test.rb +0 -634
  20. data/test/action_view_extensions/form_helper_test.rb +0 -172
  21. data/test/components/custom_components_test.rb +0 -62
  22. data/test/components/label_test.rb +0 -352
  23. data/test/form_builder/association_test.rb +0 -252
  24. data/test/form_builder/button_test.rb +0 -48
  25. data/test/form_builder/error_notification_test.rb +0 -80
  26. data/test/form_builder/error_test.rb +0 -281
  27. data/test/form_builder/general_test.rb +0 -539
  28. data/test/form_builder/hint_test.rb +0 -150
  29. data/test/form_builder/input_field_test.rb +0 -195
  30. data/test/form_builder/label_test.rb +0 -136
  31. data/test/form_builder/wrapper_test.rb +0 -378
  32. data/test/generators/simple_form_generator_test.rb +0 -43
  33. data/test/inputs/boolean_input_test.rb +0 -243
  34. data/test/inputs/collection_check_boxes_input_test.rb +0 -323
  35. data/test/inputs/collection_radio_buttons_input_test.rb +0 -446
  36. data/test/inputs/collection_select_input_test.rb +0 -380
  37. data/test/inputs/color_input_test.rb +0 -10
  38. data/test/inputs/datetime_input_test.rb +0 -176
  39. data/test/inputs/disabled_test.rb +0 -92
  40. data/test/inputs/discovery_test.rb +0 -142
  41. data/test/inputs/file_input_test.rb +0 -17
  42. data/test/inputs/general_test.rb +0 -133
  43. data/test/inputs/grouped_collection_select_input_test.rb +0 -183
  44. data/test/inputs/hidden_input_test.rb +0 -32
  45. data/test/inputs/numeric_input_test.rb +0 -177
  46. data/test/inputs/priority_input_test.rb +0 -50
  47. data/test/inputs/readonly_test.rb +0 -102
  48. data/test/inputs/required_test.rb +0 -158
  49. data/test/inputs/rich_text_area_input_test.rb +0 -15
  50. data/test/inputs/string_input_test.rb +0 -158
  51. data/test/inputs/text_input_test.rb +0 -37
  52. data/test/simple_form_test.rb +0 -18
  53. data/test/support/discovery_inputs.rb +0 -65
  54. data/test/support/misc_helpers.rb +0 -274
  55. data/test/support/mock_controller.rb +0 -30
  56. data/test/support/models.rb +0 -357
  57. data/test/test_helper.rb +0 -95
@@ -1,446 +0,0 @@
1
- # frozen_string_literal: true
2
- # encoding: UTF-8
3
- require 'test_helper'
4
-
5
- class CollectionRadioButtonsInputTest < ActionView::TestCase
6
- test 'input generates boolean radio buttons by default for radio types' do
7
- with_input_for @user, :active, :radio_buttons
8
- assert_select 'input[type=radio][value=true].radio_buttons#user_active_true'
9
- assert_select 'input[type=radio][value=false].radio_buttons#user_active_false'
10
- end
11
-
12
- test 'input as radio generates internal labels by default' do
13
- with_input_for @user, :active, :radio_buttons
14
- assert_select 'label[for=user_active_true]', 'Yes'
15
- assert_select 'label[for=user_active_false]', 'No'
16
- end
17
-
18
- test 'input as radio generates internal labels with accurate `for` values with nested boolean style' do
19
- swap SimpleForm, boolean_style: :nested do
20
- with_input_for @user, :active, :radio_buttons
21
- assert_select 'label[for=user_active_true]', 'Yes'
22
- assert_select 'label[for=user_active_false]', 'No'
23
- end
24
- end
25
-
26
- test 'nested label does not duplicate input id' do
27
- swap SimpleForm, boolean_style: :nested do
28
- with_input_for @user, :active, :radio_buttons, id: 'nested_id'
29
- assert_select 'input#user_active_true'
30
- assert_no_select 'label#user_active_true'
31
- end
32
- end
33
-
34
- test 'input as radio uses i18n to translate internal labels' do
35
- store_translations(:en, simple_form: { yes: 'Sim', no: 'Não' }) do
36
- with_input_for @user, :active, :radio_buttons
37
- assert_select 'label[for=user_active_true]', 'Sim'
38
- assert_select 'label[for=user_active_false]', 'Não'
39
- end
40
- end
41
-
42
- test 'input radio does not include for attribute by default' do
43
- with_input_for @user, :gender, :radio_buttons, collection: %i[male female]
44
- assert_select 'label'
45
- assert_no_select 'label[for=user_gender]'
46
- end
47
-
48
- test 'input radio includes for attribute when giving as html option' do
49
- with_input_for @user, :gender, :radio_buttons, collection: %i[male female], label_html: { for: 'gender' }
50
- assert_select 'label[for=gender]'
51
- end
52
-
53
- test 'input marks the checked value when using boolean and radios' do
54
- @user.active = false
55
- with_input_for @user, :active, :radio_buttons
56
- assert_no_select 'input[type=radio][value=true][checked]'
57
- assert_select 'input[type=radio][value=false][checked]'
58
- end
59
-
60
- test 'input allows overriding collection for radio types' do
61
- with_input_for @user, :name, :radio_buttons, collection: %w[Jose Carlos]
62
- assert_select 'input[type=radio][value=Jose]'
63
- assert_select 'input[type=radio][value=Carlos]'
64
- assert_select 'label.collection_radio_buttons[for=user_name_jose]', 'Jose'
65
- assert_select 'label.collection_radio_buttons[for=user_name_carlos]', 'Carlos'
66
- end
67
-
68
- test 'input does automatic collection translation for radio types using defaults key' do
69
- store_translations(:en, simple_form: { options: { defaults: {
70
- gender: { male: 'Male', female: 'Female' }
71
- } } } ) do
72
- with_input_for @user, :gender, :radio_buttons, collection: %i[male female]
73
- assert_select 'input[type=radio][value=male]'
74
- assert_select 'input[type=radio][value=female]'
75
- assert_select 'label.collection_radio_buttons[for=user_gender_male]', 'Male'
76
- assert_select 'label.collection_radio_buttons[for=user_gender_female]', 'Female'
77
- end
78
- end
79
-
80
- test 'input does automatic collection translation for radio types using specific object key' do
81
- store_translations(:en, simple_form: { options: { user: {
82
- gender: { male: 'Male', female: 'Female' }
83
- } } } ) do
84
- with_input_for @user, :gender, :radio_buttons, collection: %i[male female]
85
- assert_select 'input[type=radio][value=male]'
86
- assert_select 'input[type=radio][value=female]'
87
- assert_select 'label.collection_radio_buttons[for=user_gender_male]', 'Male'
88
- assert_select 'label.collection_radio_buttons[for=user_gender_female]', 'Female'
89
- end
90
- end
91
-
92
- test 'input does automatic collection translation and preserve html markup' do
93
- swap SimpleForm, boolean_style: :nested do
94
- store_translations(:en, simple_form: { options: { user: {
95
- gender: { male_html: '<strong>Male</strong>', female_html: '<strong>Female</strong>' }
96
- } } } ) do
97
- with_input_for @user, :gender, :radio_buttons, collection: %i[male female]
98
- assert_select 'input[type=radio][value=male]'
99
- assert_select 'input[type=radio][value=female]'
100
- assert_select 'label[for=user_gender_male] strong', 'Male'
101
- assert_select 'label[for=user_gender_female] strong', 'Female'
102
- end
103
- end
104
- end
105
-
106
- test 'input does automatic collection translation with keys prefixed with _html and a string value' do
107
- swap SimpleForm, boolean_style: :nested do
108
- store_translations(:en, simple_form: { options: { user: {
109
- gender: { male_html: 'Male', female_html: 'Female' }
110
- } } } ) do
111
- with_input_for @user, :gender, :radio_buttons, collection: %i[male female]
112
- assert_select 'input[type=radio][value=male]'
113
- assert_select 'input[type=radio][value=female]'
114
- assert_select 'label[for=user_gender_male]', 'Male'
115
- assert_select 'label[for=user_gender_female]', 'Female'
116
- end
117
- end
118
- end
119
-
120
- test 'input marks the current radio value by default' do
121
- @user.name = "Carlos"
122
- with_input_for @user, :name, :radio_buttons, collection: %w[Jose Carlos]
123
- assert_select 'input[type=radio][value=Carlos][checked=checked]'
124
- end
125
-
126
- test 'input accepts html options as the last element of collection' do
127
- with_input_for @user, :name, :radio_buttons, collection: [['Jose', 'jose', class: 'foo']]
128
- assert_select 'input.foo[type=radio][value=jose]'
129
- end
130
-
131
- test 'input allows using a collection with text/value arrays' do
132
- with_input_for @user, :name, :radio_buttons, collection: [%w[Jose jose], %w[Carlos carlos]]
133
- assert_select 'input[type=radio][value=jose]'
134
- assert_select 'input[type=radio][value=carlos]'
135
- assert_select 'label.collection_radio_buttons', 'Jose'
136
- assert_select 'label.collection_radio_buttons', 'Carlos'
137
- end
138
-
139
- test 'input allows using a collection with a Proc' do
140
- with_input_for @user, :name, :radio_buttons, collection: proc { %w[Jose Carlos] }
141
- assert_select 'label.collection_radio_buttons', 'Jose'
142
- assert_select 'label.collection_radio_buttons', 'Carlos'
143
- end
144
-
145
- test 'input allows overriding only label method for collections' do
146
- with_input_for @user, :name, :radio_buttons,
147
- collection: %w[Jose Carlos],
148
- label_method: :upcase
149
- assert_select 'label.collection_radio_buttons', 'JOSE'
150
- assert_select 'label.collection_radio_buttons', 'CARLOS'
151
- end
152
-
153
- test 'input allows overriding only value method for collections' do
154
- with_input_for @user, :name, :radio_buttons,
155
- collection: %w[Jose Carlos],
156
- value_method: :upcase
157
- assert_select 'input[type=radio][value=JOSE]'
158
- assert_select 'input[type=radio][value=CARLOS]'
159
- end
160
-
161
- test 'input allows overriding label and value method for collections' do
162
- with_input_for @user, :name, :radio_buttons,
163
- collection: %w[Jose Carlos],
164
- label_method: :upcase,
165
- value_method: :downcase
166
- assert_select 'input[type=radio][value=jose]'
167
- assert_select 'input[type=radio][value=carlos]'
168
- assert_select 'label.collection_radio_buttons', 'JOSE'
169
- assert_select 'label.collection_radio_buttons', 'CARLOS'
170
- end
171
-
172
- test 'input allows overriding label and value method using a lambda for collections' do
173
- with_input_for @user, :name, :radio_buttons,
174
- collection: %w[Jose Carlos],
175
- label_method: ->(i) { i.upcase },
176
- value_method: ->(i) { i.downcase }
177
- assert_select 'input[type=radio][value=jose]'
178
- assert_select 'input[type=radio][value=carlos]'
179
- assert_select 'label.collection_radio_buttons', 'JOSE'
180
- assert_select 'label.collection_radio_buttons', 'CARLOS'
181
- end
182
-
183
- test 'collection input with radio type generates required html attribute' do
184
- with_input_for @user, :name, :radio_buttons, collection: %w[Jose Carlos]
185
- assert_select 'input[type=radio].required'
186
- assert_select 'input[type=radio][required]'
187
- end
188
-
189
- test 'collection input with radio type generates aria-required html attribute' do
190
- with_input_for @user, :name, :radio_buttons, collection: %w[Jose Carlos]
191
- assert_select 'input[type=radio].required'
192
- assert_select 'input[type=radio][aria-required=true]'
193
- end
194
-
195
- test 'input radio does not wrap the collection by default' do
196
- with_input_for @user, :active, :radio_buttons
197
-
198
- assert_select 'form input[type=radio]', count: 2
199
- assert_no_select 'form ul'
200
- end
201
-
202
- test 'input radio wraps the collection in the configured collection wrapper tag' do
203
- swap SimpleForm, collection_wrapper_tag: :ul do
204
- with_input_for @user, :active, :radio_buttons
205
-
206
- assert_select 'form ul input[type=radio]', count: 2
207
- end
208
- end
209
-
210
- test 'input radio does not wrap the collection when configured with falsy values' do
211
- swap SimpleForm, collection_wrapper_tag: false do
212
- with_input_for @user, :active, :radio_buttons
213
-
214
- assert_select 'form input[type=radio]', count: 2
215
- assert_no_select 'form ul'
216
- end
217
- end
218
-
219
- test 'input radio allows overriding the collection wrapper tag at input level' do
220
- swap SimpleForm, collection_wrapper_tag: :ul do
221
- with_input_for @user, :active, :radio_buttons, collection_wrapper_tag: :section
222
-
223
- assert_select 'form section input[type=radio]', count: 2
224
- assert_no_select 'form ul'
225
- end
226
- end
227
-
228
- test 'input radio allows disabling the collection wrapper tag at input level' do
229
- swap SimpleForm, collection_wrapper_tag: :ul do
230
- with_input_for @user, :active, :radio_buttons, collection_wrapper_tag: false
231
-
232
- assert_select 'form input[type=radio]', count: 2
233
- assert_no_select 'form ul'
234
- end
235
- end
236
-
237
- test 'input radio renders the wrapper tag with the configured wrapper class' do
238
- swap SimpleForm, collection_wrapper_tag: :ul, collection_wrapper_class: 'inputs-list' do
239
- with_input_for @user, :active, :radio_buttons
240
-
241
- assert_select 'form ul.inputs-list input[type=radio]', count: 2
242
- end
243
- end
244
-
245
- test 'input radio allows giving wrapper class at input level only' do
246
- swap SimpleForm, collection_wrapper_tag: :ul do
247
- with_input_for @user, :active, :radio_buttons, collection_wrapper_class: 'items-list'
248
-
249
- assert_select 'form ul.items-list input[type=radio]', count: 2
250
- end
251
- end
252
-
253
- test 'input radio uses both configured and given wrapper classes for wrapper tag' do
254
- swap SimpleForm, collection_wrapper_tag: :ul, collection_wrapper_class: 'inputs-list' do
255
- with_input_for @user, :active, :radio_buttons, collection_wrapper_class: 'items-list'
256
-
257
- assert_select 'form ul.inputs-list.items-list input[type=radio]', count: 2
258
- end
259
- end
260
-
261
- test 'input radio wraps each item in the configured item wrapper tag' do
262
- swap SimpleForm, item_wrapper_tag: :li do
263
- with_input_for @user, :active, :radio_buttons
264
-
265
- assert_select 'form li input[type=radio]', count: 2
266
- end
267
- end
268
-
269
- test 'input radio does not wrap items when configured with falsy values' do
270
- swap SimpleForm, item_wrapper_tag: false do
271
- with_input_for @user, :active, :radio_buttons
272
-
273
- assert_select 'form input[type=radio]', count: 2
274
- assert_no_select 'form li'
275
- end
276
- end
277
-
278
- test 'input radio allows overriding the item wrapper tag at input level' do
279
- swap SimpleForm, item_wrapper_tag: :li do
280
- with_input_for @user, :active, :radio_buttons, item_wrapper_tag: :dl
281
-
282
- assert_select 'form dl input[type=radio]', count: 2
283
- assert_no_select 'form li'
284
- end
285
- end
286
-
287
- test 'input radio allows disabling the item wrapper tag at input level' do
288
- swap SimpleForm, item_wrapper_tag: :ul do
289
- with_input_for @user, :active, :radio_buttons, item_wrapper_tag: false
290
-
291
- assert_select 'form input[type=radio]', count: 2
292
- assert_no_select 'form li'
293
- end
294
- end
295
-
296
- test 'input radio wraps items in a span tag by default' do
297
- with_input_for @user, :active, :radio_buttons
298
-
299
- assert_select 'form span input[type=radio]', count: 2
300
- end
301
-
302
- test 'input radio renders the item wrapper tag with a default class "radio"' do
303
- with_input_for @user, :active, :radio_buttons, item_wrapper_tag: :li
304
-
305
- assert_select 'form li.radio input[type=radio]', count: 2
306
- end
307
-
308
- test 'input radio renders the item wrapper tag with the configured item wrapper class' do
309
- swap SimpleForm, item_wrapper_tag: :li, item_wrapper_class: 'item' do
310
- with_input_for @user, :active, :radio_buttons
311
-
312
- assert_select 'form li.radio.item input[type=radio]', count: 2
313
- end
314
- end
315
-
316
- test 'input radio allows giving item wrapper class at input level only' do
317
- swap SimpleForm, item_wrapper_tag: :li do
318
- with_input_for @user, :active, :radio_buttons, item_wrapper_class: 'item'
319
-
320
- assert_select 'form li.radio.item input[type=radio]', count: 2
321
- end
322
- end
323
-
324
- test 'input radio uses both configured and given item wrapper classes for item wrapper tag' do
325
- swap SimpleForm, item_wrapper_tag: :li, item_wrapper_class: 'item' do
326
- with_input_for @user, :active, :radio_buttons, item_wrapper_class: 'inline'
327
-
328
- assert_select 'form li.radio.item.inline input[type=radio]', count: 2
329
- end
330
- end
331
-
332
- test 'input radio respects the nested boolean style config, generating nested label > input' do
333
- swap SimpleForm, boolean_style: :nested do
334
- with_input_for @user, :active, :radio_buttons
335
-
336
- assert_select 'span.radio > label > input#user_active_true[type=radio]'
337
- assert_select 'span.radio > label', 'Yes'
338
- assert_select 'span.radio > label > input#user_active_false[type=radio]'
339
- assert_select 'span.radio > label', 'No'
340
- assert_no_select 'label.collection_radio_buttons'
341
- end
342
- end
343
-
344
- test 'input radio with nested style does not overrides configured item wrapper tag' do
345
- swap SimpleForm, boolean_style: :nested, item_wrapper_tag: :li do
346
- with_input_for @user, :active, :radio_buttons
347
-
348
- assert_select 'li.radio > label > input'
349
- end
350
- end
351
-
352
- test 'input radio with nested style does not overrides given item wrapper tag' do
353
- swap SimpleForm, boolean_style: :nested do
354
- with_input_for @user, :active, :radio_buttons, item_wrapper_tag: :li
355
-
356
- assert_select 'li.radio > label > input'
357
- end
358
- end
359
-
360
- test 'input radio with nested style accepts giving extra wrapper classes' do
361
- swap SimpleForm, boolean_style: :nested do
362
- with_input_for @user, :active, :radio_buttons, item_wrapper_class: "inline"
363
-
364
- assert_select 'span.radio.inline > label > input'
365
- end
366
- end
367
-
368
- test 'input radio with nested style renders item labels with specified class' do
369
- swap SimpleForm, boolean_style: :nested do
370
- with_input_for @user, :active, :radio_buttons, item_label_class: "test"
371
-
372
- assert_select 'span.radio > label.test > input'
373
- end
374
- end
375
-
376
- test 'input radio with nested style and falsey input wrapper renders item labels with specified class' do
377
- swap SimpleForm, boolean_style: :nested, item_wrapper_tag: false do
378
- with_input_for @user, :active, :radio_buttons, item_label_class: "radio-inline"
379
-
380
- assert_select 'label.radio-inline > input'
381
- assert_no_select 'span.radio'
382
- end
383
- end
384
-
385
- test 'input radio wrapper class are not included when set to falsey' do
386
- swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do
387
- with_input_for @user, :gender, :radio_buttons, collection: %i[male female]
388
-
389
- assert_no_select 'label.radio'
390
- end
391
- end
392
-
393
- test 'input radio custom wrapper class is included when include input wrapper class is falsey' do
394
- swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do
395
- with_input_for @user, :gender, :radio_buttons, collection: %i[male female], item_wrapper_class: 'custom'
396
-
397
- assert_no_select 'label.radio'
398
- assert_select 'span.custom'
399
- end
400
- end
401
-
402
- test 'input radio with nested style and namespace uses the right for attribute' do
403
- swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do
404
- with_concat_form_for @user, namespace: :foo do |f|
405
- concat f.input :gender, as: :radio_buttons, collection: %i[male female]
406
- end
407
-
408
- assert_select 'label[for=foo_user_gender_male]'
409
- assert_select 'label[for=foo_user_gender_female]'
410
- end
411
- end
412
-
413
- test 'input radio with nested style and index uses the right for attribute' do
414
- swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do
415
- with_concat_form_for @user, index: 1 do |f|
416
- concat f.input :gender, as: :radio_buttons, collection: %i[male female]
417
- end
418
-
419
- assert_select 'label[for=user_1_gender_male]'
420
- assert_select 'label[for=user_1_gender_female]'
421
- end
422
- end
423
-
424
- test 'input radio with nested style accetps non-string attribute as label' do
425
- swap SimpleForm, boolean_style: :nested do
426
- with_input_for @user, :amount,
427
- :radio_buttons,
428
- collection: { 100 => 'hundred', 200 => 'two_hundred' },
429
- label_method: :first,
430
- value_method: :second
431
-
432
- assert_select 'input[type=radio][value=hundred]'
433
- assert_select 'input[type=radio][value=two_hundred]'
434
- assert_select 'span.radio > label', '100'
435
- assert_select 'span.radio > label', '200'
436
- end
437
- end
438
-
439
- test 'input check boxes with inline style support label custom classes' do
440
- swap SimpleForm, boolean_style: :inline do
441
- with_input_for @user, :gender, :radio_buttons, collection: %i[male female], item_label_class: 'beautiful-label'
442
-
443
- assert_select 'label.beautiful-label', count: 2
444
- end
445
- end
446
- end