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
@@ -8,48 +8,48 @@ class BuilderTest < ActionView::TestCase
8
8
  end
9
9
  end
10
10
 
11
- def with_collection_radio(object, attribute, collection, value_method, text_method, options={}, html_options={})
11
+ def with_collection_radio_buttons(object, attribute, collection, value_method, text_method, options={}, html_options={}, &block)
12
12
  with_concat_form_for(object) do |f|
13
- f.collection_radio attribute, collection, value_method, text_method, options, html_options
13
+ f.collection_radio_buttons attribute, collection, value_method, text_method, options, html_options, &block
14
14
  end
15
15
  end
16
16
 
17
- def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options={}, html_options={})
17
+ def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options={}, html_options={}, &block)
18
18
  with_concat_form_for(object) do |f|
19
- f.collection_check_boxes attribute, collection, value_method, text_method, options, html_options
19
+ f.collection_check_boxes attribute, collection, value_method, text_method, options, html_options, &block
20
20
  end
21
21
  end
22
22
 
23
23
  # COLLECTION RADIO
24
24
  test 'collection radio accepts a collection and generate inputs from value method' do
25
- with_collection_radio @user, :active, [true, false], :to_s, :to_s
25
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s
26
26
 
27
27
  assert_select 'form input[type=radio][value=true]#user_active_true'
28
28
  assert_select 'form input[type=radio][value=false]#user_active_false'
29
29
  end
30
30
 
31
31
  test 'collection radio accepts a collection and generate inputs from label method' do
32
- with_collection_radio @user, :active, [true, false], :to_s, :to_s
32
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s
33
33
 
34
- assert_select 'form label.collection_radio[for=user_active_true]', 'true'
35
- assert_select 'form label.collection_radio[for=user_active_false]', 'false'
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
36
  end
37
37
 
38
38
  test 'collection radio handles camelized collection values for labels correctly' do
39
- with_collection_radio @user, :active, ['Yes', 'No'], :to_s, :to_s
39
+ with_collection_radio_buttons @user, :active, ['Yes', 'No'], :to_s, :to_s
40
40
 
41
- assert_select 'form label.collection_radio[for=user_active_yes]', 'Yes'
42
- assert_select 'form label.collection_radio[for=user_active_no]', 'No'
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
43
  end
44
44
 
45
- test 'colection radio should sanitize collection values for labels correctly' do
46
- with_collection_radio @user, :name, ['$0.99', '$1.99'], :to_s, :to_s
47
- assert_select 'label.collection_radio[for=user_name_099]', '$0.99'
48
- assert_select 'label.collection_radio[for=user_name_199]', '$1.99'
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
49
  end
50
50
 
51
51
  test 'collection radio accepts checked item' do
52
- with_collection_radio @user, :active, [[1, true], [0, false]], :last, :first, :checked => true
52
+ with_collection_radio_buttons @user, :active, [[1, true], [0, false]], :last, :first, :checked => true
53
53
 
54
54
  assert_select 'form input[type=radio][value=true][checked=checked]'
55
55
  assert_no_select 'form input[type=radio][value=false][checked=checked]'
@@ -57,7 +57,7 @@ class BuilderTest < ActionView::TestCase
57
57
 
58
58
  test 'collection radio accepts multiple disabled items' do
59
59
  collection = [[1, true], [0, false], [2, 'other']]
60
- with_collection_radio @user, :active, collection, :last, :first, :disabled => [true, false]
60
+ with_collection_radio_buttons @user, :active, collection, :last, :first, :disabled => [true, false]
61
61
 
62
62
  assert_select 'form input[type=radio][value=true][disabled=disabled]'
63
63
  assert_select 'form input[type=radio][value=false][disabled=disabled]'
@@ -66,7 +66,7 @@ class BuilderTest < ActionView::TestCase
66
66
 
67
67
  test 'collection radio accepts single disable item' do
68
68
  collection = [[1, true], [0, false]]
69
- with_collection_radio @user, :active, collection, :last, :first, :disabled => true
69
+ with_collection_radio_buttons @user, :active, collection, :last, :first, :disabled => true
70
70
 
71
71
  assert_select 'form input[type=radio][value=true][disabled=disabled]'
72
72
  assert_no_select 'form input[type=radio][value=false][disabled=disabled]'
@@ -74,105 +74,187 @@ class BuilderTest < ActionView::TestCase
74
74
 
