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