simple_form 1.5.2 → 2.0.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 (108) hide show
  1. data/CHANGELOG.md +234 -0
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +816 -0
  4. data/lib/generators/simple_form/install_generator.rb +15 -1
  5. data/lib/generators/simple_form/templates/README +12 -0
  6. data/lib/generators/simple_form/templates/_form.html.erb +2 -2
  7. data/lib/generators/simple_form/templates/_form.html.haml +2 -2
  8. data/lib/generators/simple_form/templates/_form.html.slim +4 -4
  9. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb.tt +176 -0
  10. data/lib/simple_form/action_view_extensions/builder.rb +206 -59
  11. data/lib/simple_form/action_view_extensions/form_helper.rb +30 -23
  12. data/lib/simple_form/components/errors.rb +6 -24
  13. data/lib/simple_form/components/hints.rb +7 -21
  14. data/lib/simple_form/components/html5.rb +26 -0
  15. data/lib/simple_form/components/labels.rb +22 -14
  16. data/lib/simple_form/components/maxlength.rb +41 -0
  17. data/lib/simple_form/components/min_max.rb +49 -0
  18. data/lib/simple_form/components/pattern.rb +34 -0
  19. data/lib/simple_form/components/placeholders.rb +5 -17
  20. data/lib/simple_form/components/readonly.rb +22 -0
  21. data/lib/simple_form/components.rb +11 -1
  22. data/lib/simple_form/core_ext/hash.rb +16 -0
  23. data/lib/simple_form/error_notification.rb +9 -3
  24. data/lib/simple_form/form_builder.rb +105 -28
  25. data/lib/simple_form/helpers/autofocus.rb +11 -0
  26. data/lib/simple_form/helpers/disabled.rb +15 -0
  27. data/lib/simple_form/helpers/readonly.rb +15 -0
  28. data/lib/simple_form/helpers/required.rb +10 -11
  29. data/lib/simple_form/helpers/validators.rb +4 -4
  30. data/lib/simple_form/helpers.rb +7 -4
  31. data/lib/simple_form/inputs/base.rb +53 -81
  32. data/lib/simple_form/inputs/boolean_input.rb +46 -4
  33. data/lib/simple_form/inputs/collection_check_boxes_input.rb +21 -0
  34. data/lib/simple_form/inputs/collection_input.rb +27 -13
  35. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +67 -0
  36. data/lib/simple_form/inputs/collection_select_input.rb +14 -0
  37. data/lib/simple_form/inputs/date_time_input.rb +10 -6
  38. data/lib/simple_form/inputs/grouped_collection_select_input.rb +41 -0
  39. data/lib/simple_form/inputs/hidden_input.rb +3 -6
  40. data/lib/simple_form/inputs/numeric_input.rb +3 -51
  41. data/lib/simple_form/inputs/password_input.rb +1 -2
  42. data/lib/simple_form/inputs/priority_input.rb +2 -2
  43. data/lib/simple_form/inputs/range_input.rb +1 -3
  44. data/lib/simple_form/inputs/string_input.rb +6 -8
  45. data/lib/simple_form/inputs/text_input.rb +1 -2
  46. data/lib/simple_form/inputs.rb +17 -13
  47. data/lib/simple_form/version.rb +1 -1
  48. data/lib/simple_form/wrappers/builder.rb +103 -0
  49. data/lib/simple_form/wrappers/many.rb +69 -0
  50. data/lib/simple_form/wrappers/root.rb +34 -0
  51. data/lib/simple_form/wrappers/single.rb +18 -0
  52. data/lib/simple_form/wrappers.rb +8 -0
  53. data/lib/simple_form.rb +118 -48
  54. data/test/action_view_extensions/builder_test.rb +285 -102
  55. data/test/action_view_extensions/form_helper_test.rb +32 -10
  56. data/test/components/label_test.rb +44 -5
  57. data/test/form_builder/association_test.rb +177 -0
  58. data/test/form_builder/button_test.rb +47 -0
  59. data/test/{error_notification_test.rb → form_builder/error_notification_test.rb} +18 -1
  60. data/test/form_builder/error_test.rb +121 -0
  61. data/test/form_builder/general_test.rb +356 -0
  62. data/test/form_builder/hint_test.rb +123 -0
  63. data/test/form_builder/input_field_test.rb +63 -0
  64. data/test/form_builder/label_test.rb +65 -0
  65. data/test/form_builder/wrapper_test.rb +149 -0
  66. data/test/generators/simple_form_generator_test.rb +32 -0
  67. data/test/inputs/boolean_input_test.rb +101 -0
  68. data/test/inputs/collection_check_boxes_input_test.rb +224 -0
  69. data/test/inputs/collection_radio_buttons_input_test.rb +326 -0
  70. data/test/inputs/collection_select_input_test.rb +241 -0
  71. data/test/inputs/datetime_input_test.rb +99 -0
  72. data/test/inputs/disabled_test.rb +38 -0
  73. data/test/inputs/discovery_test.rb +61 -0
  74. data/test/inputs/file_input_test.rb +16 -0
  75. data/test/inputs/general_test.rb +69 -0
  76. data/test/inputs/grouped_collection_select_input_test.rb +118 -0
  77. data/test/inputs/hidden_input_test.rb +30 -0
  78. data/test/inputs/numeric_input_test.rb +167 -0
  79. data/test/inputs/priority_input_test.rb +43 -0
  80. data/test/inputs/readonly_test.rb +61 -0
  81. data/test/inputs/required_test.rb +113 -0
  82. data/test/inputs/string_input_test.rb +140 -0
  83. data/test/inputs/text_input_test.rb +24 -0
  84. data/test/support/misc_helpers.rb +53 -12
  85. data/test/support/mock_controller.rb +2 -2
  86. data/test/support/models.rb +20 -5
  87. data/test/test_helper.rb +11 -12
  88. metadata +124 -96
  89. data/.gitignore +0 -3
  90. data/.gitmodules +0 -3
  91. data/.travis.yml +0 -15
  92. data/CHANGELOG.rdoc +0 -159
  93. data/Gemfile +0 -9
  94. data/README.rdoc +0 -466
  95. data/Rakefile +0 -27
  96. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +0 -93
  97. data/lib/simple_form/components/wrapper.rb +0 -38
  98. data/lib/simple_form/helpers/has_errors.rb +0 -15
  99. data/lib/simple_form/helpers/maxlength.rb +0 -24
  100. data/lib/simple_form/helpers/pattern.rb +0 -28
  101. data/simple_form.gemspec +0 -25
  102. data/test/components/error_test.rb +0 -56
  103. data/test/components/hint_test.rb +0 -74
  104. data/test/components/wrapper_test.rb +0 -63
  105. data/test/custom_components.rb +0 -7
  106. data/test/form_builder_test.rb +0 -930
  107. data/test/inputs_test.rb +0 -995
  108. /data/test/{discovery_inputs.rb → support/discovery_inputs.rb} +0 -0
