simple_form 2.0.0 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of simple_form might be problematic. Click here for more details.

Files changed (103) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +97 -198
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +572 -296
  5. data/lib/generators/simple_form/install_generator.rb +17 -7
  6. data/lib/generators/simple_form/templates/README +3 -4
  7. data/lib/generators/simple_form/templates/_form.html.erb +1 -0
  8. data/lib/generators/simple_form/templates/_form.html.haml +1 -0
  9. data/lib/generators/simple_form/templates/config/initializers/{simple_form.rb.tt → simple_form.rb} +57 -63
  10. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +155 -0
  11. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +111 -0
  12. data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +14 -7
  13. data/lib/simple_form/action_view_extensions/builder.rb +5 -305
  14. data/lib/simple_form/action_view_extensions/form_helper.rb +18 -20
  15. data/lib/simple_form/components/errors.rb +30 -3
  16. data/lib/simple_form/components/hints.rb +10 -3
  17. data/lib/simple_form/components/html5.rb +17 -3
  18. data/lib/simple_form/components/label_input.rb +21 -2
  19. data/lib/simple_form/components/labels.rb +16 -11
  20. data/lib/simple_form/components/maxlength.rb +19 -12
  21. data/lib/simple_form/components/min_max.rb +4 -2
  22. data/lib/simple_form/components/minlength.rb +48 -0
  23. data/lib/simple_form/components/pattern.rb +5 -4
  24. data/lib/simple_form/components/placeholders.rb +3 -2
  25. data/lib/simple_form/components/readonly.rb +3 -2
  26. data/lib/simple_form/components.rb +15 -11
  27. data/lib/simple_form/error_notification.rb +4 -3
  28. data/lib/simple_form/form_builder.rb +283 -105
  29. data/lib/simple_form/helpers/autofocus.rb +1 -0
  30. data/lib/simple_form/helpers/disabled.rb +1 -0
  31. data/lib/simple_form/helpers/readonly.rb +1 -0
  32. data/lib/simple_form/helpers/required.rb +1 -0
  33. data/lib/simple_form/helpers/validators.rb +4 -3
  34. data/lib/simple_form/helpers.rb +7 -6
  35. data/lib/simple_form/i18n_cache.rb +1 -0
  36. data/lib/simple_form/inputs/base.rb +76 -23
  37. data/lib/simple_form/inputs/block_input.rb +3 -2
  38. data/lib/simple_form/inputs/boolean_input.rb +55 -16
  39. data/lib/simple_form/inputs/collection_check_boxes_input.rb +2 -1
  40. data/lib/simple_form/inputs/collection_input.rb +41 -18
  41. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +11 -19
  42. data/lib/simple_form/inputs/collection_select_input.rb +5 -2
  43. data/lib/simple_form/inputs/date_time_input.rb +23 -12
  44. data/lib/simple_form/inputs/file_input.rb +5 -2
  45. data/lib/simple_form/inputs/grouped_collection_select_input.rb +16 -3
  46. data/lib/simple_form/inputs/hidden_input.rb +5 -2
  47. data/lib/simple_form/inputs/numeric_input.rb +4 -8
  48. data/lib/simple_form/inputs/password_input.rb +6 -4
  49. data/lib/simple_form/inputs/priority_input.rb +5 -2
  50. data/lib/simple_form/inputs/range_input.rb +2 -1
  51. data/lib/simple_form/inputs/string_input.rb +6 -4
  52. data/lib/simple_form/inputs/text_input.rb +6 -3
  53. data/lib/simple_form/inputs.rb +20 -17
  54. data/lib/simple_form/map_type.rb +1 -0
  55. data/lib/simple_form/railtie.rb +15 -0
  56. data/lib/simple_form/tags.rb +69 -0
  57. data/lib/simple_form/version.rb +2 -1
  58. data/lib/simple_form/wrappers/builder.rb +12 -35
  59. data/lib/simple_form/wrappers/leaf.rb +29 -0
  60. data/lib/simple_form/wrappers/many.rb +12 -7
  61. data/lib/simple_form/wrappers/root.rb +7 -4
  62. data/lib/simple_form/wrappers/single.rb +12 -3
  63. data/lib/simple_form/wrappers.rb +3 -1
  64. data/lib/simple_form.rb +118 -63
  65. data/test/action_view_extensions/builder_test.rb +230 -164
  66. data/test/action_view_extensions/form_helper_test.rb +107 -39
  67. data/test/components/label_test.rb +105 -87
  68. data/test/form_builder/association_test.rb +131 -62
  69. data/test/form_builder/button_test.rb +15 -14
  70. data/test/form_builder/error_notification_test.rb +11 -10
  71. data/test/form_builder/error_test.rb +188 -34
  72. data/test/form_builder/general_test.rb +247 -102
  73. data/test/form_builder/hint_test.rb +59 -32
  74. data/test/form_builder/input_field_test.rb +138 -25
  75. data/test/form_builder/label_test.rb +84 -13
  76. data/test/form_builder/wrapper_test.rb +236 -33
  77. data/test/generators/simple_form_generator_test.rb +15 -4
  78. data/test/inputs/boolean_input_test.rb +147 -13
  79. data/test/inputs/collection_check_boxes_input_test.rb +166 -71
  80. data/test/inputs/collection_radio_buttons_input_test.rb +229 -113
  81. data/test/inputs/collection_select_input_test.rb +222 -85
  82. data/test/inputs/datetime_input_test.rb +134 -47
  83. data/test/inputs/disabled_test.rb +62 -21
  84. data/test/inputs/discovery_test.rb +70 -10
  85. data/test/inputs/file_input_test.rb +4 -3
  86. data/test/inputs/general_test.rb +90 -26
  87. data/test/inputs/grouped_collection_select_input_test.rb +88 -23
  88. data/test/inputs/hidden_input_test.rb +7 -5
  89. data/test/inputs/numeric_input_test.rb +56 -46
  90. data/test/inputs/priority_input_test.rb +31 -16
  91. data/test/inputs/readonly_test.rb +68 -27
  92. data/test/inputs/required_test.rb +63 -18
  93. data/test/inputs/string_input_test.rb +76 -51
  94. data/test/inputs/text_input_test.rb +21 -8
  95. data/test/simple_form_test.rb +9 -0
  96. data/test/support/discovery_inputs.rb +39 -2
  97. data/test/support/misc_helpers.rb +176 -20
  98. data/test/support/mock_controller.rb +13 -7
  99. data/test/support/models.rb +187 -71
  100. data/test/test_helper.rb +38 -39
  101. metadata +53 -39
  102. data/lib/simple_form/core_ext/hash.rb +0 -16
  103. data/test/support/mock_response.rb +0 -14
