simple_form 3.0.2 → 3.1.0.rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +35 -32
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +179 -73
  5. data/lib/generators/simple_form/install_generator.rb +2 -2
  6. data/lib/generators/simple_form/templates/README +3 -4
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +25 -5
  8. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +108 -24
  9. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +1 -1
  10. data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
  11. data/lib/simple_form/action_view_extensions/form_helper.rb +1 -1
  12. data/lib/simple_form/components/errors.rb +30 -2
  13. data/lib/simple_form/components/hints.rb +2 -2
  14. data/lib/simple_form/components/html5.rb +1 -1
  15. data/lib/simple_form/components/label_input.rb +20 -2
  16. data/lib/simple_form/components/labels.rb +10 -6
  17. data/lib/simple_form/components/maxlength.rb +1 -1
  18. data/lib/simple_form/components/min_max.rb +1 -1
  19. data/lib/simple_form/components/pattern.rb +1 -1
  20. data/lib/simple_form/components/placeholders.rb +2 -2
  21. data/lib/simple_form/components/readonly.rb +1 -1
  22. data/lib/simple_form/form_builder.rb +121 -72
  23. data/lib/simple_form/helpers.rb +5 -5
  24. data/lib/simple_form/inputs/base.rb +33 -11
  25. data/lib/simple_form/inputs/block_input.rb +1 -1
  26. data/lib/simple_form/inputs/boolean_input.rb +27 -14
  27. data/lib/simple_form/inputs/collection_input.rb +32 -9
  28. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +6 -11
  29. data/lib/simple_form/inputs/collection_select_input.rb +4 -2
  30. data/lib/simple_form/inputs/date_time_input.rb +12 -2
  31. data/lib/simple_form/inputs/file_input.rb +4 -2
  32. data/lib/simple_form/inputs/grouped_collection_select_input.rb +15 -3
  33. data/lib/simple_form/inputs/hidden_input.rb +4 -2
  34. data/lib/simple_form/inputs/numeric_input.rb +5 -4
  35. data/lib/simple_form/inputs/password_input.rb +4 -2
  36. data/lib/simple_form/inputs/priority_input.rb +4 -2
  37. data/lib/simple_form/inputs/range_input.rb +1 -1
  38. data/lib/simple_form/inputs/string_input.rb +4 -2
  39. data/lib/simple_form/inputs/text_input.rb +4 -2
  40. data/lib/simple_form/railtie.rb +7 -0
  41. data/lib/simple_form/tags.rb +8 -0
  42. data/lib/simple_form/version.rb +1 -1
  43. data/lib/simple_form/wrappers/builder.rb +6 -6
  44. data/lib/simple_form/wrappers/leaf.rb +28 -0
  45. data/lib/simple_form/wrappers/many.rb +6 -6
  46. data/lib/simple_form/wrappers/root.rb +1 -1
  47. data/lib/simple_form/wrappers/single.rb +5 -3
  48. data/lib/simple_form/wrappers.rb +1 -0
  49. data/lib/simple_form.rb +46 -6
  50. data/test/action_view_extensions/builder_test.rb +5 -5
  51. data/test/action_view_extensions/form_helper_test.rb +13 -13
  52. data/test/components/label_test.rb +36 -36
  53. data/test/form_builder/association_test.rb +21 -4
  54. data/test/form_builder/button_test.rb +5 -5
  55. data/test/form_builder/error_notification_test.rb +1 -1
  56. data/test/form_builder/error_test.rb +78 -17
  57. data/test/form_builder/general_test.rb +65 -60
  58. data/test/form_builder/hint_test.rb +15 -15
  59. data/test/form_builder/input_field_test.rb +37 -13
  60. data/test/form_builder/label_test.rb +44 -12
  61. data/test/form_builder/wrapper_test.rb +120 -19
  62. data/test/generators/simple_form_generator_test.rb +2 -2
  63. data/test/inputs/boolean_input_test.rb +54 -6
  64. data/test/inputs/collection_check_boxes_input_test.rb +63 -17
  65. data/test/inputs/collection_radio_buttons_input_test.rb +112 -36
  66. data/test/inputs/collection_select_input_test.rb +141 -36
  67. data/test/inputs/datetime_input_test.rb +116 -49
  68. data/test/inputs/disabled_test.rb +15 -15
  69. data/test/inputs/discovery_test.rb +56 -6
  70. data/test/inputs/file_input_test.rb +2 -2
  71. data/test/inputs/general_test.rb +20 -20
  72. data/test/inputs/grouped_collection_select_input_test.rb +38 -2
  73. data/test/inputs/hidden_input_test.rb +4 -4
  74. data/test/inputs/numeric_input_test.rb +24 -24
  75. data/test/inputs/priority_input_test.rb +7 -7
  76. data/test/inputs/readonly_test.rb +19 -19
  77. data/test/inputs/required_test.rb +13 -13
  78. data/test/inputs/string_input_test.rb +41 -21
  79. data/test/inputs/text_input_test.rb +4 -4
  80. data/test/simple_form_test.rb +8 -0
  81. data/test/support/discovery_inputs.rb +32 -2
  82. data/test/support/misc_helpers.rb +71 -5
  83. data/test/support/models.rb +50 -24
  84. metadata +6 -5
