simple_form 5.1.0 → 5.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -0
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +64 -16
  5. data/lib/generators/simple_form/install_generator.rb +2 -2
  6. data/lib/generators/simple_form/templates/README +1 -1
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +1 -1
  8. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +126 -194
  9. data/lib/simple_form/components/label_input.rb +1 -1
  10. data/lib/simple_form/form_builder.rb +2 -6
  11. data/lib/simple_form/inputs/boolean_input.rb +6 -2
  12. data/lib/simple_form/inputs/grouped_collection_select_input.rb +1 -1
  13. data/lib/simple_form/inputs/priority_input.rb +16 -2
  14. data/lib/simple_form/railtie.rb +4 -0
  15. data/lib/simple_form/version.rb +1 -1
  16. data/lib/simple_form/wrappers/leaf.rb +1 -1
  17. data/lib/simple_form.rb +9 -5
  18. metadata +11 -83
  19. data/test/action_view_extensions/builder_test.rb +0 -634
  20. data/test/action_view_extensions/form_helper_test.rb +0 -172
  21. data/test/components/custom_components_test.rb +0 -62
  22. data/test/components/label_test.rb +0 -352
  23. data/test/form_builder/association_test.rb +0 -252
  24. data/test/form_builder/button_test.rb +0 -48
  25. data/test/form_builder/error_notification_test.rb +0 -80
  26. data/test/form_builder/error_test.rb +0 -281
  27. data/test/form_builder/general_test.rb +0 -539
  28. data/test/form_builder/hint_test.rb +0 -150
  29. data/test/form_builder/input_field_test.rb +0 -195
  30. data/test/form_builder/label_test.rb +0 -136
  31. data/test/form_builder/wrapper_test.rb +0 -378
  32. data/test/generators/simple_form_generator_test.rb +0 -43
  33. data/test/inputs/boolean_input_test.rb +0 -243
  34. data/test/inputs/collection_check_boxes_input_test.rb +0 -323
  35. data/test/inputs/collection_radio_buttons_input_test.rb +0 -446
  36. data/test/inputs/collection_select_input_test.rb +0 -380
  37. data/test/inputs/color_input_test.rb +0 -10
  38. data/test/inputs/datetime_input_test.rb +0 -176
  39. data/test/inputs/disabled_test.rb +0 -92
  40. data/test/inputs/discovery_test.rb +0 -142
  41. data/test/inputs/file_input_test.rb +0 -17
  42. data/test/inputs/general_test.rb +0 -133
  43. data/test/inputs/grouped_collection_select_input_test.rb +0 -183
  44. data/test/inputs/hidden_input_test.rb +0 -32
  45. data/test/inputs/numeric_input_test.rb +0 -177
  46. data/test/inputs/priority_input_test.rb +0 -50
  47. data/test/inputs/readonly_test.rb +0 -102
  48. data/test/inputs/required_test.rb +0 -158
  49. data/test/inputs/rich_text_area_input_test.rb +0 -15
  50. data/test/inputs/string_input_test.rb +0 -158
  51. data/test/inputs/text_input_test.rb +0 -37
  52. data/test/simple_form_test.rb +0 -18
  53. data/test/support/discovery_inputs.rb +0 -65
  54. data/test/support/misc_helpers.rb +0 -274
  55. data/test/support/mock_controller.rb +0 -30
  56. data/test/support/models.rb +0 -357
  57. data/test/test_helper.rb +0 -95
