simple_form 4.0.1 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +68 -8
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +47 -51
  5. data/lib/generators/simple_form/templates/README +2 -3
  6. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +1 -7
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +26 -25
  8. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +1 -1
  9. data/lib/simple_form.rb +22 -5
  10. data/lib/simple_form/components/errors.rb +5 -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 +30 -10
  15. data/lib/simple_form/inputs.rb +2 -0
  16. data/lib/simple_form/inputs/base.rb +4 -4
  17. data/lib/simple_form/inputs/boolean_input.rb +1 -0
  18. data/lib/simple_form/inputs/collection_check_boxes_input.rb +1 -1
  19. data/lib/simple_form/inputs/collection_input.rb +3 -5
  20. data/lib/simple_form/inputs/color_input.rb +14 -0
  21. data/lib/simple_form/inputs/priority_input.rb +0 -4
  22. data/lib/simple_form/inputs/rich_text_area_input.rb +12 -0
  23. data/lib/simple_form/inputs/string_input.rb +1 -1
  24. data/lib/simple_form/tags.rb +6 -2
  25. data/lib/simple_form/version.rb +1 -1
  26. data/lib/simple_form/wrappers/root.rb +8 -3
  27. data/test/action_view_extensions/builder_test.rb +22 -4
  28. data/test/components/label_test.rb +0 -4
  29. data/test/form_builder/association_test.rb +6 -0
  30. data/test/form_builder/error_test.rb +6 -0
  31. data/test/form_builder/general_test.rb +23 -19
  32. data/test/form_builder/input_field_test.rb +3 -9
  33. data/test/form_builder/label_test.rb +1 -1
  34. data/test/form_builder/wrapper_test.rb +8 -1
  35. data/test/inputs/boolean_input_test.rb +8 -0
  36. data/test/inputs/collection_check_boxes_input_test.rb +8 -4
  37. data/test/inputs/collection_radio_buttons_input_test.rb +8 -4
  38. data/test/inputs/collection_select_input_test.rb +6 -4
  39. data/test/inputs/color_input_test.rb +10 -0
  40. data/test/inputs/datetime_input_test.rb +2 -12
  41. data/test/inputs/disabled_test.rb +13 -0
  42. data/test/inputs/discovery_test.rb +21 -0
  43. data/test/inputs/priority_input_test.rb +6 -14
  44. data/test/inputs/rich_text_area_input_test.rb +15 -0
  45. data/test/inputs/string_input_test.rb +8 -15
  46. data/test/support/discovery_inputs.rb +7 -0
  47. data/test/support/misc_helpers.rb +8 -2
  48. data/test/support/models.rb +29 -6
  49. data/test/test_helper.rb +7 -4
  50. metadata +44 -40
  51. data/lib/simple_form/i18n_cache.rb +0 -23
@@ -224,6 +224,12 @@ class ErrorTest < ActionView::TestCase
224
224
  assert_no_select 'span.error'
225
225
  end
226
226
 
227
+ test 'input with custom error works when form does not use a model' do
228
+ with_form_for :user, :active, error: "Super User Active! cannot be blank"
229
+
230
+ assert_select 'span.error'
231
+ end
232
+
227
233
  test 'input with custom error works when using full_error component' do
228
234
  swap_wrapper :default, custom_wrapper_with_full_error do
229
235
  error_text = "Super User Name! cannot be blank"
@@ -239,31 +239,29 @@ class FormBuilderTest < ActionView::TestCase
239
239
  assert_select 'form select#user_updated_at_1i.datetime'
240
240
  end
241
241
 
242
- test 'builder generates file for file columns' do
243
- @user.avatar = MiniTest::Mock.new
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'
242
+ test 'builder generates file input for ActiveStorage >= 5.2 and Refile >= 0.2.0 <= 0.4.0' do
243
+ with_form_for UserWithAttachment.build, :avatar
244
+ assert_select 'form input#user_with_attachment_avatar.file'
249
245
  end
250
246
 
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)
247
+ test 'builder generates file input for ActiveStorage::Attached::Many' do
248
+ with_form_for UserWithAttachment.build, :avatars
249
+ assert_select 'form input#user_with_attachment_avatars.file'
250
+ end
255
251
 
256
- with_form_for @user, :avatar
257
- assert_select 'form input#user_avatar.file'
252
+ test 'builder generates file input for Refile >= 0.3.0 and CarrierWave >= 0.2.2' do
253
+ with_form_for UserWithAttachment.build, :cover
254
+ assert_select 'form input#user_with_attachment_cover.file'
258
255
  end
