simple_form 1.2.0 → 1.4.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 (58) hide show
  1. data/.gitignore +2 -0
  2. data/.gitmodules +3 -0
  3. data/.travis.yml +6 -0
  4. data/CHANGELOG.rdoc +109 -0
  5. data/Gemfile +8 -0
  6. data/Gemfile.lock +82 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.rdoc +180 -53
  9. data/Rakefile +27 -0
  10. data/lib/generators/simple_form/USAGE +1 -1
  11. data/lib/generators/simple_form/install_generator.rb +5 -6
  12. data/lib/generators/simple_form/templates/_form.html.erb +2 -12
  13. data/lib/generators/simple_form/templates/_form.html.haml +10 -0
  14. data/lib/generators/simple_form/templates/_form.html.slim +10 -0
  15. data/lib/generators/simple_form/templates/en.yml +14 -0
  16. data/lib/generators/simple_form/templates/simple_form.rb +66 -12
  17. data/lib/simple_form/action_view_extensions/builder.rb +99 -40
  18. data/lib/simple_form/action_view_extensions/form_helper.rb +29 -6
  19. data/lib/simple_form/components/errors.rb +16 -6
  20. data/lib/simple_form/components/hints.rb +2 -2
  21. data/lib/simple_form/components/label_input.rb +13 -0
  22. data/lib/simple_form/components/labels.rb +5 -7
  23. data/lib/simple_form/components/placeholders.rb +22 -0
  24. data/lib/simple_form/components/wrapper.rb +19 -2
  25. data/lib/simple_form/components.rb +6 -4
  26. data/lib/simple_form/error_notification.rb +42 -0
  27. data/lib/simple_form/form_builder.rb +187 -72
  28. data/lib/simple_form/has_errors.rb +14 -0
  29. data/lib/simple_form/inputs/base.rb +106 -24
  30. data/lib/simple_form/inputs/block_input.rb +3 -2
  31. data/lib/simple_form/inputs/boolean_input.rb +22 -0
  32. data/lib/simple_form/inputs/collection_input.rb +46 -17
  33. data/lib/simple_form/inputs/date_time_input.rb +11 -5
  34. data/lib/simple_form/inputs/hidden_input.rb +11 -2
  35. data/lib/simple_form/inputs/mapping_input.rb +12 -6
  36. data/lib/simple_form/inputs/numeric_input.rb +55 -6
  37. data/lib/simple_form/inputs/priority_input.rb +5 -1
  38. data/lib/simple_form/inputs/string_input.rb +25 -11
  39. data/lib/simple_form/inputs.rb +1 -0
  40. data/lib/simple_form/map_type.rb +9 -6
  41. data/lib/simple_form/version.rb +1 -1
  42. data/lib/simple_form.rb +92 -8
  43. data/simple_form.gemspec +22 -0
  44. data/test/action_view_extensions/builder_test.rb +187 -51
  45. data/test/action_view_extensions/form_helper_test.rb +17 -5
  46. data/test/components/error_test.rb +23 -12
  47. data/test/components/hint_test.rb +4 -8
  48. data/test/components/label_test.rb +79 -11
  49. data/test/components/wrapper_test.rb +63 -0
  50. data/test/discovery_inputs.rb +21 -0
  51. data/test/error_notification_test.rb +62 -0
  52. data/test/form_builder_test.rb +332 -35
  53. data/test/inputs_test.rb +516 -53
  54. data/test/support/misc_helpers.rb +32 -4
  55. data/test/support/mock_controller.rb +4 -4
  56. data/test/support/models.rb +75 -11
  57. data/test/test_helper.rb +28 -12
  58. metadata +51 -11
data/test/inputs_test.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # encoding: UTF-8
1
2
  require 'test_helper'
2
3
 
3
4
  class InputTest < ActionView::TestCase
@@ -5,13 +6,13 @@ class InputTest < ActionView::TestCase
5
6
  setup do
6
7
  SimpleForm::Inputs::CollectionInput.reset_i18n_cache :boolean_collection
7
8
  end
8
-
9
+
9
10
  def with_input_for(object, attribute_name, type, options={})
10
- concat(simple_form_for object do |f|
11
- concat f.input(attribute_name, options.merge(:as => type))
12
- end)
11
+ with_concat_form_for(object) do |f|
12
+ f.input(attribute_name, options.merge(:as => type))
13
+ end
13
14
  end
14
-
15
+
15
16
  # ALL
16
17
  test 'input should generate css class based on default input type' do
17
18
  with_input_for @user, :name, :string
@@ -26,25 +27,118 @@ class InputTest < ActionView::TestCase
26
27
  assert_select 'select.datetime'
27
28
  end
28
29
 