@@ -1,19 +1,75 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: UTF-8
2
3
  require 'test_helper'
3
4
 
4
- # Tests for all different kinds of inputs.
5
- class DateTimeInputTest < ActionView::TestCase
6
- # DateTime input
7
- test 'input should generate a datetime select by default for datetime attributes' do
5
+ # Tests for datetime, date and time inputs when HTML5 compatibility is enabled in the wrapper.
6
+ class DateTimeInputWithHtml5Test < ActionView::TestCase
7
+ test 'input generates a datetime input for datetime attributes if HTML5 compatibility is explicitly enbled' do
8
+ with_input_for @user, :created_at, :datetime, html5: true
9
+
10
+ if ActionPack::VERSION::STRING >= '5'
11
+ assert_select 'input[type="datetime-local"]'
12
+ elsif ActionPack::VERSION::STRING < '5'
13
+ assert_select 'input[type="datetime"]'
14
+ end
15
+ end
16
+
17
+ test 'input generates a datetime select for datetime attributes' do
8
18
  with_input_for @user, :created_at, :datetime
9
- 1.upto(5) do |i|
10
- assert_select "form select.datetime#user_created_at_#{i}i"
19
+
20
+ assert_select 'select.datetime'
21
+ end
22
+
23
+ test 'input generates a date input for date attributes if HTML5 compatibility is explicitly enbled' do
24
+ with_input_for @user, :born_at, :date, html5: true
25
+
26
+ assert_select 'input[type="date"]'
27
+ end
28
+
29
+ test 'input generates a date select for date attributes' do
30
+ with_input_for @user, :born_at, :date
31
+
32
+ assert_select 'select.date'
33
+ end
34
+
35
+ test 'input generates a time input for time attributes if HTML5 compatibility is explicitly enbled' do
36
+ with_input_for @user, :delivery_time, :time, html5: true
37
+
38
+ assert_select 'input[type="time"]'
39
+ end
40
+
41
+ test 'input generates a time select for time attributes' do
42
+ with_input_for @user, :delivery_time, :time
43
+
44
+ assert_select 'select.time'
45
+ end
46
+
47
+ test 'input generates required html attribute' do
48
+ with_input_for @user, :delivery_time, :time, required: true, html5: true
49
+ assert_select 'input.required'
50
+ assert_select 'input[required]'
51
+ end
52
+
53
+ test 'input has an aria-required html attribute' do
54
+ with_input_for @user, :delivery_time, :time, required: true, html5: true
55
+ assert_select 'input[aria-required=true]'
56
+ end
57
+ end
58
+
59
+ # Tests for datetime, date and time inputs when HTML5 compatibility is enabled in the wrapper.
60
+ class DateTimeInputWithoutHtml5Test < ActionView::TestCase
61
+ test 'input generates a datetime select by default for datetime attributes' do
62
+ swap_wrapper do
63
+ with_input_for @user, :created_at, :datetime
64
+ 1.upto(5) do |i|
65
+ assert_select "form select.datetime#user_created_at_#{i}i"
66
+ end
11
67
  end