259
256
 
260
- test 'builder generates file for attributes that are real db columns but have file methods' do
261
- @user.home_picture = MiniTest::Mock.new
262
- @user.home_picture.expect(:mounted_as, true)
263
- @user.home_picture.expect(:!, false)
257
+ test 'builder generates file input for Refile >= 0.4.0 and Shrine >= 0.9.0' do
258
+ with_form_for UserWithAttachment.build, :profile_image
259
+ assert_select 'form input#user_with_attachment_profile_image.file'
260
+ end
264
261
 
265
- with_form_for @user, :home_picture
266
- assert_select 'form input#user_home_picture.file'
262
+ test 'builder generates file input for Paperclip ~> 2.0' do
263
+ with_form_for UserWithAttachment.build, :portrait
264
+ assert_select 'form input#user_with_attachment_portrait.file'
267
265
  end
268
266
 
269
267
  test 'build generates select if a collection is given' do
@@ -271,6 +269,12 @@ class FormBuilderTest < ActionView::TestCase
271
269
  assert_select 'form select#user_age.select'
272
270
  end
273
271
 
272
+ test 'builder does not generate url fields for columns that contain only the letters url' do
273
+ with_form_for @user, :hourly
274
+ assert_no_select 'form input#user_url.string.url'
275
+ assert_select 'form input#user_hourly.string'
276
+ end
277
+
274
278
  test 'builder allows overriding default input type for text' do
275
279
  with_form_for @user, :name, as: :text
276
280
  assert_no_select 'form input#user_name'
@@ -3,12 +3,6 @@ require 'test_helper'
3
3
 
4
4
  # Tests for f.input_field
5
5
  class InputFieldTest < ActionView::TestCase
6
- def with_input_field_for(object, *args)
7
- with_concat_form_for(object) do |f|
8
- f.input_field(*args)
9
- end
10
- end
11
-
12
6
  test "builder input_field only renders the input tag, nothing else" do
13
7
  with_input_field_for @user, :name
14
8
 
@@ -85,13 +79,13 @@ class InputFieldTest < ActionView::TestCase
85
79
  test 'builder input_field infers pattern from attributes' do
86
80
  with_input_field_for @other_validating_user, :country, as: :string, pattern: true
87
81
 
88
- assert_select 'input[pattern="\w+"]'
82
+ assert_select "input:match('pattern', ?)", /\w+/
89
83
  end
90
84
 
91
85
  test 'builder input_field accepts custom pattern' do
92
86
  with_input_field_for @other_validating_user, :country, as: :string, pattern: '\d+'
93
87
 
94
- assert_select 'input[pattern="\d+"]'
88
+ assert_select "input:match('pattern', ?)", /\\d+/
95
89
  end
96
90
 
97
91
  test 'builder input_field uses readonly component' do
@@ -138,7 +132,7 @@ class InputFieldTest < ActionView::TestCase
138
132
  swap_wrapper :default, custom_wrapper_with_html5_components do
139
133
  with_input_field_for @user, :name, pattern: '\w+'
140
134
 
141
- assert_select 'input[pattern="\w+"]'
135
+ assert_select "input:match('pattern', ?)", /\w+/
142
136
  end
143
137
  end
144
138
 
@@ -19,7 +19,7 @@ class LabelTest < ActionView::TestCase
19
19
  assert_select 'label.string[for=user_name]', /Name/
20
20
  end
21
21
 
22
- test 'builder generates a label for the boolean attrbiute' do
22
+ test 'builder generates a label for the boolean attribute' do
23
23
  with_label_for @user, :name, as: :boolean
24
24
  assert_select 'label.boolean[for=user_name]', /Name/
25
25
  assert_no_select 'label[as=boolean]'
@@ -59,6 +59,13 @@ class WrapperTest < ActionView::TestCase
59
59
  assert_no_select 'input.is-invalid'
60
60
  end
61
61
 
62
+ test 'wrapper does not determine if valid class is needed when it is set to nil' do
63
+ @user.instance_eval { undef errors }
64
+ with_form_for @user, :name, wrapper: custom_wrapper_with_input_valid_class(valid_class: nil)
65
+
66
+ assert_no_select 'div.field_without_errors'
67
+ end
68
+
62
69
  test 'wrapper adds hint class for attribute with a hint' do
63
70
  with_form_for @user, :name, hint: 'hint'
64
71
  assert_select 'div.field_with_hint'
@@ -161,7 +168,7 @@ class WrapperTest < ActionView::TestCase
161
168
  test 'custom wrappers can have full error message on attributes' do