29
- # TextFieldInput
30
- test 'input should map text field to string attribute' do
30
+ test 'input should generate disabled elements based on the disabled option' do
31
+ with_input_for @user, :name, :string, :disabled => true
32
+ assert_select 'input.string[disabled]'
33
+ with_input_for @user, :description, :text, :disabled => true
34
+ assert_select 'textarea.text[disabled]'
35
+ with_input_for @user, :age, :integer, :disabled => true
36
+ assert_select 'input.integer[disabled]'
37
+ with_input_for @user, :born_at, :date, :disabled => true
38
+ assert_select 'select.date[disabled]'
39
+ with_input_for @user, :created_at, :datetime, :disabled => true
40
+ assert_select 'select.datetime[disabled]'
41
+
42
+ with_input_for @user, :name, :string, :disabled => false
43
+ assert_select 'input.string:not([disabled])'
44
+ with_input_for @user, :description, :text, :disabled => false
45
+ assert_select 'textarea.text:not([disabled])'
46
+ with_input_for @user, :age, :integer, :disabled => false
47
+ assert_select 'input.integer:not([disabled])'
48
+ with_input_for @user, :born_at, :date, :disabled => false
49
+ assert_select 'select.date:not([disabled])'
50
+ with_input_for @user, :created_at, :datetime, :disabled => false
51
+ assert_select 'select.datetime:not([disabled])'
52
+
31
53
  with_input_for @user, :name, :string
32
- assert_select 'input[name=\'user[name]\'][id=user_name][value=New in Simple Form!][type=text]'
54
+ assert_select 'input.string:not([disabled])'
55
+ with_input_for @user, :description, :text
56
+ assert_select 'textarea.text:not([disabled])'
57
+ with_input_for @user, :age, :integer
58
+ assert_select 'input.integer:not([disabled])'
59
+ with_input_for @user, :born_at, :date
60
+ assert_select 'select.date:not([disabled])'
61
+ with_input_for @user, :created_at, :datetime
62
+ assert_select 'select.datetime:not([disabled])'
33
63
  end
34
64
 
35
- test 'input should generate an integer text field for integer attributes ' do
65
+ test 'input should generate autofocus attribute based on the autofocus option' do
66
+ with_input_for @user, :name, :string, :autofocus => true
67
+ assert_select 'input.string[autofocus]'
68
+ with_input_for @user, :description, :text, :autofocus => true
69
+ assert_select 'textarea.text[autofocus]'
70
+ with_input_for @user, :age, :integer, :autofocus => true
71
+ assert_select 'input.integer[autofocus]'
72
+ with_input_for @user, :born_at, :date, :autofocus => true
73
+ assert_select 'select.date[autofocus]'
74
+ with_input_for @user, :created_at, :datetime, :autofocus => true
75
+ assert_select 'select.datetime[autofocus]'
76
+
77
+ with_input_for @user, :name, :string, :autofocus => false
78
+ assert_select 'input.string:not([autofocus])'
79
+ with_input_for @user, :description, :text, :autofocus => false
80
+ assert_select 'textarea.text:not([autofocus])'
81
+ with_input_for @user, :age, :integer, :autofocus => false
82
+ assert_select 'input.integer:not([autofocus])'
83
+ with_input_for @user, :born_at, :date, :autofocus => false
84
+ assert_select 'select.date:not([autofocus])'
85
+ with_input_for @user, :created_at, :datetime, :autofocus => false
86
+ assert_select 'select.datetime:not([autofocus])'
87
+
88
+ with_input_for @user, :name, :string
89
+ assert_select 'input.string:not([autofocus])'
90
+ with_input_for @user, :description, :text
91
+ assert_select 'textarea.text:not([autofocus])'
36
92
  with_input_for @user, :age, :integer
37
- assert_select 'input[type=text].integer#user_age'
93
+ assert_select 'input.integer:not([autofocus])'
94
+ with_input_for @user, :born_at, :date
95
+ assert_select 'select.date:not([autofocus])'
96
+ with_input_for @user, :created_at, :datetime
97
+ assert_select 'select.datetime:not([autofocus])'
38
98
  end
39
-
40
- test 'input should generate a float text field for float attributes ' do
41
- with_input_for @user, :age, :float
42
- assert_select 'input[type=text].float#user_age'
99
+
100
+ test "when not using HTML5, it does not generate autofocus attribute" do
101
+ swap SimpleForm, :html5 => false do
102
+ with_input_for @user, :name, :string, :autofocus => true
103
+ assert_no_select 'input.string[autofocus]'
104
+ end
43
105
  end