12
68
  end
13
69
 
14
- test 'input should be able to pass options to datetime select' do
15
- with_input_for @user, :created_at, :datetime,
16
- :disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
70
+ test 'input is able to pass options to datetime select' do
71
+ with_input_for @user, :created_at, :datetime, html5: false,
72
+ disabled: true, prompt: { year: 'ano', month: 'mês', day: 'dia' }
17
73
 
18
74
  assert_select 'select.datetime[disabled=disabled]'
19
75
  assert_select 'select.datetime option', 'ano'
@@ -21,17 +77,31 @@ class DateTimeInputTest < ActionView::TestCase
21
77
  assert_select 'select.datetime option', 'dia'
22
78
  end
23
79
 
24
- test 'input should generate a date select for date attributes' do
25
- with_input_for @user, :born_at, :date
26
- assert_select 'select.date#user_born_at_1i'
27
- assert_select 'select.date#user_born_at_2i'
28
- assert_select 'select.date#user_born_at_3i'
29
- assert_no_select 'select.date#user_born_at_4i'
80
+ test 'input generates a datetime input for datetime attributes if HTML5 compatibility is explicitly enabled' do
81
+ swap_wrapper do
82
+ with_input_for @user, :created_at, :datetime, html5: true
83
+
84
+ if ActionPack::VERSION::STRING >= '5'
85
+ assert_select 'input[type="datetime-local"]'
86
+ elsif ActionPack::VERSION::STRING < '5'
87
+ assert_select 'input[type="datetime"]'
88
+ end
89
+ end
90
+ end
91
+
92
+ test 'input generates a date select for date attributes' do
93
+ swap_wrapper do
94
+ with_input_for @user, :born_at, :date
95
+ assert_select 'select.date#user_born_at_1i'
96
+ assert_select 'select.date#user_born_at_2i'
97
+ assert_select 'select.date#user_born_at_3i'
98
+ assert_no_select 'select.date#user_born_at_4i'
99
+ end
30
100
  end
31
101
 
32
- test 'input should be able to pass options to date select' do
33
- with_input_for @user, :born_at, :date, :as => :date,
34
- :disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
102
+ test 'input is able to pass options to date select' do
103
+ with_input_for @user, :born_at, :date, as: :date, html5: false,
104
+ disabled: true, prompt: { year: 'ano', month: 'mês', day: 'dia' }
35
105
 
36
106
  assert_select 'select.date[disabled=disabled]'
37
107
  assert_select 'select.date option', 'ano'
@@ -39,61 +109,78 @@ class DateTimeInputTest < ActionView::TestCase
39
109
  assert_select 'select.date option', 'dia'
40
110
  end
41
111
 
42
- test 'input should be able to pass :default to date select' do
43
- with_input_for @user, :born_at, :date, :default => Date.today
44
- assert_select "select.date option[value=#{Date.today.year}][selected=selected]"
112
+ test 'input is able to pass :default to date select' do
113
+ with_input_for @user, :born_at, :date, default: Date.today, html5: false
114
+ assert_select "select.date option[value='#{Date.today.year}'][selected=selected]"
45
115
  end
46
116
 
