simple_form 2.0.0 → 3.5.1
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.
Potentially problematic release.
This version of simple_form might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +97 -198
- data/MIT-LICENSE +1 -1
- data/README.md +572 -296
- data/lib/generators/simple_form/install_generator.rb +17 -7
- data/lib/generators/simple_form/templates/README +3 -4
- data/lib/generators/simple_form/templates/_form.html.erb +1 -0
- data/lib/generators/simple_form/templates/_form.html.haml +1 -0
- data/lib/generators/simple_form/templates/config/initializers/{simple_form.rb.tt → simple_form.rb} +57 -63
- data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +155 -0
- data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +111 -0
- data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +14 -7
- data/lib/simple_form/action_view_extensions/builder.rb +5 -305
- data/lib/simple_form/action_view_extensions/form_helper.rb +18 -20
- data/lib/simple_form/components/errors.rb +30 -3
- data/lib/simple_form/components/hints.rb +10 -3
- data/lib/simple_form/components/html5.rb +17 -3
- data/lib/simple_form/components/label_input.rb +21 -2
- data/lib/simple_form/components/labels.rb +16 -11
- data/lib/simple_form/components/maxlength.rb +19 -12
- data/lib/simple_form/components/min_max.rb +4 -2
- data/lib/simple_form/components/minlength.rb +48 -0
- data/lib/simple_form/components/pattern.rb +5 -4
- data/lib/simple_form/components/placeholders.rb +3 -2
- data/lib/simple_form/components/readonly.rb +3 -2
- data/lib/simple_form/components.rb +15 -11
- data/lib/simple_form/error_notification.rb +4 -3
- data/lib/simple_form/form_builder.rb +283 -105
- data/lib/simple_form/helpers/autofocus.rb +1 -0
- data/lib/simple_form/helpers/disabled.rb +1 -0
- data/lib/simple_form/helpers/readonly.rb +1 -0
- data/lib/simple_form/helpers/required.rb +1 -0
- data/lib/simple_form/helpers/validators.rb +4 -3
- data/lib/simple_form/helpers.rb +7 -6
- data/lib/simple_form/i18n_cache.rb +1 -0
- data/lib/simple_form/inputs/base.rb +76 -23
- data/lib/simple_form/inputs/block_input.rb +3 -2
- data/lib/simple_form/inputs/boolean_input.rb +55 -16
- data/lib/simple_form/inputs/collection_check_boxes_input.rb +2 -1
- data/lib/simple_form/inputs/collection_input.rb +41 -18
- data/lib/simple_form/inputs/collection_radio_buttons_input.rb +11 -19
- data/lib/simple_form/inputs/collection_select_input.rb +5 -2
- data/lib/simple_form/inputs/date_time_input.rb +23 -12
- data/lib/simple_form/inputs/file_input.rb +5 -2
- data/lib/simple_form/inputs/grouped_collection_select_input.rb +16 -3
- data/lib/simple_form/inputs/hidden_input.rb +5 -2
- data/lib/simple_form/inputs/numeric_input.rb +4 -8
- data/lib/simple_form/inputs/password_input.rb +6 -4
- data/lib/simple_form/inputs/priority_input.rb +5 -2
- data/lib/simple_form/inputs/range_input.rb +2 -1
- data/lib/simple_form/inputs/string_input.rb +6 -4
- data/lib/simple_form/inputs/text_input.rb +6 -3
- data/lib/simple_form/inputs.rb +20 -17
- data/lib/simple_form/map_type.rb +1 -0
- data/lib/simple_form/railtie.rb +15 -0
- data/lib/simple_form/tags.rb +69 -0
- data/lib/simple_form/version.rb +2 -1
- data/lib/simple_form/wrappers/builder.rb +12 -35
- data/lib/simple_form/wrappers/leaf.rb +29 -0
- data/lib/simple_form/wrappers/many.rb +12 -7
- data/lib/simple_form/wrappers/root.rb +7 -4
- data/lib/simple_form/wrappers/single.rb +12 -3
- data/lib/simple_form/wrappers.rb +3 -1
- data/lib/simple_form.rb +118 -63
- data/test/action_view_extensions/builder_test.rb +230 -164
- data/test/action_view_extensions/form_helper_test.rb +107 -39
- data/test/components/label_test.rb +105 -87
- data/test/form_builder/association_test.rb +131 -62
- data/test/form_builder/button_test.rb +15 -14
- data/test/form_builder/error_notification_test.rb +11 -10
- data/test/form_builder/error_test.rb +188 -34
- data/test/form_builder/general_test.rb +247 -102
- data/test/form_builder/hint_test.rb +59 -32
- data/test/form_builder/input_field_test.rb +138 -25
- data/test/form_builder/label_test.rb +84 -13
- data/test/form_builder/wrapper_test.rb +236 -33
- data/test/generators/simple_form_generator_test.rb +15 -4
- data/test/inputs/boolean_input_test.rb +147 -13
- data/test/inputs/collection_check_boxes_input_test.rb +166 -71
- data/test/inputs/collection_radio_buttons_input_test.rb +229 -113
- data/test/inputs/collection_select_input_test.rb +222 -85
- data/test/inputs/datetime_input_test.rb +134 -47
- data/test/inputs/disabled_test.rb +62 -21
- data/test/inputs/discovery_test.rb +70 -10
- data/test/inputs/file_input_test.rb +4 -3
- data/test/inputs/general_test.rb +90 -26
- data/test/inputs/grouped_collection_select_input_test.rb +88 -23
- data/test/inputs/hidden_input_test.rb +7 -5
- data/test/inputs/numeric_input_test.rb +56 -46
- data/test/inputs/priority_input_test.rb +31 -16
- data/test/inputs/readonly_test.rb +68 -27
- data/test/inputs/required_test.rb +63 -18
- data/test/inputs/string_input_test.rb +76 -51
- data/test/inputs/text_input_test.rb +21 -8
- data/test/simple_form_test.rb +9 -0
- data/test/support/discovery_inputs.rb +39 -2
- data/test/support/misc_helpers.rb +176 -20
- data/test/support/mock_controller.rb +13 -7
- data/test/support/models.rb +187 -71
- data/test/test_helper.rb +38 -39
- metadata +53 -39
- data/lib/simple_form/core_ext/hash.rb +0 -16
- data/test/support/mock_response.rb +0 -14
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
1
2
|
# encoding: UTF-8
|
|
2
3
|
require 'test_helper'
|
|
3
4
|
|
|
@@ -8,7 +9,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
8
9
|
end
|
|
9
10
|
end
|
|
10
11
|
|
|
11
|
-
test 'nested simple fields
|
|
12
|
+
test 'nested simple fields yields an instance of FormBuilder' do
|
|
12
13
|
simple_form_for :user do |f|
|
|
13
14
|
f.simple_fields_for :posts do |posts_form|
|
|
14
15
|
assert posts_form.instance_of?(SimpleForm::FormBuilder)
|
|
@@ -22,32 +23,66 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
22
23
|
end
|
|
23
24
|
end
|
|
24
25
|
|
|
25
|
-
test 'builder
|
|
26
|
+
test 'builder works without controller' do
|
|
27
|
+
stub_any_instance ActionView::TestCase, :controller, nil do
|
|
28
|
+
simple_form_for @user do |f|
|
|
29
|
+
assert f.input(:name)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
test 'builder works with decorated object responsive to #to_model' do
|
|
35
|
+
assert_nothing_raised do
|
|
36
|
+
with_form_for @decorated_user, :name
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
test 'builder input allows a block to configure input' do
|
|
26
41
|
with_form_for @user, :name do
|
|
27
|
-
text_field_tag :foo, :bar, :
|
|
42
|
+
text_field_tag :foo, :bar, id: :cool
|
|
28
43
|
end
|
|
29
44
|
assert_no_select 'input.string'
|
|
30
45
|
assert_select 'input#cool'
|
|
31
46
|
end
|
|
32
47
|
|
|
33
|
-
test 'builder
|
|
34
|
-
swap SimpleForm, :
|
|
48
|
+
test 'builder allows adding custom input mappings for default input types' do
|
|
49
|
+
swap SimpleForm, input_mappings: { /count$/ => :integer } do
|
|
35
50
|
with_form_for @user, :post_count
|
|
36
51
|
assert_no_select 'form input#user_post_count.string'
|
|
37
52
|
assert_select 'form input#user_post_count.numeric.integer'
|
|
38
53
|
end
|
|
39
54
|
end
|
|
40
55
|
|
|
41
|
-
test 'builder
|
|
42
|
-
swap SimpleForm, :
|
|
56
|
+
test 'builder does not override custom input mappings for custom collection' do
|
|
57
|
+
swap SimpleForm, input_mappings: { /gender$/ => :check_boxes } do
|
|
58
|
+
with_concat_form_for @user do |f|
|
|
59
|
+
f.input :gender, collection: %i[male female]
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
assert_no_select 'select option', 'Male'
|
|
63
|
+
assert_select 'input[type=checkbox][value=male]'
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
test 'builder allows to skip input_type class' do
|
|
68
|
+
swap SimpleForm, generate_additional_classes_for: %i[label wrapper] do
|
|
43
69
|
with_form_for @user, :post_count
|
|
44
70
|
assert_no_select "form input#user_post_count.integer"
|
|
45
71
|
assert_select "form input#user_post_count"
|
|
46
72
|
end
|
|
47
73
|
end
|
|
48
74
|
|
|
49
|
-
test 'builder
|
|
50
|
-
swap SimpleForm, :
|
|
75
|
+
test 'builder allows to add additional classes only for wrapper' do
|
|
76
|
+
swap SimpleForm, generate_additional_classes_for: [:wrapper] do
|
|
77
|
+
with_form_for @user, :post_count
|
|
78
|
+
assert_no_select "form input#user_post_count.string"
|
|
79
|
+
assert_no_select "form label#user_post_count.string"
|
|
80
|
+
assert_select "form div.input.string"
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
test 'builder allows adding custom input mappings for integer input types' do
|
|
85
|
+
swap SimpleForm, input_mappings: { /lock_version/ => :hidden } do
|
|
51
86
|
with_form_for @user, :lock_version
|
|
52
87
|
assert_no_select 'form input#user_lock_version.integer'
|
|
53
88
|
assert_select 'form input#user_lock_version.hidden'
|
|
@@ -55,7 +90,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
55
90
|
end
|
|
56
91
|
|
|
57
92
|
test 'builder uses the first matching custom input map when more than one matches' do
|
|
58
|
-
swap SimpleForm, :
|
|
93
|
+
swap SimpleForm, input_mappings: { /count$/ => :integer, /^post_/ => :password } do
|
|
59
94
|
with_form_for @user, :post_count
|
|
60
95
|
assert_no_select 'form input#user_post_count.password'
|
|
61
96
|
assert_select 'form input#user_post_count.numeric.integer'
|
|
@@ -63,292 +98,402 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
63
98
|
end
|
|
64
99
|
|
|
65
100
|
test 'builder uses the custom map only for matched attributes' do
|
|
66
|
-
swap SimpleForm, :
|
|
101
|
+
swap SimpleForm, input_mappings: { /lock_version/ => :hidden } do
|
|
67
102
|
with_form_for @user, :post_count
|
|
68
103
|
assert_no_select 'form input#user_post_count.hidden'
|
|
69
104
|
assert_select 'form input#user_post_count.string'
|
|
70
105
|
end
|
|
71
106
|
end
|
|
72
107
|
|
|
108
|
+
test 'builder allow to use numbers in the model name' do
|
|
109
|
+
user = UserNumber1And2.build(tags: [Tag.new(nil, 'Tag1')])
|
|
110
|
+
|
|
111
|
+
with_concat_form_for(user, url: '/') do |f|
|
|
112
|
+
f.simple_fields_for(:tags) do |tags|
|
|
113
|
+
tags.input :name
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
assert_select 'form .user_number1_and2_tags_name'
|
|
118
|
+
assert_no_select 'form .user_number1_and2_tags_1_name'
|
|
119
|
+
end
|
|
120
|
+
|
|
73
121
|
# INPUT TYPES
|
|
74
|
-
test 'builder
|
|
122
|
+
test 'builder generates text fields for string columns' do
|
|
75
123
|
with_form_for @user, :name
|
|
76
124
|
assert_select 'form input#user_name.string'
|
|
77
125
|
end
|
|
78
126
|
|
|
79
|
-
test 'builder
|
|
127
|
+
test 'builder generates text areas for text columns' do
|
|
80
128
|
with_form_for @user, :description
|
|
129
|
+
assert_no_select 'form input#user_description.string'
|
|
130
|
+
assert_select 'form textarea#user_description.text'
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
test 'builder generates text areas for text columns when hinted' do
|
|
134
|
+
with_form_for @user, :description, as: :text
|
|
135
|
+
assert_no_select 'form input#user_description.string'
|
|
81
136
|
assert_select 'form textarea#user_description.text'
|
|
82
137
|
end
|
|
83
138
|
|
|
84
|
-
test 'builder
|
|
139
|
+
test 'builder generates text field for text columns when hinted' do
|
|
140
|
+
with_form_for @user, :description, as: :string
|
|
141
|
+
assert_no_select 'form textarea#user_description.text'
|
|
142
|
+
assert_select 'form input#user_description.string'
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
test 'builder generates a checkbox for boolean columns' do
|
|
85
146
|
with_form_for @user, :active
|
|
86
147
|
assert_select 'form input[type=checkbox]#user_active.boolean'
|
|
87
148
|
end
|
|
88
149
|
|
|
89
|
-
test 'builder
|
|
150
|
+
test 'builder uses integer text field for integer columns' do
|
|
90
151
|
with_form_for @user, :age
|
|
91
152
|
assert_select 'form input#user_age.numeric.integer'
|
|
92
153
|
end
|
|
93
154
|
|
|
94
|
-
test 'builder
|
|
155
|
+
test 'builder generates decimal text field for decimal columns' do
|
|
95
156
|
with_form_for @user, :credit_limit
|
|
96
157
|
assert_select 'form input#user_credit_limit.numeric.decimal'
|
|
97
158
|
end
|
|
98
159
|
|
|
99
|
-
test 'builder
|
|
160
|
+
test 'builder generates uuid fields for uuid columns' do
|
|
161
|
+
with_form_for @user, :uuid
|
|
162
|
+
if defined? ActiveModel::Type
|
|
163
|
+
assert_select 'form input#user_uuid.string.string'
|
|
164
|
+
else
|
|
165
|
+
assert_select 'form input#user_uuid.string.uuid'
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
test 'builder generates password fields for columns that matches password' do
|
|
100
170
|
with_form_for @user, :password
|
|
101
171
|
assert_select 'form input#user_password.password'
|
|
102
172
|
end
|
|
103
173
|
|
|
104
|
-
test 'builder
|
|
174
|
+
test 'builder generates country fields for columns that matches country' do
|
|
105
175
|
with_form_for @user, :residence_country
|
|
106
176
|
assert_select 'form select#user_residence_country.country'
|
|
107
177
|
end
|
|
108
178
|
|
|
109
|
-
test 'builder
|
|
179
|
+
test 'builder generates time_zone fields for columns that matches time_zone' do
|
|
110
180
|
with_form_for @user, :time_zone
|
|
111
181
|
assert_select 'form select#user_time_zone.time_zone'
|
|
112
182
|
end
|
|
113
183
|
|
|
114
|
-
test 'builder
|
|
184
|
+
test 'builder generates email fields for columns that matches email' do
|
|
115
185
|
with_form_for @user, :email
|
|
116
186
|
assert_select 'form input#user_email.string.email'
|
|
117
187
|
end
|
|
118
188
|
|
|
119
|
-
test 'builder
|
|
189
|
+
test 'builder generates tel fields for columns that matches phone' do
|
|
120
190
|
with_form_for @user, :phone_number
|
|
121
191
|
assert_select 'form input#user_phone_number.string.tel'
|
|
122
192
|
end
|
|
123
193
|
|
|
124
|
-
test 'builder
|
|
194
|
+
test 'builder generates url fields for columns that matches url' do
|
|
125
195
|
with_form_for @user, :url
|
|
126
196
|
assert_select 'form input#user_url.string.url'
|
|
127
197
|
end
|
|
128
198
|
|
|
129
|
-
test 'builder
|
|
199
|
+
test 'builder generates date select for date columns' do
|
|
130
200
|
with_form_for @user, :born_at
|
|
131
201
|
assert_select 'form select#user_born_at_1i.date'
|
|
132
202
|
end
|
|
133
203
|
|
|
134
|
-
test 'builder
|
|
204
|
+
test 'builder generates time select for time columns' do
|
|
135
205
|
with_form_for @user, :delivery_time
|
|
136
206
|
assert_select 'form select#user_delivery_time_4i.time'
|
|
137
207
|
end
|
|
138
208
|
|
|
139
|
-
test 'builder
|
|
209
|
+
test 'builder generates datetime select for datetime columns' do
|
|
140
210
|
with_form_for @user, :created_at
|
|
141
211
|
assert_select 'form select#user_created_at_1i.datetime'
|
|
142
212
|
end
|
|
143
213
|
|
|
144
|
-
test 'builder
|
|
214
|
+
test 'builder generates datetime select for timestamp columns' do
|
|
145
215
|
with_form_for @user, :updated_at
|
|
146
216
|
assert_select 'form select#user_updated_at_1i.datetime'
|
|
147
217
|
end
|
|
148
218
|
|
|
149
|
-
test 'builder
|
|
150
|
-
@user.avatar =
|
|
151
|
-
@user.avatar.
|
|
152
|
-
@user.avatar.expects(:respond_to?).with(:file?).returns(false)
|
|
153
|
-
@user.avatar.expects(:respond_to?).with(:public_filename).returns(true)
|
|
219
|
+
test 'builder generates file for file columns' do
|
|
220
|
+
@user.avatar = MiniTest::Mock.new
|
|
221
|
+
@user.avatar.expect(:public_filename, true)
|
|
154
222
|
|
|
155
223
|
with_form_for @user, :avatar
|
|
156
224
|
assert_select 'form input#user_avatar.file'
|
|
157
225
|
end
|
|
158
226
|
|
|
159
|
-
test 'builder
|
|
160
|
-
@user.home_picture =
|
|
161
|
-
@user.home_picture.
|
|
227
|
+
test 'builder generates file for attributes that are real db columns but have file methods' do
|
|
228
|
+
@user.home_picture = MiniTest::Mock.new
|
|
229
|
+
@user.home_picture.expect(:mounted_as, true)
|
|
162
230
|
|
|
163
231
|
with_form_for @user, :home_picture
|
|
164
232
|
assert_select 'form input#user_home_picture.file'
|
|
165
233
|
end
|
|
166
234
|
|
|
167
|
-
test 'build
|
|
168
|
-
with_form_for @user, :age, :
|
|
235
|
+
test 'build generates select if a collection is given' do
|
|
236
|
+
with_form_for @user, :age, collection: 1..60
|
|
169
237
|
assert_select 'form select#user_age.select'
|
|
170
238
|
end
|
|
171
239
|
|
|
172
|
-
test 'builder
|
|
173
|
-
with_form_for @user, :name, :
|
|
240
|
+
test 'builder allows overriding default input type for text' do
|
|
241
|
+
with_form_for @user, :name, as: :text
|
|
174
242
|
assert_no_select 'form input#user_name'
|
|
175
243
|
assert_select 'form textarea#user_name.text'
|
|
244
|
+
end
|
|
176
245
|
|
|
177
|
-
|
|
246
|
+
test 'builder allows overriding default input type for radio_buttons' do
|
|
247
|
+
with_form_for @user, :active, as: :radio_buttons
|
|
178
248
|
assert_no_select 'form input[type=checkbox]'
|
|
179
|
-
assert_select 'form input.radio_buttons[type=radio]', :
|
|
249
|
+
assert_select 'form input.radio_buttons[type=radio]', count: 2
|
|
250
|
+
end
|
|
180
251
|
|
|
181
|
-
|
|
252
|
+
test 'builder allows overriding default input type for string' do
|
|
253
|
+
with_form_for @user, :born_at, as: :string
|
|
182
254
|
assert_no_select 'form select'
|
|
183
255
|
assert_select 'form input#user_born_at.string'
|
|
184
256
|
end
|
|
185
257
|
|
|
186
258
|
# COMMON OPTIONS
|
|
187
|
-
test
|
|
188
|
-
|
|
259
|
+
# Remove this test when SimpleForm.form_class is removed in 4.x
|
|
260
|
+
test 'builder adds chosen form class' do
|
|
261
|
+
ActiveSupport::Deprecation.silence do
|
|
262
|
+
swap SimpleForm, form_class: :my_custom_class do
|
|
263
|
+
with_form_for @user, :name
|
|
264
|
+
assert_select 'form.my_custom_class'
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# Remove this test when SimpleForm.form_class is removed in 4.x
|
|
270
|
+
test 'builder adds chosen form class and default form class' do
|
|
271
|
+
ActiveSupport::Deprecation.silence do
|
|
272
|
+
swap SimpleForm, form_class: "my_custom_class", default_form_class: "my_default_class" do
|
|
273
|
+
with_form_for @user, :name
|
|
274
|
+
assert_select 'form.my_custom_class.my_default_class'
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
test 'builder adds default form class' do
|
|
280
|
+
swap SimpleForm, default_form_class: "default_class" do
|
|
189
281
|
with_form_for @user, :name
|
|
190
|
-
assert_select 'form.
|
|
282
|
+
assert_select 'form.default_class'
|
|
191
283
|
end
|
|
192
284
|
end
|
|
193
285
|
|
|
194
|
-
test 'builder
|
|
195
|
-
with_form_for @user, :name, :
|
|
286
|
+
test 'builder allows passing options to input' do
|
|
287
|
+
with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
|
|
196
288
|
assert_select 'form input#my_input.my_input.string'
|
|
197
289
|
end
|
|
198
290
|
|
|
199
|
-
test 'builder
|
|
200
|
-
with_form_for @user, :name, :
|
|
291
|
+
test 'builder does not propagate input options to wrapper' do
|
|
292
|
+
with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
|
|
201
293
|
assert_no_select 'form div.input.my_input.string'
|
|
202
294
|
assert_select 'form input#my_input.my_input.string'
|
|
203
295
|
end
|
|
204
296
|
|
|
205
|
-
test 'builder
|
|
297
|
+
test 'builder does not propagate input options to wrapper with custom wrapper' do
|
|
298
|
+
swap_wrapper :default, custom_wrapper_with_wrapped_input do
|
|
299
|
+
with_form_for @user, :name, input_html: { class: 'my_input' }
|
|
300
|
+
assert_no_select 'form div.input.my_input'
|
|
301
|
+
assert_select 'form input.my_input.string'
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
test 'builder does not propagate label options to wrapper with custom wrapper' do
|
|
306
|
+
swap_wrapper :default, custom_wrapper_with_wrapped_label do
|
|
307
|
+
with_form_for @user, :name, label_html: { class: 'my_label' }
|
|
308
|
+
assert_no_select 'form div.label.my_label'
|
|
309
|
+
assert_select 'form label.my_label.string'
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
test 'builder generates an input with label' do
|
|
206
314
|
with_form_for @user, :name
|
|
207
315
|
assert_select 'form label.string[for=user_name]', /Name/
|
|
208
316
|
end
|
|
209
317
|
|
|
210
|
-
test 'builder
|
|
211
|
-
with_form_for @user, :name, :
|
|
318
|
+
test 'builder is able to disable the label for an input' do
|
|
319
|
+
with_form_for @user, :name, label: false
|
|
212
320
|
assert_no_select 'form label'
|
|
213
321
|
end
|
|
214
322
|
|
|
215
|
-
test 'builder
|
|
216
|
-
with_form_for @user, :name, :
|
|
323
|
+
test 'builder is able to disable the label for an input and return a html safe string' do
|
|
324
|
+
with_form_for @user, :name, label: false, wrapper: custom_wrapper_with_wrapped_label_input
|
|
325
|
+
assert_select 'form input#user_name'
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
test 'builder uses custom label' do
|
|
329
|
+
with_form_for @user, :name, label: 'Yay!'
|
|
217
330
|
assert_select 'form label', /Yay!/
|
|
218
331
|
end
|
|
219
332
|
|
|
220
|
-
test 'builder
|
|
221
|
-
with_form_for @user, :name, :
|
|
333
|
+
test 'builder passes options to label' do
|
|
334
|
+
with_form_for @user, :name, label_html: { id: "cool" }
|
|
222
335
|
assert_select 'form label#cool', /Name/
|
|
223
336
|
end
|
|
224
337
|
|
|
225
|
-
test 'builder
|
|
338
|
+
test 'builder does not generate hints for an input' do
|
|
226
339
|
with_form_for @user, :name
|
|
227
340
|
assert_no_select 'span.hint'
|
|
228
341
|
end
|
|
229
342
|
|
|
230
|
-
test 'builder
|
|
231
|
-
with_form_for @user, :name, :
|
|
343
|
+
test 'builder is able to add a hint for an input' do
|
|
344
|
+
with_form_for @user, :name, hint: 'test'
|
|
232
345
|
assert_select 'span.hint', 'test'
|
|
233
346
|
end
|
|
234
347
|
|
|
235
|
-
test 'builder
|
|
236
|
-
store_translations(:en, :
|
|
237
|
-
|
|
238
|
-
|
|
348
|
+
test 'builder is able to disable a hint even if it exists in i18n' do
|
|
349
|
+
store_translations(:en, simple_form: { hints: { name: 'Hint test' } }) do
|
|
350
|
+
stub_any_instance(SimpleForm::Inputs::Base, :hint, -> { raise 'Never' }) do
|
|
351
|
+
with_form_for @user, :name, hint: false
|
|
352
|
+
assert_no_select 'span.hint'
|
|
353
|
+
end
|
|
239
354
|
end
|
|
240
355
|
end
|
|
241
356
|
|
|
242
|
-
test 'builder
|
|
243
|
-
with_form_for @user, :name, :
|
|
357
|
+
test 'builder passes options to hint' do
|
|
358
|
+
with_form_for @user, :name, hint: 'test', hint_html: { id: "cool" }
|
|
244
359
|
assert_select 'span.hint#cool', 'test'
|
|
245
360
|
end
|
|
246
361
|
|
|
247
|
-
test 'builder
|
|
362
|
+
test 'builder generates errors for attribute without errors' do
|
|
248
363
|
with_form_for @user, :credit_limit
|
|
249
364
|
assert_no_select 'span.errors'
|
|
250
365
|
end
|
|
251
366
|
|
|
252
|
-
test 'builder
|
|
367
|
+
test 'builder generates errors for attribute with errors' do
|
|
253
368
|
with_form_for @user, :name
|
|
254
|
-
assert_select 'span.error', "
|
|
369
|
+
assert_select 'span.error', "cannot be blank"
|
|
255
370
|
end
|
|
256
371
|
|
|
257
|
-
test 'builder
|
|
258
|
-
with_form_for @user, :name, :
|
|
372
|
+
test 'builder is able to disable showing errors for an input' do
|
|
373
|
+
with_form_for @user, :name, error: false
|
|
259
374
|
assert_no_select 'span.error'
|
|
260
375
|
end
|
|
261
376
|
|
|
262
|
-
test 'builder
|
|
263
|
-
with_form_for @user, :name, :
|
|
264
|
-
assert_select 'span.error#cool', "
|
|
377
|
+
test 'builder passes options to errors' do
|
|
378
|
+
with_form_for @user, :name, error_html: { id: "cool" }
|
|
379
|
+
assert_select 'span.error#cool', "cannot be blank"
|
|
265
380
|
end
|
|
266
381
|
|
|
267
|
-
test 'placeholder
|
|
268
|
-
store_translations(:en, :
|
|
269
|
-
:
|
|
382
|
+
test 'placeholder does not be generated when set to false' do
|
|
383
|
+
store_translations(:en, simple_form: { placeholders: { user: {
|
|
384
|
+
name: 'Name goes here'
|
|
270
385
|
} } }) do
|
|
271
|
-
with_form_for @user, :name, :
|
|
386
|
+
with_form_for @user, :name, placeholder: false
|
|
272
387
|
assert_no_select 'input[placeholder]'
|
|
273
388
|
end
|
|
274
389
|
end
|
|
275
390
|
|
|
276
391
|
# DEFAULT OPTIONS
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
392
|
+
%i[input input_field].each do |method|
|
|
393
|
+
test "builder receives a default argument and pass it to the inputs when calling '#{method}'" do
|
|
394
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
395
|
+
f.public_send(method, :name)
|
|
396
|
+
end
|
|
397
|
+
assert_select 'input.default_class'
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
test "builder receives a default argument and pass it to the inputs without changing the defaults when calling '#{method}'" do
|
|
401
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
|
|
402
|
+
concat(f.public_send(method, :name))
|
|
403
|
+
concat(f.public_send(method, :credit_limit))
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
assert_select "input.string.default_class[name='user[name]']"
|
|
407
|
+
assert_no_select "input.string[name='user[credit_limit]']"
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
test "builder receives a default argument and pass it to the inputs and nested form when calling '#{method}'" do
|
|
411
|
+
@user.company = Company.new(1, 'Empresa')
|
|
412
|
+
|
|
413
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
414
|
+
concat(f.public_send(method, :name))
|
|
415
|
+
concat(f.simple_fields_for(:company) do |company_form|
|
|
416
|
+
concat(company_form.public_send(method, :name))
|
|
417
|
+
end)
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
assert_select "input.string.default_class[name='user[name]']"
|
|
421
|
+
assert_select "input.string.default_class[name='user[company_attributes][name]']"
|
|
280
422
|
end
|
|
281
|
-
assert_select 'input.default_class'
|
|
282
423
|
end
|
|
283
424
|
|
|
284
|
-
test
|
|
285
|
-
with_concat_form_for @user, :
|
|
286
|
-
f.input :name, :
|
|
425
|
+
test "builder receives a default argument and pass it to the inputs when calling 'input', respecting the specific options" do
|
|
426
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
427
|
+
f.input :name, input_html: { id: 'specific_id' }
|
|
287
428
|
end
|
|
288
429
|
assert_select 'input.default_class#specific_id'
|
|
289
430
|
end
|
|
290
431
|
|
|
291
|
-
test
|
|
292
|
-
with_concat_form_for @user, :
|
|
293
|
-
f.
|
|
432
|
+
test "builder receives a default argument and pass it to the inputs when calling 'input_field', respecting the specific options" do
|
|
433
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
434
|
+
f.input_field :name, id: 'specific_id'
|
|
294
435
|
end
|
|
295
436
|
assert_select 'input.default_class#specific_id'
|
|
296
437
|
end
|
|
297
438
|
|
|
298
|
-
test
|
|
299
|
-
with_concat_form_for @user, :
|
|
300
|
-
|
|
301
|
-
concat(f.input :credit_limit)
|
|
439
|
+
test "builder receives a default argument and pass it to the inputs when calling 'input', overwriting the defaults with specific options" do
|
|
440
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
|
|
441
|
+
f.input :name, input_html: { id: 'specific_id' }
|
|
302
442
|
end
|
|
443
|
+
assert_select 'input.default_class#specific_id'
|
|
444
|
+
end
|
|
303
445
|
|
|
304
|
-
|
|
305
|
-
|
|
446
|
+
test "builder receives a default argument and pass it to the inputs when calling 'input_field', overwriting the defaults with specific options" do
|
|
447
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
|
|
448
|
+
f.input_field :name, id: 'specific_id'
|
|
449
|
+
end
|
|
450
|
+
assert_select 'input.default_class#specific_id'
|
|
306
451
|
end
|
|
307
452
|
|
|
308
453
|
# WITHOUT OBJECT
|
|
309
|
-
test 'builder
|
|
454
|
+
test 'builder generates properly when object is not present' do
|
|
310
455
|
with_form_for :project, :name
|
|
311
456
|
assert_select 'form input.string#project_name'
|
|
312
457
|
end
|
|
313
458
|
|
|
314
|
-
test 'builder
|
|
459
|
+
test 'builder generates password fields based on attribute name when object is not present' do
|
|
315
460
|
with_form_for :project, :password_confirmation
|
|
316
461
|
assert_select 'form input[type=password].password#project_password_confirmation'
|
|
317
462
|
end
|
|
318
463
|
|
|
319
|
-
test 'builder
|
|
464
|
+
test 'builder generates text fields by default for all attributes when object is not present' do
|
|
320
465
|
with_form_for :project, :created_at
|
|
321
466
|
assert_select 'form input.string#project_created_at'
|
|
322
467
|
with_form_for :project, :budget
|
|
323
468
|
assert_select 'form input.string#project_budget'
|
|
324
469
|
end
|
|
325
470
|
|
|
326
|
-
test 'builder
|
|
327
|
-
with_form_for :project, :created_at, :
|
|
471
|
+
test 'builder allows overriding input type when object is not present' do
|
|
472
|
+
with_form_for :project, :created_at, as: :datetime
|
|
328
473
|
assert_select 'form select.datetime#project_created_at_1i'
|
|
329
|
-
with_form_for :project, :budget, :
|
|
474
|
+
with_form_for :project, :budget, as: :decimal
|
|
330
475
|
assert_select 'form input.decimal#project_budget'
|
|
331
476
|
end
|
|
332
477
|
|
|
333
478
|
# CUSTOM FORM BUILDER
|
|
334
|
-
test 'custom builder
|
|
479
|
+
test 'custom builder inherits mappings' do
|
|
335
480
|
with_custom_form_for @user, :email
|
|
336
481
|
assert_select 'form input[type=email]#user_email.custom'
|
|
337
482
|
end
|
|
338
483
|
|
|
339
|
-
test 'form with CustomMapTypeFormBuilder
|
|
484
|
+
test 'form with CustomMapTypeFormBuilder uses custom map type builder' do
|
|
340
485
|
with_concat_custom_mapping_form_for(:user) do |user|
|
|
341
486
|
assert user.instance_of?(CustomMapTypeFormBuilder)
|
|
342
487
|
end
|
|
343
488
|
end
|
|
344
489
|
|
|
345
|
-
test 'form with CustomMapTypeFormBuilder
|
|
490
|
+
test 'form with CustomMapTypeFormBuilder uses custom mapping' do
|
|
346
491
|
with_concat_custom_mapping_form_for(:user) do |user|
|
|
347
492
|
assert_equal SimpleForm::Inputs::StringInput, user.class.mappings[:custom_type]
|
|
348
493
|
end
|
|
349
494
|
end
|
|
350
495
|
|
|
351
|
-
test 'form without CustomMapTypeFormBuilder
|
|
496
|
+
test 'form without CustomMapTypeFormBuilder does not use custom mapping' do
|
|
352
497
|
with_concat_form_for(:user) do |user|
|
|
353
498
|
assert_nil user.class.mappings[:custom_type]
|
|
354
499
|
end
|