simple_form 3.0.4 → 5.0.3

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 (107) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +199 -33
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +453 -128
  5. data/lib/generators/simple_form/install_generator.rb +4 -3
  6. data/lib/generators/simple_form/templates/README +3 -5
  7. data/lib/generators/simple_form/templates/_form.html.erb +2 -0
  8. data/lib/generators/simple_form/templates/_form.html.haml +2 -0
  9. data/lib/generators/simple_form/templates/_form.html.slim +1 -0
  10. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +47 -16
  11. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +418 -23
  12. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +101 -5
  13. data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
  14. data/lib/simple_form/action_view_extensions/builder.rb +2 -0
  15. data/lib/simple_form/action_view_extensions/form_helper.rb +10 -3
  16. data/lib/simple_form/components/errors.rb +39 -6
  17. data/lib/simple_form/components/hints.rb +3 -2
  18. data/lib/simple_form/components/html5.rb +16 -5
  19. data/lib/simple_form/components/label_input.rb +21 -2
  20. data/lib/simple_form/components/labels.rb +22 -11
  21. data/lib/simple_form/components/maxlength.rb +9 -5
  22. data/lib/simple_form/components/min_max.rb +2 -1
  23. data/lib/simple_form/components/minlength.rb +38 -0
  24. data/lib/simple_form/components/pattern.rb +2 -1
  25. data/lib/simple_form/components/placeholders.rb +4 -3
  26. data/lib/simple_form/components/readonly.rb +2 -1
  27. data/lib/simple_form/components.rb +2 -0
  28. data/lib/simple_form/error_notification.rb +1 -0
  29. data/lib/simple_form/form_builder.rb +220 -89
  30. data/lib/simple_form/helpers/autofocus.rb +1 -0
  31. data/lib/simple_form/helpers/disabled.rb +1 -0
  32. data/lib/simple_form/helpers/readonly.rb +1 -0
  33. data/lib/simple_form/helpers/required.rb +1 -0
  34. data/lib/simple_form/helpers/validators.rb +2 -1
  35. data/lib/simple_form/helpers.rb +6 -5
  36. data/lib/simple_form/i18n_cache.rb +1 -0
  37. data/lib/simple_form/inputs/base.rb +62 -16
  38. data/lib/simple_form/inputs/block_input.rb +2 -1
  39. data/lib/simple_form/inputs/boolean_input.rb +40 -16
  40. data/lib/simple_form/inputs/collection_check_boxes_input.rb +3 -2
  41. data/lib/simple_form/inputs/collection_input.rb +37 -14
  42. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +9 -13
  43. data/lib/simple_form/inputs/collection_select_input.rb +5 -2
  44. data/lib/simple_form/inputs/color_input.rb +14 -0
  45. data/lib/simple_form/inputs/date_time_input.rb +24 -9
  46. data/lib/simple_form/inputs/file_input.rb +5 -2
  47. data/lib/simple_form/inputs/grouped_collection_select_input.rb +16 -3
  48. data/lib/simple_form/inputs/hidden_input.rb +5 -2
  49. data/lib/simple_form/inputs/numeric_input.rb +6 -4
  50. data/lib/simple_form/inputs/password_input.rb +6 -3
  51. data/lib/simple_form/inputs/priority_input.rb +5 -6
  52. data/lib/simple_form/inputs/range_input.rb +2 -1
  53. data/lib/simple_form/inputs/rich_text_area_input.rb +12 -0
  54. data/lib/simple_form/inputs/string_input.rb +7 -4
  55. data/lib/simple_form/inputs/text_input.rb +6 -3
  56. data/lib/simple_form/inputs.rb +3 -0
  57. data/lib/simple_form/map_type.rb +1 -0
  58. data/lib/simple_form/railtie.rb +8 -0
  59. data/lib/simple_form/tags.rb +13 -2
  60. data/lib/simple_form/version.rb +2 -1
  61. data/lib/simple_form/wrappers/builder.rb +7 -6
  62. data/lib/simple_form/wrappers/leaf.rb +29 -0
  63. data/lib/simple_form/wrappers/many.rb +7 -6
  64. data/lib/simple_form/wrappers/root.rb +10 -3
  65. data/lib/simple_form/wrappers/single.rb +7 -4
  66. data/lib/simple_form/wrappers.rb +2 -0
  67. data/lib/simple_form.rb +137 -21
  68. data/test/action_view_extensions/builder_test.rb +64 -45
  69. data/test/action_view_extensions/form_helper_test.rb +36 -16
  70. data/test/components/custom_components_test.rb +62 -0
  71. data/test/components/label_test.rb +70 -41
  72. data/test/form_builder/association_test.rb +85 -37
  73. data/test/form_builder/button_test.rb +11 -10
  74. data/test/form_builder/error_notification_test.rb +2 -1
  75. data/test/form_builder/error_test.rb +146 -33
  76. data/test/form_builder/general_test.rb +183 -81
  77. data/test/form_builder/hint_test.rb +24 -18
  78. data/test/form_builder/input_field_test.rb +105 -75
  79. data/test/form_builder/label_test.rb +68 -13
  80. data/test/form_builder/wrapper_test.rb +197 -22
  81. data/test/generators/simple_form_generator_test.rb +8 -7
  82. data/test/inputs/boolean_input_test.rb +97 -6
  83. data/test/inputs/collection_check_boxes_input_test.rb +117 -25
  84. data/test/inputs/collection_radio_buttons_input_test.rb +176 -54
  85. data/test/inputs/collection_select_input_test.rb +189 -77
  86. data/test/inputs/color_input_test.rb +10 -0
  87. data/test/inputs/datetime_input_test.rb +121 -50
  88. data/test/inputs/disabled_test.rb +29 -15
  89. data/test/inputs/discovery_test.rb +79 -6
  90. data/test/inputs/file_input_test.rb +3 -2
  91. data/test/inputs/general_test.rb +23 -22
  92. data/test/inputs/grouped_collection_select_input_test.rb +54 -17
  93. data/test/inputs/hidden_input_test.rb +5 -4
  94. data/test/inputs/numeric_input_test.rb +48 -44
  95. data/test/inputs/priority_input_test.rb +17 -16
  96. data/test/inputs/readonly_test.rb +20 -19
  97. data/test/inputs/required_test.rb +58 -13
  98. data/test/inputs/rich_text_area_input_test.rb +15 -0
  99. data/test/inputs/string_input_test.rb +58 -36
  100. data/test/inputs/text_input_test.rb +20 -7
  101. data/test/simple_form_test.rb +9 -0
  102. data/test/support/discovery_inputs.rb +40 -2
  103. data/test/support/misc_helpers.rb +113 -5
  104. data/test/support/mock_controller.rb +7 -1
  105. data/test/support/models.rb +162 -39
  106. data/test/test_helper.rb +19 -4
  107. metadata +51 -43
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: UTF-8
2
3
  require 'test_helper'