47
- test 'input should generate a time select for time attributes' do
48
- with_input_for @user, :delivery_time, :time
49
- assert_select 'input[type=hidden]#user_delivery_time_1i'
50
- assert_select 'input[type=hidden]#user_delivery_time_2i'
51
- assert_select 'input[type=hidden]#user_delivery_time_3i'
52
- assert_select 'select.time#user_delivery_time_4i'
53
- assert_select 'select.time#user_delivery_time_5i'
117
+ test 'input generates a date input for date attributes if HTML5 compatibility is explicitly enabled' do
118
+ swap_wrapper do
119
+ with_input_for @user, :born_at, :date, html5: true
120
+
121
+ assert_select 'input[type="date"]'
122
+ end
123
+ end
124
+
125
+ test 'input generates a time select for time attributes' do
126
+ swap_wrapper do
127
+ with_input_for @user, :delivery_time, :time
128
+ assert_select 'input[type=hidden]#user_delivery_time_1i'
129
+ assert_select 'input[type=hidden]#user_delivery_time_2i'
130
+ assert_select 'input[type=hidden]#user_delivery_time_3i'
131
+ assert_select 'select.time#user_delivery_time_4i'
132
+ assert_select 'select.time#user_delivery_time_5i'
133
+ end
54
134
  end
55
135
 
56
- test 'input should be able to pass options to time select' do
57
- with_input_for @user, :delivery_time, :time, :required => true,
58
- :disabled => true, :prompt => { :hour => 'hora', :minute => 'minuto' }
136
+ test 'input is able to pass options to time select' do
137
+ with_input_for @user, :delivery_time, :time, required: true, html5: false,
138
+ disabled: true, prompt: { hour: 'hora', minute: 'minuto' }
59
139
 
60
140
  assert_select 'select.time[disabled=disabled]'
61
141
  assert_select 'select.time option', 'hora'
62
142
  assert_select 'select.time option', 'minuto'
63
143
  end
64
144
 
65
- test 'label should use i18n to get target for date input type' do
66
- store_translations(:en, :date => { :order => [:month, :day, :year] }) do
67
- with_input_for :project, :created_at, :date
145
+ test 'input generates a time input for time attributes if HTML5 compatibility is explicitly enabled' do
146
+ swap_wrapper do
147
+ with_input_for @user, :delivery_time, :time, html5: true
148
+
149
+ assert_select 'input[type="time"]'
150
+ end
151
+ end
152
+
153
+ test 'label uses i18n to get target for date input type' do
154
+ store_translations(:en, date: { order: %w[month day year] }) do
155
+ with_input_for :project, :created_at, :date, html5: false
68
156
  assert_select 'label[for=project_created_at_2i]'
69
157
  end
70
158
  end
71
159
 
72
- test 'label should use i18n to get target for datetime input type' do
73
- store_translations(:en, :date => { :order => [:month, :day, :year] }) do
74
- with_input_for :project, :created_at, :datetime
160
+ test 'label uses i18n to get target for datetime input type' do
161
+ store_translations(:en, date: { order: %w[month day year] }) do
162
+ with_input_for :project, :created_at, :datetime, html5: false
75
163
  assert_select 'label[for=project_created_at_2i]'
76
164
  end
77
165
  end
78
166
 
79
- test 'label should use order to get target when date input type' do
80
- with_input_for :project, :created_at, :date, :order => [:month, :year, :day]
167
+ test 'label uses order to get target when date input type' do
168
+ with_input_for :project, :created_at, :date, order: %w[month year day], html5: false
81
169
  assert_select 'label[for=project_created_at_2i]'
82
170
  end
83
171
 
84
- test 'label should use order to get target when datetime input type' do
85
- with_input_for :project, :created_at, :datetime, :order => [:month, :year, :day]
172
+ test 'label uses order to get target when datetime input type' do
173
+ with_input_for :project, :created_at, :datetime, order: %w[month year day], html5: false
86
174
  assert_select 'label[for=project_created_at_2i]'
87
175
  end
88
176
 
89
- test 'label should point to first option when time input type' do
90
- with_input_for :project, :created_at, :time
177
+ test 'label points to first option when time input type' do
178
+ with_input_for :project, :created_at, :time, html5: false
91
179
  assert_select 'label[for=project_created_at_4i]'
92
180
  end
93
181
 
94
- test 'date time input should not generate invalid required html attribute' do
95
- with_input_for @user, :delivery_time, :time, :required => true
96
- assert_select 'select.required'
97
- assert_no_select 'select[required]'
182
+ test 'label points to attribute name if HTML5 compatibility is explicitly enabled' do
183
+ with_input_for :project, :created_at, :date, html5: true
184
+ assert_select 'label[for=project_created_at]'
98
185
  end
99
186
  end