44
-
45
- test 'input should generate a decimal text field for decimal attributes ' do
46
- with_input_for @user, :age, :decimal
47
- assert_select 'input[type=text].decimal#user_age'
106
+
107
+ test 'input should render components according to an optional :components option' do
108
+ with_input_for @user, :name, :string, :components => [:input, :label]
109
+ assert_select 'input + label'
110
+
111
+ with_input_for @user, :age, :integer, :components => [:input, :label]
112
+ assert_select 'input + label'
113
+
114
+ with_input_for @user, :active, :boolean, :components => [:label, :input]
115
+ assert_select 'label + input'
116
+
117
+ with_input_for @user, :description, :text, :components => [:input, :label]
118
+ assert_select 'textarea + label'
119
+
120
+ with_input_for @user, :password, :password, :components => [:input, :label]
121
+ assert_select 'input + label'
122
+
123
+ with_input_for @user, :name, :file, :components => [:input, :label]
124
+ assert_select 'input + label'
125
+
126
+ with_input_for @user, :country, :country, :components => [:input, :label]
127
+ assert_select 'select + label'
128
+
129
+ with_input_for @user, :time_zone, :time_zone, :components => [:input, :label]
130
+ assert_select 'select + label'
131
+ end
132
+
133
+ # StringInput
134
+ test 'input should map text field to string attribute' do
135
+ with_input_for @user, :name, :string
136
+ assert_select "input#user_name[type=text][name='user[name]'][value=New in Simple Form!]"
137
+ end
138
+
139
+ test 'input should generate a password field for password attributes' do
140
+ with_input_for @user, :password, :password
141
+ assert_select "input#user_password.password[type=password][name='user[password]']"
48
142
  end
49
143
 
50
144
  test 'input should use default text size for decimal attributes' do
@@ -62,20 +156,241 @@ class InputTest < ActionView::TestCase
62
156
  assert_select 'input.string[size=50]'
63
157
  end
64
158
 
65
- # MappingInput
66
- test 'input should generate a text area for text attributes' do
67
- with_input_for @user, :description, :text
68
- assert_select 'textarea.text#user_description'
159
+ test 'input should use default text size for password attributes' do
160
+ with_input_for @user, :password, :password
161
+ assert_select 'input.password[type=password][size=50]'
69
162
  end
70
163
 
