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