@@ -1,38 +1,79 @@
1
+ # frozen_string_literal: true
1
2
  require 'test_helper'
2
3
 
3
4
  class DisabledTest < ActionView::TestCase
4
- test 'input should generate disabled elements based on the disabled option' do
5
- with_input_for @user, :name, :string, :disabled => true
5
+ test 'string input is disabled when disabled option is true' do
6
+ with_input_for @user, :name, :string, disabled: true
6
7
  assert_select 'input.string.disabled[disabled]'
7
- with_input_for @user, :description, :text, :disabled => true
8
+ end
9
+
10
+ test 'text input is disabled when disabled option is true' do
11
+ with_input_for @user, :description, :text, disabled: true
8
12
  assert_select 'textarea.text.disabled[disabled]'
9
- with_input_for @user, :age, :integer, :disabled => true
13
+ end
14
+
15
+ test 'numeric input is disabled when disabled option is true' do
16
+ with_input_for @user, :age, :integer, disabled: true
10
17
  assert_select 'input.integer.disabled[disabled]'
11
- with_input_for @user, :born_at, :date, :disabled => true
18
+ end
19
+
20
+ test 'date input is disabled when disabled option is true' do
21
+ with_input_for @user, :born_at, :date, disabled: true
12
22
  assert_select 'select.date.disabled[disabled]'
13
- with_input_for @user, :created_at, :datetime, :disabled => true
23
+ end
24
+
25
+ test 'datetime input is disabled when disabled option is true' do
26
+ with_input_for @user, :created_at, :datetime, disabled: true
14
27
  assert_select 'select.datetime.disabled[disabled]'
28
+ end
29
+
30
+ test 'string input does not be disabled when disabled option is false' do
31
+ with_input_for @user, :name, :string, disabled: false
32
+ assert_no_select 'input.string.disabled[disabled]'
33
+ end
34
+
35
+ test 'text input does not be disabled when disabled option is false' do
36
+ with_input_for @user, :description, :text, disabled: false
37
+ assert_no_select 'textarea.text.disabled[disabled]'
38
+ end
15
39
 
16
- with_input_for @user, :name, :string, :disabled => false
17
- assert_select 'input.string:not(.disabled[disabled])'
18
- with_input_for @user, :description, :text, :disabled => false
19
- assert_select 'textarea.text:not(.disabled[disabled])'
20
- with_input_for @user, :age, :integer, :disabled => false
21
- assert_select 'input.integer:not(.disabled[disabled])'
22
- with_input_for @user, :born_at, :date, :disabled => false
23
- assert_select 'select.date:not(.disabled[disabled])'
24
- with_input_for @user, :created_at, :datetime, :disabled => false
25
- assert_select 'select.datetime:not(.disabled[disabled])'
40
+ test 'numeric input does not be disabled when disabled option is false' do
41
+ with_input_for @user, :age, :integer, disabled: false
42
+ assert_no_select 'input.integer.disabled[disabled]'
43
+ end
44
+
45
+ test 'date input does not be disabled when disabled option is false' do
46
+ with_input_for @user, :born_at, :date, disabled: false
47
+ assert_no_select 'select.date.disabled[disabled]'
48
+ end
26
49
 
50
+ test 'datetime input does not be disabled when disabled option is false' do
51
+ with_input_for @user, :created_at, :datetime, disabled: false
52
+ assert_no_select 'select.datetime.disabled[disabled]'
53
+ end
54
+
55
+ test 'string input does not be disabled when disabled option is not present' do
27
56
  with_input_for @user, :name, :string
28
- assert_select 'input.string:not(.disabled[disabled])'
57
+ assert_no_select 'input.string.disabled[disabled]'
58
+ end
59
+
60
+ test 'text input does not be disabled when disabled option is not present' do
29
61
  with_input_for @user, :description, :text
30
- assert_select 'textarea.text:not(.disabled[disabled])'
62
+ assert_no_select 'textarea.text.disabled[disabled]'
63
+ end
64
+
65
+ test 'numeric input does not be disabled when disabled option is not present' do
31
66
  with_input_for @user, :age, :integer
32
- assert_select 'input.integer:not(.disabled[disabled])'
67
+ assert_no_select 'input.integer.disabled[disabled]'
68
+ end
69
+
70
+ test 'date input does not be disabled when disabled option is not present' do
33
71
  with_input_for @user, :born_at, :date