164
+ test 'input should get maxlength from column definition for password attributes' do
165
+ with_input_for @user, :password, :password
166
+ assert_select 'input.password[type=password][maxlength=100]'
167
+ end
168
+
169
+ test 'when not using HTML5, does not show maxlength attribute' do
170
+ swap SimpleForm, :html5 => false do
171
+ with_input_for @user, :password, :password
172
+ assert_no_select 'input[type=password][maxlength]'
173
+ end
174
+ end
175
+
176
+ test 'input should not generate placeholder by default' do
177
+ with_input_for @user, :name, :string
178
+ assert_no_select 'input[placeholder]'
179
+ end
180
+
181
+ test 'input should accept the placeholder option' do
182
+ with_input_for @user, :name, :string, :placeholder => 'Put in some text'
183
+ assert_select 'input.string[placeholder=Put in some text]'
184
+ end
185
+
186
+ test 'input should generate a password field for password attributes that accept placeholder' do
187
+ with_input_for @user, :password, :password, :placeholder => 'Password Confirmation'
188
+ assert_select 'input[type=password].password[placeholder=Password Confirmation]#user_password'
189
+ end
190
+
191
+ test 'input should use i18n to translate placeholder text' do
192
+ store_translations(:en, :simple_form => { :placeholders => { :user => {
193
+ :name => 'Name goes here'
194
+ } } }) do
195
+ with_input_for @user, :name, :string
196
+ assert_select 'input.string[placeholder=Name goes here]'
197
+ end
198
+ end
199
+
200
+ [:email, :url, :search, :tel].each do |type|
201
+ test "input should allow type #{type}" do
202
+ with_input_for @user, :name, type
203
+ assert_select "input.string.#{type}"
204
+ assert_select "input[type=#{type}]"
205
+ end
206
+
207
+ test "input should not allow type #{type} if HTML5 compatibility is disabled" do
208
+ swap SimpleForm, :html5 => false do
209
+ with_input_for @user, :name, type
210
+ assert_no_select "input[type=#{type}]"
211
+ end
212
+ end
213
+ end
214
+
215
+ # NumericInput
216
+ test 'input should generate an integer text field for integer attributes ' do
217
+ with_input_for @user, :age, :integer
218
+ assert_select 'input[type=number].integer#user_age'
219
+ end
220
+
221
+ test 'input should generate a float text field for float attributes ' do
222
+ with_input_for @user, :age, :float
223
+ assert_select 'input[type=number].float#user_age'
224
+ end
225
+
226
+ test 'input should generate a decimal text field for decimal attributes ' do
227
+ with_input_for @user, :age, :decimal
228
+ assert_select 'input[type=number].decimal#user_age'
229
+ end
230
+
231
+ test 'input should not generate min attribute by default' do
232
+ with_input_for @user, :age, :integer
233
+ assert_no_select 'input[min]'
234
+ end
235
+
236
+ test 'input should not generate max attribute by default' do
237
+ with_input_for @user, :age, :integer
238
+ assert_no_select 'input[max]'
239
+ end
240
+
241
+ test 'input should infer min value from integer attributes with greater than validation' do
242
+ with_input_for @other_validating_user, :age, :float
243
+ assert_no_select 'input[min]'
244
+
245
+ with_input_for @other_validating_user, :age, :integer
246
+ assert_select 'input[min=18]'
247
+ end
248
+
249
+ test 'input should infer min value from integer attributes with greater than validation using symbol' do
250
+ with_input_for @validating_user, :amount, :float
251
+ assert_no_select 'input[min]'
252
+
253
+ with_input_for @validating_user, :amount, :integer
254
+ assert_select 'input[min=11]'
255
+ end
256
+
257
+ test 'input should infer min value from integer attributes with greater than or equal to validation using symbol' do
258
+ with_input_for @validating_user, :attempts, :float
259
+ assert_select 'input[min=1]'
260
+
261
+ with_input_for @validating_user, :attempts, :integer
262
+ assert_select 'input[min=1]'
263
+ end
264
+
265
+ test 'input should infer min value from integer attributes with greater than validation using proc' do
266
+ with_input_for @other_validating_user, :amount, :float
267
+ assert_no_select 'input[min]'
268
+
269
+ with_input_for @other_validating_user, :amount, :integer
270
+ assert_select 'input[min=20]'
271
+ end
272
+
273
+ test 'input should infer min value from integer attributes with greater than or equal to validation using proc' do
274
+ with_input_for @other_validating_user, :attempts, :float
275
+ assert_select 'input[min=19]'
276
+
277
+ with_input_for @other_validating_user, :attempts, :integer
278
+ assert_select 'input[min=19]'
279
+ end
280
+
281
+ test 'input should infer max value from attributes with less than validation' do
282
+ with_input_for @other_validating_user, :age, :float
283
+ assert_no_select 'input[max]'
284
+
285
+ with_input_for @other_validating_user, :age, :integer
286
+ assert_select 'input[max=99]'
287
+ end
288
+
289
+ test 'input should infer max value from attributes with less than validation using symbol' do
290
+ with_input_for @validating_user, :amount, :float
291
+ assert_no_select 'input[max]'
292
+
293
+ with_input_for @validating_user, :amount, :integer
294
+ assert_select 'input[max=99]'
295
+ end
296
+
297
+ test 'input should infer max value from attributes with less than or equal to validation using symbol' do
298
+ with_input_for @validating_user, :attempts, :float
299
+ assert_select 'input[max=100]'
300
+
301
+ with_input_for @validating_user, :attempts, :integer
302
+ assert_select 'input[max=100]'
303
+ end
304
+
305
+ test 'input should infer max value from attributes with less than validation using proc' do
306
+ with_input_for @other_validating_user, :amount, :float
307
+ assert_no_select 'input[max]'
308
+
309
+ with_input_for @other_validating_user, :amount, :integer
310
+ assert_select 'input[max=118]'
311
+ end
312
+
313
+ test 'input should infer max value from attributes with less than or equal to validation using proc' do
314
+ with_input_for @other_validating_user, :attempts, :float
315
+ assert_select 'input[max=119]'
316
+
317
+ with_input_for @other_validating_user, :attempts, :integer
318
+ assert_select 'input[max=119]'
319
+ end
320
+
321
+ test 'input should have step value of any except for integer attribute' do
322
+ with_input_for @validating_user, :age, :float
323
+ assert_select 'input[step="any"]'
324
+
325
+ with_input_for @validating_user, :age, :integer
326
+ assert_select 'input[step=1]'
327
+ end
328
+
329
+ test 'numeric input should not generate placeholder by default' do
330
+ with_input_for @user, :age, :integer
331
+ assert_no_select 'input[placeholder]'
332
+ end
333
+
334
+ test 'numeric input should accept the placeholder option' do
335
+ with_input_for @user, :age, :integer, :placeholder => 'Put in your age'
336
+ assert_select 'input.integer[placeholder=Put in your age]'
337
+ end
338
+
339
+ test 'numeric input should use i18n to translate placeholder text' do
340
+ store_translations(:en, :simple_form => { :placeholders => { :user => {
341
+ :age => 'Age goes here'
342
+ } } }) do
343
+ with_input_for @user, :age, :integer
344
+ assert_select 'input.integer[placeholder=Age goes here]'
345
+ end
346
+ end
347
+
348
+ # Numeric input but HTML5 disabled
349
+ test ' when not using HTML5 input should not generate field with type number and use text instead' do
350
+ swap SimpleForm, :html5 => false do
351
+ with_input_for @user, :age, :integer
352
+ assert_no_select "input[type=number]"
353
+ assert_no_select "input#user_age[text]"
354
+ end
355
+ end
356
+
357
+ test 'when not using HTML5 input should not use min or max or step attributes' do
358
+ swap SimpleForm, :html5 => false do
359
+ with_input_for @validating_user, :age, :integer
360
+ assert_no_select "input[min]"
361
+ assert_no_select "input[max]"
362
+ assert_no_select "input[step]"
363
+ end
364
+ end
365
+
366
+ [:integer, :float, :decimal].each do |type|
367
+ test "#{type} input should infer min value from attributes with greater than or equal validation" do
368
+ with_input_for @validating_user, :age, type
369
+ assert_select 'input[min=18]'
370
+ end
371
+
372
+ test "#{type} input should infer the max value from attributes with less than or equal to validation" do
373
+ with_input_for @validating_user, :age, type
374
+ assert_select 'input[max=99]'
375
+ end
376
+ end
377
+
378
+ # BooleanInput
71
379
  test 'input should generate a checkbox by default for boolean attributes' do