@@ -2,46 +2,46 @@
2
2
  require 'test_helper'
3
3
 
4
4
  class PriorityInputTest < ActionView::TestCase
5
- test 'input should generate a country select field' do
5
+ test 'input generates a country select field' do
6
6
  with_input_for @user, :country, :country
7
7
  assert_select 'select#user_country'
8
8
  assert_select 'select option[value=Brazil]', 'Brazil'
9
9
  assert_no_select 'select option[value=][disabled=disabled]'
10
10
  end
11
11
 
12
- test 'input should generate a country select with SimpleForm default' do
12
+ test 'input generates a country select with SimpleForm default' do
13
13
  swap SimpleForm, country_priority: [ 'Brazil' ] do
14
14
  with_input_for @user, :country, :country
15
15
  assert_select 'select option[value=][disabled=disabled]'
16
16
  end
17
17
  end
18
18
 
19
- test 'input should generate a time zone select field' do
19
+ test 'input generates a time zone select field' do
20
20
  with_input_for @user, :time_zone, :time_zone
21
21
  assert_select 'select#user_time_zone'
22
22
  assert_select 'select option[value=Brasilia]', '(GMT-03:00) Brasilia'
23
23
  assert_no_select 'select option[value=][disabled=disabled]'
24
24
  end
25
25
 
26
- test 'input should generate a time zone select field with default' do
26
+ test 'input generates a time zone select field with default' do
27
27
  with_input_for @user, :time_zone, :time_zone, default: 'Brasilia'
28
28
  assert_select 'select option[value=Brasilia][selected=selected]'
29
29
  assert_no_select 'select option[value=]'
30
30
  end
31
31
 
32
- test 'input should generate a time zone select using options priority' do
32
+ test 'input generates a time zone select using options priority' do
33
33
  with_input_for @user, :time_zone, :time_zone, priority: /Brasilia/
34
34
  assert_select 'select option[value=][disabled=disabled]'
35
35
  assert_no_select 'select option[value=]', /^$/
36
36
  end
37
37
 
38
- test 'priority input should not generate invalid required html attribute' do
38
+ test 'priority input does not generate invalid required html attribute' do
39
39
  with_input_for @user, :country, :country
40
40
  assert_select 'select.required'
41
41
  assert_no_select 'select[required]'
42
42
  end
43
43
 
44
- test 'priority input should not generate invalid aria-required html attribute' do
44
+ test 'priority input does not generate invalid aria-required html attribute' do
45
45
  with_input_for @user, :country, :country
46
46
  assert_select 'select.required'
47
47
  assert_no_select 'select[aria-required]'
@@ -1,98 +1,98 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class ReadonlyTest < ActionView::TestCase
4
- test 'string input should generate readonly elements when readonly option is true' do
4
+ test 'string input generates readonly elements when readonly option is true' do
5
5
  with_input_for @user, :name, :string, readonly: true
6
6
  assert_select 'input.string.readonly[readonly]'
7
7
  end
8
8
 
9
- test 'text input should generate readonly elements when readonly option is true' do
9
+ test 'text input generates readonly elements when readonly option is true' do
10
10
  with_input_for @user, :description, :text, readonly: true
11
11
  assert_select 'textarea.text.readonly[readonly]'
12
12
  end
13
13
 