34
- assert_select 'select.date:not(.disabled[disabled])'
72
+ assert_no_select 'select.date.disabled[disabled]'
73
+ end
74
+
75
+ test 'datetime input does not be disabled when disabled option is not present' do
35
76
  with_input_for @user, :created_at, :datetime
36
- assert_select 'select.datetime:not(.disabled[disabled])'
77
+ assert_no_select 'select.datetime.disabled[disabled]'
37
78
  end
38
79
  end
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
1
2
  require 'test_helper'
2
3
 
3
4
  class DiscoveryTest < ActionView::TestCase
4
5
  # Setup new inputs and remove them after the test.
5
- def discovery(value=false)
6
- swap SimpleForm, :cache_discovery => value do
6
+ def discovery(value = false)
7
+ swap SimpleForm, cache_discovery: value do
7
8
  begin
8
9
  load "support/discovery_inputs.rb"
9
10
  yield
@@ -12,11 +13,16 @@ class DiscoveryTest < ActionView::TestCase
12
13
  Object.send :remove_const, :StringInput
13
14
  Object.send :remove_const, :NumericInput
14
15
  Object.send :remove_const, :CustomizedInput
16
+ Object.send :remove_const, :DeprecatedInput
17
+ Object.send :remove_const, :CollectionSelectInput
18
+ CustomInputs.send :remove_const, :CustomizedInput
19
+ CustomInputs.send :remove_const, :PasswordInput
20
+ CustomInputs.send :remove_const, :NumericInput
15
21
  end
16
22
  end
17
23
  end
18
24
 
19
- test 'builder should not discover new inputs if cached' do
25
+ test 'builder does not discover new inputs if cached' do
20
26
  with_form_for @user, :name
21
27
  assert_select 'form input#user_name.string'
22
28
 
@@ -26,18 +32,18 @@ class DiscoveryTest < ActionView::TestCase
26
32
  end
27
33
  end
28
34
 
29
- test 'builder should discover new inputs' do
35
+ test 'builder discovers new inputs' do
30
36
  discovery do
31
- with_form_for @user, :name, :as => :customized
37
+ with_form_for @user, :name, as: :customized
32
38
  assert_select 'form section input#user_name.string'
33
39
  end
34
40
  end
35
41
 
36
- test 'builder should not discover new inputs if discovery is off' do
42
+ test 'builder does not discover new inputs if discovery is off' do
37
43
  with_form_for @user, :name
38
44
  assert_select 'form input#user_name.string'
39
45
 
40
- swap SimpleForm, :inputs_discovery => false do
46
+ swap SimpleForm, inputs_discovery: false do
41
47
  discovery do
42
48
  with_form_for @user, :name
43
49
  assert_no_select 'form section input#user_name.string'
@@ -45,17 +51,71 @@ class DiscoveryTest < ActionView::TestCase
45
51
  end
46
52
  end
47
53
 
48
- test 'builder should discover new inputs from mappings if not cached' do
54
+ test 'builder discovers new inputs from mappings if not cached' do
49
55
  discovery do
50
56
  with_form_for @user, :name
51
57
  assert_select 'form section input#user_name.string'
52
58
  end
53
59
  end
54
60
 
55
- test 'builder should discover new inputs from internal fallbacks if not cached' do
61
+ test 'builder discovers new inputs from internal fallbacks if not cached' do
56
62
  discovery do
57
63
  with_form_for @user, :age
58
64
  assert_select 'form section input#user_age.numeric.integer'
59
65
  end
60
66
  end
61
- end
67
+
68
+ test 'builder discovers new maped inputs from configured namespaces if not cached' do
69
+ discovery do
70
+ swap SimpleForm, custom_inputs_namespaces: ['CustomInputs'] do
71
+ with_form_for @user, :password
72
+ assert_select 'form input#user_password.password-custom-input'
73
+ end
74
+ end
75
+ end
76
+
77
+ test 'builder discovers new maped inputs from configured namespaces before the ones from top level namespace' do
78
+ discovery do
79
+ swap SimpleForm, custom_inputs_namespaces: ['CustomInputs'] do
80
+ with_form_for @user, :age
81
+ assert_select 'form input#user_age.numeric-custom-input'
82
+ end
83
+ end
84
+ end
85
+
86
+ test 'builder discovers new custom inputs from configured namespace before the ones from top level namespace' do
87
+ discovery do
88
+ swap SimpleForm, custom_inputs_namespaces: ['CustomInputs'] do
89
+ with_form_for @user, :name, as: 'customized'
90
+ assert_select 'form input#user_name.customized-namespace-custom-input'
91
+ end
92
+ end
93
+ end
94
+
95
+ test 'raises error when configured namespace does not exists' do
96
+ discovery do
97
+ swap SimpleForm, custom_inputs_namespaces: ['InvalidNamespace'] do
98
+ assert_raise NameError do
99
+ with_form_for @user, :age
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ test 'new inputs can override the input_html_options' do
106
+ discovery do
107
+ with_form_for @user, :active, as: :select
108
+ assert_select 'form select#user_active.select.chosen'
109
+ end
110
+ end
111
+
112
+ test 'inputs method without wrapper_options are deprecated' do
113
+ discovery do
114
+ assert_deprecated do
115
+ with_form_for @user, :name, as: :deprecated
116
+ end
117
+
118
+ assert_select 'form section input#user_name.string'
119
+ end
120
+ end
121
+ end
@@ -1,14 +1,15 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: UTF-8
2
3
  require 'test_helper'