75
75
  test 'collection radio accepts html options as input' do
76
76
  collection = [[1, true], [0, false]]
77
- with_collection_radio @user, :active, collection, :last, :first, {}, :class => 'radio'
77
+ with_collection_radio_buttons @user, :active, collection, :last, :first, {}, :class => 'special-radio'
78
78
 
79
- assert_select 'form input[type=radio][value=true].radio#user_active_true'
80
- assert_select 'form input[type=radio][value=false].radio#user_active_false'
79
+ assert_select 'form input[type=radio][value=true].special-radio#user_active_true'
80
+ assert_select 'form input[type=radio][value=false].special-radio#user_active_false'
81
81
  end
82
82
 
83
- test 'collection radio wraps the collection in the configured collection wrapper tag' do
84
- swap SimpleForm, :collection_wrapper_tag => :ul do
85
- with_collection_radio @user, :active, [true, false], :to_s, :to_s
83
+ test 'collection radio wraps the collection in the given collection wrapper tag' do
84
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul
86
85
 
87
- assert_select 'form ul input[type=radio][value=true]#user_active_true'
88
- assert_select 'form ul input[type=radio][value=false]#user_active_false'
89
- end
86
+ assert_select 'form ul input[type=radio]', :count => 2
90
87
  end
91
88
 
92
- test 'collection radio wraps the collection in the given collection wrapper tag' do
93
- with_collection_radio @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul
89
+ test 'collection radio does not render any wrapper tag by default' do
90
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s
94
91
 
95
- assert_select 'form ul input[type=radio][value=true]#user_active_true'
96
- assert_select 'form ul input[type=radio][value=false]#user_active_false'
92
+ assert_select 'form input[type=radio]', :count => 2
93
+ assert_no_select 'form ul'
97
94
  end
98
95
 
99
- test 'collection radio does not wrap the collection in the explicitly false collection wrapper tag' do
100
- swap SimpleForm, :collection_wrapper_tag => :ul do
101
- with_collection_radio @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => false
96
+ test 'collection radio does not wrap the collection when given falsy values' do
97
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => false
102
98
 
103
- assert_no_select 'form ul'
104
- assert_no_select 'form ul'
105
- end
99
+ assert_select 'form input[type=radio]', :count => 2
100
+ assert_no_select 'form ul'
106
101
  end
107
102
 
108
- test 'collection radio does not wrap the collection in the explicitly nil collection wrapper tag' do
109
- swap SimpleForm, :collection_wrapper_tag => :ul do
110
- with_collection_radio @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => nil
103
+ test 'collection radio uses the given class for collection wrapper tag' do
104
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s,
105
+ :collection_wrapper_tag => :ul, :collection_wrapper_class => "items-list"
111
106
 
112
- assert_no_select 'form ul'
113
- assert_no_select 'form ul'
114
- end
107
+ assert_select 'form ul.items-list input[type=radio]', :count => 2
115
108
  end
116
109
 
117
- test 'collection radio does not wrap the collection by default' do
118
- with_collection_radio @user, :active, [true, false], :to_s, :to_s
110
+ test 'collection radio uses no class for collection wrapper tag when no wrapper tag is given' do
111
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s,
112
+ :collection_wrapper_class => "items-list"
119
113
 
114
+ assert_select 'form input[type=radio]', :count => 2
120
115
  assert_no_select 'form ul'
116
+ assert_no_select '.items-list'
121
117
  end
122
118
 
123
- test 'collection radio wraps each label/radio in the configured item wrapper tag' do
124
- swap SimpleForm, :item_wrapper_tag => :li do
125
- with_collection_radio @user, :active, [true, false], :to_s, :to_s
119
+ test 'collection radio uses no class for collection wrapper tag by default' do
120
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul
126
121
 
127
- assert_select 'form li input[type=radio][value=true]#user_active_true'
128
- assert_select 'form li input[type=radio][value=false]#user_active_false'
129
- end
122
+ assert_select 'form ul'
123
+ assert_no_select 'form ul[class]'
130
124
  end
131
125
 
132
- test 'collection radio wraps each label/radio in the given item wrapper tag' do
133
- with_collection_radio @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => :li
126
+ test 'collection radio wrap items in a span tag by default' do
127
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s
134
128
 
