simple_form 1.3.0 → 1.4.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.
- data/.gitignore +2 -0
- data/.gitmodules +3 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.rdoc +109 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +82 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +73 -12
- data/Rakefile +27 -0
- data/lib/generators/simple_form/templates/_form.html.erb +1 -1
- data/lib/generators/simple_form/templates/_form.html.haml +2 -10
- data/lib/generators/simple_form/templates/_form.html.slim +10 -0
- data/lib/generators/simple_form/templates/simple_form.rb +20 -4
- data/lib/simple_form/action_view_extensions/builder.rb +39 -15
- data/lib/simple_form/action_view_extensions/form_helper.rb +5 -5
- data/lib/simple_form/components/errors.rb +5 -1
- data/lib/simple_form/components/labels.rb +2 -2
- data/lib/simple_form/form_builder.rb +131 -38
- data/lib/simple_form/inputs/base.rb +52 -6
- data/lib/simple_form/inputs/collection_input.rb +5 -1
- data/lib/simple_form/inputs/date_time_input.rb +4 -0
- data/lib/simple_form/inputs/mapping_input.rb +0 -6
- data/lib/simple_form/inputs/numeric_input.rb +15 -7
- data/lib/simple_form/inputs/priority_input.rb +5 -1
- data/lib/simple_form/inputs/string_input.rb +14 -3
- data/lib/simple_form/map_type.rb +6 -3
- data/lib/simple_form/version.rb +1 -1
- data/lib/simple_form.rb +28 -1
- data/simple_form.gemspec +22 -0
- data/test/action_view_extensions/builder_test.rb +39 -7
- data/test/action_view_extensions/form_helper_test.rb +12 -0
- data/test/components/label_test.rb +54 -0
- data/test/discovery_inputs.rb +21 -0
- data/test/form_builder_test.rb +230 -0
- data/test/inputs_test.rb +234 -14
- data/test/support/misc_helpers.rb +26 -0
- data/test/support/models.rb +41 -3
- data/test/test_helper.rb +13 -4
- metadata +24 -27
data/test/inputs_test.rb
CHANGED
|
@@ -62,6 +62,48 @@ class InputTest < ActionView::TestCase
|
|
|
62
62
|
assert_select 'select.datetime:not([disabled])'
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
+
test 'input should generate autofocus attribute based on the autofocus option' do
|
|
66
|
+
with_input_for @user, :name, :string, :autofocus => true
|
|
67
|
+
assert_select 'input.string[autofocus]'
|
|
68
|
+
with_input_for @user, :description, :text, :autofocus => true
|
|
69
|
+
assert_select 'textarea.text[autofocus]'
|
|
70
|
+
with_input_for @user, :age, :integer, :autofocus => true
|
|
71
|
+
assert_select 'input.integer[autofocus]'
|
|
72
|
+
with_input_for @user, :born_at, :date, :autofocus => true
|
|
73
|
+
assert_select 'select.date[autofocus]'
|
|
74
|
+
with_input_for @user, :created_at, :datetime, :autofocus => true
|
|
75
|
+
assert_select 'select.datetime[autofocus]'
|
|
76
|
+
|
|
77
|
+
with_input_for @user, :name, :string, :autofocus => false
|
|
78
|
+
assert_select 'input.string:not([autofocus])'
|
|
79
|
+
with_input_for @user, :description, :text, :autofocus => false
|
|
80
|
+
assert_select 'textarea.text:not([autofocus])'
|
|
81
|
+
with_input_for @user, :age, :integer, :autofocus => false
|
|
82
|
+
assert_select 'input.integer:not([autofocus])'
|
|
83
|
+
with_input_for @user, :born_at, :date, :autofocus => false
|
|
84
|
+
assert_select 'select.date:not([autofocus])'
|
|
85
|
+
with_input_for @user, :created_at, :datetime, :autofocus => false
|
|
86
|
+
assert_select 'select.datetime:not([autofocus])'
|
|
87
|
+
|
|
88
|
+
with_input_for @user, :name, :string
|
|
89
|
+
assert_select 'input.string:not([autofocus])'
|
|
90
|
+
with_input_for @user, :description, :text
|
|
91
|
+
assert_select 'textarea.text:not([autofocus])'
|
|
92
|
+
with_input_for @user, :age, :integer
|
|
93
|
+
assert_select 'input.integer:not([autofocus])'
|
|
94
|
+
with_input_for @user, :born_at, :date
|
|
95
|
+
assert_select 'select.date:not([autofocus])'
|
|
96
|
+
with_input_for @user, :created_at, :datetime
|
|
97
|
+
assert_select 'select.datetime:not([autofocus])'
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
test "when not using HTML5, it does not generate autofocus attribute" do
|
|
101
|
+
swap SimpleForm, :html5 => false do
|
|
102
|
+
with_input_for @user, :name, :string, :autofocus => true
|
|
103
|
+
assert_no_select 'input.string[autofocus]'
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
65
107
|
test 'input should render components according to an optional :components option' do
|
|
66
108
|
with_input_for @user, :name, :string, :components => [:input, :label]
|
|
67
109
|
assert_select 'input + label'
|
|
@@ -91,7 +133,12 @@ class InputTest < ActionView::TestCase
|
|
|
91
133
|
# StringInput
|
|
92
134
|
test 'input should map text field to string attribute' do
|
|
93
135
|
with_input_for @user, :name, :string
|
|
94
|
-
assert_select
|
|
136
|
+
assert_select "input#user_name[type=text][name='user[name]'][value=New in Simple Form!]"
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
test 'input should generate a password field for password attributes' do
|
|
140
|
+
with_input_for @user, :password, :password
|
|
141
|
+
assert_select "input#user_password.password[type=password][name='user[password]']"
|
|
95
142
|
end
|
|
96
143
|
|
|
97
144
|
test 'input should use default text size for decimal attributes' do
|
|
@@ -109,6 +156,23 @@ class InputTest < ActionView::TestCase
|
|
|
109
156
|
assert_select 'input.string[size=50]'
|
|
110
157
|
end
|
|
111
158
|
|
|
159
|
+
test 'input should use default text size for password attributes' do
|
|
160
|
+
with_input_for @user, :password, :password
|
|
161
|
+
assert_select 'input.password[type=password][size=50]'
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
test 'input should get maxlength from column definition for password attributes' do
|
|
165
|
+
with_input_for @user, :password, :password
|
|
166
|
+
assert_select 'input.password[type=password][maxlength=100]'
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
test 'when not using HTML5, does not show maxlength attribute' do
|
|
170
|
+
swap SimpleForm, :html5 => false do
|
|
171
|
+
with_input_for @user, :password, :password
|
|
172
|
+
assert_no_select 'input[type=password][maxlength]'
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
112
176
|
test 'input should not generate placeholder by default' do
|
|
113
177
|
with_input_for @user, :name, :string
|
|
114
178
|
assert_no_select 'input[placeholder]'
|
|
@@ -119,6 +183,11 @@ class InputTest < ActionView::TestCase
|
|
|
119
183
|
assert_select 'input.string[placeholder=Put in some text]'
|
|
120
184
|
end
|
|
121
185
|
|
|
186
|
+
test 'input should generate a password field for password attributes that accept placeholder' do
|
|
187
|
+
with_input_for @user, :password, :password, :placeholder => 'Password Confirmation'
|
|
188
|
+
assert_select 'input[type=password].password[placeholder=Password Confirmation]#user_password'
|
|
189
|
+
end
|
|
190
|
+
|
|
122
191
|
test 'input should use i18n to translate placeholder text' do
|
|
123
192
|
store_translations(:en, :simple_form => { :placeholders => { :user => {
|
|
124
193
|
:name => 'Name goes here'
|
|
@@ -132,6 +201,14 @@ class InputTest < ActionView::TestCase
|
|
|
132
201
|
test "input should allow type #{type}" do
|
|
133
202
|
with_input_for @user, :name, type
|
|
134
203
|
assert_select "input.string.#{type}"
|
|
204
|
+
assert_select "input[type=#{type}]"
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
test "input should not allow type #{type} if HTML5 compatibility is disabled" do
|
|
208
|
+
swap SimpleForm, :html5 => false do
|
|
209
|
+
with_input_for @user, :name, type
|
|
210
|
+
assert_no_select "input[type=#{type}]"
|
|
211
|
+
end
|
|
135
212
|
end
|
|
136
213
|
end
|
|
137
214
|
|
|
@@ -169,6 +246,38 @@ class InputTest < ActionView::TestCase
|
|
|
169
246
|
assert_select 'input[min=18]'
|
|
170
247
|
end
|
|
171
248
|
|
|
249
|
+
test 'input should infer min value from integer attributes with greater than validation using symbol' do
|
|
250
|
+
with_input_for @validating_user, :amount, :float
|
|
251
|
+
assert_no_select 'input[min]'
|
|
252
|
+
|
|
253
|
+
with_input_for @validating_user, :amount, :integer
|
|
254
|
+
assert_select 'input[min=11]'
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
test 'input should infer min value from integer attributes with greater than or equal to validation using symbol' do
|
|
258
|
+
with_input_for @validating_user, :attempts, :float
|
|
259
|
+
assert_select 'input[min=1]'
|
|
260
|
+
|
|
261
|
+
with_input_for @validating_user, :attempts, :integer
|
|
262
|
+
assert_select 'input[min=1]'
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
test 'input should infer min value from integer attributes with greater than validation using proc' do
|
|
266
|
+
with_input_for @other_validating_user, :amount, :float
|
|
267
|
+
assert_no_select 'input[min]'
|
|
268
|
+
|
|
269
|
+
with_input_for @other_validating_user, :amount, :integer
|
|
270
|
+
assert_select 'input[min=20]'
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
test 'input should infer min value from integer attributes with greater than or equal to validation using proc' do
|
|
274
|
+
with_input_for @other_validating_user, :attempts, :float
|
|
275
|
+
assert_select 'input[min=19]'
|
|
276
|
+
|
|
277
|
+
with_input_for @other_validating_user, :attempts, :integer
|
|
278
|
+
assert_select 'input[min=19]'
|
|
279
|
+
end
|
|
280
|
+
|
|
172
281
|
test 'input should infer max value from attributes with less than validation' do
|
|
173
282
|
with_input_for @other_validating_user, :age, :float
|
|
174
283
|
assert_no_select 'input[max]'
|
|
@@ -177,9 +286,41 @@ class InputTest < ActionView::TestCase
|
|
|
177
286
|
assert_select 'input[max=99]'
|
|
178
287
|
end
|
|
179
288
|
|
|
180
|
-
test 'input should infer
|
|
289
|
+
test 'input should infer max value from attributes with less than validation using symbol' do
|
|
290
|
+
with_input_for @validating_user, :amount, :float
|
|
291
|
+
assert_no_select 'input[max]'
|
|
292
|
+
|
|
293
|
+
with_input_for @validating_user, :amount, :integer
|
|
294
|
+
assert_select 'input[max=99]'
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
test 'input should infer max value from attributes with less than or equal to validation using symbol' do
|
|
298
|
+
with_input_for @validating_user, :attempts, :float
|
|
299
|
+
assert_select 'input[max=100]'
|
|
300
|
+
|
|
301
|
+
with_input_for @validating_user, :attempts, :integer
|
|
302
|
+
assert_select 'input[max=100]'
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
test 'input should infer max value from attributes with less than validation using proc' do
|
|
306
|
+
with_input_for @other_validating_user, :amount, :float
|
|
307
|
+
assert_no_select 'input[max]'
|
|
308
|
+
|
|
309
|
+
with_input_for @other_validating_user, :amount, :integer
|
|
310
|
+
assert_select 'input[max=118]'
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
test 'input should infer max value from attributes with less than or equal to validation using proc' do
|
|
314
|
+
with_input_for @other_validating_user, :attempts, :float
|
|
315
|
+
assert_select 'input[max=119]'
|
|
316
|
+
|
|
317
|
+
with_input_for @other_validating_user, :attempts, :integer
|
|
318
|
+
assert_select 'input[max=119]'
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
test 'input should have step value of any except for integer attribute' do
|
|
181
322
|
with_input_for @validating_user, :age, :float
|
|
182
|
-
|
|
323
|
+
assert_select 'input[step="any"]'
|
|
183
324
|
|
|
184
325
|
with_input_for @validating_user, :age, :integer
|
|
185
326
|
assert_select 'input[step=1]'
|
|
@@ -204,6 +345,24 @@ class InputTest < ActionView::TestCase
|
|
|
204
345
|
end
|
|
205
346
|
end
|
|
206
347
|
|
|
348
|
+
# Numeric input but HTML5 disabled
|
|
349
|
+
test ' when not using HTML5 input should not generate field with type number and use text instead' do
|
|
350
|
+
swap SimpleForm, :html5 => false do
|
|
351
|
+
with_input_for @user, :age, :integer
|
|
352
|
+
assert_no_select "input[type=number]"
|
|
353
|
+
assert_no_select "input#user_age[text]"
|
|
354
|
+
end
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
test 'when not using HTML5 input should not use min or max or step attributes' do
|
|
358
|
+
swap SimpleForm, :html5 => false do
|
|
359
|
+
with_input_for @validating_user, :age, :integer
|
|
360
|
+
assert_no_select "input[min]"
|
|
361
|
+
assert_no_select "input[max]"
|
|
362
|
+
assert_no_select "input[step]"
|
|
363
|
+
end
|
|
364
|
+
end
|
|
365
|
+
|
|
207
366
|
[:integer, :float, :decimal].each do |type|
|
|
208
367
|
test "#{type} input should infer min value from attributes with greater than or equal validation" do
|
|
209
368
|
with_input_for @validating_user, :age, type
|
|
@@ -234,22 +393,12 @@ class InputTest < ActionView::TestCase
|
|
|
234
393
|
assert_select 'textarea.text[placeholder=Put in some text]'
|
|
235
394
|
end
|
|
236
395
|
|
|
237
|
-
test 'input should generate a password field for password attributes' do
|
|
238
|
-
with_input_for @user, :password, :password
|
|
239
|
-
assert_select 'input[type=password].password#user_password'
|
|
240
|
-
end
|
|
241
|
-
|
|
242
|
-
test 'input should generate a password field for password attributes that accept placeholder' do
|
|
243
|
-
with_input_for @user, :password, :password, :placeholder => 'Password Confirmation'
|
|
244
|
-
assert_select 'input[type=password].password[placeholder=Password Confirmation]#user_password'
|
|
245
|
-
end
|
|
246
|
-
|
|
247
396
|
test 'input should generate a file field' do
|
|
248
397
|
with_input_for @user, :name, :file
|
|
249
398
|
assert_select 'input#user_name[type=file]'
|
|
250
399
|
end
|
|
251
400
|
|
|
252
|
-
test "input should generate a file field that
|
|
401
|
+
test "input should generate a file field that doesn't accept placeholder" do
|
|
253
402
|
with_input_for @user, :name, :file, :placeholder => 'Put in some text'
|
|
254
403
|
assert_no_select 'input[placeholder]'
|
|
255
404
|
end
|
|
@@ -321,6 +470,12 @@ class InputTest < ActionView::TestCase
|
|
|
321
470
|
assert_no_select 'select option[value=]', /^$/
|
|
322
471
|
end
|
|
323
472
|
|
|
473
|
+
test 'priority input should not generate invalid required html attribute' do
|
|
474
|
+
with_input_for @user, :country, :country
|
|
475
|
+
assert_select 'select.required'
|
|
476
|
+
assert_no_select 'select[required]'
|
|
477
|
+
end
|
|
478
|
+
|
|
324
479
|
# DateTime input
|
|
325
480
|
test 'input should generate a datetime select by default for datetime attributes' do
|
|
326
481
|
with_input_for @user, :created_at, :datetime
|
|
@@ -395,6 +550,12 @@ class InputTest < ActionView::TestCase
|
|
|
395
550
|
assert_select 'label[for=project_created_at_4i]'
|
|
396
551
|
end
|
|
397
552
|
|
|
553
|
+
test 'date time input should not generate invalid required html attribute' do
|
|
554
|
+
with_input_for @user, :delivery_time, :time, :required => true
|
|
555
|
+
assert_select 'select.required'
|
|
556
|
+
assert_no_select 'select[required]'
|
|
557
|
+
end
|
|
558
|
+
|
|
398
559
|
# CollectionInput
|
|
399
560
|
test 'input should generate boolean radio buttons by default for radio types' do
|
|
400
561
|
with_input_for @user, :active, :radio
|
|
@@ -416,6 +577,13 @@ class InputTest < ActionView::TestCase
|
|
|
416
577
|
end
|
|
417
578
|
end
|
|
418
579
|
|
|
580
|
+
test 'input should mark the checked value when using boolean and radios' do
|
|
581
|
+
@user.active = false
|
|
582
|
+
with_input_for @user, :active, :radio
|
|
583
|
+
assert_no_select 'input[type=radio][value=true][checked]'
|
|
584
|
+
assert_select 'input[type=radio][value=false][checked]'
|
|
585
|
+
end
|
|
586
|
+
|
|
419
587
|
test 'input should generate a boolean select with options by default for select types' do
|
|
420
588
|
with_input_for @user, :active, :select
|
|
421
589
|
assert_select 'select.select#user_active'
|
|
@@ -450,6 +618,13 @@ class InputTest < ActionView::TestCase
|
|
|
450
618
|
assert_select 'select option[selected=selected]', '18'
|
|
451
619
|
end
|
|
452
620
|
|
|
621
|
+
test 'input should mark the selected value when using booleans and select' do
|
|
622
|
+
@user.active = false
|
|
623
|
+
with_input_for @user, :active, :select
|
|
624
|
+
assert_no_select 'select option[selected][value=true]', 'Yes'
|
|
625
|
+
assert_select 'select option[selected][value=false]', 'No'
|
|
626
|
+
end
|
|
627
|
+
|
|
453
628
|
test 'input should set the correct value when using a collection that includes floats' do
|
|
454
629
|
with_input_for @user, :age, :select, :collection => [2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
|
|
455
630
|
assert_select 'select option[value="2.0"]'
|
|
@@ -559,6 +734,31 @@ class InputTest < ActionView::TestCase
|
|
|
559
734
|
assert_select 'label.collection_radio', 'CARLOS'
|
|
560
735
|
end
|
|
561
736
|
|
|
737
|
+
test 'input should allow overriding label and value method using a lambda for collection selects' do
|
|
738
|
+
with_input_for @user, :name, :select,
|
|
739
|
+
:collection => ['Jose' , 'Carlos'],
|
|
740
|
+
:label_method => lambda { |i| i.upcase },
|
|
741
|
+
:value_method => lambda { |i| i.downcase }
|
|
742
|
+
assert_select 'select option[value=jose]', "JOSE"
|
|
743
|
+
assert_select 'select option[value=carlos]', "CARLOS"
|
|
744
|
+
end
|
|
745
|
+
|
|
746
|
+
test 'input should allow overriding only label but not value method using a lambda for collection select' do
|
|
747
|
+
with_input_for @user, :name, :select,
|
|
748
|
+
:collection => ['Jose' , 'Carlos'],
|
|
749
|
+
:label_method => lambda { |i| i.upcase }
|
|
750
|
+
assert_select 'select option[value=Jose]', "JOSE"
|
|
751
|
+
assert_select 'select option[value=Carlos]', "CARLOS"
|
|
752
|
+
end
|
|
753
|
+
|
|
754
|
+
test 'input should allow overriding only value but not label method using a lambda for collection select' do
|
|
755
|
+
with_input_for @user, :name, :select,
|
|
756
|
+
:collection => ['Jose' , 'Carlos'],
|
|
757
|
+
:value_method => lambda { |i| i.downcase }
|
|
758
|
+
assert_select 'select option[value=jose]', "Jose"
|
|
759
|
+
assert_select 'select option[value=carlos]', "Carlos"
|
|
760
|
+
end
|
|
761
|
+
|
|
562
762
|
test 'input should allow symbols for collections' do
|
|
563
763
|
with_input_for @user, :name, :select, :collection => [:jose, :carlos]
|
|
564
764
|
assert_select 'select.select#user_name'
|
|
@@ -566,6 +766,26 @@ class InputTest < ActionView::TestCase
|
|
|
566
766
|
assert_select 'select option[value=carlos]', 'carlos'
|
|
567
767
|
end
|
|
568
768
|
|
|
769
|
+
test 'collection input with radio type should generate required html attribute' do
|
|
770
|
+
with_input_for @user, :name, :radio, :collection => ['Jose' , 'Carlos']
|
|
771
|
+
assert_select 'input[type=radio].required'
|
|
772
|
+
assert_select 'input[type=radio][required]'
|
|
773
|
+
end
|
|
774
|
+
|
|
775
|
+
test 'when not using HTML5, collection input with radio type should not generate required html attribute' do
|
|
776
|
+
swap SimpleForm, :html5 => false do
|
|
777
|
+
with_input_for @user, :name, :radio, :collection => ['Jose' , 'Carlos']
|
|
778
|
+
assert_select 'input[type=radio].required'
|
|
779
|
+
assert_no_select 'input[type=radio][required]'
|
|
780
|
+
end
|
|
781
|
+
end
|
|
782
|
+
|
|
783
|
+
test 'collection input with select type should not generate invalid required html attribute' do
|
|
784
|
+
with_input_for @user, :name, :select, :collection => ['Jose' , 'Carlos']
|
|
785
|
+
assert_select 'select.required'
|
|
786
|
+
assert_no_select 'select[required]'
|
|
787
|
+
end
|
|
788
|
+
|
|
569
789
|
# With no object
|
|
570
790
|
test 'input should be generated properly when object is not present' do
|
|
571
791
|
with_input_for :project, :name, :string
|
|
@@ -28,4 +28,30 @@ module MiscHelpers
|
|
|
28
28
|
def with_concat_form_for(object, &block)
|
|
29
29
|
concat simple_form_for(object, &block)
|
|
30
30
|
end
|
|
31
|
+
|
|
32
|
+
def with_concat_custom_form_for(object, &block)
|
|
33
|
+
concat custom_form_for(object, &block)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def custom_form_for(object, *args, &block)
|
|
37
|
+
simple_form_for(object, *(args << { :builder => CustomFormBuilder }), &block)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def custom_mapping_form_for(object, *args, &block)
|
|
41
|
+
simple_form_for(object, *(args << { :builder => CustomMapTypeFormBuilder }), &block)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def with_concat_custom_mapping_form_for(object, &block)
|
|
45
|
+
concat custom_mapping_form_for(object, &block)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class CustomFormBuilder < SimpleForm::FormBuilder
|
|
50
|
+
def input(attribute_name, *args, &block)
|
|
51
|
+
super(attribute_name, *(args << { :input_html => { :class => 'custom' } }), &block)
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
class CustomMapTypeFormBuilder < SimpleForm::FormBuilder
|
|
56
|
+
map_type :custom_type, :to => SimpleForm::Inputs::StringInput
|
|
31
57
|
end
|
data/test/support/models.rb
CHANGED
|
@@ -38,9 +38,11 @@ class User
|
|
|
38
38
|
extend ActiveModel::Naming
|
|
39
39
|
include ActiveModel::Conversion
|
|
40
40
|
|
|
41
|
-
attr_accessor :id, :name, :company, :company_id, :time_zone, :active, :
|
|
42
|
-
:
|
|
43
|
-
:
|
|
41
|
+
attr_accessor :id, :name, :company, :company_id, :time_zone, :active, :age,
|
|
42
|
+
:description, :created_at, :updated_at, :credit_limit, :password, :url,
|
|
43
|
+
:delivery_time, :born_at, :special_company_id, :country, :tags, :tag_ids,
|
|
44
|
+
:avatar, :home_picture, :email, :status, :residence_country, :phone_number,
|
|
45
|
+
:post_count, :lock_version, :amount, :attempts
|
|
44
46
|
|
|
45
47
|
def initialize(options={})
|
|
46
48
|
options.each do |key, value|
|
|
@@ -70,6 +72,10 @@ class User
|
|
|
70
72
|
when :delivery_time then :time
|
|
71
73
|
when :created_at then :datetime
|
|
72
74
|
when :updated_at then :timestamp
|
|
75
|
+
when :lock_version then :integer
|
|
76
|
+
when :home_picture then :string
|
|
77
|
+
when :amount then :integer
|
|
78
|
+
when :attempts then :integer
|
|
73
79
|
end
|
|
74
80
|
Column.new(attribute, column_type, limit)
|
|
75
81
|
end
|
|
@@ -122,6 +128,30 @@ class ValidatingUser < User
|
|
|
122
128
|
:greater_than_or_equal_to => 18,
|
|
123
129
|
:less_than_or_equal_to => 99,
|
|
124
130
|
:only_integer => true
|
|
131
|
+
validates_numericality_of :amount,
|
|
132
|
+
:greater_than => :min_amount,
|
|
133
|
+
:less_than => :max_amount,
|
|
134
|
+
:only_integer => true
|
|
135
|
+
validates_numericality_of :attempts,
|
|
136
|
+
:greater_than_or_equal_to => :min_attempts,
|
|
137
|
+
:less_than_or_equal_to => :max_attempts,
|
|
138
|
+
:only_integer => true
|
|
139
|
+
|
|
140
|
+
def min_amount
|
|
141
|
+
10
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def max_amount
|
|
145
|
+
100
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def min_attempts
|
|
149
|
+
1
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def max_attempts
|
|
153
|
+
100
|
|
154
|
+
end
|
|
125
155
|
end
|
|
126
156
|
|
|
127
157
|
class OtherValidatingUser < User
|
|
@@ -130,4 +160,12 @@ class OtherValidatingUser < User
|
|
|
130
160
|
:greater_than => 17,
|
|
131
161
|
:less_than => 100,
|
|
132
162
|
:only_integer => true
|
|
163
|
+
validates_numericality_of :amount,
|
|
164
|
+
:greater_than => Proc.new { |user| user.age },
|
|
165
|
+
:less_than => Proc.new { |user| user.age + 100},
|
|
166
|
+
:only_integer => true
|
|
167
|
+
validates_numericality_of :attempts,
|
|
168
|
+
:greater_than_or_equal_to => Proc.new { |user| user.age },
|
|
169
|
+
:less_than_or_equal_to => Proc.new { |user| user.age + 100},
|
|
170
|
+
:only_integer => true
|
|
133
171
|
end
|
data/test/test_helper.rb
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
require 'rubygems'
|
|
2
|
-
require 'bundler'
|
|
3
|
-
|
|
4
|
-
Bundler.setup
|
|
2
|
+
require 'bundler/setup'
|
|
5
3
|
|
|
6
4
|
require 'test/unit'
|
|
7
5
|
require 'mocha'
|
|
@@ -10,8 +8,17 @@ require 'active_model'
|
|
|
10
8
|
require 'action_controller'
|
|
11
9
|
require 'action_view'
|
|
12
10
|
require 'action_view/template'
|
|
11
|
+
|
|
12
|
+
# Rails 3.0.4 is missing this "deprecation" require.
|
|
13
|
+
require 'active_support/core_ext/module/deprecation'
|
|
13
14
|
require 'action_view/test_case'
|
|
14
15
|
|
|
16
|
+
module Rails
|
|
17
|
+
def self.env
|
|
18
|
+
ActiveSupport::StringInquirer.new("test")
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
15
22
|
$:.unshift File.expand_path("../../lib", __FILE__)
|
|
16
23
|
require 'simple_form'
|
|
17
24
|
|
|
@@ -57,7 +64,9 @@ class ActionView::TestCase
|
|
|
57
64
|
:description => 'Hello!',
|
|
58
65
|
:created_at => Time.now,
|
|
59
66
|
:age => 19,
|
|
60
|
-
:
|
|
67
|
+
:amount => 15,
|
|
68
|
+
:attempts => 1,
|
|
69
|
+
:company => [1]
|
|
61
70
|
}.merge(options))
|
|
62
71
|
|
|
63
72
|
@other_validating_user = OtherValidatingUser.new({
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: simple_form
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
5
|
-
prerelease:
|
|
4
|
+
hash: 7
|
|
5
|
+
prerelease:
|
|
6
6
|
segments:
|
|
7
7
|
- 1
|
|
8
|
-
-
|
|
8
|
+
- 4
|
|
9
9
|
- 0
|
|
10
|
-
version: 1.
|
|
10
|
+
version: 1.4.0
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- "Jos\xC3\xA9 Valim"
|
|
@@ -16,39 +16,34 @@ autorequire:
|
|
|
16
16
|
bindir: bin
|
|
17
17
|
cert_chain: []
|
|
18
18
|
|
|
19
|
-
date:
|
|
19
|
+
date: 2011-05-18 00:00:00 -03:00
|
|
20
20
|
default_executable:
|
|
21
|
-
dependencies:
|
|
22
|
-
|
|
23
|
-
prerelease: false
|
|
24
|
-
type: :runtime
|
|
25
|
-
name: rails
|
|
26
|
-
version_requirements: &id001 !ruby/object:Gem::Requirement
|
|
27
|
-
none: false
|
|
28
|
-
requirements:
|
|
29
|
-
- - ~>
|
|
30
|
-
- !ruby/object:Gem::Version
|
|
31
|
-
hash: 7
|
|
32
|
-
segments:
|
|
33
|
-
- 3
|
|
34
|
-
- 0
|
|
35
|
-
- 0
|
|
36
|
-
version: 3.0.0
|
|
37
|
-
requirement: *id001
|
|
21
|
+
dependencies: []
|
|
22
|
+
|
|
38
23
|
description: Forms made easy!
|
|
39
24
|
email: contact@plataformatec.com.br
|
|
40
25
|
executables: []
|
|
41
26
|
|
|
42
27
|
extensions: []
|
|
43
28
|
|
|
44
|
-
extra_rdoc_files:
|
|
45
|
-
|
|
29
|
+
extra_rdoc_files: []
|
|
30
|
+
|
|
46
31
|
files:
|
|
32
|
+
- .gitignore
|
|
33
|
+
- .gitmodules
|
|
34
|
+
- .travis.yml
|
|
35
|
+
- CHANGELOG.rdoc
|
|
36
|
+
- Gemfile
|
|
37
|
+
- Gemfile.lock
|
|
38
|
+
- MIT-LICENSE
|
|
39
|
+
- README.rdoc
|
|
40
|
+
- Rakefile
|
|
47
41
|
- init.rb
|
|
48
42
|
- lib/generators/simple_form/USAGE
|
|
49
43
|
- lib/generators/simple_form/install_generator.rb
|
|
50
44
|
- lib/generators/simple_form/templates/_form.html.erb
|
|
51
45
|
- lib/generators/simple_form/templates/_form.html.haml
|
|
46
|
+
- lib/generators/simple_form/templates/_form.html.slim
|
|
52
47
|
- lib/generators/simple_form/templates/en.yml
|
|
53
48
|
- lib/generators/simple_form/templates/simple_form.rb
|
|
54
49
|
- lib/simple_form.rb
|
|
@@ -78,13 +73,14 @@ files:
|
|
|
78
73
|
- lib/simple_form/inputs/string_input.rb
|
|
79
74
|
- lib/simple_form/map_type.rb
|
|
80
75
|
- lib/simple_form/version.rb
|
|
81
|
-
-
|
|
76
|
+
- simple_form.gemspec
|
|
82
77
|
- test/action_view_extensions/builder_test.rb
|
|
83
78
|
- test/action_view_extensions/form_helper_test.rb
|
|
84
79
|
- test/components/error_test.rb
|
|
85
80
|
- test/components/hint_test.rb
|
|
86
81
|
- test/components/label_test.rb
|
|
87
82
|
- test/components/wrapper_test.rb
|
|
83
|
+
- test/discovery_inputs.rb
|
|
88
84
|
- test/error_notification_test.rb
|
|
89
85
|
- test/form_builder_test.rb
|
|
90
86
|
- test/inputs_test.rb
|
|
@@ -123,8 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
123
119
|
version: "0"
|
|
124
120
|
requirements: []
|
|
125
121
|
|
|
126
|
-
rubyforge_project:
|
|
127
|
-
rubygems_version: 1.
|
|
122
|
+
rubyforge_project: simple_form
|
|
123
|
+
rubygems_version: 1.6.2
|
|
128
124
|
signing_key:
|
|
129
125
|
specification_version: 3
|
|
130
126
|
summary: Forms made easy!
|
|
@@ -135,6 +131,7 @@ test_files:
|
|
|
135
131
|
- test/components/hint_test.rb
|
|
136
132
|
- test/components/label_test.rb
|
|
137
133
|
- test/components/wrapper_test.rb
|
|
134
|
+
- test/discovery_inputs.rb
|
|
138
135
|
- test/error_notification_test.rb
|
|
139
136
|
- test/form_builder_test.rb
|
|
140
137
|
- test/inputs_test.rb
|