14
- test 'numeric input should generate readonly elements when readonly option is true' do
14
+ test 'numeric input generates readonly elements when readonly option is true' do
15
15
  with_input_for @user, :age, :integer, readonly: true
16
16
  assert_select 'input.integer.readonly[readonly]'
17
17
  end
18
18
 
19
- test 'date input should generate readonly elements when readonly option is true' do
19
+ test 'date input generates readonly elements when readonly option is true' do
20
20
  with_input_for @user, :born_at, :date, readonly: true
21
21
  assert_select 'select.date.readonly[readonly]'
22
22
  end
23
23
 
24
- test 'datetime input should generate readonly elements when readonly option is true' do
24
+ test 'datetime input generates readonly elements when readonly option is true' do
25
25
  with_input_for @user, :created_at, :datetime, readonly: true
26
26
  assert_select 'select.datetime.readonly[readonly]'
27
27
  end
28
28
 
29
- test 'string input should generate readonly elements when readonly option is false' do
29
+ test 'string input generates readonly elements when readonly option is false' do
30
30
  with_input_for @user, :name, :string, readonly: false
31
31
  assert_no_select 'input.string.readonly[readonly]'
32
32
  end
33
33
 
34
- test 'text input should generate readonly elements when readonly option is false' do
34
+ test 'text input generates readonly elements when readonly option is false' do
35
35
  with_input_for @user, :description, :text, readonly: false
36
36
  assert_no_select 'textarea.text.readonly[readonly]'
37
37
  end
38
38
 
39
- test 'numeric input should generate readonly elements when readonly option is false' do
39
+ test 'numeric input generates readonly elements when readonly option is false' do
40
40
  with_input_for @user, :age, :integer, readonly: false
41
41
  assert_no_select 'input.integer.readonly[readonly]'
42
42
  end
43
43
 
44
- test 'date input should generate readonly elements when readonly option is false' do
44
+ test 'date input generates readonly elements when readonly option is false' do
45
45
  with_input_for @user, :born_at, :date, readonly: false
46
46
  assert_no_select 'select.date.readonly[readonly]'
47
47
  end
48
48
 
49
- test 'datetime input should generate readonly elements when readonly option is false' do
49
+ test 'datetime input generates readonly elements when readonly option is false' do
50
50
  with_input_for @user, :created_at, :datetime, readonly: false
51
51
  assert_no_select 'select.datetime.readonly[readonly]'
52
52
  end
53
53
 
54
- test 'string input should generate readonly elements when readonly option is not present' do
54
+ test 'string input generates readonly elements when readonly option is not present' do
55
55
  with_input_for @user, :name, :string
56
56
  assert_no_select 'input.string.readonly[readonly]'
57
57
  end
58
58
 
59
- test 'text input should generate readonly elements when readonly option is not present' do
59
+ test 'text input generates readonly elements when readonly option is not present' do
60
60
  with_input_for @user, :description, :text
61
61
  assert_no_select 'textarea.text.readonly[readonly]'
62
62
  end
63
63
 
64
- test 'numeric input should generate readonly elements when readonly option is not present' do
64
+ test 'numeric input generates readonly elements when readonly option is not present' do
65
65
  with_input_for @user, :age, :integer
66
66
  assert_no_select 'input.integer.readonly[readonly]'
67
67
  end
68
68
 
69
- test 'date input should generate readonly elements when readonly option is not present' do
69
+ test 'date input generates readonly elements when readonly option is not present' do
70
70
  with_input_for @user, :born_at, :date
71
71
  assert_no_select 'select.date.readonly[readonly]'
72
72
  end
73
73
 
74
- test 'datetime input should generate readonly elements when readonly option is not present' do
74
+ test 'datetime input generates readonly elements when readonly option is not present' do
75
75
  with_input_for @user, :created_at, :datetime
76
76
  assert_no_select 'select.datetime.readonly[readonly]'
77
77
  end
78
78
 
79
- test 'input should generate readonly attribute when the field is readonly and the object is persisted' do
79
+ test 'input generates readonly attribute when the field is readonly and the object is persisted' do
80
80
  with_input_for @user, :credit_card, :string, readonly: :lookup
81
81
  assert_select 'input.string.readonly[readonly]'
82
82
  end
83
83
 