135
- assert_select 'form li input[type=radio][value=true]#user_active_true'
136
- assert_select 'form li input[type=radio][value=false]#user_active_false'
129
+ assert_select 'form span input[type=radio][value=true]#user_active_true + label'
130
+ assert_select 'form span input[type=radio][value=false]#user_active_false + label'
137
131
  end
138
132
 
139
- test 'collection radio does not wrap each label/radio in the explicitly false item wrapper tag' do
140
- with_collection_radio @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => false
133
+ test 'collection radio wraps each item in the given item wrapper tag' do
134
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => :li
141
135
 
142
- assert_no_select 'form span input[type=radio][value=true]#user_active_true'
143
- assert_no_select 'form span input[type=radio][value=false]#user_active_false'
136
+ assert_select 'form li input[type=radio]', :count => 2
144
137
  end
145
138
 
146
- test 'collection radio does not wrap each label/radio in the explicitly nil item wrapper tag' do
147
- with_collection_radio @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => nil
139
+ test 'collection radio does not wrap each item when given explicitly falsy value' do
140
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => false
148
141
 
149
- assert_no_select 'form span input[type=radio][value=true]#user_active_true'
150
- assert_no_select 'form span input[type=radio][value=false]#user_active_false'
142
+ assert_select 'form input[type=radio]'
143
+ assert_no_select 'form span input[type=radio]'
151
144
  end
152
145
 
153
- test 'collection radio wrap items in a span tag by default' do
154
- with_collection_radio @user, :active, [true, false], :to_s, :to_s
146
+ test 'collection radio uses the given class for item wrapper tag' do
147
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s,
148
+ :item_wrapper_tag => :li, :item_wrapper_class => "inline"
155
149
 
156
- assert_select 'form span input[type=radio][value=true]#user_active_true + label'
157
- assert_select 'form span input[type=radio][value=false]#user_active_false + label'
150
+ assert_select "form li.inline input[type=radio]", :count => 2
151
+ end
152
+
153
+ test 'collection radio uses no class for item wrapper tag when no wrapper tag is given' do
154
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s,
155
+ :item_wrapper_tag => nil, :item_wrapper_class => "inline"
156
+
157
+ assert_select 'form input[type=radio]', :count => 2
158
+ assert_no_select 'form li'
159
+ assert_no_select '.inline'
160
+ end
161
+
162
+ test 'collection radio uses no class for item wrapper tag by default' do
163
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s,
164
+ :item_wrapper_tag => :li
165
+
166
+ assert_select "form li", :count => 2
167
+ assert_no_select "form li[class]"
158
168
  end
159
169
 
160
170
  test 'collection radio does not wrap input inside the label' do
161
- with_collection_radio @user, :active, [true, false], :to_s, :to_s
171
+ with_collection_radio_buttons @user, :active, [true, false], :to_s, :to_s
162
172
 
173
+ assert_select 'form input[type=radio] + label'
163
174
  assert_no_select 'form label input'
164
175
  end
165
176
 
