simple_form 1.3.1 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of simple_form might be problematic. Click here for more details.
- data/.travis.yml +6 -0
- data/CHANGELOG.rdoc +24 -0
- data/Gemfile +3 -8
- data/Gemfile.lock +36 -36
- data/MIT-LICENSE +1 -1
- data/README.rdoc +70 -23
- 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.rb +28 -1
- data/lib/simple_form/action_view_extensions/builder.rb +9 -10
- data/lib/simple_form/action_view_extensions/form_helper.rb +2 -1
- data/lib/simple_form/components/errors.rb +5 -1
- data/lib/simple_form/components/labels.rb +1 -1
- data/lib/simple_form/form_builder.rb +117 -29
- data/lib/simple_form/inputs/base.rb +42 -6
- data/lib/simple_form/inputs/collection_input.rb +0 -1
- 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/string_input.rb +14 -3
- data/lib/simple_form/map_type.rb +1 -1
- data/lib/simple_form/version.rb +1 -1
- 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 +44 -0
- data/test/discovery_inputs.rb +21 -0
- data/test/form_builder_test.rb +181 -0
- data/test/inputs_test.rb +150 -14
- data/test/support/misc_helpers.rb +12 -0
- data/test/support/models.rb +40 -3
- data/test/test_helper.rb +13 -4
- metadata +10 -6
data/test/inputs_test.rb
CHANGED
@@ -97,6 +97,13 @@ class InputTest < ActionView::TestCase
|
|
97
97
|
assert_select 'select.datetime:not([autofocus])'
|
98
98
|
end
|
99
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
|
+
|
100
107
|
test 'input should render components according to an optional :components option' do
|
101
108
|
with_input_for @user, :name, :string, :components => [:input, :label]
|
102
109
|
assert_select 'input + label'
|
@@ -126,7 +133,12 @@ class InputTest < ActionView::TestCase
|
|
126
133
|
# StringInput
|
127
134
|
test 'input should map text field to string attribute' do
|
128
135
|
with_input_for @user, :name, :string
|
129
|
-
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]']"
|
130
142
|
end
|
131
143
|
|
132
144
|
test 'input should use default text size for decimal attributes' do
|
@@ -144,6 +156,23 @@ class InputTest < ActionView::TestCase
|
|
144
156
|
assert_select 'input.string[size=50]'
|
145
157
|
end
|
146
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
|
+
|
147
176
|
test 'input should not generate placeholder by default' do
|
148
177
|
with_input_for @user, :name, :string
|
149
178
|
assert_no_select 'input[placeholder]'
|
@@ -154,6 +183,11 @@ class InputTest < ActionView::TestCase
|
|
154
183
|
assert_select 'input.string[placeholder=Put in some text]'
|
155
184
|
end
|
156
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
|
+
|
157
191
|
test 'input should use i18n to translate placeholder text' do
|
158
192
|
store_translations(:en, :simple_form => { :placeholders => { :user => {
|
159
193
|
:name => 'Name goes here'
|
@@ -167,6 +201,14 @@ class InputTest < ActionView::TestCase
|
|
167
201
|
test "input should allow type #{type}" do
|
168
202
|
with_input_for @user, :name, type
|
169
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
|
170
212
|
end
|
171
213
|
end
|
172
214
|
|
@@ -204,6 +246,38 @@ class InputTest < ActionView::TestCase
|
|
204
246
|
assert_select 'input[min=18]'
|
205
247
|
end
|
206
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
|
+
|
207
281
|
test 'input should infer max value from attributes with less than validation' do
|
208
282
|
with_input_for @other_validating_user, :age, :float
|
209
283
|
assert_no_select 'input[max]'
|
@@ -212,9 +286,41 @@ class InputTest < ActionView::TestCase
|
|
212
286
|
assert_select 'input[max=99]'
|
213
287
|
end
|
214
288
|
|
215
|
-
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
|
216
322
|
with_input_for @validating_user, :age, :float
|
217
|
-
|
323
|
+
assert_select 'input[step="any"]'
|
218
324
|
|
219
325
|
with_input_for @validating_user, :age, :integer
|
220
326
|
assert_select 'input[step=1]'
|
@@ -239,6 +345,24 @@ class InputTest < ActionView::TestCase
|
|
239
345
|
end
|
240
346
|
end
|
241
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
|
+
|
242
366
|
[:integer, :float, :decimal].each do |type|
|
243
367
|
test "#{type} input should infer min value from attributes with greater than or equal validation" do
|
244
368
|
with_input_for @validating_user, :age, type
|
@@ -269,22 +393,12 @@ class InputTest < ActionView::TestCase
|
|
269
393
|
assert_select 'textarea.text[placeholder=Put in some text]'
|
270
394
|
end
|
271
395
|
|
272
|
-
test 'input should generate a password field for password attributes' do
|
273
|
-
with_input_for @user, :password, :password
|
274
|
-
assert_select 'input[type=password].password#user_password'
|
275
|
-
end
|
276
|
-
|
277
|
-
test 'input should generate a password field for password attributes that accept placeholder' do
|
278
|
-
with_input_for @user, :password, :password, :placeholder => 'Password Confirmation'
|
279
|
-
assert_select 'input[type=password].password[placeholder=Password Confirmation]#user_password'
|
280
|
-
end
|
281
|
-
|
282
396
|
test 'input should generate a file field' do
|
283
397
|
with_input_for @user, :name, :file
|
284
398
|
assert_select 'input#user_name[type=file]'
|
285
399
|
end
|
286
400
|
|
287
|
-
test "input should generate a file field that
|
401
|
+
test "input should generate a file field that doesn't accept placeholder" do
|
288
402
|
with_input_for @user, :name, :file, :placeholder => 'Put in some text'
|
289
403
|
assert_no_select 'input[placeholder]'
|
290
404
|
end
|
@@ -463,6 +577,13 @@ class InputTest < ActionView::TestCase
|
|
463
577
|
end
|
464
578
|
end
|
465
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
|
+
|
466
587
|
test 'input should generate a boolean select with options by default for select types' do
|
467
588
|
with_input_for @user, :active, :select
|
468
589
|
assert_select 'select.select#user_active'
|
@@ -497,6 +618,13 @@ class InputTest < ActionView::TestCase
|
|
497
618
|
assert_select 'select option[selected=selected]', '18'
|
498
619
|
end
|
499
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
|
+
|
500
628
|
test 'input should set the correct value when using a collection that includes floats' do
|
501
629
|
with_input_for @user, :age, :select, :collection => [2.0, 2.5, 3.0, 3.5, 4.0, 4.5]
|
502
630
|
assert_select 'select option[value="2.0"]'
|
@@ -644,6 +772,14 @@ class InputTest < ActionView::TestCase
|
|
644
772
|
assert_select 'input[type=radio][required]'
|
645
773
|
end
|
646
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
|
+
|
647
783
|
test 'collection input with select type should not generate invalid required html attribute' do
|
648
784
|
with_input_for @user, :name, :select, :collection => ['Jose' , 'Carlos']
|
649
785
|
assert_select 'select.required'
|
@@ -36,6 +36,14 @@ module MiscHelpers
|
|
36
36
|
def custom_form_for(object, *args, &block)
|
37
37
|
simple_form_for(object, *(args << { :builder => CustomFormBuilder }), &block)
|
38
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
|
39
47
|
end
|
40
48
|
|
41
49
|
class CustomFormBuilder < SimpleForm::FormBuilder
|
@@ -43,3 +51,7 @@ class CustomFormBuilder < SimpleForm::FormBuilder
|
|
43
51
|
super(attribute_name, *(args << { :input_html => { :class => 'custom' } }), &block)
|
44
52
|
end
|
45
53
|
end
|
54
|
+
|
55
|
+
class CustomMapTypeFormBuilder < SimpleForm::FormBuilder
|
56
|
+
map_type :custom_type, :to => SimpleForm::Inputs::StringInput
|
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|
|
@@ -71,6 +73,9 @@ class User
|
|
71
73
|
when :created_at then :datetime
|
72
74
|
when :updated_at then :timestamp
|
73
75
|
when :lock_version then :integer
|
76
|
+
when :home_picture then :string
|
77
|
+
when :amount then :integer
|
78
|
+
when :attempts then :integer
|
74
79
|
end
|
75
80
|
Column.new(attribute, column_type, limit)
|
76
81
|
end
|
@@ -123,6 +128,30 @@ class ValidatingUser < User
|
|
123
128
|
:greater_than_or_equal_to => 18,
|
124
129
|
:less_than_or_equal_to => 99,
|
125
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
|
126
155
|
end
|
127
156
|
|
128
157
|
class OtherValidatingUser < User
|
@@ -131,4 +160,12 @@ class OtherValidatingUser < User
|
|
131
160
|
:greater_than => 17,
|
132
161
|
:less_than => 100,
|
133
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
|
134
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:
|
4
|
+
hash: 7
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 1.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 1.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Jos\xC3\xA9 Valim"
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-
|
19
|
+
date: 2011-05-18 00:00:00 -03:00
|
20
20
|
default_executable:
|
21
21
|
dependencies: []
|
22
22
|
|
@@ -31,6 +31,7 @@ extra_rdoc_files: []
|
|
31
31
|
files:
|
32
32
|
- .gitignore
|
33
33
|
- .gitmodules
|
34
|
+
- .travis.yml
|
34
35
|
- CHANGELOG.rdoc
|
35
36
|
- Gemfile
|
36
37
|
- Gemfile.lock
|
@@ -42,6 +43,7 @@ files:
|
|
42
43
|
- lib/generators/simple_form/install_generator.rb
|
43
44
|
- lib/generators/simple_form/templates/_form.html.erb
|
44
45
|
- lib/generators/simple_form/templates/_form.html.haml
|
46
|
+
- lib/generators/simple_form/templates/_form.html.slim
|
45
47
|
- lib/generators/simple_form/templates/en.yml
|
46
48
|
- lib/generators/simple_form/templates/simple_form.rb
|
47
49
|
- lib/simple_form.rb
|
@@ -78,6 +80,7 @@ files:
|
|
78
80
|
- test/components/hint_test.rb
|
79
81
|
- test/components/label_test.rb
|
80
82
|
- test/components/wrapper_test.rb
|
83
|
+
- test/discovery_inputs.rb
|
81
84
|
- test/error_notification_test.rb
|
82
85
|
- test/form_builder_test.rb
|
83
86
|
- test/inputs_test.rb
|
@@ -117,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
120
|
requirements: []
|
118
121
|
|
119
122
|
rubyforge_project: simple_form
|
120
|
-
rubygems_version: 1.
|
123
|
+
rubygems_version: 1.6.2
|
121
124
|
signing_key:
|
122
125
|
specification_version: 3
|
123
126
|
summary: Forms made easy!
|
@@ -128,6 +131,7 @@ test_files:
|
|
128
131
|
- test/components/hint_test.rb
|
129
132
|
- test/components/label_test.rb
|
130
133
|
- test/components/wrapper_test.rb
|
134
|
+
- test/discovery_inputs.rb
|
131
135
|
- test/error_notification_test.rb
|
132
136
|
- test/form_builder_test.rb
|
133
137
|
- test/inputs_test.rb
|