84
- test 'input should not generate readonly attribute when the field is readonly and the object is not persisted' do
84
+ test 'input does not generate readonly attribute when the field is readonly and the object is not persisted' do
85
85
  @user.new_record!
86
86
  with_input_for @user, :credit_card, :string, readonly: :lookup
87
87
  assert_no_select 'input.string.readonly[readonly]'
88
88
  end
89
89
 
90
- test 'input should not generate readonly attribute when the field is not readonly and the object is persisted' do
90
+ test 'input does not generate readonly attribute when the field is not readonly and the object is persisted' do
91
91
  with_input_for @user, :name, :string
92
92
  assert_no_select 'input.string.readonly[readonly]'
93
93
  end
94
94
 
95
- test 'input should not generate readonly attribute when the component is not used' do
95
+ test 'input does not generate readonly attribute when the component is not used' do
96
96
  swap_wrapper do
97
97
  with_input_for @user, :credit_card, :string
98
98
  assert_no_select 'input.string.readonly[readonly]'
@@ -2,26 +2,26 @@ require 'test_helper'
2
2
 
3
3
  class RequiredTest < ActionView::TestCase
4
4
  # REQUIRED AND PRESENCE VALIDATION
5
- test 'builder input should obtain required from ActiveModel::Validations when it is included' do
5
+ test 'builder input obtains required from ActiveModel::Validations when it is included' do
6
6
  with_form_for @validating_user, :name
7
7
  assert_select 'input.required[required]#validating_user_name'
8
8
  with_form_for @validating_user, :status
9
9
  assert_select 'input.optional#validating_user_status'
10
10
  end
11
11
 
12
- test 'builder input should allow overriding required when ActiveModel::Validations is included' do
12
+ test 'builder input allows overriding required when ActiveModel::Validations is included' do
13
13
  with_form_for @validating_user, :name, required: false
14
14
  assert_select 'input.optional#validating_user_name'
15
15
  with_form_for @validating_user, :status, required: true
16
16
  assert_select 'input.required[required]#validating_user_status'
17
17
  end
18
18
 
19
- test 'builder input should be required by default when ActiveModel::Validations is not included' do
19
+ test 'builder input is required by default when ActiveModel::Validations is not included' do
20
20
  with_form_for @user, :name
21
21
  assert_select 'input.required[required]#user_name'
22
22
  end
23
23
 
24
- test 'builder input should not be required by default when ActiveModel::Validations is not included if option is set to false' do
24
+ test 'builder input does not be required by default when ActiveModel::Validations is not included if option is set to false' do
25
25
  swap SimpleForm, required_by_default: false do
26
26
  with_form_for @user, :name
27
27
  assert_select 'input.optional#user_name'
@@ -29,7 +29,7 @@ class RequiredTest < ActionView::TestCase
29
29
  end
30
30
  end
31
31
 
32
- test 'when not using browser validations, input should not generate required html attribute' do
32
+ test 'when not using browser validations, input does not generate required html attribute' do
33
33
  swap SimpleForm, browser_validations: false do
34
34
  with_input_for @user, :name, :string
35
35
  assert_select 'input[type=text].required'
@@ -37,7 +37,7 @@ class RequiredTest < ActionView::TestCase
37
37
  end
38
38
  end
39
39
 
40
- test 'builder input should allow disabling required when ActiveModel::Validations is not included' do
40
+ test 'builder input allows disabling required when ActiveModel::Validations is not included' do
41
41
  with_form_for @user, :name, required: false
42
42
  assert_no_select 'input.required'
43
43
  assert_no_select 'input[required]'
@@ -53,14 +53,14 @@ class RequiredTest < ActionView::TestCase
53
53
  end
54
54
 
55
55
  # VALIDATORS :if :unless
56
- test 'builder input should not be required when ActiveModel::Validations is included and if option is present' do
56
+ test 'builder input does not be required when ActiveModel::Validations is included and if option is present' do
57
57
  with_form_for @validating_user, :age
58
58
  assert_no_select 'input.required'
59
59
  assert_no_select 'input[required]'
60
60
  assert_select 'input.optional#validating_user_age'
61
61
  end
62
62
 
63
- test 'builder input should not be required when ActiveModel::Validations is included and unless option is present' do
63
+ test 'builder input does not be required when ActiveModel::Validations is included and unless option is present' do
64
64
  with_form_for @validating_user, :amount