177
+ test 'collection radio accepts a block to render the label as radio button wrapper' do
178
+ with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
179
+ b.label { b.radio_button }
180
+ end
181
+
182
+ assert_select 'label[for=user_active_true] > input#user_active_true[type=radio]'
183
+ assert_select 'label[for=user_active_false] > input#user_active_false[type=radio]'
184
+ end
185
+
186
+ test 'collection radio accepts a block to change the order of label and radio button' do
187
+ with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
188
+ b.label + b.radio_button
189
+ end
190
+
191
+ assert_select 'label[for=user_active_true] + input#user_active_true[type=radio]'
192
+ assert_select 'label[for=user_active_false] + input#user_active_false[type=radio]'
193
+ end
194
+
195
+ test 'collection radio with block helpers accept extra html options' do
196
+ with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
197
+ b.label(:class => "radio_button") + b.radio_button(:class => "radio_button")
198
+ end
199
+
200
+ assert_select 'label.radio_button[for=user_active_true] + input#user_active_true.radio_button[type=radio]'
201
+ assert_select 'label.radio_button[for=user_active_false] + input#user_active_false.radio_button[type=radio]'
202
+ end
203
+
204
+ test 'collection radio with block helpers allows access to current text and value' do
205
+ with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
206
+ b.label(:"data-value" => b.value) { b.radio_button + b.text }
207
+ end
208
+
209
+ assert_select 'label[for=user_active_true][data-value=true]', 'true' do
210
+ assert_select 'input#user_active_true[type=radio]'
211
+ end
212
+ assert_select 'label[for=user_active_false][data-value=false]', 'false' do
213
+ assert_select 'input#user_active_false[type=radio]'
214
+ end
215
+ end
216
+
217
+ test 'collection radio with block helpers allows access to the current object item in the collection to access extra properties' do
218
+ with_collection_radio_buttons :user, :active, [true, false], :to_s, :to_s do |b|
219
+ b.label(:class => b.object) { b.radio_button + b.text }
220
+ end
221
+
222
+ assert_select 'label.true[for=user_active_true]', 'true' do
223
+ assert_select 'input#user_active_true[type=radio]'
224
+ end
225
+ assert_select 'label.false[for=user_active_false]', 'false' do
226
+ assert_select 'input#user_active_false[type=radio]'
227
+ end
228
+ end
229
+
230
+ test 'collection_radio helper is deprecated in favor of collection_radio_buttons' do
231
+ assert_deprecated "[SIMPLE_FORM] The `collection_radio` helper is deprecated, " \
232
+ "please use `collection_radio_buttons` instead" do
233
+ with_concat_form_for(@user) do |f|
234
+ f.collection_radio :active, [true, false], :to_s, :to_s
235
+ end
236
+ end
237
+
238
+ assert_select 'input[type=radio][value=true]'
239
+ assert_select 'input[type=radio][value=false]'
240
+ end
241
+
166
242
  # COLLECTION CHECK BOX
167
243
  test 'collection check box accepts a collection and generate a serie of checkboxes for value method' do
168
244
  collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
169
245
  with_collection_check_boxes @user, :tag_ids, collection, :id, :name
170
246
 
171
- assert_select "form input[type=hidden][name='user[tag_ids][]'][value=]"
172
247
  assert_select 'form input#user_tag_ids_1[type=checkbox][value=1]'
173
248
  assert_select 'form input#user_tag_ids_2[type=checkbox][value=2]'
174
249
  end
175
250
 
251
+ 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
252
+ collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
253
+ with_collection_check_boxes @user, :tag_ids, collection, :id, :name
254
+
255
+ assert_select "form input[type=hidden][name='user[tag_ids][]'][value=]", :count => 1
256
+ end
257
+
176
258
  test 'collection check box accepts a collection and generate a serie of checkboxes with labels for label method' do
177
259
  collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')]
178
260
  with_collection_check_boxes @user, :tag_ids, collection, :id, :name
@@ -188,7 +270,7 @@ class BuilderTest < ActionView::TestCase
188
270
  assert_select 'form label.collection_check_boxes[for=user_active_no]', 'No'
189
271
  end
190
272
 
191
- test 'colection check box should sanitize collection values for labels correctly' do
273
+ test 'collection check box should sanitize collection values for labels correctly' do
192
274
  with_collection_check_boxes @user, :name, ['$0.99', '$1.99'], :to_s, :to_s
193
275
  assert_select 'label.collection_check_boxes[for=user_name_099]', '$0.99'
194
276
  assert_select 'label.collection_check_boxes[for=user_name_199]', '$1.99'
@@ -212,6 +294,16 @@ class BuilderTest < ActionView::TestCase
212
294
  assert_no_select 'form input[type=checkbox][value=2][checked=checked]'
213
295
  end
214
296
 
297
+ test 'collection check box accepts selected values as :checked option and override the model values' do
298
+ collection = (1..3).map{|i| [i, "Tag #{i}"] }
299
+ @user.tag_ids = [2]
300
+ with_collection_check_boxes @user, :tag_ids, collection, :first, :last, :checked => [1, 3]
301
+
302
+ assert_select 'form input[type=checkbox][value=1][checked=checked]'
303
+ assert_select 'form input[type=checkbox][value=3][checked=checked]'
304
+ assert_no_select 'form input[type=checkbox][value=2][checked=checked]'
305
+ end
306
+
215
307
  test 'collection check box accepts multiple disabled items' do
216
308
  collection = (1..3).map{|i| [i, "Tag #{i}"] }
217
309
  with_collection_check_boxes @user, :tag_ids, collection, :first, :last, :disabled => [1, 3]