3
4
 
4
5
  class FileInputTest < ActionView::TestCase
5
- test 'input should generate a file field' do
6
+ test 'input generates a file field' do
6
7
  with_input_for @user, :name, :file
7
8
  assert_select 'input#user_name[type=file]'
8
9
  end
9
10
 
10
- test "input should generate a file field that doesn't accept placeholder" do
11
- store_translations(:en, :simple_form => { :placeholders => { :user => { :name => "text" } } }) do
11
+ test "input generates a file field that doesn't accept placeholder" do
12
+ store_translations(:en, simple_form: { placeholders: { user: { name: "text" } } }) do
12
13
  with_input_for @user, :name, :file
13
14
  assert_no_select 'input[placeholder]'
14
15
  end
@@ -1,8 +1,9 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: UTF-8
2
3
  require 'test_helper'
3
4
 
4
5
  class InputTest < ActionView::TestCase
5
- test 'input should generate css class based on default input type' do
6
+ test 'input generates css class based on default input type' do
6
7
  with_input_for @user, :name, :string
7
8
  assert_select 'input.string'
8
9
  with_input_for @user, :description, :text
@@ -15,55 +16,118 @@ class InputTest < ActionView::TestCase
15
16
  assert_select 'select.datetime'
16
17
  end
17
18
 
18
- test 'input should generate autofocus attribute based on the autofocus option' do
19
- with_input_for @user, :name, :string, :autofocus => true
19
+ test 'string input generates autofocus attribute when autofocus option is true' do
20
+ with_input_for @user, :name, :string, autofocus: true
20
21
  assert_select 'input.string[autofocus]'
21
- with_input_for @user, :description, :text, :autofocus => true
22
+ end
23
+
24
+ test 'input accepts input_class configuration' do
25
+ swap SimpleForm, input_class: :xlarge do
26
+ with_input_for @user, :name, :string
27
+ assert_select 'input.xlarge'
28
+ assert_no_select 'div.xlarge'
29
+ end
30
+ end
31
+
32
+ test 'input does not add input_class when configured to not generate additional classes for input' do
33
+ swap SimpleForm, input_class: 'xlarge', generate_additional_classes_for: [:wrapper] do
34
+ with_input_for @user, :name, :string
35
+ assert_select 'input'
36
+ assert_no_select '.xlarge'
37
+ end
38
+ end
39
+
40
+ test 'text input generates autofocus attribute when autofocus option is true' do
41
+ with_input_for @user, :description, :text, autofocus: true
22
42
  assert_select 'textarea.text[autofocus]'
23
- with_input_for @user, :age, :integer, :autofocus => true
43
+ end
44
+
45
+ test 'numeric input generates autofocus attribute when autofocus option is true' do
46
+ with_input_for @user, :age, :integer, autofocus: true
24
47
  assert_select 'input.integer[autofocus]'
25
- with_input_for @user, :born_at, :date, :autofocus => true
48
+ end
49
+
50
+ test 'date input generates autofocus attribute when autofocus option is true' do
51
+ with_input_for @user, :born_at, :date, autofocus: true
26
52
  assert_select 'select.date[autofocus]'
27
- with_input_for @user, :created_at, :datetime, :autofocus => true
53
+ end
54
+
55
+ test 'datetime input generates autofocus attribute when autofocus option is true' do
56
+ with_input_for @user, :created_at, :datetime, autofocus: true
28
57
  assert_select 'select.datetime[autofocus]'