65
65
  assert_no_select 'input.required'
66
66
  assert_no_select 'input[required]'
@@ -68,7 +68,7 @@ class RequiredTest < ActionView::TestCase
68
68
  end
69
69
 
70
70
  # VALIDATORS :on
71
- test 'builder input should be required when validation is on create and is not persisted' do
71
+ test 'builder input is required when validation is on create and is not persisted' do
72
72
  @validating_user.new_record!
73
73
  with_form_for @validating_user, :action
74
74
  assert_select 'input.required'
@@ -76,14 +76,14 @@ class RequiredTest < ActionView::TestCase
76
76
  assert_select 'input.required[required]#validating_user_action'
77
77
  end
78
78
 
79
- test 'builder input should not be required when validation is on create and is persisted' do
79
+ test 'builder input does not be required when validation is on create and is persisted' do
80
80
  with_form_for @validating_user, :action
81
81
  assert_no_select 'input.required'
82
82
  assert_no_select 'input[required]'
83
83
  assert_select 'input.optional#validating_user_action'
84
84
  end
85
85
 
86
- test 'builder input should be required when validation is on save' do
86
+ test 'builder input is required when validation is on save' do
87
87
  with_form_for @validating_user, :credit_limit
88
88
  assert_select 'input.required'
89
89
  assert_select 'input[required]'
@@ -96,14 +96,14 @@ class RequiredTest < ActionView::TestCase
96
96
  assert_select 'input.required[required]#validating_user_credit_limit'
97
97
  end
98
98
 
99
- test 'builder input should be required when validation is on update and is persisted' do
99
+ test 'builder input is required when validation is on update and is persisted' do
100
100
  with_form_for @validating_user, :phone_number
101
101
  assert_select 'input.required'
102
102
  assert_select 'input[required]'
103
103
  assert_select 'input.required[required]#validating_user_phone_number'
104
104
  end
105
105
 
106
- test 'builder input should not be required when validation is on update and is not persisted' do
106
+ test 'builder input does not be required when validation is on update and is not persisted' do
107
107
  @validating_user.new_record!
108
108
  with_form_for @validating_user, :phone_number
109
109
  assert_no_select 'input.required'
@@ -2,100 +2,100 @@
2
2
  require 'test_helper'
3
3
 
4
4
  class StringInputTest < ActionView::TestCase
5
- test 'input should map text field to string attribute' do
5
+ test 'input maps text field to string attribute' do
6
6
  with_input_for @user, :name, :string
7
7
  assert_select "input#user_name[type=text][name='user[name]'][value=New in SimpleForm!]"
8
8
  end
9
9
 
10
- test 'input should generate a password field for password attributes' do
10
+ test 'input generates a password field for password attributes' do
11
11
  with_input_for @user, :password, :password
12
12
  assert_select "input#user_password.password[type=password][name='user[password]']"
13
13
  end
14
14
 
15
- test 'input should get maxlength from column definition for string attributes' do
15
+ test 'input gets maxlength from column definition for string attributes' do
16
16
  with_input_for @user, :name, :string
17
17
  assert_select 'input.string[maxlength=100]'
18
18
  end
19
19
 
20
- test 'input should not get maxlength from column without size definition for string attributes' do
20
+ test 'input does not get maxlength from column without size definition for string attributes' do
21
21
  with_input_for @user, :action, :string
22
22
  assert_no_select 'input.string[maxlength]'
23
23
  end
24
24
 
25
- test 'input should get maxlength from column definition for password attributes' do
25
+ test 'input gets maxlength from column definition for password attributes' do
26
26
  with_input_for @user, :password, :password
27
27
  assert_select 'input.password[type=password][maxlength=100]'
28
28
  end
29
29
 
30
- test 'input should infer maxlength column definition from validation when present' do
30
+ test 'input infers maxlength column definition from validation when present' do
31
31
  with_input_for @validating_user, :name, :string
32
32
  assert_select 'input.string[maxlength=25]'
33
33
  end
34
34
 
35
- test 'input should not get maxlength from validation when tokenizer present' do
35
+ test 'input does not get maxlength from validation when tokenizer present' do
36
36
  with_input_for @validating_user, :action, :string