@@ -262,71 +354,152 @@ class BuilderTest < ActionView::TestCase
262
354
  assert_select 'form label.collection_check_boxes[for=user_post_tag_ids_2]', 'Tag 2'
263
355
  end
264
356
 
265
- test 'collection check box wraps the collection in the configured collection wrapper tag' do
266
- swap SimpleForm, :collection_wrapper_tag => :ul do
267
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
357
+ test 'collection check boxes wraps the collection in the given collection wrapper tag' do
358
+ with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul
268
359
 
269
- assert_select 'form ul input[type=checkbox][value=true]#user_active_true'
270
- assert_select 'form ul input[type=checkbox][value=false]#user_active_false'
271
- end
360
+ assert_select 'form ul input[type=checkbox]', :count => 2
272
361
  end
273
362
 
274
- test 'collection check box wraps the collection in the given collection wrapper tag' do
275
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul
363
+ test 'collection check boxes does not render any wrapper tag by default' do
364
+ with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
276
365
 
277
- assert_select 'form ul input[type=checkbox][value=true]#user_active_true'
278
- assert_select 'form ul input[type=checkbox][value=false]#user_active_false'
366
+ assert_select 'form input[type=checkbox]', :count => 2
367
+ assert_no_select 'form ul'
279
368
  end
280
369
 
281
- test 'collection check box does not wrap the collection in the explicitly false collection wrapper tag' do
282
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => false, :item_wrapper_tag => false
370
+ test 'collection check boxes does not wrap the collection when given falsy values' do
371
+ with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => false
283
372
 
284
- assert_select 'form > input[type=checkbox][value=true]#user_active_true'
285
- assert_select 'form > input[type=checkbox][value=false]#user_active_false'
373
+ assert_select 'form input[type=checkbox]', :count => 2
374
+ assert_no_select 'form ul'
286
375
  end
287
376
 
288
- test 'collection check box does not wrap the collection by default' do
289
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
377
+ test 'collection check boxes uses the given class for collection wrapper tag' do
378
+ with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s,
379
+ :collection_wrapper_tag => :ul, :collection_wrapper_class => "items-list"
380
+
381
+ assert_select 'form ul.items-list input[type=checkbox]', :count => 2
382
+ end
290
383
 
384
+ test 'collection check boxes uses no class for collection wrapper tag when no wrapper tag is given' do
385
+ with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s,
386
+ :collection_wrapper_class => "items-list"
387
+
388
+ assert_select 'form input[type=checkbox]', :count => 2
291
389
  assert_no_select 'form ul'
390
+ assert_no_select '.items-list'
292
391
  end
293
392
 
294
- test 'collection check box wraps each label/radio in the configured item wrapper tag' do
295
- swap SimpleForm, :item_wrapper_tag => :li do
296
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
393
+ test 'collection check boxes uses no class for collection wrapper tag by default' do
394
+ with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :collection_wrapper_tag => :ul
297
395
 
298
- assert_select 'form li input[type=checkbox][value=true]#user_active_true'
299
- assert_select 'form li input[type=checkbox][value=false]#user_active_false'
300
- end
396
+ assert_select 'form ul'
397
+ assert_no_select 'form ul[class]'
398
+ end
399
+
400
+ test 'collection check boxes wrap items in a span tag by default' do
401
+ with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
402
+
403
+ assert_select 'form span input[type=checkbox]', :count => 2
301
404
  end
302
405
 
303
- test 'collection check box wraps each label/radio in the given item wrapper tag' do
406
+ test 'collection check boxes wraps each item in the given item wrapper tag' do
304
407
  with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => :li
305
408
 
306
- assert_select 'form li input[type=checkbox][value=true]#user_active_true'
307
- assert_select 'form li input[type=checkbox][value=false]#user_active_false'
409
+ assert_select 'form li input[type=checkbox]', :count => 2
308
410
  end
309
411
 
310
- test 'collection check box does not wrapp each label/radio in the explicitly false item wrapper tag' do
412
+ test 'collection check boxes does not wrap each item when given explicitly falsy value' do
311
413
  with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s, :item_wrapper_tag => false
312
414
 
313
- assert_select 'form > input[type=checkbox][value=true]#user_active_true'
314
- assert_select 'form > input[type=checkbox][value=false]#user_active_false'
415
+ assert_select 'form input[type=checkbox]'
416
+ assert_no_select 'form span input[type=checkbox]'
315
417
  end