162
169
  swap_wrapper :default, custom_wrapper_with_full_error do
163
170
  with_form_for @user, :name
164
- assert_select 'span.error', "Name cannot be blank"
171
+ assert_select 'span.error', "Super User Name! cannot be blank"
165
172
  end
166
173
  end
167
174
 
@@ -107,6 +107,14 @@ class BooleanInputTest < ActionView::TestCase
107
107
  end
108
108
  end
109
109
 
110
+ test 'input boolean with nested generates a disabled hidden field with the form attribute when it is given' do
111
+ swap SimpleForm, boolean_style: :nested do
112
+ with_input_for @user, :active, :boolean, input_html: { form: 'form_id' }
113
+
114
+ assert_select "input[type=hidden][form=form_id]+ label.boolean > input.boolean"
115
+ end
116
+ end
117
+
110
118
  test 'input accepts changing boolean style to nested through given options' do
111
119
  with_input_for @user, :active, :boolean, boolean_style: :nested
112
120
  assert_select 'label[for=user_active]', 'Active'
@@ -3,10 +3,6 @@
3
3
  require 'test_helper'
4
4
 
5
5
  class CollectionCheckBoxesInputTest < ActionView::TestCase
6
- setup do
7
- SimpleForm::Inputs::CollectionCheckBoxesInput.reset_i18n_cache :boolean_collection
8
- end
9
-
10
6
  test 'input check boxes does not include for attribute by default' do
11
7
  with_input_for @user, :gender, :check_boxes, collection: %i[male female]
12
8
  assert_select 'label'
@@ -316,4 +312,12 @@ class CollectionCheckBoxesInputTest < ActionView::TestCase
316
312
  assert_select 'span.checkbox > label', '200'
317
313
  end
318
314
  end
315
+
316
+ test 'input check boxes with inline style support label custom classes' do
317
+ swap SimpleForm, boolean_style: :inline do
318
+ with_input_for @user, :gender, :check_boxes, collection: %i[male female], item_label_class: 'beautiful-label'
319
+
320
+ assert_select 'label.beautiful-label', count: 2
321
+ end
322
+ end
319
323
  end
@@ -3,10 +3,6 @@
3
3
  require 'test_helper'
4
4
 
5
5
  class CollectionRadioButtonsInputTest < ActionView::TestCase
6
- setup do
7
- SimpleForm::Inputs::CollectionRadioButtonsInput.reset_i18n_cache :boolean_collection
8
- end
9
-
10
6
  test 'input generates boolean radio buttons by default for radio types' do
11
7
  with_input_for @user, :active, :radio_buttons
12
8
  assert_select 'input[type=radio][value=true].radio_buttons#user_active_true'
@@ -439,4 +435,12 @@ class CollectionRadioButtonsInputTest < ActionView::TestCase
439
435
  assert_select 'span.radio > label', '200'
440
436
  end
441
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
442
446
  end
@@ -3,10 +3,6 @@
3
3
  require 'test_helper'
4
4
 
5
5
  class CollectionSelectInputTest < ActionView::TestCase
6
- setup do
7
- SimpleForm::Inputs::CollectionSelectInput.reset_i18n_cache :boolean_collection
8
- end
9
-
10
6
  test 'input generates a boolean select with options by default for select types' do
11
7
  with_input_for @user, :active, :select
12
8
  assert_select 'select.select#user_active'
@@ -284,6 +280,12 @@ class CollectionSelectInputTest < ActionView::TestCase
284
280
  assert_select 'select[required]'
285
281
  end
286
282
 
283
+ test "collection input generated aria-label should contain 'true'" do
284
+ with_input_for @user, :age, :select, collection: 18..30, prompt: "Please select foo"
285
+ assert_select 'select.required'
286
+ assert_select 'select[aria-required=true]'
287
+ end
288
+
287
289
  test 'collection input with select type does not generate required html attribute without blank option' do
288
290
  with_input_for @user, :name, :select, include_blank: false, collection: %w[Jose Carlos]
289
291
  assert_select 'select.required'
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class ColorInputTest < ActionView::TestCase
6
+ test 'input generates a color field' do
7
+ with_input_for @user, :favorite_color, :color
8
+ assert_select 'input[type=color].color#user_favorite_color'
9
+ end
10
+ end
@@ -6,12 +6,7 @@ require 'test_helper'
6
6
  class DateTimeInputWithHtml5Test < ActionView::TestCase
7
7
  test 'input generates a datetime input for datetime attributes if HTML5 compatibility is explicitly enbled' do