37
37
  assert_no_select 'input.string[maxlength]'
38
38
  end
39
39
 
40
- test 'input should get maxlength from validation when :is option present' do
40
+ test 'input gets maxlength from validation when :is option present' do
41
41
  with_input_for @validating_user, :home_picture, :string
42
42
  assert_select 'input.string[maxlength=12]'
43
43
  end
44
44
 
45
- test 'input maxlength should be the column limit plus one to make room for decimal point' do
45
+ test 'input maxlength is the column limit plus one to make room for decimal point' do
46
46
  with_input_for @user, :credit_limit, :string
47
47
 
48
48
  assert_select "input.string[maxlength=16]"
49
49
  end
50
50
 
51
- test 'input should not generate placeholder by default' do
51
+ test 'input does not generate placeholder by default' do
52
52
  with_input_for @user, :name, :string
53
53
  assert_no_select 'input[placeholder]'
54
54
  end
55
55
 
56
- test 'input should accept the placeholder option' do
56
+ test 'input accepts the placeholder option' do
57
57
  with_input_for @user, :name, :string, placeholder: 'Put in some text'
58
58
  assert_select 'input.string[placeholder=Put in some text]'
59
59
  end
60
60
 
61
- test 'input should generate a password field for password attributes that accept placeholder' do
61
+ test 'input generates a password field for password attributes that accept placeholder' do
62
62
  with_input_for @user, :password, :password, placeholder: 'Password Confirmation'
63
63
  assert_select 'input[type=password].password[placeholder=Password Confirmation]#user_password'
64
64
  end
65
65
 
66
- test 'input should not infer pattern from attributes by default' do
66
+ test 'input does not infer pattern from attributes by default' do
67
67
  with_input_for @other_validating_user, :country, :string
68
68
  assert_no_select 'input[pattern="\w+"]'
69
69
  end
70
70
 
71
- test 'input should infer pattern from attributes' do
71
+ test 'input infers pattern from attributes' do
72
72
  with_input_for @other_validating_user, :country, :string, pattern: true
73
73
  assert_select 'input[pattern="\w+"]'
74
74
  end
75
75
 
76
- test 'input should infer pattern from attributes using proc' do
76
+ test 'input infers pattern from attributes using proc' do
77
77
  with_input_for @other_validating_user, :name, :string, pattern: true
78
78
  assert_select 'input[pattern="\w+"]'
79
79
  end
80
80
 
81
- test 'input should not infer pattern from attributes if root default is false' do
81
+ test 'input does not infer pattern from attributes if root default is false' do
82
82
  swap_wrapper do
83
83
  with_input_for @other_validating_user, :country, :string
84
84
  assert_no_select 'input[pattern="\w+"]'
85
85
  end
86
86
  end
87
87
 
88
- test 'input should use given pattern from attributes' do
88
+ test 'input uses given pattern from attributes' do
89
89
  with_input_for @other_validating_user, :country, :string, input_html: { pattern: "\\d+" }
90
90
  assert_select 'input[pattern="\d+"]'
91
91
  end
92
92
 
93
- test 'input should not use pattern if model has :without validation option' do
93
+ test 'input does not use pattern if model has :without validation option' do
94
94
  with_input_for @other_validating_user, :description, :string, pattern: true
95
95
  assert_no_select 'input[pattern="\d+"]'
96
96
  end
97
97
 
98
- test 'input should use i18n to translate placeholder text' do
98
+ test 'input uses i18n to translate placeholder text' do
99
99
  store_translations(:en, simple_form: { placeholders: { user: {
100
100
  name: 'Name goes here'
101
101
  } } }) do
@@ -104,14 +104,34 @@ class StringInputTest < ActionView::TestCase
104
104
  end
105
105
  end
106
106
 
107
+ test 'input uses custom i18n scope to translate placeholder text' do
108
+ store_translations(:en, my_scope: { placeholders: { user: {
109
+ name: 'Name goes here'
110
+ } } }) do
111
+ swap SimpleForm, i18n_scope: :my_scope do
112
+ with_input_for @user, :name, :string
113
+ assert_select 'input.string[placeholder=Name goes here]'
114
+ end
115
+ end
116
+ end
117
+
118
+ test 'input translates a key prefixed with _html and return the html markup' do
119
+ store_translations(:en, simple_form: { labels: { user: {
120
+ name_html: '<b>Name</b>'
121
+ } } }) do
122
+ with_input_for @user, :name, :string
123
+ assert_select 'label b', 'Name'
124
+ end
125
+ end
126
+
107
127
  [:email, :url, :search, :tel].each do |type|