72
380
  with_input_for @user, :active, :boolean
73
381
  assert_select 'input[type=checkbox].boolean#user_active'
382
+ assert_select 'input.boolean + label.boolean.optional'
74
383
  end
75
-
76
- test 'input should generate a password field for password attributes' do
77
- with_input_for @user, :password, :password
78
- assert_select 'input[type=password].password#user_password'
384
+
385
+ # MappingInput
386
+ test 'input should generate a text area for text attributes' do
387
+ with_input_for @user, :description, :text
388
+ assert_select 'textarea.text#user_description'
389
+ end
390
+
391
+ test 'input should generate a text area for text attributes that accept placeholder' do
392
+ with_input_for @user, :description, :text, :placeholder => 'Put in some text'
393
+ assert_select 'textarea.text[placeholder=Put in some text]'
79
394
  end
80
395
 
81
396
  test 'input should generate a file field' do
@@ -83,6 +398,19 @@ class InputTest < ActionView::TestCase
83
398
  assert_select 'input#user_name[type=file]'
84
399
  end
85
400
 
401
+ test "input should generate a file field that doesn't accept placeholder" do
402
+ with_input_for @user, :name, :file, :placeholder => 'Put in some text'
403
+ assert_no_select 'input[placeholder]'
404
+ end
405
+
406
+ test 'mapping input should generate an error if type is not found' do
407
+ with_concat_form_for(@user) do |f|
408
+ assert_raise(RuntimeError, "Could not find method for nil") do
409
+ SimpleForm::Inputs::MappingInput.new(f, "unknown", nil, nil, {}).input
410
+ end
411
+ end
412
+ end
413
+
86
414
  # HiddenInput
87
415
  test 'input should generate a hidden field' do
88
416
  with_input_for @user, :name, :hidden
@@ -100,6 +428,14 @@ class InputTest < ActionView::TestCase
100
428
  assert_no_select 'label'
101
429
  end
102
430
 
431
+ test 'required/optional options should not be generated for hidden inputs' do
432
+ with_input_for @user, :name, :hidden
433
+ assert_no_select 'input.required'
434
+ assert_no_select 'input[required]'
435
+ assert_no_select 'input.optional'
436
+ assert_select 'input.hidden#user_name'
437
+ end
438
+
103
439
  # PriorityInput
104
440
  test 'input should generate a country select field' do
105
441
  with_input_for @user, :country, :country
@@ -107,7 +443,7 @@ class InputTest < ActionView::TestCase
107
443
  assert_select 'select option[value=Brazil]', 'Brazil'
108
444
  assert_no_select 'select option[value=][disabled=disabled]'
109
445
  end
110
-
446
+
111
447
  test 'input should generate a country select with simple form default' do
112
448
  swap SimpleForm, :country_priority => [ 'Brazil' ] do
113
449
  with_input_for @user, :country, :country
@@ -134,6 +470,12 @@ class InputTest < ActionView::TestCase
134
470
  assert_no_select 'select option[value=]', /^$/
135
471
  end
136
472
 
473
+ test 'priority input should not generate invalid required html attribute' do
474
+ with_input_for @user, :country, :country
475
+ assert_select 'select.required'
476
+ assert_no_select 'select[required]'
477
+ end
478
+
137
479
  # DateTime input
138
480
  test 'input should generate a datetime select by default for datetime attributes' do
139
481
  with_input_for @user, :created_at, :datetime
@@ -141,17 +483,17 @@ class InputTest < ActionView::TestCase
141
483
  assert_select "form select.datetime#user_created_at_#{i}i"
142
484
  end
143
485
  end
144
-
486
+
145
487
  test 'input should be able to pass options to datetime select' do
146
488
  with_input_for @user, :created_at, :datetime,