@@ -1,634 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
-
4
- class BuilderTest < ActionView::TestCase
5
- def with_custom_form_for(object, *args, &block)
6
- with_concat_custom_form_for(object) do |f|
7
- assert f.instance_of?(CustomFormBuilder)
8
- yield f
9
- end
10
- end
11
-
12
- def with_collection_radio_buttons(object, attribute, collection, value_method, text_method, options = {}, html_options = {}, &block)
13
- with_concat_form_for(object) do |f|
14
- f.collection_radio_buttons attribute, collection, value_method, text_method, options, html_options, &block
15
- end
16
- end
17
-
18
- def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options = {}, html_options = {}, &block)
19
- with_concat_form_for(object) do |f|
20
- f.collection_check_boxes attribute, collection, value_method, text_method, options, html_options, &block
21
- end
22
- end
23
-
24
- # COLLECTION RADIO
25
- test "collection radio accepts a collection and generate inputs from value method" do
26
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s
27
-
28
- assert_select 'form input[type=radio][value=true]#user_active_true'
29
- assert_select 'form input[type=radio][value=false]#user_active_false'
30
- end
31
-
32
- test "collection radio accepts a collection and generate inputs from label method" do
33
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s
34
-
35
- assert_select 'form label.collection_radio_buttons[for=user_active_true]', 'true'
36
- assert_select 'form label.collection_radio_buttons[for=user_active_false]', 'false'
37
- end
38
-
39
- test "collection radio handles camelized collection values for labels correctly" do
40
- with_collection_radio_buttons @user, :active, %w[Yes No], :to_s, :to_s
41
-
42
- assert_select 'form label.collection_radio_buttons[for=user_active_yes]', 'Yes'
43
- assert_select 'form label.collection_radio_buttons[for=user_active_no]', 'No'
44
- end
45
-
46
- test "collection radio sanitizes collection values for labels correctly" do
47
- with_collection_radio_buttons @user, :name, ['$0.99', '$1.99'], :to_s, :to_s
48
-
49
- # Rails 6 changed the way it sanitizes the values
50
- # https://github.com/rails/rails/blob/6-0-stable/actionview/lib/action_view/helpers/tags/base.rb#L141
51
- # https://github.com/rails/rails/blob/5-2-stable/actionview/lib/action_view/helpers/tags/base.rb#L141
52
- if ActionView::VERSION::MAJOR == 5
53
- assert_select 'label.collection_radio_buttons[for=user_name_099]', '$0.99'
54
- assert_select 'label.collection_radio_buttons[for=user_name_199]', '$1.99'
55
- else
56
- assert_select 'label.collection_radio_buttons[for=user_name_0_99]', '$0.99'
57
- assert_select 'label.collection_radio_buttons[for=user_name_1_99]', '$1.99'
58
- end
59
- end
60
-
61
- test "collection radio checks the correct value to local variables" do
62
- user = User.build(active: false)
63
- with_collection_radio_buttons user, :active, [true, false], :to_s, :to_s
64
-
65
- assert_select 'form input[type=radio][value=true]'
66
- assert_select 'form input[type=radio][value=false][checked=checked]'
67
- end
68
-
69
- test "collection radio accepts checked item" do
70
- with_collection_radio_buttons @user, :active, [[1, true], [0, false]], :last, :first, checked: true
71
-
72
- assert_select 'form input[type=radio][value=true][checked=checked]'
73
- assert_no_select 'form input[type=radio][value=false][checked=checked]'
74
- end
75
-
76
- test "collection radio accepts checked item which has a value of false" do
77
- with_collection_radio_buttons @user, :active, [[1, true], [0, false]], :last, :first, checked: false
78
- assert_no_select 'form input[type=radio][value=true][checked=checked]'
79
- assert_select 'form input[type=radio][value=false][checked=checked]'
80
- end
81
-
82
- test "collection radio accepts multiple disabled items" do
83
- collection = [[1, true], [0, false], [2, 'other']]
84
- with_collection_radio_buttons @user, :active, collection, :last, :first, disabled: [true, false]
85
-
86
- assert_select 'form input[type=radio][value=true][disabled=disabled]'
87
- assert_select 'form input[type=radio][value=false][disabled=disabled]'
88
- assert_no_select 'form input[type=radio][value=other][disabled=disabled]'
89
- end
90
-
91
- test "collection radio accepts single disable item" do
92
- collection = [[1, true], [0, false]]
93
- with_collection_radio_buttons @user, :active, collection, :last, :first, disabled: true
94
-
95
- assert_select 'form input[type=radio][value=true][disabled=disabled]'
96
- assert_no_select 'form input[type=radio][value=false][disabled=disabled]'
97
- end
98
-
99
- test "collection radio accepts html options as input" do
100
- collection = [[1, true], [0, false]]
101
- with_collection_radio_buttons @user, :active, collection, :last, :first, {}, class: 'special-radio'
102
-
103
- assert_select 'form input[type=radio][value=true].special-radio#user_active_true'
104
- assert_select 'form input[type=radio][value=false].special-radio#user_active_false'
105
- end
106
-
107
- test "collection radio wraps the collection in the given collection wrapper tag" do
108
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s, collection_wrapper_tag: :ul
109
-
110
- assert_select 'form ul input[type=radio]', count: 2
111
- end
112
-
113
- test "collection radio does not render any wrapper tag by default" do
114
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s
115
-
116
- assert_select 'form input[type=radio]', count: 2
117
- assert_no_select 'form ul'
118
- end
119
-
120
- test "collection radio does not wrap the collection when given falsy values" do
121
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s, collection_wrapper_tag: false
122
-
123
- assert_select 'form input[type=radio]', count: 2
124
- assert_no_select 'form ul'
125
- end
126
-
127
- test "collection radio uses the given class for collection wrapper tag" do
128
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s,
129
- collection_wrapper_tag: :ul, collection_wrapper_class: "items-list"
130
-
131
- assert_select 'form ul.items-list input[type=radio]', count: 2
132
- end
133
-
134
- test "collection radio uses no class for collection wrapper tag when no wrapper tag is given" do
135
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s,
136
- collection_wrapper_class: "items-list"
137
-
138
- assert_select 'form input[type=radio]', count: 2
139
- assert_no_select 'form ul'
140
- assert_no_select '.items-list'
141
- end
142
-
143
- test "collection radio uses no class for collection wrapper tag by default" do
144
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s, collection_wrapper_tag: :ul
145
-
146
- assert_select 'form ul'
147
- assert_no_select 'form ul[class]'
148
- end
149
-
150
- test "collection radio wrap items in a span tag by default" do
151
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s
152
-
153
- assert_select 'form span input[type=radio][value=true]#user_active_true + label'
154
- assert_select 'form span input[type=radio][value=false]#user_active_false + label'
155
- end
156
-
157
- test "collection radio wraps each item in the given item wrapper tag" do
158
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s, item_wrapper_tag: :li
159
-
160
- assert_select 'form li input[type=radio]', count: 2
161
- end
162
-
163
- test "collection radio does not wrap each item when given explicitly falsy value" do
164
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s, item_wrapper_tag: false
165
-
166
- assert_select 'form input[type=radio]'
167
- assert_no_select 'form span input[type=radio]'
168
- end
169
-
170
- test "collection radio uses the given class for item wrapper tag" do
171
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s,
172
- item_wrapper_tag: :li, item_wrapper_class: "inline"
173
-
174
- assert_select "form li.inline input[type=radio]", count: 2
175
- end
176
-
177
- test "collection radio uses no class for item wrapper tag when no wrapper tag is given" do
178
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s,
179
- item_wrapper_tag: nil, item_wrapper_class: "inline"
180
-
181
- assert_select 'form input[type=radio]', count: 2
182
- assert_no_select 'form li'
183
- assert_no_select '.inline'
184
- end
185
-
186
- test "collection radio uses no class for item wrapper tag by default" do
187
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s,
188
- item_wrapper_tag: :li
189
-
190
- assert_select "form li", count: 2
191
- assert_no_select "form li[class]"
192
- end
193
-
194
- test "collection radio does not wrap input inside the label" do
195
- with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s
196
-
197
- assert_select 'form input[type=radio] + label'
198
- assert_no_select 'form label input'
199
- end
200
-
201
- test "collection radio accepts a block to render the label as radio button wrapper" do
202
- with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
203
- b.label { b.radio_button }
204
- end
205
-
206
- assert_select 'label[for=user_active_true] > input#user_active_true[type=radio]'
207
- assert_select 'label[for=user_active_false] > input#user_active_false[type=radio]'
208
- end
209
-
210
- test "collection radio accepts a block to change the order of label and radio button" do
211
- with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
212
- b.label + b.radio_button
213
- end
214
-
215
- assert_select 'label[for=user_active_true] + input#user_active_true[type=radio]'
216
- assert_select 'label[for=user_active_false] + input#user_active_false[type=radio]'
217
- end
218
-
219
- test "collection radio with block helpers accept extra html options" do
220
- with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
221
- b.label(class: "radio_button") + b.radio_button(class: "radio_button")
222
- end
223
-
224
- assert_select 'label.radio_button[for=user_active_true] + input#user_active_true.radio_button[type=radio]'
225
- assert_select 'label.radio_button[for=user_active_false] + input#user_active_false.radio_button[type=radio]'
226
- end
227
-
228
- test "collection radio with block helpers allows access to current text and value" do
229
- with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
230
- b.label(:"data-value" => b.value) { b.radio_button + b.text }
231
- end
232
-
233
- assert_select 'label[for=user_active_true][data-value=true]', 'true' do
234
- assert_select 'input#user_active_true[type=radio]'
235
- end
236
- assert_select 'label[for=user_active_false][data-value=false]', 'false' do
237
- assert_select 'input#user_active_false[type=radio]'
238
- end
239
- end
240
-
241
- test "collection radio with block helpers allows access to the current object item in the collection to access extra properties" do
242
- with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
243
- b.label(class: b.object) { b.radio_button + b.text }
244
- end
245
-
246
- assert_select 'label.true[for=user_active_true]', 'true' do
247
- assert_select 'input#user_active_true[type=radio]'
248
- end
249
- assert_select 'label.false[for=user_active_false]', 'false' do
250
- assert_select 'input#user_active_false[type=radio]'
251
- end
252
- end
253
-
254
- test "collection radio with block helpers does not leak the template" do
255
- with_concat_form_for(@user) do |f|
256
- collection_input = f.collection_radio_buttons :active, [true, false], :to_s, :to_s do |b|
257
- b.label(class: b.object) { b.radio_button + b.text }
258
- end
259
- concat collection_input
260
-
261
- concat f.hidden_field :name
262
- end
263
-
264
- assert_select 'label.true[for=user_active_true]', text: 'true', count: 1 do
265
- assert_select 'input#user_active_true[type=radio]'
266
- end
267
- assert_select 'label.false[for=user_active_false]', text: 'false', count: 1 do
268
- assert_select 'input#user_active_false[type=radio]'
269
- end
270
- end
271
- # COLLECTION CHECK BOX
272
- test "collection check box accepts a collection and generate a serie of checkboxes for value method" do
273
- collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
274
- with_collection_check_boxes @user, :tag_ids, collection, :id, :name
275
-
276
- assert_select 'form input#user_tag_ids_1[type=checkbox][value="1"]'
277
- assert_select 'form input#user_tag_ids_2[type=checkbox][value="2"]'
278
- end
279
-
280
- test "collection check box generates only one hidden field for the entire collection, to ensure something will be sent back to the server when posting an empty collection" do
281
- collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
282
- with_collection_check_boxes @user, :tag_ids, collection, :id, :name
283
-
284
- assert_select "form input[type=hidden][name='user[tag_ids][]'][value='']", count: 1
285
- end
286
-
287
- test "collection check box accepts a collection and generate a serie of checkboxes with labels for label method" do
288
- collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
289
- with_collection_check_boxes @user, :tag_ids, collection, :id, :name
290
-
291
- assert_select 'form label.collection_check_boxes[for=user_tag_ids_1]', 'Tag 1'
292
- assert_select 'form label.collection_check_boxes[for=user_tag_ids_2]', 'Tag 2'
293
- end
294
-
295
- test "collection check box handles camelized collection values for labels correctly" do
296
- with_collection_check_boxes @user, :active, %w[Yes No], :to_s, :to_s
297
-
298
- assert_select 'form label.collection_check_boxes[for=user_active_yes]', 'Yes'
299
- assert_select 'form label.collection_check_boxes[for=user_active_no]', 'No'
300
- end
301
-
302
- test "collection check box sanitizes collection values for labels correctly" do
303
- with_collection_check_boxes @user, :name, ['$0.99', '$1.99'], :to_s, :to_s
304
-
305
- # Rails 6 changed the way it sanitizes the values
306
- # https://github.com/rails/rails/blob/6-0-stable/actionview/lib/action_view/helpers/tags/base.rb#L141
307
- # https://github.com/rails/rails/blob/5-2-stable/actionview/lib/action_view/helpers/tags/base.rb#L141
308
- if ActionView::VERSION::MAJOR == 5
309
- assert_select 'label.collection_check_boxes[for=user_name_099]', '$0.99'
310
- assert_select 'label.collection_check_boxes[for=user_name_199]', '$1.99'
311
- else
312
- assert_select 'label.collection_check_boxes[for=user_name_0_99]', '$0.99'
313
- assert_select 'label.collection_check_boxes[for=user_name_1_99]', '$1.99'
314
- end
315
- end
316
-
317
- test "collection check box checks the correct value to local variables" do
318
- user = User.build(tag_ids: [1, 3])
319
- collection = (1..3).map { |i| [i, "Tag #{i}"] }
320
-
321
- with_collection_check_boxes user, :tag_ids, collection, :first, :last
322
-
323
- assert_select 'form input[type=checkbox][value="1"][checked=checked]'
324
- assert_select 'form input[type=checkbox][value="3"][checked=checked]'
325
- assert_no_select 'form input[type=checkbox][value="2"][checked=checked]'
326
- end
327
-
328
- test "collection check box accepts selected values as :checked option" do
329
- collection = (1..3).map { |i| [i, "Tag #{i}"] }
330
- with_collection_check_boxes @user, :tag_ids, collection, :first, :last, checked: [1, 3]
331
-
332
- assert_select 'form input[type=checkbox][value="1"][checked=checked]'
333
- assert_select 'form input[type=checkbox][value="3"][checked=checked]'
334
- assert_no_select 'form input[type=checkbox][value="2"][checked=checked]'
335
- end
336
-
337
- test "collection check boxes accepts selected string values as :checked option" do
338
- collection = (1..3).map { |i| [i, "Category #{i}"] }
339
- with_collection_check_boxes :user, :category_ids, collection, :first, :last, checked: %w[1 3]
340
-
341
- assert_select 'input[type=checkbox][value="1"][checked=checked]'
342
- assert_select 'input[type=checkbox][value="3"][checked=checked]'
343
- assert_no_select 'input[type=checkbox][value="2"][checked=checked]'
344
- end
345
-
346
- test "collection check box accepts a single checked value" do
347
- collection = (1..3).map { |i| [i, "Tag #{i}"] }
348
- with_collection_check_boxes @user, :tag_ids, collection, :first, :last, checked: 3
349
-
350
- assert_select 'form input[type=checkbox][value="3"][checked=checked]'
351
- assert_no_select 'form input[type=checkbox][value="1"][checked=checked]'
352
- assert_no_select 'form input[type=checkbox][value="2"][checked=checked]'
353
- end
354
-
355
- test "collection check box accepts selected values as :checked option and override the model values" do
356
- collection = (1..3).map { |i| [i, "Tag #{i}"] }
357
- @user.tag_ids = [2]
358
- with_collection_check_boxes @user, :tag_ids, collection, :first, :last, checked: [1, 3]
359
-
360
- assert_select 'form input[type=checkbox][value="1"][checked=checked]'
361
- assert_select 'form input[type=checkbox][value="3"][checked=checked]'
362
- assert_no_select 'form input[type=checkbox][value="2"][checked=checked]'
363
- end
364
-
365
- test "collection check box accepts multiple disabled items" do
366
- collection = (1..3).map { |i| [i, "Tag #{i}"] }
367
- with_collection_check_boxes @user, :tag_ids, collection, :first, :last, disabled: [1, 3]
368
-
369
- assert_select 'form input[type=checkbox][value="1"][disabled=disabled]'
370
- assert_select 'form input[type=checkbox][value="3"][disabled=disabled]'
371
- assert_no_select 'form input[type=checkbox][value="2"][disabled=disabled]'
372
- end
373
-
374
- test "collection check box accepts single disable item" do
375
- collection = (1..3).map { |i| [i, "Tag #{i}"] }
376
- with_collection_check_boxes @user, :tag_ids, collection, :first, :last, disabled: 1
377
-
378
- assert_select 'form input[type=checkbox][value="1"][disabled=disabled]'
379
- assert_no_select 'form input[type=checkbox][value="3"][disabled=disabled]'
380
- assert_no_select 'form input[type=checkbox][value="2"][disabled=disabled]'
381
- end
382
-
383
- test "collection check box accepts a proc to disabled items" do
384
- collection = (1..3).map { |i| [i, "Tag #{i}"] }
385
- with_collection_check_boxes @user, :tag_ids, collection, :first, :last, disabled: proc { |i| i.first == 1 }
386
-
387
- assert_select 'form input[type=checkbox][value="1"][disabled=disabled]'
388
- assert_no_select 'form input[type=checkbox][value="3"][disabled=disabled]'
389
- assert_no_select 'form input[type=checkbox][value="2"][disabled=disabled]'
390
- end
391
-
392
- test "collection check box accepts html options" do
393
- collection = [[1, 'Tag 1'], [2, 'Tag 2']]
394
- with_collection_check_boxes @user, :tag_ids, collection, :first, :last, {}, class: 'check'
395
-
396
- assert_select 'form input.check[type=checkbox][value="1"]'
397
- assert_select 'form input.check[type=checkbox][value="2"]'
398
- end
399
-
400
- test "collection check box with fields for" do
401
- collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
402
- with_concat_form_for(@user) do |f|
403
- f.fields_for(:post) do |p|
404
- p.collection_check_boxes :tag_ids, collection, :id, :name
405
- end
406
- end
407
-
408
- assert_select 'form input#user_post_tag_ids_1[type=checkbox][value="1"]'
409
- assert_select 'form input#user_post_tag_ids_2[type=checkbox][value="2"]'
410
-
411
- assert_select 'form label.collection_check_boxes[for=user_post_tag_ids_1]', 'Tag 1'
412
- assert_select 'form label.collection_check_boxes[for=user_post_tag_ids_2]', 'Tag 2'
413
- end
414
-
415
- test "collection check boxes wraps the collection in the given collection wrapper tag" do
416
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, collection_wrapper_tag: :ul
417
-
418
- assert_select 'form ul input[type=checkbox]', count: 2
419
- end
420
-
421
- test "collection check boxes does not render any wrapper tag by default" do
422
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
423
-
424
- assert_select 'form input[type=checkbox]', count: 2
425
- assert_no_select 'form ul'
426
- end
427
-
428
- test "collection check boxes does not wrap the collection when given falsy values" do
429
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, collection_wrapper_tag: false
430
-
431
- assert_select 'form input[type=checkbox]', count: 2
432
- assert_no_select 'form ul'
433
- end
434
-
435
- test "collection check boxes uses the given class for collection wrapper tag" do
436
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s,
437
- collection_wrapper_tag: :ul, collection_wrapper_class: "items-list"
438
-
439
- assert_select 'form ul.items-list input[type=checkbox]', count: 2
440
- end
441
-
442
- test "collection check boxes uses no class for collection wrapper tag when no wrapper tag is given" do
443
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s,
444
- collection_wrapper_class: "items-list"
445
-
446
- assert_select 'form input[type=checkbox]', count: 2
447
- assert_no_select 'form ul'
448
- assert_no_select '.items-list'
449
- end
450
-
451
- test "collection check boxes uses no class for collection wrapper tag by default" do
452
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, collection_wrapper_tag: :ul
453
-
454
- assert_select 'form ul'
455
- assert_no_select 'form ul[class]'
456
- end
457
-
458
- test "collection check boxes wrap items in a span tag by default" do
459
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
460
-
461
- assert_select 'form span input[type=checkbox]', count: 2
462
- end
463
-
464
- test "collection check boxes wraps each item in the given item wrapper tag" do
465
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, item_wrapper_tag: :li
466
-
467
- assert_select 'form li input[type=checkbox]', count: 2
468
- end
469
-
470
- test "collection check boxes does not wrap each item when given explicitly falsy value" do
471
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, item_wrapper_tag: false
472
-
473
- assert_select 'form input[type=checkbox]'
474
- assert_no_select 'form span input[type=checkbox]'
475
- end
476
-
477
- test "collection check boxes uses the given class for item wrapper tag" do
478
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s,
479
- item_wrapper_tag: :li, item_wrapper_class: "inline"
480
-
481
- assert_select "form li.inline input[type=checkbox]", count: 2
482
- end
483
-
484
- test "collection check boxes uses no class for item wrapper tag when no wrapper tag is given" do
485
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s,
486
- item_wrapper_tag: nil, item_wrapper_class: "inline"
487
-
488
- assert_select 'form input[type=checkbox]', count: 2
489
- assert_no_select 'form li'
490
- assert_no_select '.inline'
491
- end
492
-
493
- test "collection check boxes uses no class for item wrapper tag by default" do
494
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s,
495
- item_wrapper_tag: :li
496
-
497
- assert_select "form li", count: 2
498
- assert_no_select "form li[class]"
499
- end
500
-
501
- test "collection check box does not wrap input inside the label" do
502
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
503
-
504
- assert_select 'form input[type=checkbox] + label'
505
- assert_no_select 'form label input'
506
- end
507
-
508
- test "collection check boxes accepts a block to render the label as check box wrapper" do
509
- with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
510
- b.label { b.check_box }
511
- end
512
-
513
- assert_select 'label[for=user_active_true] > input#user_active_true[type=checkbox]'
514
- assert_select 'label[for=user_active_false] > input#user_active_false[type=checkbox]'
515
- end
516
-
517
- test "collection check boxes accepts a block to change the order of label and check box" do
518
- with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
519
- b.label + b.check_box
520
- end
521
-
522
- assert_select 'label[for=user_active_true] + input#user_active_true[type=checkbox]'
523
- assert_select 'label[for=user_active_false] + input#user_active_false[type=checkbox]'
524
- end
525
-
526
- test "collection check boxes with block helpers accept extra html options" do
527
- with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
528
- b.label(class: "check_box") + b.check_box(class: "check_box")
529
- end
530
-
531
- assert_select 'label.check_box[for=user_active_true] + input#user_active_true.check_box[type=checkbox]'
532
- assert_select 'label.check_box[for=user_active_false] + input#user_active_false.check_box[type=checkbox]'
533
- end
534
-
535
- test "collection check boxes with block helpers allows access to current text and value" do
536
- with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
537
- b.label(:"data-value" => b.value) { b.check_box + b.text }
538
- end
539
-
540
- assert_select 'label[for=user_active_true][data-value=true]', 'true' do
541
- assert_select 'input#user_active_true[type=checkbox]'
542
- end
543
- assert_select 'label[for=user_active_false][data-value=false]', 'false' do
544
- assert_select 'input#user_active_false[type=checkbox]'
545
- end
546
- end
547
-
548
- test "collection check boxes with block helpers allows access to the current object item in the collection to access extra properties" do
549
- with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
550
- b.label(class: b.object) { b.check_box + b.text }
551
- end
552
-
553
- assert_select 'label.true[for=user_active_true]', 'true' do
554
- assert_select 'input#user_active_true[type=checkbox]'
555
- end
556
- assert_select 'label.false[for=user_active_false]', 'false' do
557
- assert_select 'input#user_active_false[type=checkbox]'
558
- end
559
- end
560
-
561
- test "collection check boxes with block helpers does not leak the template" do
562
- with_concat_form_for(@user) do |f|
563
- collection_input = f.collection_check_boxes :active, [true, false], :to_s, :to_s do |b|
564
- b.label(class: b.object) { b.check_box + b.text }
565
- end
566
- concat collection_input
567
-
568
- concat f.hidden_field :name
569
- end
570
-
571
- assert_select 'label.true[for=user_active_true]', text: 'true', count: 1 do
572
- assert_select 'input#user_active_true[type=checkbox]'
573
- end
574
- assert_select 'label.false[for=user_active_false]', text: 'false', count: 1 do
575
- assert_select 'input#user_active_false[type=checkbox]'
576
- end
577
- end
578
-
579
- # SIMPLE FIELDS
580
- test "simple fields for is available and yields an instance of FormBuilder" do
581
- with_concat_form_for(@user) do |f|
582
- f.simple_fields_for(:posts) do |posts_form|
583
- assert posts_form.instance_of?(SimpleForm::FormBuilder)
584
- end
585
- end
586
- end
587
-
588
- test "fields for with a hash like model yeilds an instance of FormBuilder" do
589
- with_concat_form_for(:user) do |f|
590
- f.simple_fields_for(:author, HashBackedAuthor.new) do |author|
591
- assert author.instance_of?(SimpleForm::FormBuilder)
592
- author.input :name
593
- end
594
- end
595
-
596
- assert_select "input[name='user[author][name]'][value='hash backed author']"
597
- end
598
-
599
- test "fields for yields an instance of CustomBuilder if main builder is a CustomBuilder" do
600
- with_custom_form_for(:user) do |f|
601
- f.simple_fields_for(:company) do |company|
602
- assert company.instance_of?(CustomFormBuilder)
603
- end
604
- end
605
- end
606
-
607
- test "fields for yields an instance of FormBuilder if it was set in options" do
608
- with_custom_form_for(:user) do |f|
609
- f.simple_fields_for(:company, builder: SimpleForm::FormBuilder) do |company|
610
- assert company.instance_of?(SimpleForm::FormBuilder)
611
- end
612
- end
613
- end
614
-
615
- test "fields inherits wrapper option from the parent form" do
616
- swap_wrapper :another do
617
- simple_form_for(:user, wrapper: :another) do |f|
618
- f.simple_fields_for(:company) do |company|
619
- assert_equal :another, company.options[:wrapper]
620
- end
621
- end
622
- end
623
- end
624
-
625
- test "fields overrides wrapper option from the parent form" do
626
- swap_wrapper :another do
627
- simple_form_for(:user, wrapper: :another) do |f|
628
- f.simple_fields_for(:company, wrapper: false) do |company|
629
- assert_equal false, company.options[:wrapper]
630
- end
631
- end
632
- end
633
- end
634
- end