108
- test "input should allow type #{type}" do
128
+ test "input allows type #{type}" do
109
129
  with_input_for @user, :name, type
110
130
  assert_select "input.string.#{type}"
111
131
  assert_select "input[type=#{type}]"
112
132
  end
113
133
 
114
- test "input should not allow type #{type} if HTML5 compatibility is disabled" do
134
+ test "input does not allow type #{type} if HTML5 compatibility is disabled" do
115
135
  swap_wrapper do
116
136
  with_input_for @user, :name, type
117
137
  assert_select "input[type=text]"
@@ -2,22 +2,22 @@
2
2
  require 'test_helper'
3
3
 
4
4
  class TextInputTest < ActionView::TestCase
5
- test 'input should generate a text area for text attributes' do
5
+ test 'input generates a text area for text attributes' do
6
6
  with_input_for @user, :description, :text
7
7
  assert_select 'textarea.text#user_description'
8
8
  end
9
9
 
10
- test 'input should generate a text area for text attributes that accept placeholder' do
10
+ test 'input generates a text area for text attributes that accept placeholder' do
11
11
  with_input_for @user, :description, :text, placeholder: 'Put in some text'
12
12
  assert_select 'textarea.text[placeholder=Put in some text]'
13
13
  end
14
14
 
15
- test 'input should get maxlength from column definition for text attributes' do
15
+ test 'input gets maxlength from column definition for text attributes' do
16
16
  with_input_for @user, :description, :text
17
17
  assert_select 'textarea.text[maxlength=200]'
18
18
  end
19
19
 
20
- test 'input should infer maxlength column definition from validation when present for text attributes' do
20
+ test 'input infers maxlength column definition from validation when present for text attributes' do
21
21
  with_input_for @validating_user, :description, :text
22
22
  assert_select 'textarea.text[maxlength=50]'
23
23
  end
@@ -6,4 +6,12 @@ class SimpleFormTest < ActiveSupport::TestCase
6
6
  assert_equal SimpleForm, config
7
7
  end
8
8
  end
9
+
10
+ test 'setup block configure Simple Form' do
11
+ SimpleForm.setup do |config|
12
+ assert_equal SimpleForm, config
13
+ end
14
+
15
+ assert_equal true, SimpleForm.configured?
16
+ end
9
17
  end
@@ -1,16 +1,26 @@
1
1
  class StringInput < SimpleForm::Inputs::StringInput
2
- def input
2
+ def input(wrapper_options = nil)
3
3
  "<section>#{super}</section>".html_safe
4
4
  end
5
5
  end
6
6
 
7
7
  class NumericInput < SimpleForm::Inputs::NumericInput
8
- def input
8
+ def input(wrapper_options = nil)
9
9
  "<section>#{super}</section>".html_safe
10
10
  end
11
11
  end
12
12
 
13
13
  class CustomizedInput < SimpleForm::Inputs::StringInput
14
+ def input(wrapper_options = nil)
15
+ "<section>#{super}</section>".html_safe
16
+ end
17
+
18
+ def input_method
19
+ :text_field
20
+ end
21
+ end
22
+
23
+ class DeprecatedInput < SimpleForm::Inputs::StringInput
14
24
  def input
15
25
  "<section>#{super}</section>".html_safe
16
26
  end
@@ -25,3 +35,23 @@ class CollectionSelectInput < SimpleForm::Inputs::CollectionSelectInput
25
35
  super.push('chosen')
26
36
  end
27
37
  end
38
+
39
+ module CustomInputs
40
+ class CustomizedInput < SimpleForm::Inputs::StringInput
41
+ def input_html_classes
42
+ super.push('customized-namespace-custom-input')
43
+ end
44
+ end
45
+
46
+ class PasswordInput < SimpleForm::Inputs::PasswordInput
47
+ def input_html_classes
48
+ super.push('password-custom-input')
49
+ end
50
+ end
51
+
52
+ class NumericInput < SimpleForm::Inputs::PasswordInput
53
+ def input_html_classes
54
+ super.push('numeric-custom-input')
55
+ end
56
+ end
57
+ end
@@ -46,12 +46,12 @@ module MiscHelpers
46
46
  end