147
489
  :disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
148
-
490
+
149
491
  assert_select 'select.datetime[disabled=disabled]'
150
492
  assert_select 'select.datetime option', 'ano'
151
493
  assert_select 'select.datetime option', 'mês'
152
494
  assert_select 'select.datetime option', 'dia'
153
495
  end
154
-
496
+
155
497
  test 'input should generate a date select for date attributes' do
156
498
  with_input_for @user, :born_at, :date
157
499
  assert_select 'select.date#user_born_at_1i'
@@ -159,11 +501,11 @@ class InputTest < ActionView::TestCase
159
501
  assert_select 'select.date#user_born_at_3i'
160
502
  assert_no_select 'select.date#user_born_at_4i'
161
503
  end
162
-
504
+
163
505
  test 'input should be able to pass options to date select' do
164
506
  with_input_for @user, :born_at, :date, :as => :date,
165
507
  :disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
166
-
508
+
167
509
  assert_select 'select.date[disabled=disabled]'
168
510
  assert_select 'select.date option', 'ano'
169
511
  assert_select 'select.date option', 'mês'
@@ -183,11 +525,11 @@ class InputTest < ActionView::TestCase
183
525
  assert_select 'select.time#user_delivery_time_4i'
184
526
  assert_select 'select.time#user_delivery_time_5i'
185
527
  end
186
-
528
+
187
529
  test 'input should be able to pass options to time select' do
188
530
  with_input_for @user, :delivery_time, :time, :required => true,
189
531
  :disabled => true, :prompt => { :hour => 'hora', :minute => 'minuto' }
190
-
532
+
191
533
  assert_select 'select.time[disabled=disabled]'
192
534
  assert_select 'select.time option', 'hora'
193
535
  assert_select 'select.time option', 'minuto'
@@ -208,19 +550,25 @@ class InputTest < ActionView::TestCase
208
550
  assert_select 'label[for=project_created_at_4i]'
209
551
  end
210
552
 
553
+ test 'date time input should not generate invalid required html attribute' do
554
+ with_input_for @user, :delivery_time, :time, :required => true
555
+ assert_select 'select.required'
556
+ assert_no_select 'select[required]'
557
+ end
558
+
211
559
  # CollectionInput
212
560
  test 'input should generate boolean radio buttons by default for radio types' do
213
561
  with_input_for @user, :active, :radio
214
562
  assert_select 'input[type=radio][value=true].radio#user_active_true'
215
563
  assert_select 'input[type=radio][value=false].radio#user_active_false'
216
564
  end
217
-
565
+
218
566
  test 'input as radio should generate internal labels by default' do
219
567
  with_input_for @user, :active, :radio
220
568
  assert_select 'label[for=user_active_true]', 'Yes'
221
569
  assert_select 'label[for=user_active_false]', 'No'
222
570
  end
223
-
571
+
224
572
  test 'input as radio should use i18n to translate internal labels' do
225
573
  store_translations(:en, :simple_form => { :yes => 'Sim', :no => 'Não' }) do
226
574
  with_input_for @user, :active, :radio
@@ -228,14 +576,21 @@ class InputTest < ActionView::TestCase
228
576
  assert_select 'label[for=user_active_false]', 'Não'
229
577
  end
230
578
  end
231
-
579
+
580
+ test 'input should mark the checked value when using boolean and radios' do
581
+ @user.active = false
582
+ with_input_for @user, :active, :radio
583
+ assert_no_select 'input[type=radio][value=true][checked]'
584
+ assert_select 'input[type=radio][value=false][checked]'
585
+ end
586
+
232
587
  test 'input should generate a boolean select with options by default for select types' do
233
588
  with_input_for @user, :active, :select
234
589
  assert_select 'select.select#user_active'
235
590
  assert_select 'select option[value=true]', 'Yes'
236
591
  assert_select 'select option[value=false]', 'No'
237
592
  end
238
-
593
+
239
594
  test 'input as select should use i18n to translate select boolean options' do
240
595
  store_translations(:en, :simple_form => { :yes => 'Sim', :no => 'Não' }) do
241
596
  with_input_for @user, :active, :select
@@ -243,53 +598,82 @@ class InputTest < ActionView::TestCase
243
598
  assert_select 'select option[value=false]', 'Não'
244
599
  end
245
600
  end
246
-
601
+
247
602
  test 'input should allow overriding collection for select types' do
248
603
  with_input_for @user, :name, :select, :collection => ['Jose', 'Carlos']
249
604
  assert_select 'select.select#user_name'
250
605
  assert_select 'select option', 'Jose'
251
606
  assert_select 'select option', 'Carlos'
252
607
  end
253
-
608
+
254
609
  test 'input should mark the selected value by default' do
255
610
  @user.name = "Carlos"