316
418
 
317
- test 'collection check box wrap items in a span tag by default' do
318
- with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
419
+ test 'collection check boxes uses the given class for item wrapper tag' do
420
+ with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s,
421
+ :item_wrapper_tag => :li, :item_wrapper_class => "inline"
422
+
423
+ assert_select "form li.inline input[type=checkbox]", :count => 2
424
+ end
425
+
426
+ test 'collection check boxes uses no class for item wrapper tag when no wrapper tag is given' do
427
+ with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s,
428
+ :item_wrapper_tag => nil, :item_wrapper_class => "inline"
429
+
430
+ assert_select 'form input[type=checkbox]', :count => 2
431
+ assert_no_select 'form li'
432
+ assert_no_select '.inline'
433
+ end
434
+
435
+ test 'collection check boxes uses no class for item wrapper tag by default' do
436
+ with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s,
437
+ :item_wrapper_tag => :li
319
438
 
320
- assert_select 'form span input[type=checkbox][value=true]#user_active_true + label'
321
- assert_select 'form span input[type=checkbox][value=false]#user_active_false + label'
439
+ assert_select "form li", :count => 2
440
+ assert_no_select "form li[class]"
322
441
  end
323
442
 
324
443
  test 'collection check box does not wrap input inside the label' do
325
444
  with_collection_check_boxes @user, :active, [true, false], :to_s, :to_s
326
445
 
446
+ assert_select 'form input[type=checkbox] + label'
327
447
  assert_no_select 'form label input'
328
448
  end
329
449
 
450
+ test 'collection check boxes accepts a block to render the label as check box wrapper' do
451
+ with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
452
+ b.label { b.check_box }
453
+ end
454
+
455
+ assert_select 'label[for=user_active_true] > input#user_active_true[type=checkbox]'
456
+ assert_select 'label[for=user_active_false] > input#user_active_false[type=checkbox]'
457
+ end
458
+
459
+ test 'collection check boxes accepts a block to change the order of label and check box' do
460
+ with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
461
+ b.label + b.check_box
462
+ end
463
+
464
+ assert_select 'label[for=user_active_true] + input#user_active_true[type=checkbox]'
465
+ assert_select 'label[for=user_active_false] + input#user_active_false[type=checkbox]'
466
+ end
467
+
468
+ test 'collection check boxes with block helpers accept extra html options' do
469
+ with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
470
+ b.label(:class => "check_box") + b.check_box(:class => "check_box")
471
+ end
472
+
473
+ assert_select 'label.check_box[for=user_active_true] + input#user_active_true.check_box[type=checkbox]'
474
+ assert_select 'label.check_box[for=user_active_false] + input#user_active_false.check_box[type=checkbox]'
475
+ end
476
+
477
+ test 'collection check boxes with block helpers allows access to current text and value' do
478
+ with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
479
+ b.label(:"data-value" => b.value) { b.check_box + b.text }
480
+ end
481
+
482
+ assert_select 'label[for=user_active_true][data-value=true]', 'true' do
483
+ assert_select 'input#user_active_true[type=checkbox]'
484
+ end
485
+ assert_select 'label[for=user_active_false][data-value=false]', 'false' do
486
+ assert_select 'input#user_active_false[type=checkbox]'
487
+ end
488
+ end
489
+
490
+ test 'collection check boxes with block helpers allows access to the current object item in the collection to access extra properties' do
491
+ with_collection_check_boxes :user, :active, [true, false], :to_s, :to_s do |b|
492
+ b.label(:class => b.object) { b.check_box + b.text }
493
+ end
494
+
495
+ assert_select 'label.true[for=user_active_true]', 'true' do
496
+ assert_select 'input#user_active_true[type=checkbox]'
497
+ end
498
+ assert_select 'label.false[for=user_active_false]', 'false' do
499
+ assert_select 'input#user_active_false[type=checkbox]'
500
+ end
501
+ end
502
+
330
503
  # SIMPLE FIELDS
331
504
  test 'simple fields for is available and yields an instance of FormBuilder' do
332
505
  with_concat_form_for(@user) do |f|
@@ -364,4 +537,14 @@ class BuilderTest < ActionView::TestCase
364
537
  end
365
538
  end
366
539
  end