47
47
  end
48
48
 
49
- def swap_wrapper(name=:default, wrapper=self.custom_wrapper)
50
- old = SimpleForm.wrappers[name]
51
- SimpleForm.wrappers[name] = wrapper
49
+ def swap_wrapper(name = :default, wrapper = self.custom_wrapper)
50
+ old = SimpleForm.wrappers[name.to_s]
51
+ SimpleForm.wrappers[name.to_s] = wrapper
52
52
  yield
53
53
  ensure
54
- SimpleForm.wrappers[name] = old
54
+ SimpleForm.wrappers[name.to_s] = old
55
55
  end
56
56
 
57
57
  def custom_wrapper
@@ -68,6 +68,48 @@ module MiscHelpers
68
68
  end
69
69
  end
70
70
 
71
+ def custom_wrapper_with_wrapped_optional_component
72
+ SimpleForm.build tag: :section, class: "custom_wrapper" do |b|
73
+ b.wrapper tag: :div, class: 'no_output_wrapper' do |ba|
74
+ ba.optional :hint, wrap_with: { tag: :p, class: 'omg_hint' }
75
+ end
76
+ end
77
+ end
78
+
79
+ def custom_wrapper_with_unless_blank
80
+ SimpleForm.build tag: :section, class: "custom_wrapper" do |b|
81
+ b.wrapper tag: :div, class: 'no_output_wrapper', unless_blank: true do |ba|
82
+ ba.optional :hint, wrap_with: { tag: :p, class: 'omg_hint' }
83
+ end
84
+ end
85
+ end
86
+
87
+ def custom_wrapper_with_input_class
88
+ SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
89
+ b.use :label
90
+ b.use :input, class: 'inline-class'
91
+ end
92
+ end
93
+
94
+ def custom_wrapper_with_label_class
95
+ SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
96
+ b.use :label, class: 'inline-class'
97
+ b.use :input
98
+ end
99
+ end
100
+
101
+ def custom_wrapper_with_input_attributes
102
+ SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
103
+ b.use :input, data: { modal: true }
104
+ end
105
+ end
106
+
107
+ def custom_wrapper_with_label_input_class
108
+ SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
109
+ b.use :label_input, class: 'inline-class'
110
+ end
111
+ end
112
+
71
113
  def custom_wrapper_with_wrapped_input
72
114
  SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
73
115
  b.wrapper tag: :div, class: 'elem' do |component|
@@ -112,6 +154,30 @@ module MiscHelpers
112
154
  end
113
155
  end
114
156
 
157
+ def custom_wrapper_with_additional_attributes
158
+ SimpleForm.build tag: :div, class: 'custom_wrapper', html: { data: { wrapper: :test }, title: 'some title' } do |b|
159
+ b.use :label_input
160
+ end
161
+ end
162
+
163
+ def custom_wrapper_with_full_error
164
+ SimpleForm.build tag: :div, class: 'custom_wrapper' do |b|
165
+ b.use :full_error, wrap_with: { tag: :span, class: :error }
166
+ end
167
+ end
168
+
169
+ def custom_wrapper_with_label_text
170
+ SimpleForm.build :label_text => proc { |label, required| "**#{label}**" } do |b|
171
+ b.use :label_input
172
+ end
173
+ end
174
+
175
+ def custom_wrapper_with_custom_label_component
176
+ SimpleForm.build tag: :span, class: 'custom_wrapper' do |b|
177
+ b.use :label_text
178
+ end
179
+ end
180
+
115
181
  def custom_form_for(object, *args, &block)
116
182
  simple_form_for(object, *args, { builder: CustomFormBuilder }, &block)
117
183
  end
@@ -142,7 +208,7 @@ module MiscHelpers
142
208
  end
143
209
  end
144
210
 
145
- def with_input_for(object, attribute_name, type, options={})
211
+ def with_input_for(object, attribute_name, type, options = {})
146
212
  with_concat_form_for(object) do |f|
147
213
  f.input(attribute_name, options.merge(as: type))
148
214
  end