256
611
  with_input_for @user, :name, :select, :collection => ['Jose', 'Carlos']
257
612
  assert_select 'select option[selected=selected]', 'Carlos'
258
613
  end
259
-
614
+
260
615
  test 'input should mark the selected value also when using integers' do
261
616
  @user.age = 18
262
617
  with_input_for @user, :age, :select, :collection => 18..60
263
618
  assert_select 'select option[selected=selected]', '18'
264
619
  end
265
-
620
+
621
+ test 'input should mark the selected value when using booleans and select' do
622
+ @user.active = false
623
+ with_input_for @user, :active, :select
624
+ assert_no_select 'select option[selected][value=true]', 'Yes'
625
+ assert_select 'select option[selected][value=false]', 'No'
626
+ end
627
+
628
+ test 'input should set the correct value when using a collection that includes floats' do
629
+ with_input_for @user, :age, :select, :collection => [2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
630
+ assert_select 'select option[value="2.0"]'
631
+ assert_select 'select option[value="2.5"]'
632
+ end
633
+
634
+ test 'input should set the correct values when using a collection that uses mixed values' do
635
+ with_input_for @user, :age, :select, :collection => ["Hello Kitty", 2, 4.5, :johnny, nil, true, false]
636
+ assert_select 'select option[value="Hello Kitty"]'
637
+ assert_select 'select option[value="2"]'
638
+ assert_select 'select option[value="4.5"]'
639
+ assert_select 'select option[value="johnny"]'
640
+ assert_select 'select option[value=""]'
641
+ assert_select 'select option[value="true"]'
642
+ assert_select 'select option[value="false"]'
643
+ end
644
+
645
+ test 'input should include a blank option even if :include_blank is set to false if the collection includes a nil value' do
646
+ with_input_for @user, :age, :select, :collection => [nil], :include_blank => false
647
+ assert_select 'select option[value=""]'
648
+ end
649
+
266
650
  test 'input should automatically set include blank' do
267
651
  with_input_for @user, :age, :select, :collection => 18..30
268
652
  assert_select 'select option[value=]', ''
269
653
  end
270
-
654
+
271
655
  test 'input should not set include blank if otherwise is told' do
272
656
  with_input_for @user, :age, :select, :collection => 18..30, :include_blank => false
273
657
  assert_no_select 'select option[value=]', ''
274
658
  end
275
-
659
+
276
660
  test 'input should not set include blank if prompt is given' do
277
661
  with_input_for @user, :age, :select, :collection => 18..30, :prompt => "Please select foo"
278
662
  assert_no_select 'select option[value=]', ''
279
663
  end
280
-
664
+
281
665
  test 'input should not set include blank if multiple is given' do
282
666
  with_input_for @user, :age, :select, :collection => 18..30, :input_html => { :multiple => true }
283
667
  assert_no_select 'select option[value=]', ''
284
668
  end
285
-
669
+
286
670
  test 'input should detect label and value on collections' do
287
671
  users = [ setup_new_user(:id => 1, :name => "Jose"), setup_new_user(:id => 2, :name => "Carlos") ]
288
672
  with_input_for @user, :description, :select, :collection => users
289
673
  assert_select 'select option[value=1]', 'Jose'
290
674
  assert_select 'select option[value=2]', 'Carlos'
291
675
  end
292
-
676
+
293
677
  test 'input should allow overriding collection for radio types' do
294
678
  with_input_for @user, :name, :radio, :collection => ['Jose', 'Carlos']
295
679
  assert_select 'input[type=radio][value=Jose]'
@@ -297,13 +681,13 @@ class InputTest < ActionView::TestCase
297
681
  assert_select 'label.collection_radio', 'Jose'
298
682
  assert_select 'label.collection_radio', 'Carlos'
299
683
  end
300
-
684
+
301
685
  test 'input should mark the current radio value by default' do
302
686
  @user.name = "Carlos"
303
687
  with_input_for @user, :name, :radio, :collection => ['Jose', 'Carlos']
304
688
  assert_select 'input[type=radio][value=Carlos][checked=checked]'
305
689
  end
306
-
690
+
307
691
  test 'input should allow using a collection with text/value arrays' do
308
692
  with_input_for @user, :name, :radio, :collection => [['Jose', 'jose'], ['Carlos', 'carlos']]
309
693
  assert_select 'input[type=radio][value=jose]'
@@ -311,7 +695,23 @@ class InputTest < ActionView::TestCase
311
695
  assert_select 'label.collection_radio', 'Jose'
312
696
  assert_select 'label.collection_radio', 'Carlos'
313
697
  end
314
-
698
+
699
+ test 'input should allow overriding only label method for collections' do
700
+ with_input_for @user, :name, :radio,
701
+ :collection => ['Jose' , 'Carlos'],
702
+ :label_method => :upcase
703
+ assert_select 'label.collection_radio', 'JOSE'
704
+ assert_select 'label.collection_radio', 'CARLOS'
705
+ end
706
+
707
+ test 'input should allow overriding only value method for collections' do
708
+ with_input_for @user, :name, :radio,
709
+ :collection => ['Jose' , 'Carlos'],
710
+ :value_method => :upcase
711
+ assert_select 'input[type=radio][value=JOSE]'
712
+ assert_select 'input[type=radio][value=CARLOS]'
713
+ end
714
+
315
715
  test 'input should allow overriding label and value method for collections' do
316
716
  with_input_for @user, :name, :radio,
317
717
  :collection => ['Jose' , 'Carlos'],
@@ -322,19 +722,82 @@ class InputTest < ActionView::TestCase
322
722
  assert_select 'label.collection_radio', 'JOSE'
323
723
  assert_select 'label.collection_radio', 'CARLOS'
324
724
  end
325
-
725
+
726
+ test 'input should allow overriding label and value method using a lambda for collections' do
727
+ with_input_for @user, :name, :radio,
728
+ :collection => ['Jose' , 'Carlos'],
729
+ :label_method => lambda { |i| i.upcase },
730
+ :value_method => lambda { |i| i.downcase }
731
+ assert_select 'input[type=radio][value=jose]'
732
+ assert_select 'input[type=radio][value=carlos]'
733
+ assert_select 'label.collection_radio', 'JOSE'
734
+ assert_select 'label.collection_radio', 'CARLOS'
735
+ end
736
+
737
+ test 'input should allow overriding label and value method using a lambda for collection selects' do
738
+ with_input_for @user, :name, :select,
739
+ :collection => ['Jose' , 'Carlos'],
740
+ :label_method => lambda { |i| i.upcase },
741
+ :value_method => lambda { |i| i.downcase }
742
+ assert_select 'select option[value=jose]', "JOSE"
743
+ assert_select 'select option[value=carlos]', "CARLOS"
744
+ end
745
+
746
+ test 'input should allow overriding only label but not value method using a lambda for collection select' do
747
+ with_input_for @user, :name, :select,
748
+ :collection => ['Jose' , 'Carlos'],
749
+ :label_method => lambda { |i| i.upcase }
750
+ assert_select 'select option[value=Jose]', "JOSE"
751
+ assert_select 'select option[value=Carlos]', "CARLOS"
752
+ end
753
+
754
+ test 'input should allow overriding only value but not label method using a lambda for collection select' do
755
+ with_input_for @user, :name, :select,
756
+ :collection => ['Jose' , 'Carlos'],
757
+ :value_method => lambda { |i| i.downcase }
758
+ assert_select 'select option[value=jose]', "Jose"
759
+ assert_select 'select option[value=carlos]', "Carlos"
760
+ end
761
+
762
+ test 'input should allow symbols for collections' do
763
+ with_input_for @user, :name, :select, :collection => [:jose, :carlos]
764
+ assert_select 'select.select#user_name'
765
+ assert_select 'select option[value=jose]', 'jose'
766
+ assert_select 'select option[value=carlos]', 'carlos'
767
+ end
768
+
769
+ test 'collection input with radio type should generate required html attribute' do
770
+ with_input_for @user, :name, :radio, :collection => ['Jose' , 'Carlos']
771
+ assert_select 'input[type=radio].required'
772
+ assert_select 'input[type=radio][required]'
773
+ end
774
+
775
+ test 'when not using HTML5, collection input with radio type should not generate required html attribute' do
776
+ swap SimpleForm, :html5 => false do
777
+ with_input_for @user, :name, :radio, :collection => ['Jose' , 'Carlos']
778
+ assert_select 'input[type=radio].required'
779
+ assert_no_select 'input[type=radio][required]'
780
+ end
781
+ end
782
+
783
+ test 'collection input with select type should not generate invalid required html attribute' do
784
+ with_input_for @user, :name, :select, :collection => ['Jose' , 'Carlos']
785
+ assert_select 'select.required'
786
+ assert_no_select 'select[required]'
787
+ end
788
+
326
789
  # With no object
327
790
  test 'input should be generated properly when object is not present' do
328
791
  with_input_for :project, :name, :string
329
792
  assert_select 'input.string.required#project_name'
330
793
  end
331
-
794
+
332
795
  test 'input as radio should be generated properly when object is not present ' do
333
796
  with_input_for :project, :name, :radio
334
797
  assert_select 'input.radio#project_name_true'
335
798
  assert_select 'input.radio#project_name_false'
336
799
  end
337
-
800
+
338
801
  test 'input as select with collection should be generated properly when object is not present' do
339
802
  with_input_for :project, :name, :select, :collection => ['Jose', 'Carlos']
340
803
  assert_select 'select.select#project_name'