540
+
541
+ test 'fields inherites wrapper option from the parent form' do
542
+ swap_wrapper :another do
543
+ simple_form_for(:user, :wrapper => :another) do |f|
544
+ f.simple_fields_for(:company) do |company|
545
+ assert_equal :another, company.options[:wrapper]
546
+ end
547
+ end
548
+ end
549
+ end
367
550
  end
@@ -2,23 +2,23 @@ require 'test_helper'
2
2
 
3
3
  class FormHelperTest < ActionView::TestCase
4
4
 
5
- test 'simple form for yields an instance of FormBuilder' do
5
+ test 'SimpleForm for yields an instance of FormBuilder' do
6
6
  simple_form_for :user do |f|
7
7
  assert f.instance_of?(SimpleForm::FormBuilder)
8
8
  end
9
9
  end
10
10
 
11
- test 'simple form should add default class to form' do
11
+ test 'SimpleForm should add default class to form' do
12
12
  concat(simple_form_for(:user) do |f| end)
13
13
  assert_select 'form.simple_form'
14
14
  end
15
15
 
16
- test 'simple form should use default browser validations by default' do
16
+ test 'SimpleForm should use default browser validations by default' do
17
17
  concat(simple_form_for(:user) do |f| end)
18
18
  assert_no_select 'form[novalidate]'
19
19
  end
20
20
 
21
- test 'simple form should not use default browser validations if specified in the configuration options' do
21
+ test 'SimpleForm should not use default browser validations if specified in the configuration options' do
22
22
  swap SimpleForm, :browser_validations => false do
23
23
  concat(simple_form_for(:user) do |f| end)
24
24
  assert_select 'form[novalidate="novalidate"]'
@@ -37,27 +37,49 @@ class FormHelperTest < ActionView::TestCase
37
37
  end
38
38
  end
39
39
 
40
- test 'simple form should add object name as css class to form when object is not present' do
40
+ test 'SimpleForm should add object name as css class to form when object is not present' do
41
41
  concat(simple_form_for(:user) do |f| end)
42
42
  assert_select 'form.simple_form.user'
43
43
  end
44
44
 
45
- test 'simple form should add object class name as css class to form' do
45
+ test 'SimpleForm should add :as option as css class to form when object is not present' do
46
+ concat(simple_form_for(:user, :as => 'superuser') do |f| end)
47
+ assert_select 'form.simple_form.superuser'
48
+ end
49
+
50
+ test 'SimpleForm should add object class name with new prefix as css class to form if record is not persisted' do
51
+ @user.new_record!
46
52
  concat(simple_form_for(@user) do |f| end)
47
- assert_select 'form.simple_form.user'
53
+ assert_select 'form.simple_form.new_user'
54
+ end
55
+
56
+ test 'SimpleForm should add :as option with new prefix as css class to form if record is not persisted' do
57
+ @user.new_record!
58
+ concat(simple_form_for(@user, :as => 'superuser') do |f| end)
59
+ assert_select 'form.simple_form.new_superuser'
60
+ end
61
+
62
+ test 'SimpleForm should add edit class prefix as css class to form if record is persisted' do
63
+ concat(simple_form_for(@user) do |f| end)
64
+ assert_select 'form.simple_form.edit_user'
65
+ end
66
+
67
+ test 'SimpleForm should add :as options with edit prefix as css class to form if record is persisted' do
68
+ concat(simple_form_for(@user, :as => 'superuser') do |f| end)
69
+ assert_select 'form.simple_form.edit_superuser'
48
70
  end
49
71
 
50
- test 'simple form should not add object class to form if css_class is specified' do
72
+ test 'SimpleForm should not add object class to form if css_class is specified' do
51
73
  concat(simple_form_for(:user, :html => {:class => nil}) do |f| end)
52
74
  assert_no_select 'form.user'
53
75
  end
54
76
 
55
- test 'simple form should add custom class to form if css_class is specified' do
77
+ test 'SimpleForm should add custom class to form if css_class is specified' do
56
78
  concat(simple_form_for(:user, :html => {:class => 'my_class'}) do |f| end)
57
79
  assert_select 'form.my_class'
58
80
  end
59
81
 
60
- test 'pass options to simple form' do
82
+ test 'pass options to SimpleForm' do
61
83
  concat(simple_form_for(:user, :url => '/account', :html => { :id => 'my_form' }) do |f| end)
62
84
  assert_select 'form#my_form'
63
85
  assert_select 'form[action=/account]'