@@ -0,0 +1,326 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+
4
+ class CollectionRadioButtonsInputTest < ActionView::TestCase
5
+ setup do
6
+ SimpleForm::Inputs::CollectionRadioButtonsInput.reset_i18n_cache :boolean_collection
7
+ end
8
+
9
+ test 'input :as => :radio is deprecated in favor of :as => :radio_buttons' do
10
+ assert_deprecated "[SIMPLE_FORM] Using `:as => :radio` as " \
11
+ "input type is deprecated, please change it to `:as => :radio_buttons`." do
12
+ with_input_for @user, :active, :radio
13
+ end
14
+
15
+ assert_select 'input[type=radio].radio_buttons', :count => 2
16
+ end
17
+
18
+ test 'input should generate boolean radio buttons by default for radio types' do
19
+ with_input_for @user, :active, :radio_buttons
20
+ assert_select 'input[type=radio][value=true].radio_buttons#user_active_true'
21
+ assert_select 'input[type=radio][value=false].radio_buttons#user_active_false'
22
+ end
23
+
24
+ test 'input as radio should generate internal labels by default' do
25
+ with_input_for @user, :active, :radio_buttons
26
+ assert_select 'label[for=user_active_true]', 'Yes'
27
+ assert_select 'label[for=user_active_false]', 'No'
28
+ end
29
+
30
+ test 'input as radio should use i18n to translate internal labels' do
31
+ store_translations(:en, :simple_form => { :yes => 'Sim', :no => 'Não' }) do
32
+ with_input_for @user, :active, :radio_buttons
33
+ assert_select 'label[for=user_active_true]', 'Sim'
34
+ assert_select 'label[for=user_active_false]', 'Não'
35
+ end
36
+ end
37
+
38
+ test 'input radio should not include for attribute by default' do
39
+ with_input_for @user, :gender, :radio_buttons, :collection => [:male, :female]
40
+ assert_select 'label'
41
+ assert_no_select 'label[for=user_gender]'
42
+ end
43
+
44
+ test 'input radio should include for attribute when giving as html option' do
45
+ with_input_for @user, :gender, :radio_buttons, :collection => [:male, :female], :label_html => { :for => 'gender' }
46
+ assert_select 'label[for=gender]'
47
+ end
48
+
49
+ test 'input should mark the checked value when using boolean and radios' do
50
+ @user.active = false
51
+ with_input_for @user, :active, :radio_buttons
52
+ assert_no_select 'input[type=radio][value=true][checked]'
53
+ assert_select 'input[type=radio][value=false][checked]'
54
+ end
55
+
56
+ test 'input should allow overriding collection for radio types' do
57
+ with_input_for @user, :name, :radio_buttons, :collection => ['Jose', 'Carlos']
58
+ assert_select 'input[type=radio][value=Jose]'
59
+ assert_select 'input[type=radio][value=Carlos]'
60
+ assert_select 'label.collection_radio_buttons', 'Jose'
61
+ assert_select 'label.collection_radio_buttons', 'Carlos'
62
+ end
63
+
64
+ test 'input should do automatic collection translation for radio types using defaults key' do
65
+ store_translations(:en, :simple_form => { :options => { :defaults => {
66
+ :gender => { :male => 'Male', :female => 'Female'}
67
+ } } } ) do
68
+ with_input_for @user, :gender, :radio_buttons, :collection => [:male, :female]
69
+ assert_select 'input[type=radio][value=male]'
70
+ assert_select 'input[type=radio][value=female]'
71
+ assert_select 'label.collection_radio_buttons', 'Male'
72
+ assert_select 'label.collection_radio_buttons', 'Female'
73
+ end
74
+ end
75
+
76
+ test 'input should do automatic collection translation for radio types using specific object key' do
77
+ store_translations(:en, :simple_form => { :options => { :user => {
78
+ :gender => { :male => 'Male', :female => 'Female'}
79
+ } } } ) do
80
+ with_input_for @user, :gender, :radio_buttons, :collection => [:male, :female]
81
+ assert_select 'input[type=radio][value=male]'
82
+ assert_select 'input[type=radio][value=female]'
83
+ assert_select 'label.collection_radio_buttons', 'Male'
84
+ assert_select 'label.collection_radio_buttons', 'Female'
85
+ end
86
+ end
87
+
88
+ test 'input should mark the current radio value by default' do
89
+ @user.name = "Carlos"
90
+ with_input_for @user, :name, :radio_buttons, :collection => ['Jose', 'Carlos']
91
+ assert_select 'input[type=radio][value=Carlos][checked=checked]'
92
+ end
93
+
94
+ test 'input should allow using a collection with text/value arrays' do
95
+ with_input_for @user, :name, :radio_buttons, :collection => [['Jose', 'jose'], ['Carlos', 'carlos']]
96
+ assert_select 'input[type=radio][value=jose]'
97
+ assert_select 'input[type=radio][value=carlos]'
98
+ assert_select 'label.collection_radio_buttons', 'Jose'
99
+ assert_select 'label.collection_radio_buttons', 'Carlos'
100
+ end
101
+
102
+ test 'input should allow using a collection with a Proc' do
103
+ with_input_for @user, :name, :radio_buttons, :collection => Proc.new { ['Jose', 'Carlos' ] }
104
+ assert_select 'label.collection_radio_buttons', 'Jose'
105
+ assert_select 'label.collection_radio_buttons', 'Carlos'
106
+ end
107
+
108
+ test 'input should allow overriding only label method for collections' do
109
+ with_input_for @user, :name, :radio_buttons,
110
+ :collection => ['Jose' , 'Carlos'],
111
+ :label_method => :upcase
112
+ assert_select 'label.collection_radio_buttons', 'JOSE'
113
+ assert_select 'label.collection_radio_buttons', 'CARLOS'
114
+ end
115
+
116
+ test 'input should allow overriding only value method for collections' do
117
+ with_input_for @user, :name, :radio_buttons,
118
+ :collection => ['Jose' , 'Carlos'],
119
+ :value_method => :upcase
120
+ assert_select 'input[type=radio][value=JOSE]'
121
+ assert_select 'input[type=radio][value=CARLOS]'
122
+ end
123
+
124
+ test 'input should allow overriding label and value method for collections' do
125
+ with_input_for @user, :name, :radio_buttons,
126
+ :collection => ['Jose' , 'Carlos'],
127
+ :label_method => :upcase,
128
+ :value_method => :downcase
129
+ assert_select 'input[type=radio][value=jose]'
130
+ assert_select 'input[type=radio][value=carlos]'
131
+ assert_select 'label.collection_radio_buttons', 'JOSE'
132
+ assert_select 'label.collection_radio_buttons', 'CARLOS'
133
+ end
134
+
135
+ test 'input should allow overriding label and value method using a lambda for collections' do
136
+ with_input_for @user, :name, :radio_buttons,
137
+ :collection => ['Jose' , 'Carlos'],
138
+ :label_method => lambda { |i| i.upcase },
139
+ :value_method => lambda { |i| i.downcase }
140
+ assert_select 'input[type=radio][value=jose]'
141
+ assert_select 'input[type=radio][value=carlos]'
142
+ assert_select 'label.collection_radio_buttons', 'JOSE'
143
+ assert_select 'label.collection_radio_buttons', 'CARLOS'
144
+ end
145
+
146
+ test 'collection input with radio type should generate required html attribute' do
147
+ with_input_for @user, :name, :radio_buttons, :collection => ['Jose' , 'Carlos']
148
+ assert_select 'input[type=radio].required'
149
+ assert_select 'input[type=radio][required]'
150
+ end
151
+
152
+ test 'input radio does not wrap the collection by default' do
153
+ with_input_for @user, :active, :radio_buttons
154
+
155
+ assert_select 'form input[type=radio]', :count => 2
156
+ assert_no_select 'form ul'
157
+ end
158
+
159
+ test 'input radio wraps the collection in the configured collection wrapper tag' do
160
+ swap SimpleForm, :collection_wrapper_tag => :ul do
161
+ with_input_for @user, :active, :radio_buttons
162
+
163
+ assert_select 'form ul input[type=radio]', :count => 2
164
+ end
165
+ end
166
+
167
+ test 'input radio does not wrap the collection when configured with falsy values' do
168
+ swap SimpleForm, :collection_wrapper_tag => false do
169
+ with_input_for @user, :active, :radio_buttons
170
+
171
+ assert_select 'form input[type=radio]', :count => 2
172
+ assert_no_select 'form ul'
173
+ end
174
+ end
175
+
176
+ test 'input radio allows overriding the collection wrapper tag at input level' do
177
+ swap SimpleForm, :collection_wrapper_tag => :ul do
178
+ with_input_for @user, :active, :radio_buttons, :collection_wrapper_tag => :section
179
+
180
+ assert_select 'form section input[type=radio]', :count => 2
181
+ assert_no_select 'form ul'
182
+ end
183
+ end
184
+
185
+ test 'input radio allows disabling the collection wrapper tag at input level' do
186
+ swap SimpleForm, :collection_wrapper_tag => :ul do
187
+ with_input_for @user, :active, :radio_buttons, :collection_wrapper_tag => false
188
+
189
+ assert_select 'form input[type=radio]', :count => 2
190
+ assert_no_select 'form ul'
191
+ end
192
+ end
193
+
194
+ test 'input radio renders the wrapper tag with the configured wrapper class' do
195
+ swap SimpleForm, :collection_wrapper_tag => :ul, :collection_wrapper_class => 'inputs-list' do
196
+ with_input_for @user, :active, :radio_buttons
197
+
198
+ assert_select 'form ul.inputs-list input[type=radio]', :count => 2
199
+ end
200
+ end
201
+
202
+ test 'input radio allows giving wrapper class at input level only' do
203
+ swap SimpleForm, :collection_wrapper_tag => :ul do
204
+ with_input_for @user, :active, :radio_buttons, :collection_wrapper_class => 'items-list'
205
+
206
+ assert_select 'form ul.items-list input[type=radio]', :count => 2
207
+ end
208
+ end
209
+
210
+ test 'input radio uses both configured and given wrapper classes for wrapper tag' do
211
+ swap SimpleForm, :collection_wrapper_tag => :ul, :collection_wrapper_class => 'inputs-list' do
212
+ with_input_for @user, :active, :radio_buttons, :collection_wrapper_class => 'items-list'
213
+
214
+ assert_select 'form ul.inputs-list.items-list input[type=radio]', :count => 2
215
+ end
216
+ end
217
+
218
+ test 'input radio wraps each item in the configured item wrapper tag' do
219
+ swap SimpleForm, :item_wrapper_tag => :li do
220
+ with_input_for @user, :active, :radio_buttons
221
+
222
+ assert_select 'form li input[type=radio]', :count => 2
223
+ end
224
+ end
225
+
226
+ test 'input radio does not wrap items when configured with falsy values' do
227
+ swap SimpleForm, :item_wrapper_tag => false do
228
+ with_input_for @user, :active, :radio_buttons
229
+
230
+ assert_select 'form input[type=radio]', :count => 2
231
+ assert_no_select 'form li'
232
+ end
233
+ end
234
+
235
+ test 'input radio allows overriding the item wrapper tag at input level' do
236
+ swap SimpleForm, :item_wrapper_tag => :li do
237
+ with_input_for @user, :active, :radio_buttons, :item_wrapper_tag => :dl
238
+
239
+ assert_select 'form dl input[type=radio]', :count => 2
240
+ assert_no_select 'form li'
241
+ end
242
+ end
243
+
244
+ test 'input radio allows disabling the item wrapper tag at input level' do
245
+ swap SimpleForm, :item_wrapper_tag => :ul do
246
+ with_input_for @user, :active, :radio_buttons, :item_wrapper_tag => false
247
+
248
+ assert_select 'form input[type=radio]', :count => 2
249
+ assert_no_select 'form li'
250
+ end
251
+ end
252
+
253
+ test 'input radio wraps items in a span tag by default' do
254
+ with_input_for @user, :active, :radio_buttons
255
+
256
+ assert_select 'form span input[type=radio]', :count => 2
257
+ end
258
+
259
+ test 'input radio renders the item wrapper tag with a default class "radio"' do
260
+ with_input_for @user, :active, :radio_buttons, :item_wrapper_tag => :li
261
+
262
+ assert_select 'form li.radio input[type=radio]', :count => 2
263
+ end
264
+
265
+ test 'input radio renders the item wrapper tag with the configured item wrapper class' do
266
+ swap SimpleForm, :item_wrapper_tag => :li, :item_wrapper_class => 'item' do
267
+ with_input_for @user, :active, :radio_buttons
268
+
269
+ assert_select 'form li.radio.item input[type=radio]', :count => 2
270
+ end
271
+ end
272
+
273
+ test 'input radio allows giving item wrapper class at input level only' do
274
+ swap SimpleForm, :item_wrapper_tag => :li do
275
+ with_input_for @user, :active, :radio_buttons, :item_wrapper_class => 'item'
276
+
277
+ assert_select 'form li.radio.item input[type=radio]', :count => 2
278
+ end
279
+ end
280
+
281
+ test 'input radio uses both configured and given item wrapper classes for item wrapper tag' do
282
+ swap SimpleForm, :item_wrapper_tag => :li, :item_wrapper_class => 'item' do
283
+ with_input_for @user, :active, :radio_buttons, :item_wrapper_class => 'inline'
284
+
285
+ assert_select 'form li.radio.item.inline input[type=radio]', :count => 2
286
+ end
287
+ end
288
+
289
+ test 'input radio respects the nested boolean style config, generating nested label > input' do
290
+ swap SimpleForm, :boolean_style => :nested do
291
+ with_input_for @user, :active, :radio_buttons
292
+
293
+ assert_select 'label.radio > input#user_active_true[type=radio]'
294
+ assert_select 'label.radio', 'Yes'
295
+ assert_select 'label.radio > input#user_active_false[type=radio]'
296
+ assert_select 'label.radio', 'No'
297
+ assert_no_select 'label.collection_radio_buttons'
298
+ end
299
+ end
300
+
301
+ test 'input radio with nested style overrides configured item wrapper tag, forcing the :label' do
302
+ swap SimpleForm, :boolean_style => :nested, :item_wrapper_tag => :li do
303
+ with_input_for @user, :active, :radio_buttons
304
+
305
+ assert_select 'label.radio > input'
306
+ assert_no_select 'li'
307
+ end
308
+ end
309
+
310
+ test 'input radio with nested style overrides given item wrapper tag, forcing the :label' do
311
+ swap SimpleForm, :boolean_style => :nested do
312
+ with_input_for @user, :active, :radio_buttons, :item_wrapper_tag => :li
313
+
314
+ assert_select 'label.radio > input'
315
+ assert_no_select 'li'
316
+ end
317
+ end
318
+
319
+ test 'input radio with nested style accepts giving extra wrapper classes' do
320
+ swap SimpleForm, :boolean_style => :nested do
321
+ with_input_for @user, :active, :radio_buttons, :item_wrapper_class => "inline"
322
+
323
+ assert_select 'label.radio.inline > input'
324
+ end
325
+ end
326
+ end
@@ -0,0 +1,241 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+
4
+ class CollectionSelectInputTest < ActionView::TestCase
5
+ setup do
6
+ SimpleForm::Inputs::CollectionSelectInput.reset_i18n_cache :boolean_collection
7
+ end
8
+
9
+ test 'input should generate a boolean select with options by default for select types' do
10
+ with_input_for @user, :active, :select
11
+ assert_select 'select.select#user_active'
12
+ assert_select 'select option[value=true]', 'Yes'
13
+ assert_select 'select option[value=false]', 'No'
14
+ end
15
+
16
+ test 'input as select should use i18n to translate select boolean options' do
17
+ store_translations(:en, :simple_form => { :yes => 'Sim', :no => 'Não' }) do
18
+ with_input_for @user, :active, :select
19
+ assert_select 'select option[value=true]', 'Sim'
20
+ assert_select 'select option[value=false]', 'Não'
21
+ end
22
+ end
23
+
24
+ test 'input should allow overriding collection for select types' do
25
+ with_input_for @user, :name, :select, :collection => ['Jose', 'Carlos']
26
+ assert_select 'select.select#user_name'
27
+ assert_select 'select option', 'Jose'
28
+ assert_select 'select option', 'Carlos'
29
+ end
30
+
31
+ test 'input should do automatic collection translation for select types using defaults key' do
32
+ store_translations(:en, :simple_form => { :options => { :defaults => {
33
+ :gender => { :male => 'Male', :female => 'Female'}
34
+ } } } ) do
35
+ with_input_for @user, :gender, :select, :collection => [:male, :female]
36
+ assert_select 'select.select#user_gender'
37
+ assert_select 'select option', 'Male'
38
+ assert_select 'select option', 'Female'
39
+ end
40
+ end
41
+
42
+ test 'input should do automatic collection translation for select types using specific object key' do
43
+ store_translations(:en, :simple_form => { :options => { :user => {
44
+ :gender => { :male => 'Male', :female => 'Female'}
45
+ } } } ) do
46
+ with_input_for @user, :gender, :select, :collection => [:male, :female]
47
+ assert_select 'select.select#user_gender'
48
+ assert_select 'select option', 'Male'
49
+ assert_select 'select option', 'Female'
50
+ end
51
+ end
52
+
53
+ test 'input should mark the selected value by default' do
54
+ @user.name = "Carlos"
55
+ with_input_for @user, :name, :select, :collection => ['Jose', 'Carlos']
56
+ assert_select 'select option[selected=selected]', 'Carlos'
57
+ end
58
+
59
+ test 'input should mark the selected value also when using integers' do
60
+ @user.age = 18
61
+ with_input_for @user, :age, :select, :collection => 18..60
62
+ assert_select 'select option[selected=selected]', '18'
63
+ end
64
+
65
+ test 'input should mark the selected value when using booleans and select' do
66
+ @user.active = false
67
+ with_input_for @user, :active, :select
68
+ assert_no_select 'select option[selected][value=true]', 'Yes'
69
+ assert_select 'select option[selected][value=false]', 'No'
70
+ end
71
+
72
+ test 'input should set the correct value when using a collection that includes floats' do
73
+ with_input_for @user, :age, :select, :collection => [2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
74
+ assert_select 'select option[value="2.0"]'
75
+ assert_select 'select option[value="2.5"]'
76
+ end
77
+
78
+ test 'input should set the correct values when using a collection that uses mixed values' do
79
+ with_input_for @user, :age, :select, :collection => ["Hello Kitty", 2, 4.5, :johnny, nil, true, false]
80
+ assert_select 'select option[value="Hello Kitty"]'
81
+ assert_select 'select option[value="2"]'
82
+ assert_select 'select option[value="4.5"]'
83
+ assert_select 'select option[value="johnny"]'
84
+ assert_select 'select option[value=""]'
85
+ assert_select 'select option[value="true"]'
86
+ assert_select 'select option[value="false"]'
87
+ end
88
+
89
+ test 'input should include a blank option even if :include_blank is set to false if the collection includes a nil value' do
90
+ with_input_for @user, :age, :select, :collection => [nil], :include_blank => false
91
+ assert_select 'select option[value=""]'
92
+ end
93
+
94
+ test 'input should automatically set include blank' do
95
+ with_input_for @user, :age, :select, :collection => 18..30
96
+ assert_select 'select option[value=]', ''
97
+ end
98
+
99
+ test 'input should not set include blank if otherwise is told' do
100
+ with_input_for @user, :age, :select, :collection => 18..30, :include_blank => false
101
+ assert_no_select 'select option[value=]', ''
102
+ end
103
+
104
+ test 'input should not set include blank if prompt is given' do
105
+ with_input_for @user, :age, :select, :collection => 18..30, :prompt => "Please select foo"
106
+ assert_no_select 'select option[value=]', ''
107
+ end
108
+
109
+ test 'input should not set include blank if multiple is given' do
110
+ with_input_for @user, :age, :select, :collection => 18..30, :input_html => { :multiple => true }
111
+ assert_no_select 'select option[value=]', ''
112
+ end
113
+
114
+ test 'input should detect label and value on collections' do
115
+ users = [ setup_new_user(:id => 1, :name => "Jose"), setup_new_user(:id => 2, :name => "Carlos") ]
116
+ with_input_for @user, :description, :select, :collection => users
117
+ assert_select 'select option[value=1]', 'Jose'
118
+ assert_select 'select option[value=2]', 'Carlos'
119
+ end
120
+
121
+ test 'input should disable the anothers components when the option is a object' do
122
+ with_input_for @user, :description, :select, :collection => ["Jose", "Carlos"], :disabled => true
123
+ assert_no_select 'select option[value=Jose][disabled=disabled]', 'Jose'
124
+ assert_no_select 'select option[value=Carlos][disabled=disabled]', 'Carlos'
125
+ assert_select 'select[disabled=disabled]'
126
+ assert_select 'div.disabled'
127
+ end
128
+
129
+ test 'input should not disable the anothers components when the option is a object' do
130
+ with_input_for @user, :description, :select, :collection => ["Jose", "Carlos"], :disabled => 'Jose'
131
+ assert_select 'select option[value=Jose][disabled=disabled]', 'Jose'
132
+ assert_no_select 'select option[value=Carlos][disabled=disabled]', 'Carlos'
133
+ assert_no_select 'select[disabled=disabled]'
134
+ assert_no_select 'div.disabled'
135
+ end
136
+
137
+ test 'input should allow overriding label and value method using a lambda for collection selects' do
138
+ with_input_for @user, :name, :select,
139
+ :collection => ['Jose' , 'Carlos'],
140
+ :label_method => lambda { |i| i.upcase },
141
+ :value_method => lambda { |i| i.downcase }
142
+ assert_select 'select option[value=jose]', "JOSE"
143
+ assert_select 'select option[value=carlos]', "CARLOS"
144
+ end
145
+
146
+ test 'input should allow overriding only label but not value method using a lambda for collection select' do
147
+ with_input_for @user, :name, :select,
148
+ :collection => ['Jose' , 'Carlos'],
149
+ :label_method => lambda { |i| i.upcase }
150
+ assert_select 'select option[value=Jose]', "JOSE"
151
+ assert_select 'select option[value=Carlos]', "CARLOS"
152
+ end
153
+
154
+ test 'input should allow overriding only value but not label method using a lambda for collection select' do
155
+ with_input_for @user, :name, :select,
156
+ :collection => ['Jose' , 'Carlos'],
157
+ :value_method => lambda { |i| i.downcase }
158
+ assert_select 'select option[value=jose]', "Jose"
159
+ assert_select 'select option[value=carlos]', "Carlos"
160
+ end
161
+
162
+ test 'input should allow symbols for collections' do
163
+ with_input_for @user, :name, :select, :collection => [:jose, :carlos]
164
+ assert_select 'select.select#user_name'
165
+ assert_select 'select option[value=jose]', 'jose'
166
+ assert_select 'select option[value=carlos]', 'carlos'
167
+ end
168
+
169
+ test 'collection input with select type should generate required html attribute only with blank option' do
170
+ with_input_for @user, :name, :select, :include_blank => true, :collection => ['Jose' , 'Carlos']
171
+ assert_select 'select.required'
172
+ assert_select 'select[required]'
173
+ end
174
+
175
+ test 'collection input with select type should not generate required html attribute without blank option' do
176
+ with_input_for @user, :name, :select, :include_blank => false, :collection => ['Jose' , 'Carlos']
177
+ assert_select 'select.required'
178
+ assert_no_select 'select[required]'
179
+ end
180
+
181
+ test 'collection input with select type with multiple attribute should generate required html attribute without blank option' do
182
+ with_input_for @user, :name, :select, :include_blank => false, :input_html => {:multiple => true}, :collection => ['Jose' , 'Carlos']
183
+ assert_select 'select.required'
184
+ assert_select 'select[required]'
185
+ end
186
+
187
+ test 'collection input with select type with multiple attribute should generate required html attribute with blank option' do
188
+ with_input_for @user, :name, :select, :include_blank => true, :input_html => {:multiple => true}, :collection => ['Jose' , 'Carlos']
189
+ assert_select 'select.required'
190
+ assert_select 'select[required]'
191
+ end
192
+
193
+ test 'input should allow disabled options with a lambda for collection select' do
194
+ with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"],
195
+ :disabled => lambda { |x| x == "Carlos" }
196
+ assert_select 'select option[value=Carlos][disabled=disabled]', 'Carlos'
197
+ assert_select 'select option[value=Antonio]', 'Antonio'
198
+ assert_no_select 'select option[value=Antonio][disabled]'
199
+ end
200
+
201
+ test 'input should allow disabled and label method with lambdas for collection select' do
202
+ with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"],
203
+ :disabled => lambda { |x| x == "Carlos" }, :label_method => lambda { |x| x.upcase }
204
+ assert_select 'select option[value=Carlos][disabled=disabled]', 'CARLOS'
205
+ assert_select 'select option[value=Antonio]', 'ANTONIO'
206
+ assert_no_select 'select option[value=Antonio][disabled]'
207
+ end
208
+
209
+ test 'input should allow a non lambda disabled option with lambda label method for collections' do
210
+ with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"],
211
+ :disabled => "Carlos", :label_method => lambda { |x| x.upcase }
212
+ assert_select 'select option[value=Carlos][disabled=disabled]', 'CARLOS'
213
+ assert_select 'select option[value=Antonio]', 'ANTONIO'
214
+ assert_no_select 'select option[value=Antonio][disabled]'
215
+ end
216
+
217
+ test 'input should allow selected and label method with lambdas for collection select' do
218
+ with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"],
219
+ :selected => lambda { |x| x == "Carlos" }, :label_method => lambda { |x| x.upcase }
220
+ assert_select 'select option[value=Carlos][selected=selected]', 'CARLOS'
221
+ assert_select 'select option[value=Antonio]', 'ANTONIO'
222
+ assert_no_select 'select option[value=Antonio][selected]'
223
+ end
224
+
225
+ test 'input should allow a non lambda selected option with lambda label method for collection select' do
226
+ with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"],
227
+ :selected => "Carlos", :label_method => lambda { |x| x.upcase }
228
+ assert_select 'select option[value=Carlos][selected=selected]', 'CARLOS'
229
+ assert_select 'select option[value=Antonio]', 'ANTONIO'
230
+ assert_no_select 'select option[value=Antonio][selected]'
231
+ end
232
+
233
+ test 'input should not override default selection through attribute value with label method as lambda for collection select' do
234
+ @user.name = "Carlos"
235
+ with_input_for @user, :name, :select, :collection => ["Carlos", "Antonio"],
236
+ :label_method => lambda { |x| x.upcase }
237
+ assert_select 'select option[value=Carlos][selected=selected]', 'CARLOS'
238
+ assert_select 'select option[value=Antonio]', 'ANTONIO'
239
+ assert_no_select 'select option[value=Antonio][selected]'
240
+ end
241
+ end
@@ -0,0 +1,99 @@
1
+ # encoding: UTF-8
2
+ require 'test_helper'
3
+
4
+ # Tests for all different kinds of inputs.
5
+ class DateTimeInputTest < ActionView::TestCase
6
+ # DateTime input
7
+ test 'input should generate a datetime select by default for datetime attributes' do
8
+ with_input_for @user, :created_at, :datetime
9
+ 1.upto(5) do |i|
10
+ assert_select "form select.datetime#user_created_at_#{i}i"
11
+ end
12
+ end
13
+
14
+ test 'input should be able to pass options to datetime select' do
15
+ with_input_for @user, :created_at, :datetime,
16
+ :disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
17
+
18
+ assert_select 'select.datetime[disabled=disabled]'
19
+ assert_select 'select.datetime option', 'ano'
20
+ assert_select 'select.datetime option', 'mês'
21
+ assert_select 'select.datetime option', 'dia'
22
+ end
23
+
24
+ test 'input should generate a date select for date attributes' do
25
+ with_input_for @user, :born_at, :date
26
+ assert_select 'select.date#user_born_at_1i'
27
+ assert_select 'select.date#user_born_at_2i'
28
+ assert_select 'select.date#user_born_at_3i'
29
+ assert_no_select 'select.date#user_born_at_4i'
30
+ end
31
+
32
+ test 'input should be able to pass options to date select' do
33
+ with_input_for @user, :born_at, :date, :as => :date,
34
+ :disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
35
+
36
+ assert_select 'select.date[disabled=disabled]'
37
+ assert_select 'select.date option', 'ano'
38
+ assert_select 'select.date option', 'mês'
39
+ assert_select 'select.date option', 'dia'
40
+ end
41
+
42
+ test 'input should be able to pass :default to date select' do
43
+ with_input_for @user, :born_at, :date, :default => Date.today
44
+ assert_select "select.date option[value=#{Date.today.year}][selected=selected]"
45
+ end
46
+
47
+ test 'input should generate a time select for time attributes' do
48
+ with_input_for @user, :delivery_time, :time
49
+ assert_select 'input[type=hidden]#user_delivery_time_1i'
50
+ assert_select 'input[type=hidden]#user_delivery_time_2i'
51
+ assert_select 'input[type=hidden]#user_delivery_time_3i'
52
+ assert_select 'select.time#user_delivery_time_4i'
53
+ assert_select 'select.time#user_delivery_time_5i'
54
+ end
55
+
56
+ test 'input should be able to pass options to time select' do
57
+ with_input_for @user, :delivery_time, :time, :required => true,
58
+ :disabled => true, :prompt => { :hour => 'hora', :minute => 'minuto' }
59
+
60
+ assert_select 'select.time[disabled=disabled]'
61
+ assert_select 'select.time option', 'hora'
62
+ assert_select 'select.time option', 'minuto'
63
+ end
64
+
65
+ test 'label should use i18n to get target for date input type' do
66
+ store_translations(:en, :date => { :order => [:month, :day, :year] }) do
67
+ with_input_for :project, :created_at, :date
68
+ assert_select 'label[for=project_created_at_2i]'
69
+ end
70
+ end
71
+
72
+ test 'label should use i18n to get target for datetime input type' do
73
+ store_translations(:en, :date => { :order => [:month, :day, :year] }) do
74
+ with_input_for :project, :created_at, :datetime
75
+ assert_select 'label[for=project_created_at_2i]'
76
+ end
77
+ end
78
+
79
+ test 'label should use order to get target when date input type' do
80
+ with_input_for :project, :created_at, :date, :order => [:month, :year, :day]
81
+ assert_select 'label[for=project_created_at_2i]'
82
+ end
83
+
84
+ test 'label should use order to get target when datetime input type' do
85
+ with_input_for :project, :created_at, :datetime, :order => [:month, :year, :day]
86
+ assert_select 'label[for=project_created_at_2i]'
87
+ end
88
+
89
+ test 'label should point to first option when time input type' do
90
+ with_input_for :project, :created_at, :time
91
+ assert_select 'label[for=project_created_at_4i]'
92
+ end
93
+
94
+ test 'date time input should not generate invalid required html attribute' do
95
+ with_input_for @user, :delivery_time, :time, :required => true
96
+ assert_select 'select.required'
97
+ assert_no_select 'select[required]'
98
+ end
99
+ end