simple_form 2.1.3 → 3.0.0.rc
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -50
- data/README.md +140 -116
- data/lib/generators/simple_form/install_generator.rb +4 -4
- data/lib/generators/simple_form/templates/README +2 -2
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +13 -13
- data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +16 -16
- data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +3 -3
- data/lib/simple_form/action_view_extensions/builder.rb +1 -320
- data/lib/simple_form/action_view_extensions/form_helper.rb +2 -9
- data/lib/simple_form/components/errors.rb +1 -7
- data/lib/simple_form/components/hints.rb +2 -7
- data/lib/simple_form/components/html5.rb +5 -2
- data/lib/simple_form/components/labels.rb +4 -4
- data/lib/simple_form/components/maxlength.rb +1 -8
- data/lib/simple_form/components/pattern.rb +2 -2
- data/lib/simple_form/components.rb +1 -1
- data/lib/simple_form/error_notification.rb +2 -2
- data/lib/simple_form/form_builder.rb +148 -50
- data/lib/simple_form/helpers.rb +1 -1
- data/lib/simple_form/inputs/base.rb +3 -10
- data/lib/simple_form/inputs/block_input.rb +1 -1
- data/lib/simple_form/inputs/boolean_input.rb +6 -6
- data/lib/simple_form/inputs/collection_input.rb +7 -7
- data/lib/simple_form/inputs/numeric_input.rb +0 -6
- data/lib/simple_form/inputs/password_input.rb +0 -1
- data/lib/simple_form/inputs/string_input.rb +0 -1
- data/lib/simple_form/railtie.rb +7 -0
- data/lib/simple_form/tags.rb +61 -0
- data/lib/simple_form/version.rb +1 -1
- data/lib/simple_form/wrappers/builder.rb +5 -29
- data/lib/simple_form/wrappers/many.rb +1 -1
- data/lib/simple_form/wrappers/root.rb +1 -1
- data/lib/simple_form/wrappers.rb +1 -1
- data/lib/simple_form.rb +39 -47
- data/test/action_view_extensions/builder_test.rb +75 -95
- data/test/action_view_extensions/form_helper_test.rb +25 -16
- data/test/components/label_test.rb +46 -46
- data/test/form_builder/association_test.rb +39 -29
- data/test/form_builder/button_test.rb +4 -4
- data/test/form_builder/error_notification_test.rb +8 -8
- data/test/form_builder/error_test.rb +18 -65
- data/test/form_builder/general_test.rb +60 -74
- data/test/form_builder/hint_test.rb +23 -29
- data/test/form_builder/input_field_test.rb +12 -12
- data/test/form_builder/label_test.rb +6 -16
- data/test/form_builder/wrapper_test.rb +21 -21
- data/test/inputs/boolean_input_test.rb +23 -35
- data/test/inputs/collection_check_boxes_input_test.rb +61 -55
- data/test/inputs/collection_radio_buttons_input_test.rb +76 -79
- data/test/inputs/collection_select_input_test.rb +70 -51
- data/test/inputs/datetime_input_test.rb +17 -11
- data/test/inputs/disabled_test.rb +10 -10
- data/test/inputs/discovery_test.rb +4 -4
- data/test/inputs/file_input_test.rb +1 -1
- data/test/inputs/general_test.rb +12 -12
- data/test/inputs/grouped_collection_select_input_test.rb +20 -20
- data/test/inputs/hidden_input_test.rb +3 -2
- data/test/inputs/numeric_input_test.rb +3 -3
- data/test/inputs/priority_input_test.rb +9 -3
- data/test/inputs/readonly_test.rb +12 -12
- data/test/inputs/required_test.rb +5 -5
- data/test/inputs/string_input_test.rb +15 -25
- data/test/inputs/text_input_test.rb +1 -1
- data/test/support/misc_helpers.rb +46 -24
- data/test/support/mock_controller.rb +6 -6
- data/test/support/models.rb +62 -64
- data/test/test_helper.rb +20 -24
- metadata +37 -23
- data/lib/simple_form/core_ext/hash.rb +0 -16
|
@@ -14,10 +14,6 @@ class ErrorTest < ActionView::TestCase
|
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def with_custom_error_for(object, *args)
|
|
18
|
-
with_form_for(object, *args)
|
|
19
|
-
end
|
|
20
|
-
|
|
21
17
|
test 'error should not generate content for attribute without errors' do
|
|
22
18
|
with_error_for @user, :active
|
|
23
19
|
assert_no_select 'span.error'
|
|
@@ -36,38 +32,38 @@ class ErrorTest < ActionView::TestCase
|
|
|
36
32
|
|
|
37
33
|
test 'error should generate messages for attribute with single error' do
|
|
38
34
|
with_error_for @user, :name
|
|
39
|
-
assert_select 'span.error', "can
|
|
35
|
+
assert_select 'span.error', "can't be blank"
|
|
40
36
|
end
|
|
41
37
|
|
|
42
38
|
test 'error should generate messages for attribute with one error when using first' do
|
|
43
|
-
swap SimpleForm, :
|
|
39
|
+
swap SimpleForm, error_method: :first do
|
|
44
40
|
with_error_for @user, :age
|
|
45
41
|
assert_select 'span.error', 'is not a number'
|
|
46
42
|
end
|
|
47
43
|
end
|
|
48
44
|
|
|
49
45
|
test 'error should generate messages for attribute with several errors when using to_sentence' do
|
|
50
|
-
swap SimpleForm, :
|
|
46
|
+
swap SimpleForm, error_method: :to_sentence do
|
|
51
47
|
with_error_for @user, :age
|
|
52
48
|
assert_select 'span.error', 'is not a number and must be greater than 18'
|
|
53
49
|
end
|
|
54
50
|
end
|
|
55
51
|
|
|
56
52
|
test 'error should be able to pass html options' do
|
|
57
|
-
with_error_for @user, :name, :
|
|
53
|
+
with_error_for @user, :name, id: 'error', class: 'yay'
|
|
58
54
|
assert_select 'span#error.error.yay'
|
|
59
55
|
end
|
|
60
56
|
|
|
61
57
|
test 'error should not modify the options hash' do
|
|
62
|
-
options = { :
|
|
58
|
+
options = { id: 'error', class: 'yay' }
|
|
63
59
|
with_error_for @user, :name, options
|
|
64
60
|
assert_select 'span#error.error.yay'
|
|
65
|
-
assert_equal({ :
|
|
61
|
+
assert_equal({ id: 'error', class: 'yay' }, options)
|
|
66
62
|
end
|
|
67
63
|
|
|
68
64
|
test 'error should find errors on attribute and association' do
|
|
69
|
-
with_error_for @user, :company_id, :
|
|
70
|
-
:
|
|
65
|
+
with_error_for @user, :company_id, as: :select,
|
|
66
|
+
error_method: :to_sentence, reflection: Association.new(Company, :company, {})
|
|
71
67
|
assert_select 'span.error', 'must be valid and company must be present'
|
|
72
68
|
end
|
|
73
69
|
|
|
@@ -77,30 +73,16 @@ class ErrorTest < ActionView::TestCase
|
|
|
77
73
|
end
|
|
78
74
|
|
|
79
75
|
test 'error should generate an error tag with a clean HTML when errors options are present' do
|
|
80
|
-
with_error_for @user, :name, :
|
|
76
|
+
with_error_for @user, :name, error_tag: :p, error_prefix: 'Name', error_method: :first
|
|
81
77
|
assert_no_select 'p.error[error_html]'
|
|
82
78
|
assert_no_select 'p.error[error_tag]'
|
|
83
79
|
assert_no_select 'p.error[error_prefix]'
|
|
84
80
|
assert_no_select 'p.error[error_method]'
|
|
85
81
|
end
|
|
86
82
|
|
|
87
|
-
test 'error should escape error prefix text' do
|
|
88
|
-
with_error_for @user, :name, :error_prefix => '<b>Name</b>'
|
|
89
|
-
assert_select 'span.error', "<b>Name</b> can't be blank"
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
test 'error escapes error text' do
|
|
93
|
-
@user.errors.merge!(:action => ['must not contain <b>markup</b>'])
|
|
94
|
-
|
|
95
|
-
with_error_for @user, :action
|
|
96
|
-
|
|
97
|
-
assert_select 'span.error'
|
|
98
|
-
assert_no_select 'span.error b', 'markup'
|
|
99
|
-
end
|
|
100
|
-
|
|
101
83
|
test 'error should generate an error message with raw HTML tags' do
|
|
102
|
-
with_error_for @user, :name, :
|
|
103
|
-
assert_select 'span.error', "Name can
|
|
84
|
+
with_error_for @user, :name, error_prefix: '<b>Name</b>'
|
|
85
|
+
assert_select 'span.error', "Name can't be blank"
|
|
104
86
|
assert_select 'span.error b', "Name"
|
|
105
87
|
end
|
|
106
88
|
|
|
@@ -108,7 +90,7 @@ class ErrorTest < ActionView::TestCase
|
|
|
108
90
|
|
|
109
91
|
test 'full error should generate an full error tag for the attribute' do
|
|
110
92
|
with_full_error_for @user, :name
|
|
111
|
-
assert_select 'span.error', "Super User Name! can
|
|
93
|
+
assert_select 'span.error', "Super User Name! can't be blank"
|
|
112
94
|
end
|
|
113
95
|
|
|
114
96
|
test 'full error should generate an full error tag with a clean HTML' do
|
|
@@ -117,15 +99,15 @@ class ErrorTest < ActionView::TestCase
|
|
|
117
99
|
end
|
|
118
100
|
|
|
119
101
|
test 'full error should allow passing options to full error tag' do
|
|
120
|
-
with_full_error_for @user, :name, :
|
|
121
|
-
assert_select 'span.error#name_error', "Your name can
|
|
102
|
+
with_full_error_for @user, :name, id: 'name_error', error_prefix: "Your name"
|
|
103
|
+
assert_select 'span.error#name_error', "Your name can't be blank"
|
|
122
104
|
end
|
|
123
105
|
|
|
124
106
|
test 'full error should not modify the options hash' do
|
|
125
|
-
options = { :
|
|
107
|
+
options = { id: 'name_error' }
|
|
126
108
|
with_full_error_for @user, :name, options
|
|
127
|
-
assert_select 'span.error#name_error', "Super User Name! can
|
|
128
|
-
assert_equal({ :
|
|
109
|
+
assert_select 'span.error#name_error', "Super User Name! can't be blank"
|
|
110
|
+
assert_equal({ id: 'name_error' }, options)
|
|
129
111
|
end
|
|
130
112
|
|
|
131
113
|
# CUSTOM WRAPPERS
|
|
@@ -133,36 +115,7 @@ class ErrorTest < ActionView::TestCase
|
|
|
133
115
|
test 'error with custom wrappers works' do
|
|
134
116
|
swap_wrapper do
|
|
135
117
|
with_error_for @user, :name
|
|
136
|
-
assert_select 'span.omg_error', "can
|
|
118
|
+
assert_select 'span.omg_error', "can't be blank"
|
|
137
119
|
end
|
|
138
120
|
end
|
|
139
|
-
|
|
140
|
-
# CUSTOM ERRORS
|
|
141
|
-
|
|
142
|
-
test 'input with custom error works' do
|
|
143
|
-
with_custom_error_for(@user, :name, :error => "Super User Name! can't be blank")
|
|
144
|
-
|
|
145
|
-
assert_select 'span.error', "Super User Name! can't be blank"
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
test 'input with custom error does not generate the error if there is no error on the attribute' do
|
|
149
|
-
error_text = "Super User Active! can't be blank"
|
|
150
|
-
with_form_for @user, :active, :error => error_text
|
|
151
|
-
|
|
152
|
-
assert_no_select 'span.error'
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
test 'input with custom error escapes the error text' do
|
|
156
|
-
with_form_for @user, :name, :error => 'error must not contain <b>markup</b>'
|
|
157
|
-
|
|
158
|
-
assert_select 'span.error'
|
|
159
|
-
assert_no_select 'span.error b', 'markup'
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
test 'input with custom error does not escape the error text if it is safe' do
|
|
163
|
-
with_form_for @user, :name, :error => 'error must contain <b>markup</b>'.html_safe
|
|
164
|
-
|
|
165
|
-
assert_select 'span.error'
|
|
166
|
-
assert_select 'span.error b', 'markup'
|
|
167
|
-
end
|
|
168
121
|
end
|
|
@@ -22,16 +22,24 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
+
test 'builder should work without controller' do
|
|
26
|
+
stub_any_instance ActionView::TestCase, :controller, nil do
|
|
27
|
+
simple_form_for @user do |f|
|
|
28
|
+
assert f.input(:name)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
25
33
|
test 'builder input should allow a block to configure input' do
|
|
26
34
|
with_form_for @user, :name do
|
|
27
|
-
text_field_tag :foo, :bar, :
|
|
35
|
+
text_field_tag :foo, :bar, id: :cool
|
|
28
36
|
end
|
|
29
37
|
assert_no_select 'input.string'
|
|
30
38
|
assert_select 'input#cool'
|
|
31
39
|
end
|
|
32
40
|
|
|
33
41
|
test 'builder should allow adding custom input mappings for default input types' do
|
|
34
|
-
swap SimpleForm, :
|
|
42
|
+
swap SimpleForm, input_mappings: { /count$/ => :integer } do
|
|
35
43
|
with_form_for @user, :post_count
|
|
36
44
|
assert_no_select 'form input#user_post_count.string'
|
|
37
45
|
assert_select 'form input#user_post_count.numeric.integer'
|
|
@@ -39,7 +47,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
39
47
|
end
|
|
40
48
|
|
|
41
49
|
test 'builder should allow to skip input_type class' do
|
|
42
|
-
swap SimpleForm, :
|
|
50
|
+
swap SimpleForm, generate_additional_classes_for: [:label, :wrapper] do
|
|
43
51
|
with_form_for @user, :post_count
|
|
44
52
|
assert_no_select "form input#user_post_count.integer"
|
|
45
53
|
assert_select "form input#user_post_count"
|
|
@@ -47,7 +55,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
47
55
|
end
|
|
48
56
|
|
|
49
57
|
test 'builder should allow to add additional classes only for wrapper' do
|
|
50
|
-
swap SimpleForm, :
|
|
58
|
+
swap SimpleForm, generate_additional_classes_for: [:wrapper] do
|
|
51
59
|
with_form_for @user, :post_count
|
|
52
60
|
assert_no_select "form input#user_post_count.string"
|
|
53
61
|
assert_no_select "form label#user_post_count.string"
|
|
@@ -56,7 +64,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
56
64
|
end
|
|
57
65
|
|
|
58
66
|
test 'builder should allow adding custom input mappings for integer input types' do
|
|
59
|
-
swap SimpleForm, :
|
|
67
|
+
swap SimpleForm, input_mappings: { /lock_version/ => :hidden } do
|
|
60
68
|
with_form_for @user, :lock_version
|
|
61
69
|
assert_no_select 'form input#user_lock_version.integer'
|
|
62
70
|
assert_select 'form input#user_lock_version.hidden'
|
|
@@ -64,7 +72,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
64
72
|
end
|
|
65
73
|
|
|
66
74
|
test 'builder uses the first matching custom input map when more than one matches' do
|
|
67
|
-
swap SimpleForm, :
|
|
75
|
+
swap SimpleForm, input_mappings: { /count$/ => :integer, /^post_/ => :password } do
|
|
68
76
|
with_form_for @user, :post_count
|
|
69
77
|
assert_no_select 'form input#user_post_count.password'
|
|
70
78
|
assert_select 'form input#user_post_count.numeric.integer'
|
|
@@ -72,33 +80,13 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
72
80
|
end
|
|
73
81
|
|
|
74
82
|
test 'builder uses the custom map only for matched attributes' do
|
|
75
|
-
swap SimpleForm, :
|
|
83
|
+
swap SimpleForm, input_mappings: { /lock_version/ => :hidden } do
|
|
76
84
|
with_form_for @user, :post_count
|
|
77
85
|
assert_no_select 'form input#user_post_count.hidden'
|
|
78
86
|
assert_select 'form input#user_post_count.string'
|
|
79
87
|
end
|
|
80
88
|
end
|
|
81
89
|
|
|
82
|
-
test 'builder allow to use numbers in the model name' do
|
|
83
|
-
user = UserNumber1And2.new({
|
|
84
|
-
:id => 1,
|
|
85
|
-
:name => 'New in SimpleForm!',
|
|
86
|
-
:description => 'Hello!',
|
|
87
|
-
:created_at => Time.now
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
user.tags = [Tag.new(nil, 'Tag1')]
|
|
91
|
-
|
|
92
|
-
with_concat_form_for(user, :url => '/') do |f|
|
|
93
|
-
f.simple_fields_for(:tags) do |tags|
|
|
94
|
-
tags.input :name
|
|
95
|
-
end
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
assert_select 'form .user_number1_and2_tags_name'
|
|
99
|
-
assert_no_select 'form .user_number1_and2_tags_1_name'
|
|
100
|
-
end
|
|
101
|
-
|
|
102
90
|
# INPUT TYPES
|
|
103
91
|
test 'builder should generate text fields for string columns' do
|
|
104
92
|
with_form_for @user, :name
|
|
@@ -176,64 +164,62 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
176
164
|
end
|
|
177
165
|
|
|
178
166
|
test 'builder should generate file for file columns' do
|
|
179
|
-
@user.avatar =
|
|
180
|
-
@user.avatar.
|
|
181
|
-
@user.avatar.expects(:respond_to?).with(:file?).returns(false)
|
|
182
|
-
@user.avatar.expects(:respond_to?).with(:public_filename).returns(true)
|
|
167
|
+
@user.avatar = MiniTest::Mock.new
|
|
168
|
+
@user.avatar.expect(:public_filename, true)
|
|
183
169
|
|
|
184
170
|
with_form_for @user, :avatar
|
|
185
171
|
assert_select 'form input#user_avatar.file'
|
|
186
172
|
end
|
|
187
173
|
|
|
188
174
|
test 'builder should generate file for attributes that are real db columns but have file methods' do
|
|
189
|
-
@user.home_picture =
|
|
190
|
-
@user.home_picture.
|
|
175
|
+
@user.home_picture = MiniTest::Mock.new
|
|
176
|
+
@user.home_picture.expect(:mounted_as, true)
|
|
191
177
|
|
|
192
178
|
with_form_for @user, :home_picture
|
|
193
179
|
assert_select 'form input#user_home_picture.file'
|
|
194
180
|
end
|
|
195
181
|
|
|
196
182
|
test 'build should generate select if a collection is given' do
|
|
197
|
-
with_form_for @user, :age, :
|
|
183
|
+
with_form_for @user, :age, collection: 1..60
|
|
198
184
|
assert_select 'form select#user_age.select'
|
|
199
185
|
end
|
|
200
186
|
|
|
201
187
|
test 'builder should allow overriding default input type for text' do
|
|
202
|
-
with_form_for @user, :name, :
|
|
188
|
+
with_form_for @user, :name, as: :text
|
|
203
189
|
assert_no_select 'form input#user_name'
|
|
204
190
|
assert_select 'form textarea#user_name.text'
|
|
205
191
|
|
|
206
|
-
with_form_for @user, :active, :
|
|
192
|
+
with_form_for @user, :active, as: :radio_buttons
|
|
207
193
|
assert_no_select 'form input[type=checkbox]'
|
|
208
|
-
assert_select 'form input.radio_buttons[type=radio]', :
|
|
194
|
+
assert_select 'form input.radio_buttons[type=radio]', count: 2
|
|
209
195
|
|
|
210
|
-
with_form_for @user, :born_at, :
|
|
196
|
+
with_form_for @user, :born_at, as: :string
|
|
211
197
|
assert_no_select 'form select'
|
|
212
198
|
assert_select 'form input#user_born_at.string'
|
|
213
199
|
end
|
|
214
200
|
|
|
215
201
|
# COMMON OPTIONS
|
|
216
202
|
test 'builder should add chosen form class' do
|
|
217
|
-
swap SimpleForm, :
|
|
203
|
+
swap SimpleForm, form_class: :my_custom_class do
|
|
218
204
|
with_form_for @user, :name
|
|
219
205
|
assert_select 'form.my_custom_class'
|
|
220
206
|
end
|
|
221
207
|
end
|
|
222
208
|
|
|
223
209
|
test 'builder should allow passing options to input' do
|
|
224
|
-
with_form_for @user, :name, :
|
|
210
|
+
with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
|
|
225
211
|
assert_select 'form input#my_input.my_input.string'
|
|
226
212
|
end
|
|
227
213
|
|
|
228
214
|
test 'builder should not propagate input options to wrapper' do
|
|
229
|
-
with_form_for @user, :name, :
|
|
215
|
+
with_form_for @user, :name, input_html: { class: 'my_input', id: 'my_input' }
|
|
230
216
|
assert_no_select 'form div.input.my_input.string'
|
|
231
217
|
assert_select 'form input#my_input.my_input.string'
|
|
232
218
|
end
|
|
233
219
|
|
|
234
220
|
test 'builder should not propagate input options to wrapper with custom wrapper' do
|
|
235
221
|
swap_wrapper :default, self.custom_wrapper_with_wrapped_input do
|
|
236
|
-
with_form_for @user, :name, :
|
|
222
|
+
with_form_for @user, :name, input_html: { class: 'my_input' }
|
|
237
223
|
assert_no_select 'form div.input.my_input'
|
|
238
224
|
assert_select 'form input.my_input.string'
|
|
239
225
|
end
|
|
@@ -241,7 +227,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
241
227
|
|
|
242
228
|
test 'builder should not propagate label options to wrapper with custom wrapper' do
|
|
243
229
|
swap_wrapper :default, self.custom_wrapper_with_wrapped_label do
|
|
244
|
-
with_form_for @user, :name, :
|
|
230
|
+
with_form_for @user, :name, label_html: { class: 'my_label' }
|
|
245
231
|
assert_no_select 'form div.label.my_label'
|
|
246
232
|
assert_select 'form label.my_label.string'
|
|
247
233
|
end
|
|
@@ -253,22 +239,22 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
253
239
|
end
|
|
254
240
|
|
|
255
241
|
test 'builder should be able to disable the label for a input' do
|
|
256
|
-
with_form_for @user, :name, :
|
|
242
|
+
with_form_for @user, :name, label: false
|
|
257
243
|
assert_no_select 'form label'
|
|
258
244
|
end
|
|
259
245
|
|
|
260
246
|
test 'builder should be able to disable the label for an input and return a html safe string' do
|
|
261
|
-
with_form_for @user, :name, :
|
|
247
|
+
with_form_for @user, :name, label: false, wrapper: custom_wrapper_with_wrapped_label_input
|
|
262
248
|
assert_select 'form input#user_name'
|
|
263
249
|
end
|
|
264
250
|
|
|
265
251
|
test 'builder should use custom label' do
|
|
266
|
-
with_form_for @user, :name, :
|
|
252
|
+
with_form_for @user, :name, label: 'Yay!'
|
|
267
253
|
assert_select 'form label', /Yay!/
|
|
268
254
|
end
|
|
269
255
|
|
|
270
256
|
test 'builder should pass options to label' do
|
|
271
|
-
with_form_for @user, :name, :
|
|
257
|
+
with_form_for @user, :name, label_html: { id: "cool" }
|
|
272
258
|
assert_select 'form label#cool', /Name/
|
|
273
259
|
end
|
|
274
260
|
|
|
@@ -278,21 +264,21 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
278
264
|
end
|
|
279
265
|
|
|
280
266
|
test 'builder should be able to add a hint for a input' do
|
|
281
|
-
with_form_for @user, :name, :
|
|
267
|
+
with_form_for @user, :name, hint: 'test'
|
|
282
268
|
assert_select 'span.hint', 'test'
|
|
283
269
|
end
|
|
284
270
|
|
|
285
271
|
test 'builder should be able to disable a hint even if it exists in i18n' do
|
|
286
|
-
store_translations(:en, :
|
|
287
|
-
SimpleForm::Inputs::Base
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
272
|
+
store_translations(:en, simple_form: { hints: { name: 'Hint test' } }) do
|
|
273
|
+
stub_any_instance(SimpleForm::Inputs::Base, :hint, -> { raise 'Never' }) do
|
|
274
|
+
with_form_for @user, :name, hint: false
|
|
275
|
+
assert_no_select 'span.hint'
|
|
276
|
+
end
|
|
291
277
|
end
|
|
292
278
|
end
|
|
293
279
|
|
|
294
280
|
test 'builder should pass options to hint' do
|
|
295
|
-
with_form_for @user, :name, :
|
|
281
|
+
with_form_for @user, :name, hint: 'test', hint_html: { id: "cool" }
|
|
296
282
|
assert_select 'span.hint#cool', 'test'
|
|
297
283
|
end
|
|
298
284
|
|
|
@@ -303,24 +289,24 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
303
289
|
|
|
304
290
|
test 'builder should generate errors for attribute with errors' do
|
|
305
291
|
with_form_for @user, :name
|
|
306
|
-
assert_select 'span.error', "can
|
|
292
|
+
assert_select 'span.error', "can't be blank"
|
|
307
293
|
end
|
|
308
294
|
|
|
309
295
|
test 'builder should be able to disable showing errors for a input' do
|
|
310
|
-
with_form_for @user, :name, :
|
|
296
|
+
with_form_for @user, :name, error: false
|
|
311
297
|
assert_no_select 'span.error'
|
|
312
298
|
end
|
|
313
299
|
|
|
314
300
|
test 'builder should pass options to errors' do
|
|
315
|
-
with_form_for @user, :name, :
|
|
316
|
-
assert_select 'span.error#cool', "can
|
|
301
|
+
with_form_for @user, :name, error_html: { id: "cool" }
|
|
302
|
+
assert_select 'span.error#cool', "can't be blank"
|
|
317
303
|
end
|
|
318
304
|
|
|
319
305
|
test 'placeholder should not be generated when set to false' do
|
|
320
|
-
store_translations(:en, :
|
|
321
|
-
:
|
|
306
|
+
store_translations(:en, simple_form: { placeholders: { user: {
|
|
307
|
+
name: 'Name goes here'
|
|
322
308
|
} } }) do
|
|
323
|
-
with_form_for @user, :name, :
|
|
309
|
+
with_form_for @user, :name, placeholder: false
|
|
324
310
|
assert_no_select 'input[placeholder]'
|
|
325
311
|
end
|
|
326
312
|
end
|
|
@@ -328,14 +314,14 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
328
314
|
# DEFAULT OPTIONS
|
|
329
315
|
[:input, :input_field].each do |method|
|
|
330
316
|
test "builder should receive a default argument and pass it to the inputs when calling '#{method}'" do
|
|
331
|
-
with_concat_form_for @user, :
|
|
317
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
332
318
|
f.send(method, :name)
|
|
333
319
|
end
|
|
334
320
|
assert_select 'input.default_class'
|
|
335
321
|
end
|
|
336
322
|
|
|
337
323
|
test "builder should receive a default argument and pass it to the inputs without changing the defaults when calling '#{method}'" do
|
|
338
|
-
with_concat_form_for @user, :
|
|
324
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
|
|
339
325
|
concat(f.send(method, :name))
|
|
340
326
|
concat(f.send(method, :credit_limit))
|
|
341
327
|
end
|
|
@@ -347,7 +333,7 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
347
333
|
test "builder should receive a default argument and pass it to the inputs and nested form when calling '#{method}'" do
|
|
348
334
|
@user.company = Company.new(1, 'Empresa')
|
|
349
335
|
|
|
350
|
-
with_concat_form_for @user, :
|
|
336
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
351
337
|
concat(f.send(method, :name))
|
|
352
338
|
concat(f.simple_fields_for(:company) do |company_form|
|
|
353
339
|
concat(company_form.send(method, :name))
|
|
@@ -360,29 +346,29 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
360
346
|
end
|
|
361
347
|
|
|
362
348
|
test "builder should receive a default argument and pass it to the inputs when calling 'input', respecting the specific options" do
|
|
363
|
-
with_concat_form_for @user, :
|
|
364
|
-
f.input :name, :
|
|
349
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
350
|
+
f.input :name, input_html: { id: 'specific_id' }
|
|
365
351
|
end
|
|
366
352
|
assert_select 'input.default_class#specific_id'
|
|
367
353
|
end
|
|
368
354
|
|
|
369
355
|
test "builder should receive a default argument and pass it to the inputs when calling 'input_field', respecting the specific options" do
|
|
370
|
-
with_concat_form_for @user, :
|
|
371
|
-
f.input_field :name, :
|
|
356
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class' } } do |f|
|
|
357
|
+
f.input_field :name, id: 'specific_id'
|
|
372
358
|
end
|
|
373
359
|
assert_select 'input.default_class#specific_id'
|
|
374
360
|
end
|
|
375
361
|
|
|
376
362
|
test "builder should receive a default argument and pass it to the inputs when calling 'input', overwriting the defaults with specific options" do
|
|
377
|
-
with_concat_form_for @user, :
|
|
378
|
-
f.input :name, :
|
|
363
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
|
|
364
|
+
f.input :name, input_html: { id: 'specific_id' }
|
|
379
365
|
end
|
|
380
366
|
assert_select 'input.default_class#specific_id'
|
|
381
367
|
end
|
|
382
368
|
|
|
383
369
|
test "builder should receive a default argument and pass it to the inputs when calling 'input_field', overwriting the defaults with specific options" do
|
|
384
|
-
with_concat_form_for @user, :
|
|
385
|
-
f.input_field :name, :
|
|
370
|
+
with_concat_form_for @user, defaults: { input_html: { class: 'default_class', id: 'default_id' } } do |f|
|
|
371
|
+
f.input_field :name, id: 'specific_id'
|
|
386
372
|
end
|
|
387
373
|
assert_select 'input.default_class#specific_id'
|
|
388
374
|
end
|
|
@@ -406,9 +392,9 @@ class FormBuilderTest < ActionView::TestCase
|
|
|
406
392
|
end
|
|
407
393
|
|
|
408
394
|
test 'builder should allow overriding input type when object is not present' do
|
|
409
|
-
with_form_for :project, :created_at, :
|
|
395
|
+
with_form_for :project, :created_at, as: :datetime
|
|
410
396
|
assert_select 'form select.datetime#project_created_at_1i'
|
|
411
|
-
with_form_for :project, :budget, :
|
|
397
|
+
with_form_for :project, :budget, as: :decimal
|
|
412
398
|
assert_select 'form input.decimal#project_budget'
|
|
413
399
|
end
|
|
414
400
|
|
|
@@ -14,43 +14,37 @@ class HintTest < ActionView::TestCase
|
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
test 'hint should be generated with optional text' do
|
|
17
|
-
with_hint_for @user, :name, :
|
|
17
|
+
with_hint_for @user, :name, hint: 'Use with care...'
|
|
18
18
|
assert_select 'span.hint', 'Use with care...'
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
test 'hint should not modify the options hash' do
|
|
22
|
-
options = { :
|
|
22
|
+
options = { hint: 'Use with care...' }
|
|
23
23
|
with_hint_for @user, :name, options
|
|
24
24
|
assert_select 'span.hint', 'Use with care...'
|
|
25
|
-
assert_equal({ :
|
|
25
|
+
assert_equal({ hint: 'Use with care...' }, options)
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
test 'hint should be generated cleanly with optional text' do
|
|
29
|
-
with_hint_for @user, :name, :
|
|
29
|
+
with_hint_for @user, :name, hint: 'Use with care...', hint_tag: :span
|
|
30
30
|
assert_no_select 'span.hint[hint]'
|
|
31
31
|
assert_no_select 'span.hint[hint_tag]'
|
|
32
32
|
assert_no_select 'span.hint[hint_html]'
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
test 'hint uses the current component tag set' do
|
|
36
|
-
with_hint_for @user, :name, :
|
|
36
|
+
with_hint_for @user, :name, hint: 'Use with care...', hint_tag: :p
|
|
37
37
|
assert_select 'p.hint', 'Use with care...'
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
test 'hint should be able to pass html options' do
|
|
41
|
-
with_hint_for @user, :name, :
|
|
41
|
+
with_hint_for @user, :name, hint: 'Yay!', id: 'hint', class: 'yay'
|
|
42
42
|
assert_select 'span#hint.hint.yay'
|
|
43
43
|
end
|
|
44
44
|
|
|
45
45
|
test 'hint should be output as html_safe' do
|
|
46
|
-
with_hint_for @user, :name, :
|
|
46
|
+
with_hint_for @user, :name, hint: '<b>Bold</b> and not...'
|
|
47
47
|
assert_select 'span.hint', 'Bold and not...'
|
|
48
|
-
assert_select 'span.hint b', 'Bold'
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
test 'builder should escape hint text' do
|
|
52
|
-
with_hint_for @user, :name, :hint => '<script>alert(1337)</script>'
|
|
53
|
-
assert_select 'span.hint', "<script>alert(1337)</script>"
|
|
54
48
|
end
|
|
55
49
|
|
|
56
50
|
# Without attribute name
|
|
@@ -67,22 +61,22 @@ class HintTest < ActionView::TestCase
|
|
|
67
61
|
end
|
|
68
62
|
|
|
69
63
|
test 'hint without attribute name uses the current component tag set' do
|
|
70
|
-
with_hint_for @user, 'Hello World!', :
|
|
64
|
+
with_hint_for @user, 'Hello World!', hint_tag: :p
|
|
71
65
|
assert_no_select 'p.hint[hint]'
|
|
72
66
|
assert_no_select 'p.hint[hint_html]'
|
|
73
67
|
assert_no_select 'p.hint[hint_tag]'
|
|
74
68
|
end
|
|
75
69
|
|
|
76
70
|
test 'hint without attribute name should be able to pass html options' do
|
|
77
|
-
with_hint_for @user, 'Yay', :
|
|
71
|
+
with_hint_for @user, 'Yay', id: 'hint', class: 'yay'
|
|
78
72
|
assert_select 'span#hint.hint.yay', 'Yay'
|
|
79
73
|
end
|
|
80
74
|
|
|
81
75
|
# I18n
|
|
82
76
|
|
|
83
77
|
test 'hint should use i18n based on model, action, and attribute to lookup translation' do
|
|
84
|
-
store_translations(:en, :
|
|
85
|
-
:
|
|
78
|
+
store_translations(:en, simple_form: { hints: { user: {
|
|
79
|
+
edit: { name: 'Content of this input will be truncated...' }
|
|
86
80
|
} } }) do
|
|
87
81
|
with_hint_for @user, :name
|
|
88
82
|
assert_select 'span.hint', 'Content of this input will be truncated...'
|
|
@@ -90,8 +84,8 @@ class HintTest < ActionView::TestCase
|
|
|
90
84
|
end
|
|
91
85
|
|
|
92
86
|
test 'hint should use i18n with model and attribute to lookup translation' do
|
|
93
|
-
store_translations(:en, :
|
|
94
|
-
:
|
|
87
|
+
store_translations(:en, simple_form: { hints: { user: {
|
|
88
|
+
name: 'Content of this input will be capitalized...'
|
|
95
89
|
} } }) do
|
|
96
90
|
with_hint_for @user, :name
|
|
97
91
|
assert_select 'span.hint', 'Content of this input will be capitalized...'
|
|
@@ -99,8 +93,8 @@ class HintTest < ActionView::TestCase
|
|
|
99
93
|
end
|
|
100
94
|
|
|
101
95
|
test 'hint should use i18n under defaults namespace to lookup translation' do
|
|
102
|
-
store_translations(:en, :
|
|
103
|
-
:
|
|
96
|
+
store_translations(:en, simple_form: {
|
|
97
|
+
hints: { defaults: { name: 'Content of this input will be downcased...' } }
|
|
104
98
|
}) do
|
|
105
99
|
with_hint_for @user, :name
|
|
106
100
|
assert_select 'span.hint', 'Content of this input will be downcased...'
|
|
@@ -108,17 +102,17 @@ class HintTest < ActionView::TestCase
|
|
|
108
102
|
end
|
|
109
103
|
|
|
110
104
|
test 'hint should use i18n with lookup for association name' do
|
|
111
|
-
store_translations(:en, :
|
|
112
|
-
:
|
|
105
|
+
store_translations(:en, simple_form: { hints: {
|
|
106
|
+
user: { company: 'My company!' }
|
|
113
107
|
} } ) do
|
|
114
|
-
with_hint_for @user, :company_id, :
|
|
108
|
+
with_hint_for @user, :company_id, as: :string, reflection: Association.new(Company, :company, {})
|
|
115
109
|
assert_select 'span.hint', /My company!/
|
|
116
110
|
end
|
|
117
111
|
end
|
|
118
112
|
|
|
119
113
|
test 'hint should output translations as html_safe' do
|
|
120
|
-
store_translations(:en, :
|
|
121
|
-
:
|
|
114
|
+
store_translations(:en, simple_form: { hints: { user: {
|
|
115
|
+
edit: { name: '<b>This is bold</b> and this is not...' }
|
|
122
116
|
} } }) do
|
|
123
117
|
with_hint_for @user, :name
|
|
124
118
|
assert_select 'span.hint', 'This is bold and this is not...'
|
|
@@ -129,7 +123,7 @@ class HintTest < ActionView::TestCase
|
|
|
129
123
|
# No object
|
|
130
124
|
|
|
131
125
|
test 'hint should generate properly when object is not present' do
|
|
132
|
-
with_hint_for :project, :name, :
|
|
126
|
+
with_hint_for :project, :name, hint: 'Test without object'
|
|
133
127
|
assert_select 'span.hint', 'Test without object'
|
|
134
128
|
end
|
|
135
129
|
|
|
@@ -137,8 +131,8 @@ class HintTest < ActionView::TestCase
|
|
|
137
131
|
|
|
138
132
|
test 'hint with custom wrappers works' do
|
|
139
133
|
swap_wrapper do
|
|
140
|
-
with_hint_for @user, :name, :
|
|
141
|
-
assert_select 'div.omg_hint', "can
|
|
134
|
+
with_hint_for @user, :name, hint: "can't be blank"
|
|
135
|
+
assert_select 'div.omg_hint', "can't be blank"
|
|
142
136
|
end
|
|
143
137
|
end
|
|
144
138
|
end
|