8
8
  with_input_for @user, :created_at, :datetime, html5: true
9
-
10
- if ActionPack::VERSION::STRING >= '5'
11
- assert_select 'input[type="datetime-local"]'
12
- elsif ActionPack::VERSION::STRING < '5'
13
- assert_select 'input[type="datetime"]'
14
- end
9
+ assert_select 'input[type="datetime-local"]'
15
10
  end
16
11
 
17
12
  test 'input generates a datetime select for datetime attributes' do
@@ -80,12 +75,7 @@ class DateTimeInputWithoutHtml5Test < ActionView::TestCase
80
75
  test 'input generates a datetime input for datetime attributes if HTML5 compatibility is explicitly enabled' do
81
76
  swap_wrapper do
82
77
  with_input_for @user, :created_at, :datetime, html5: true
83
-
84
- if ActionPack::VERSION::STRING >= '5'
85
- assert_select 'input[type="datetime-local"]'
86
- elsif ActionPack::VERSION::STRING < '5'
87
- assert_select 'input[type="datetime"]'
88
- end
78
+ assert_select 'input[type="datetime-local"]'
89
79
  end
90
80
  end
91
81
 
@@ -76,4 +76,17 @@ class DisabledTest < ActionView::TestCase
76
76
  with_input_for @user, :created_at, :datetime
77
77
  assert_no_select 'select.datetime.disabled[disabled]'
78
78
  end
79
+
80
+ test 'input_field collection allows disabled select' do
81
+ with_input_field_for @user, :description, collection: ['foo', 'bar'], disabled: true
82
+ assert_select 'select[disabled]'
83
+ assert_no_select 'option[disabled]'
84
+ end
85
+
86
+ test 'input_field collection allows individual disabled options' do
87
+ with_input_field_for @user, :description, collection: ['foo', 'bar'], disabled: 'bar'
88
+ assert_no_select 'select[disabled]'
89
+ assert_no_select 'option[disabled][value=foo]'
90
+ assert_select 'option[disabled][value=bar]'
91
+ end
79
92
  end
@@ -15,6 +15,7 @@ class DiscoveryTest < ActionView::TestCase
15
15
  Object.send :remove_const, :CustomizedInput
16
16
  Object.send :remove_const, :DeprecatedInput
17
17
  Object.send :remove_const, :CollectionSelectInput
18
+ Object.send :remove_const, :FileInput
18
19
  CustomInputs.send :remove_const, :CustomizedInput
19
20
  CustomInputs.send :remove_const, :PasswordInput
20
21
  CustomInputs.send :remove_const, :NumericInput
@@ -109,6 +110,26 @@ class DiscoveryTest < ActionView::TestCase
109
110
  end
110
111
  end
111
112
 
113
+ test 'does not duplicate the html classes giving a extra class' do
114
+ discovery do
115
+ swap SimpleForm, input_class: 'custom-default-input-class' do
116
+ with_form_for @user, :active, as: :select
117
+ assert_select 'form select#user_active.select' do
118
+ # Make sure class list contains 'chosen' only once.
119
+ assert_select ":match('class', ?)", /^(?!.*\bchosen\b.*\bchosen\b).*\bchosen\b.*$/
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+ test 'new inputs can override the default input_html_classes' do
126
+ discovery do
127
+ with_form_for @user, :avatar, as: :file
128
+ assert_no_select 'form input[type=file]#user_avatar.file.file-upload'
129
+ assert_select 'form input[type=file]#user_avatar.file-upload'
130
+ end
131
+ end
132
+
112
133
  test 'inputs method without wrapper_options are deprecated' do
113
134
  discovery do
114
135
  assert_deprecated do
@@ -6,22 +6,14 @@ class PriorityInputTest < ActionView::TestCase
6
6
  test 'input generates a country select field' do
7
7
  with_input_for @user, :country, :country
8
8
  assert_select 'select#user_country'
9
- if ActionPack::VERSION::STRING >= '5'
10
- assert_select 'select option[value=BR]', 'Brazil'
11
- elsif ActionPack::VERSION::STRING < '5'
12
- assert_select 'select option[value=Brazil]', 'Brazil'
13
- end
9
+ assert_select 'select option[value=BR]', 'Brazil'
14
10
  assert_no_select 'select option[value=""][disabled=disabled]'
15
11
  end
16
12
 
17
13
  test 'input generates a country select with SimpleForm default' do
18
14
  swap SimpleForm, country_priority: [ 'Brazil' ] do
19
15
  with_input_for @user, :country, :country