3
4
 
@@ -7,34 +8,34 @@ class IsolatedLabelTest < ActionView::TestCase
7
8
  SimpleForm::Inputs::Base.reset_i18n_cache :translate_required_html
8
9
  end
9
10
 
10
- def with_label_for(object, attribute_name, type, options={})
11
+ def with_label_for(object, attribute_name, type, options = {})
11
12
  with_concat_form_for(object) do |f|
12
13
  options[:reflection] = Association.new(Company, :company, {}) if options.delete(:setup_association)
13
14
  SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).label
14
15
  end
15
16
  end
16
17
 
17
- test 'label should generate a default humanized description' do
18
+ test 'label generates a default humanized description' do
18
19
  with_label_for @user, :name, :string
19
20
  assert_select 'label[for=user_name]', /Name/
20
21
  end
21
22
 
22
- test 'label should allow a customized description' do
23
+ test 'label allows a customized description' do
23
24
  with_label_for @user, :name, :string, label: 'My label!'
24
25
  assert_select 'label[for=user_name]', /My label!/
25
26
  end
26
27
 
27
- test 'label should use human attribute name from object when available' do
28
+ test 'label uses human attribute name from object when available' do
28
29
  with_label_for @user, :description, :text
29
30
  assert_select 'label[for=user_description]', /User Description!/
30
31
  end
31
32
 
32
- test 'label should use human attribute name based on association name' do
33
+ test 'label uses human attribute name based on association name' do
33
34
  with_label_for @user, :company_id, :string, setup_association: true
34
35
  assert_select 'label', /Company Human Name!/
35
36
  end
36
37
 
37
- test 'label should use i18n based on model, action, and attribute to lookup translation' do
38
+ test 'label uses i18n based on model, action, and attribute to lookup translation' do
38
39
  @controller.action_name = "new"