58
+ end
59
+
60
+ test 'string input generates autofocus attribute when autofocus option is false' do
61
+ with_input_for @user, :name, :string, autofocus: false
62
+ assert_no_select 'input.string[autofocus]'
63
+ end
64
+
65
+ test 'text input generates autofocus attribute when autofocus option is false' do
66
+ with_input_for @user, :description, :text, autofocus: false
67
+ assert_no_select 'textarea.text[autofocus]'
68
+ end
69
+
70
+ test 'numeric input generates autofocus attribute when autofocus option is false' do
71
+ with_input_for @user, :age, :integer, autofocus: false
72
+ assert_no_select 'input.integer[autofocus]'
73
+ end
74
+
75
+ test 'date input generates autofocus attribute when autofocus option is false' do
76
+ with_input_for @user, :born_at, :date, autofocus: false
77
+ assert_no_select 'select.date[autofocus]'
78
+ end
29
79
 
30
- with_input_for @user, :name, :string, :autofocus => false
31
- assert_select 'input.string:not([autofocus])'
32
- with_input_for @user, :description, :text, :autofocus => false
33
- assert_select 'textarea.text:not([autofocus])'
34
- with_input_for @user, :age, :integer, :autofocus => false
35
- assert_select 'input.integer:not([autofocus])'
36
- with_input_for @user, :born_at, :date, :autofocus => false
37
- assert_select 'select.date:not([autofocus])'
38
- with_input_for @user, :created_at, :datetime, :autofocus => false
39
- assert_select 'select.datetime:not([autofocus])'
80
+ test 'datetime input generates autofocus attribute when autofocus option is false' do
81
+ with_input_for @user, :created_at, :datetime, autofocus: false
82
+ assert_no_select 'select.datetime[autofocus]'
83
+ end
40
84
 
85
+ test 'string input generates autofocus attribute when autofocus option is not present' do
41
86
  with_input_for @user, :name, :string
42
- assert_select 'input.string:not([autofocus])'
87
+ assert_no_select 'input.string[autofocus]'
88
+ end
89
+
90
+ test 'text input generates autofocus attribute when autofocus option is not present' do
43
91
  with_input_for @user, :description, :text
44
- assert_select 'textarea.text:not([autofocus])'
92
+ assert_no_select 'textarea.text[autofocus]'
93
+ end
94
+
95
+ test 'numeric input generates autofocus attribute when autofocus option is not present' do
45
96
  with_input_for @user, :age, :integer
46
- assert_select 'input.integer:not([autofocus])'
97
+ assert_no_select 'input.integer[autofocus]'
98
+ end
99
+
100
+ test 'date input generates autofocus attribute when autofocus option is not present' do
47
101
  with_input_for @user, :born_at, :date
48
- assert_select 'select.date:not([autofocus])'
102
+ assert_no_select 'select.date[autofocus]'
103
+ end
104
+
105
+ test 'datetime input generates autofocus attribute when autofocus option is not present' do
49
106
  with_input_for @user, :created_at, :datetime
50
- assert_select 'select.datetime:not([autofocus])'
107
+ assert_no_select 'select.datetime[autofocus]'
51
108
  end
52
109
 
53
110
  # With no object
54
- test 'input should be generated properly when object is not present' do
111
+ test 'input is generated properly when object is not present' do
55
112
  with_input_for :project, :name, :string
56
113
  assert_select 'input.string.required#project_name'
57
114
  end
58
115
 
59
- test 'input as radio should be generated properly when object is not present ' do
116
+ test 'input as radio is generated properly when object is not present ' do
60
117
  with_input_for :project, :name, :radio_buttons
61
118
  assert_select 'input.radio_buttons#project_name_true'
62
119
  assert_select 'input.radio_buttons#project_name_false'
63
120
  end
64
121
 
65
- test 'input as select with collection should be generated properly when object is not present' do
66
- with_input_for :project, :name, :select, :collection => ['Jose', 'Carlos']
122
+ test 'input as select with collection is generated properly when object is not present' do
123
+ with_input_for :project, :name, :select, collection: %w[Jose Carlos]
67
124
  assert_select 'select.select#project_name'
68
125
  end
126
+
127
+ test 'input does not generate empty css class' do
128
+ swap SimpleForm, generate_additional_classes_for: %i[wrapper label] do
129
+ with_input_for :project, :name, :string
130
+ assert_no_select 'input#project_name[class]'
131
+ end
132
+ end
69
133
  end