20
- if ActionPack::VERSION::STRING >= '5'
21
- assert_select 'select option[value="---------------"][disabled=disabled]'
22
- elsif ActionPack::VERSION::STRING < '5'
23
- assert_select 'select option[value=""][disabled=disabled]'
24
- end
16
+ assert_select 'select option[value="---------------"][disabled=disabled]'
25
17
  end
26
18
  end
27
19
 
@@ -44,15 +36,15 @@ class PriorityInputTest < ActionView::TestCase
44
36
  assert_no_select 'select option[value=""]', /^$/
45
37
  end
46
38
 
47
- test 'priority input does not generate invalid required html attribute' do
39
+ test 'priority input does generate select element with required html attribute' do
48
40
  with_input_for @user, :country, :country
49
41
  assert_select 'select.required'
50
- assert_no_select 'select[required]'
42
+ assert_select 'select[required]'
51
43
  end
52
44
 
53
- test 'priority input does not generate invalid aria-required html attribute' do
45
+ test 'priority input does generate select element with aria-required html attribute' do
54
46
  with_input_for @user, :country, :country
55
47
  assert_select 'select.required'
56
- assert_no_select 'select[aria-required]'
48
+ assert_select 'select[aria-required]'
57
49
  end
58
50
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ # encoding: UTF-8
3
+ require 'test_helper'
4
+
5
+ class RichTextAreaInputTest < ActionView::TestCase
6
+ test 'input generates a text area for text attributes' do
7
+ with_input_for @user, :description, :text
8
+ assert_select 'textarea.text#user_description'
9
+ end
10
+
11
+ test 'input generates a text area for text attributes that accept placeholder' do
12
+ with_input_for @user, :description, :text, placeholder: 'Put in some text'
13
+ assert_select 'textarea.text[placeholder="Put in some text"]'
14
+ end
15
+ end
@@ -8,6 +8,11 @@ class StringInputTest < ActionView::TestCase
8
8
  assert_select "input#user_name[type=text][name='user[name]'][value='New in SimpleForm!']"
9
9
  end
10
10
 
11
+ test 'input maps text field to citext attribute' do
12
+ with_input_for @user, :name, :citext
13
+ assert_select "input#user_name[type=text][name='user[name]'][value='New in SimpleForm!']"
14
+ end
15
+
11
16
  test 'input generates a password field for password attributes' do
12
17
  with_input_for @user, :password, :password
13
18
  assert_select "input#user_password.password[type=password][name='user[password]']"
@@ -38,18 +43,6 @@ class StringInputTest < ActionView::TestCase
38
43
  assert_select 'input.string[minlength="5"]'
39
44
  end
40
45
 
41
- if ActionPack::VERSION::STRING < '5'
42
- test 'input does not get maxlength from validation when tokenizer present' do
43
- with_input_for @validating_user, :action, :string
44
- assert_no_select 'input.string[maxlength]'
45
- end
46
-
47
- test 'input does not get minlength from validation when tokenizer present' do
48
- with_input_for @validating_user, :action, :string
49
- assert_no_select 'input.string[minlength]'
50
- end
51
- end
52
-
53
46
  test 'input gets maxlength from validation when :is option present' do
54
47
  with_input_for @validating_user, :home_picture, :string
55
48
  assert_select 'input.string[maxlength="12"]'
@@ -88,12 +81,12 @@ class StringInputTest < ActionView::TestCase
88
81
 
89
82
  test 'input infers pattern from attributes' do
90
83
  with_input_for @other_validating_user, :country, :string, pattern: true
91
- assert_select 'input[pattern="\w+"]'
84
+ assert_select "input:match('pattern', ?)", /\w+/
92
85
  end
93
86
 
94
87
  test 'input infers pattern from attributes using proc' do
95
88
  with_input_for @other_validating_user, :name, :string, pattern: true
96
- assert_select 'input[pattern="\w+"]'
89
+ assert_select "input:match('pattern', ?)", /\w+/
97
90
  end
98
91
 
99
92
  test 'input does not infer pattern from attributes if root default is false' do
@@ -105,7 +98,7 @@ class StringInputTest < ActionView::TestCase
105
98
 
106
99
  test 'input uses given pattern from attributes' do
107
100
  with_input_for @other_validating_user, :country, :string, input_html: { pattern: "\\d+" }
108
- assert_select 'input[pattern="\d+"]'
101
+ assert_select "input:match('pattern', ?)", /\\d+/
109
102
  end
110
103
 
111
104
  test 'input does not use pattern if model has :without validation option' do