39
40
  store_translations(:en, simple_form: { labels: { user: {
40
41
  new: { description: 'Nova descrição' }
@@ -44,7 +45,7 @@ class IsolatedLabelTest < ActionView::TestCase
44
45
  end
45
46
  end
46
47
 
47
- test 'label should fallback to new when action is create' do
48
+ test 'label fallbacks to new when action is create' do
48
49
  @controller.action_name = "create"
49
50
  store_translations(:en, simple_form: { labels: { user: {
50
51
  new: { description: 'Nova descrição' }
@@ -54,7 +55,7 @@ class IsolatedLabelTest < ActionView::TestCase
54
55
  end
55
56
  end
56
57
 
57
- test 'label should not explode while looking for i18n translation when action is not set' do
58
+ test 'label does not explode while looking for i18n translation when action is not set' do
58
59
  def @controller.action_name; nil; end
59
60
 
60
61
  assert_nothing_raised do
@@ -63,7 +64,7 @@ class IsolatedLabelTest < ActionView::TestCase
63
64
  assert_select 'label[for=user_description]'
64
65
  end
65
66
 
66
- test 'label should use i18n based on model and attribute to lookup translation' do
67
+ test 'label uses i18n based on model and attribute to lookup translation' do
67
68
  store_translations(:en, simple_form: { labels: { user: {
68
69
  description: 'Descrição'
69
70
  } } }) do
@@ -72,14 +73,14 @@ class IsolatedLabelTest < ActionView::TestCase
72
73
  end
73
74
  end
74
75
 
75
- test 'label should use i18n under defaults to lookup translation' do
76
+ test 'label uses i18n under defaults to lookup translation' do
76
77
  store_translations(:en, simple_form: { labels: { defaults: { age: 'Idade' } } }) do
77
78
  with_label_for @user, :age, :integer
78
79
  assert_select 'label[for=user_age]', /Idade/
79
80
  end
80
81
  end
81
82
 
82
- test 'label should not use i18n label if translate is false' do
83
+ test 'label does not use i18n label if translate is false' do
83
84
  swap SimpleForm, translate_labels: false do
84
85
  store_translations(:en, simple_form: { labels: { defaults: { age: 'Idade' } } }) do
85
86
  with_label_for @user, :age, :integer
@@ -107,7 +108,7 @@ class IsolatedLabelTest < ActionView::TestCase
107
108
  end
108
109
  end
109
110
 
110
- test 'label should do correct i18n lookup for nested models with nested translation' do
111
+ test 'label does correct i18n lookup for nested models with nested translation' do
111
112
  @user.company = Company.new(1, 'Empresa')
112
113
 
113
114
  store_translations(:en, simple_form: { labels: {
@@ -125,7 +126,7 @@ class IsolatedLabelTest < ActionView::TestCase
125
126
  end
126
127
  end
127
128
 
128
- test 'label should do correct i18n lookup for nested models with no nested translation' do
129
+ test 'label does correct i18n lookup for nested models with no nested translation' do
129
130
  @user.company = Company.new(1, 'Empresa')
130
131
 
131
132
  store_translations(:en, simple_form: { labels: {
@@ -144,7 +145,7 @@ class IsolatedLabelTest < ActionView::TestCase
144
145
  end
145
146
  end
146
147
 
147
- test 'label should do correct i18n lookup for nested has_many models with no nested translation' do
148
+ test 'label does correct i18n lookup for nested has_many models with no nested translation' do
148
149
  @user.tags = [Tag.new(1, 'Empresa')]
149
150
 
150
151
  store_translations(:en, simple_form: { labels: {
@@ -163,7 +164,7 @@ class IsolatedLabelTest < ActionView::TestCase
163
164
  end
164
165
  end
165
166
 
166
- test 'label should have css class from type' do
167
+ test 'label has css class from type' do
167
168
  with_label_for @user, :name, :string
168
169
  assert_select 'label.string'
169
170
  with_label_for @user, :description, :text
@@ -176,8 +177,8 @@ class IsolatedLabelTest < ActionView::TestCase
176
177
  assert_select 'label.datetime'
177
178
  end
178
179
 
179
- test 'label should not have css class from type when generate_additional_classes_for does not include :label' do
180
- swap SimpleForm, generate_additional_classes_for: [:wrapper, :input] do
180
+ test 'label does not have css class from type when generate_additional_classes_for does not include :label' do
181
+ swap SimpleForm, generate_additional_classes_for: %i[wrapper input] do
181
182
  with_label_for @user, :name, :string
182
183
  assert_no_select 'label.string'
183
184
  with_label_for @user, :description, :text
@@ -191,22 +192,22 @@ class IsolatedLabelTest < ActionView::TestCase
191
192
  end
192
193
  end
193
194
 
194
- test 'label should not generate empty css class' do
195
- swap SimpleForm, generate_additional_classes_for: [:wrapper, :input] do
195
+ test 'label does not generate empty css class' do
196
+ swap SimpleForm, generate_additional_classes_for: %i[wrapper input] do
196
197
  with_label_for @user, :name, :string
197
198
  assert_no_select 'label[class]'
198
199
  end
199
200
  end
200
201
 
201
- test 'label should obtain required from ActiveModel::Validations when it is included' do
202
+ test 'label obtains required from ActiveModel::Validations when it is included' do
202
203
  with_label_for @validating_user, :name, :string
203
204
  assert_select 'label.required'
204
205
  with_label_for @validating_user, :status, :string
205
206
  assert_select 'label.optional'
206
207
  end
207
208
 
208
- test 'label should not obtain required from ActiveModel::Validations when generate_additional_classes_for does not include :label' do
209
- swap SimpleForm, generate_additional_classes_for: [:wrapper, :input] do
209
+ test 'label does not obtain required from ActiveModel::Validations when generate_additional_classes_for does not include :label' do
210
+ swap SimpleForm, generate_additional_classes_for: %i[wrapper input] do
210
211
  with_label_for @validating_user, :name, :string
211
212
  assert_no_select 'label.required'
212
213
  with_label_for @validating_user, :status, :string
@@ -214,48 +215,66 @@ class IsolatedLabelTest < ActionView::TestCase
214
215
  end
215
216
  end
216
217
 
217
- test 'label should allow overriding required when ActiveModel::Validations is included' do
218
+ test 'label allows overriding required when ActiveModel::Validations is included' do
218
219
  with_label_for @validating_user, :name, :string, required: false
219
220
  assert_select 'label.optional'
220
221
  with_label_for @validating_user, :status, :string, required: true
221
222
  assert_select 'label.required'
222
223
  end
223
224
 
224
- test 'label should be required by default when ActiveModel::Validations is not included' do
225
+ test 'label is required by default when ActiveModel::Validations is not included' do
225
226
  with_label_for @user, :name, :string
226
227
  assert_select 'label.required'
227
228
  end
228
229
 
229
- test 'label should be able to disable required when ActiveModel::Validations is not included' do
230
+ test 'label is able to disable required when ActiveModel::Validations is not included' do
230
231
  with_label_for @user, :name, :string, required: false
231
232
  assert_no_select 'label.required'
232
233
  end
233
234
 
234
- test 'label should add required text when required' do
235
+ test 'label adds required text when required' do
235
236
  with_label_for @user, :name, :string
236
237
  assert_select 'label.required abbr[title=required]', '*'
237
238
  end
238
239
 
239
- test 'label should not have required text in no required inputs' do
240
+ test 'label does not have required text in no required inputs' do
240
241
  with_label_for @user, :name, :string, required: false
241
242
  assert_no_select 'form label abbr'
242
243
  end
243
244
 
244
- test 'label should use i18n to find required text' do
245
+ test 'label uses i18n to find required text' do
245
246
  store_translations(:en, simple_form: { required: { text: 'campo requerido' } }) do
246
247
  with_label_for @user, :name, :string
247
- assert_select 'form label abbr[title=campo requerido]', '*'
248
+ assert_select 'form label abbr[title="campo requerido"]', '*'
248
249
  end
249
250
  end
250
251
 
251
- test 'label should use i18n to find required mark' do
252
+ test 'label uses custom i18n scope to find required text' do
253
+ store_translations(:en, my_scope: { required: { text: 'Pflichtfeld' } }) do
254
+ swap SimpleForm, i18n_scope: :my_scope do
255
+ with_label_for @user, :name, :string
256
+ assert_select 'form label abbr[title="Pflichtfeld"]', '*'
257
+ end
258
+ end
259
+ end
260
+
261
+ test 'label uses i18n to find required mark' do
252
262
  store_translations(:en, simple_form: { required: { mark: '*-*' } }) do
253
263
  with_label_for @user, :name, :string
254
264
  assert_select 'form label abbr', '*-*'
255
265
  end
256
266
  end
257
267
 
258
- test 'label should use i18n to find required string tag' do
268
+ test 'label uses custom i18n scope to find required mark' do
269
+ store_translations(:en, my_scope: { required: { mark: '!!' } }) do
270
+ swap SimpleForm, i18n_scope: :my_scope do
271
+ with_label_for @user, :name, :string
272
+ assert_select 'form label abbr', '!!'
273
+ end
274
+ end
275
+ end
276
+
277
+ test 'label uses i18n to find required string tag' do
259
278
  store_translations(:en, simple_form: { required: { html: '<span class="required" title="requerido">*</span>' } }) do
260
279
  with_label_for @user, :name, :string
261
280
  assert_no_select 'form label abbr'
@@ -263,37 +282,47 @@ class IsolatedLabelTest < ActionView::TestCase
263
282
  end
264
283
  end
265
284
 
266
- test 'label should allow overwriting input id' do
285
+ test 'label uses custom i18n scope to find required string tag' do
286
+ store_translations(:en, my_scope: { required: { html: '<span class="mandatory" title="Pflichtfeld">!!</span>' } }) do
287
+ swap SimpleForm, i18n_scope: :my_scope do
288
+ with_label_for @user, :name, :string
289
+ assert_no_select 'form label abbr'
290
+ assert_select 'form label span.mandatory[title=Pflichtfeld]', '!!'
291
+ end
292
+ end
293
+ end
294
+
295
+ test 'label allows overwriting input id' do
267
296
  with_label_for @user, :name, :string, input_html: { id: 'my_new_id' }
268
297
  assert_select 'label[for=my_new_id]'
269
298
  end
270
299
 
271
- test 'label should allow overwriting of for attribute' do
300
+ test 'label allows overwriting of for attribute' do
272
301
  with_label_for @user, :name, :string, label_html: { for: 'my_new_id' }
273
302
  assert_select 'label[for=my_new_id]'
274
303
  end
275
304
 
276
- test 'label should allow overwriting of for attribute with input_html not containing id' do
305
+ test 'label allows overwriting of for attribute with input_html not containing id' do
277
306
  with_label_for @user, :name, :string, label_html: { for: 'my_new_id' }, input_html: { class: 'foo' }
278
307
  assert_select 'label[for=my_new_id]'
279
308
  end
280
309
 
281
- test 'label should use default input id when it was not overridden' do
310
+ test 'label uses default input id when it was not overridden' do
282
311
  with_label_for @user, :name, :string, input_html: { class: 'my_new_id' }
283
312
  assert_select 'label[for=user_name]'
284
313
  end
285
314
 
286
- test 'label should be generated properly when object is not present' do
315
+ test 'label is generated properly when object is not present' do
287
316
  with_label_for :project, :name, :string
288
317
  assert_select 'label[for=project_name]', /Name/
289
318
  end
290
319
 
291
- test 'label should include for attribute for select collection' do
292
- with_label_for @user, :sex, :select, collection: [:male, :female]
320
+ test 'label includes for attribute for select collection' do
321
+ with_label_for @user, :sex, :select, collection: %i[male female]
293
322
  assert_select 'label[for=user_sex]'
294
323
  end
295
324
 
296
- test 'label should use i18n properly when object is not present' do
325
+ test 'label uses i18n properly when object is not present' do
297
326
  store_translations(:en, simple_form: { labels: {
298
327
  project: { name: 'Nome' }
299
328
  } }) do
@@ -302,14 +331,14 @@ class IsolatedLabelTest < ActionView::TestCase
302
331
  end
303
332
  end
304
333
 
305
- test 'label should add required by default when object is not present' do
334
+ test 'label adds required by default when object is not present' do
306
335
  with_label_for :project, :name, :string
307
336
  assert_select 'label.required[for=project_name]'
308
337
  with_label_for :project, :description, :string, required: false
309
338
  assert_no_select 'label.required[for=project_description]'
310
339
  end
311
340
 
312
- test 'label should add chosen label class' do
341
+ test 'label adds chosen label class' do
313
342
  swap SimpleForm, label_class: :my_custom_class do
314
343
  with_label_for @user, :name, :string
315
344
  assert_select 'label.my_custom_class'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: UTF-8
2
3
  require 'test_helper'
3
4
 
@@ -8,12 +9,18 @@ class AssociationTest < ActionView::TestCase
8
9
  end
9
10
  end
10
11
 
11
- test 'builder should not allow creating an association input when no object exists' do
12
+ test 'builder does not allow creating an association input when no object exists' do
12
13
  assert_raise ArgumentError do
13
14
  with_association_for :post, :author
14
15
  end
15
16
  end
16
17
 
18
+ test 'builder association works with decorated object responsive to #to_model' do
19
+ assert_nothing_raised do
20
+ with_association_for @decorated_user, :company
21
+ end
22
+ end
23
+
17
24
  test 'builder association with a block calls simple_fields_for' do
18
25
  simple_form_for @user do |f|
19
26
  f.association :posts do |posts_form|
@@ -46,9 +53,9 @@ class AssociationTest < ActionView::TestCase
46
53
 
47
54
  with_association_for @user, :tags
48
55
  assert_select 'form select.select#user_tag_ids'
49
- assert_select 'form select option[value=1]', 'Tag 1'
50
- assert_select 'form select option[value=2]', 'Tag 2'
51
- assert_select 'form select option[value=3]', 'Tag 3'
56
+ assert_select 'form select option[value="1"]', 'Tag 1'
57
+ assert_select 'form select option[value="2"]', 'Tag 2'
58
+ assert_select 'form select option[value="3"]', 'Tag 3'
52
59
 
53
60
  value.verify
54
61
  end
@@ -59,9 +66,9 @@ class AssociationTest < ActionView::TestCase
59
66
 
60
67
  with_association_for @user, :tags, preload: false
61
68
  assert_select 'form select.select#user_tag_ids'
62
- assert_select 'form select option[value=1]', 'Tag 1'
63
- assert_select 'form select option[value=2]', 'Tag 2'
64
- assert_select 'form select option[value=3]', 'Tag 3'
69
+ assert_select 'form select option[value="1"]', 'Tag 1'
70
+ assert_select 'form select option[value="2"]', 'Tag 2'
71
+ assert_select 'form select option[value="3"]', 'Tag 3'
65
72
 
66
73
  assert_raises MockExpectationError do
67
74
  value.verify
@@ -74,9 +81,9 @@ class AssociationTest < ActionView::TestCase
74
81
 
75
82
  with_association_for @user, :company
76
83
  assert_select 'form select.select#user_company_id'
77
- assert_select 'form select option[value=1]', 'Company 1'
78
- assert_select 'form select option[value=2]', 'Company 2'
79
- assert_select 'form select option[value=3]', 'Company 3'
84
+ assert_select 'form select option[value="1"]', 'Company 1'
85
+ assert_select 'form select option[value="2"]', 'Company 2'
86
+ assert_select 'form select option[value="3"]', 'Company 3'
80
87
 
81
88
  assert_raises MockExpectationError do
82
89
  value.verify
@@ -87,15 +94,15 @@ class AssociationTest < ActionView::TestCase
87
94
  test 'builder creates a select for belongs_to associations' do
88
95
  with_association_for @user, :company
89
96
  assert_select 'form select.select#user_company_id'
90
- assert_select 'form select option[value=1]', 'Company 1'
91
- assert_select 'form select option[value=2]', 'Company 2'
92
- assert_select 'form select option[value=3]', 'Company 3'
97
+ assert_select 'form select option[value="1"]', 'Company 1'
98
+ assert_select 'form select option[value="2"]', 'Company 2'
99
+ assert_select 'form select option[value="3"]', 'Company 3'
93
100
  end
94
101
 
95
102
  test 'builder creates blank select if collection is nil' do
96
103
  with_association_for @user, :company, collection: nil
97
104
  assert_select 'form select.select#user_company_id'
98
- assert_no_select 'form select option[value=1]', 'Company 1'
105
+ assert_no_select 'form select option[value="1"]', 'Company 1'
99
106
  end
100
107
 
101
108
  test 'builder allows collection radio for belongs_to associations' do
@@ -108,9 +115,26 @@ class AssociationTest < ActionView::TestCase
108
115
  test 'builder allows collection to have a proc as a condition' do
109
116
  with_association_for @user, :extra_special_company
110
117
  assert_select 'form select.select#user_extra_special_company_id'
111
- assert_select 'form select option[value=1]'
112
- assert_no_select 'form select option[value=2]'
113
- assert_no_select 'form select option[value=3]'
118
+ assert_select 'form select option[value="1"]'
119
+ assert_no_select 'form select option[value="2"]'
120
+ assert_no_select 'form select option[value="3"]'
121
+ end
122
+
123
+ test 'builder allows collection to have a scope' do
124
+ with_association_for @user, :special_pictures
125
+ assert_select 'form select.select#user_special_picture_ids'
126
+ assert_select 'form select option[value="3"]', '3'
127
+ assert_no_select 'form select option[value="1"]'
128
+ assert_no_select 'form select option[value="2"]'
129
+ end
130
+
131
+ test 'builder allows collection to have a scope with parameter' do
132
+ with_association_for @user, :special_tags
133
+ assert_select 'form select.select#user_special_tag_ids'
134
+ assert_select 'form select[multiple=multiple]'
135
+ assert_select 'form select option[value="1"]', 'Tag 1'
136
+ assert_no_select 'form select option[value="2"]'
137
+ assert_no_select 'form select option[value="3"]'
114
138
  end
115
139
 
116
140
  test 'builder marks the record which already belongs to the user' do
@@ -122,20 +146,20 @@ class AssociationTest < ActionView::TestCase
122
146
  end
123
147
 
124
148
  # ASSOCIATIONS - FINDERS
125
- test 'builder should use reflection conditions to find collection' do
149
+ test 'builder uses reflection conditions to find collection' do
126
150
  with_association_for @user, :special_company
127
151
  assert_select 'form select.select#user_special_company_id'
128
- assert_select 'form select option[value=1]'
129
- assert_no_select 'form select option[value=2]'
130
- assert_no_select 'form select option[value=3]'
152
+ assert_select 'form select option[value="1"]'
153
+ assert_no_select 'form select option[value="2"]'
154
+ assert_no_select 'form select option[value="3"]'
131
155
  end
132
156
 
133
- test 'builder should allow overriding collection to association input' do
157
+ test 'builder allows overriding collection to association input' do
134
158
  with_association_for @user, :company, include_blank: false,
135
159
  collection: [Company.new(999, 'Teste')]
136
160
  assert_select 'form select.select#user_company_id'
137
- assert_no_select 'form select option[value=1]'
138
- assert_select 'form select option[value=999]', 'Teste'
161
+ assert_no_select 'form select option[value="1"]'
162
+ assert_select 'form select option[value="999"]', 'Teste'
139
163
  assert_select 'form select option', count: 1
140
164
  end
141
165
 
@@ -146,26 +170,44 @@ class AssociationTest < ActionView::TestCase
146
170
  end
147
171
  end
148
172
 
173
+ test 'builder does not call where if the given association does not respond to it' do
174
+ with_association_for @user, :friends
175
+ assert_select 'form select.select#user_friend_ids'
176
+ assert_select 'form select[multiple=multiple]'
177
+ assert_select 'form select option[value="1"]', 'Friend 1'
178
+ assert_select 'form select option[value="2"]', 'Friend 2'
179
+ assert_select 'form select option[value="3"]', 'Friend 3'
180
+ end
181
+
182
+ test 'builder does not call order if the given association does not respond to it' do
183
+ with_association_for @user, :pictures
184
+ assert_select 'form select.select#user_picture_ids'
185
+ assert_select 'form select[multiple=multiple]'
186
+ assert_select 'form select option[value="1"]', 'Picture 1'
187
+ assert_select 'form select option[value="2"]', 'Picture 2'
188
+ assert_select 'form select option[value="3"]', 'Picture 3'
189
+ end
190
+
149
191
  test 'builder creates a select with multiple options for collection associations' do
150
192
  with_association_for @user, :tags
151
193
  assert_select 'form select.select#user_tag_ids'
152
194
  assert_select 'form select[multiple=multiple]'
153
- assert_select 'form select option[value=1]', 'Tag 1'
154
- assert_select 'form select option[value=2]', 'Tag 2'
155
- assert_select 'form select option[value=3]', 'Tag 3'
195
+ assert_select 'form select option[value="1"]', 'Tag 1'
196
+ assert_select 'form select option[value="2"]', 'Tag 2'
197
+ assert_select 'form select option[value="3"]', 'Tag 3'
156
198
  end
157
199
 
158
200
  test 'builder allows size to be overwritten for collection associations' do
159
201
  with_association_for @user, :tags, input_html: { size: 10 }
160
- assert_select 'form select[multiple=multiple][size=10]'
202
+ assert_select 'form select[multiple=multiple][size="10"]'
161
203
  end
162
204
 
163
205
  test 'builder marks all selected records which already belongs to user' do
164
206
  @user.tag_ids = [1, 2]
165
207
  with_association_for @user, :tags
166
- assert_select 'form select option[value=1][selected=selected]'
167
- assert_select 'form select option[value=2][selected=selected]'
168
- assert_no_select 'form select option[value=3][selected=selected]'
208
+ assert_select 'form select option[value="1"][selected=selected]'
209
+ assert_select 'form select option[value="2"][selected=selected]'
210
+ assert_no_select 'form select option[value="3"][selected=selected]'
169
211
  end
170
212
 
171
213
  test 'builder allows a collection of check boxes for collection associations' do
@@ -179,9 +221,9 @@ class AssociationTest < ActionView::TestCase
179
221
  test 'builder marks all selected records for collection boxes' do
180
222
  @user.tag_ids = [1, 2]
181
223
  with_association_for @user, :tags, as: :check_boxes
182
- assert_select 'form input[type=checkbox][value=1][checked=checked]'
183
- assert_select 'form input[type=checkbox][value=2][checked=checked]'
184
- assert_no_select 'form input[type=checkbox][value=3][checked=checked]'
224
+ assert_select 'form input[type=checkbox][value="1"][checked=checked]'
225
+ assert_select 'form input[type=checkbox][value="2"][checked=checked]'
226
+ assert_no_select 'form input[type=checkbox][value="3"][checked=checked]'
185
227
  end
186
228
 
187
229
  test 'builder with collection support giving collection and item wrapper tags' do
@@ -192,13 +234,19 @@ class AssociationTest < ActionView::TestCase
192
234
  assert_select 'form ul li', count: 3
193
235
  end
194
236
 
195
- test 'builder with collection support should not change the options hash' do
196
- options = { as: :check_boxes, collection_wrapper_tag: :ul, item_wrapper_tag: :li}
237
+ test 'builder with collection support does not change the options hash' do
238
+ options = { as: :check_boxes, collection_wrapper_tag: :ul, item_wrapper_tag: :li }
197
239
  with_association_for @user, :tags, options
198
240
 
199
241
  assert_select 'form ul', count: 1
200
242
  assert_select 'form ul li', count: 3
201
- assert_equal({ as: :check_boxes, collection_wrapper_tag: :ul, item_wrapper_tag: :li},
243
+ assert_equal({ as: :check_boxes, collection_wrapper_tag: :ul, item_wrapper_tag: :li },
202
244
  options)
203
245
  end
246
+
247
+ test 'builder with group select considers multiple select by default' do
248
+ with_association_for @user, :tags, as: :grouped_select, group_method: :group_method
249
+
250
+ assert_select 'select[multiple="multiple"].grouped_select'
251
+ end
204
252
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: UTF-8
2
3
  require 'test_helper'
3
4
 
@@ -8,33 +9,33 @@ class ButtonTest < ActionView::TestCase
8
9
  end
9
10
  end
10
11
 
11
- test 'builder should create buttons' do
12
+ test 'builder creates buttons' do
12
13
  with_button_for :post, :submit
13
- assert_select 'form input.button[type=submit][value=Save Post]'
14
+ assert_select 'form input.button[type=submit][value="Save Post"]'
14
15
  end
15
16
 
16
- test 'builder should create buttons with options' do
17
+ test 'builder creates buttons with options' do
17
18
  with_button_for :post, :submit, class: 'my_button'
18
- assert_select 'form input.button.my_button[type=submit][value=Save Post]'
19
+ assert_select 'form input.button.my_button[type=submit][value="Save Post"]'
19
20
  end
20
21
 
21
- test 'builder should not modify the options hash' do
22
+ test 'builder does not modify the options hash' do
22
23
  options = { class: 'my_button' }
23
24
  with_button_for :post, :submit, options
24
- assert_select 'form input.button.my_button[type=submit][value=Save Post]'
25
+ assert_select 'form input.button.my_button[type=submit][value="Save Post"]'
25
26
  assert_equal({ class: 'my_button' }, options)
26
27
  end
27
28
 
28
- test 'builder should create buttons for records' do
29
+ test 'builder creates buttons for records' do
29
30
  @user.new_record!
30
31
  with_button_for @user, :submit
31
- assert_select 'form input.button[type=submit][value=Create User]'
32
+ assert_select 'form input.button[type=submit][value="Create User"]'
32
33
  end
33
34
 
34
- test "builder should use the default class from the configuration" do
35
+ test "builder uses the default class from the configuration" do
35
36
  swap SimpleForm, button_class: 'btn' do
36
37
  with_button_for :post, :submit
37
- assert_select 'form input.btn[type=submit][value=Save Post]'
38
+ assert_select 'form input.btn[type=submit][value="Save Post"]'
38
39
  end
39
40
  end
40
41
 
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: UTF-8
2
3
  require 'test_helper'
3
4
 
4
5
  # Tests for f.error_notification
5
6
  class ErrorNotificationTest < ActionView::TestCase
6
- def with_error_notification_for(object, options={}, &block)
7
+ def with_error_notification_for(object, options = {}, &block)
7
8
  with_concat_form_for(object) do |f|
8
9
  f.error_notification(options)
9
10
  end