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,11 +1,12 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: UTF-8
2
3
  require 'test_helper'
3
4
 
4
5
  class GroupedCollectionSelectInputTest < ActionView::TestCase
5
6
  test 'grouped collection accepts array collection form' do
6
7
  with_input_for @user, :tag_ids, :grouped_select,
7
- :collection => [['Authors', ['Jose', 'Carlos']], ['General', ['Bob', 'John']]],
8
- :group_method => :last
8
+ collection: [['Authors', %w[Jose Carlos]], ['General', %w[Bob John]]],
9
+ group_method: :last
9
10
 
10
11
  assert_select 'select.grouped_select#user_tag_ids' do
11
12
  assert_select 'optgroup[label=Authors]' do
@@ -22,8 +23,8 @@ class GroupedCollectionSelectInputTest < ActionView::TestCase
22
23
 
23
24
  test 'grouped collection accepts empty array collection form' do
24
25
  with_input_for @user, :tag_ids, :grouped_select,
25
- :collection => [],
26
- :group_method => :last
26
+ collection: [],
27
+ group_method: :last
27
28
 
28
29
  assert_select 'select.grouped_select#user_tag_ids'
29
30
  end
@@ -31,8 +32,8 @@ class GroupedCollectionSelectInputTest < ActionView::TestCase
31
32
 
32
33
  test 'grouped collection accepts proc as collection' do
33
34
  with_input_for @user, :tag_ids, :grouped_select,
34
- :collection => Proc.new { [['Authors', ['Jose', 'Carlos']], ['General', ['Bob', 'John']]] },
35
- :group_method => :last
35
+ collection: proc { [['Authors', %w[Jose Carlos]], ['General', %w[Bob John]]] },
36
+ group_method: :last
36
37
 
37
38
  assert_select 'select.grouped_select#user_tag_ids' do
38
39
  assert_select 'optgroup[label=Authors]' do
@@ -49,8 +50,8 @@ class GroupedCollectionSelectInputTest < ActionView::TestCase
49
50
 
50
51
  test 'grouped collection accepts hash collection form' do
51
52
  with_input_for @user, :tag_ids, :grouped_select,
52
- :collection => { 'Authors' => ['Jose', 'Carlos'], 'General' => ['Bob', 'John'] },
53
- :group_method => :last
53
+ collection: { Authors: %w[Jose Carlos], General: %w[Bob John] },
54
+ group_method: :last
54
55
 
55
56
  assert_select 'select.grouped_select#user_tag_ids' do
56
57
  assert_select 'optgroup[label=Authors]' do
@@ -67,9 +68,9 @@ class GroupedCollectionSelectInputTest < ActionView::TestCase
67
68
 
68
69
  test 'grouped collection accepts group_label_method option' do
69
70
  with_input_for @user, :tag_ids, :grouped_select,
70
- :collection => { ['Jose', 'Carlos'] => 'Authors' },
71
- :group_method => :first,
72
- :group_label_method => :last
71
+ collection: { %w[Jose Carlos] => 'Authors' },
72
+ group_method: :first,
73
+ group_label_method: :last
73
74
 
74
75
  assert_select 'select.grouped_select#user_tag_ids' do
75
76
  assert_select 'optgroup[label=Authors]' do
@@ -79,12 +80,63 @@ class GroupedCollectionSelectInputTest < ActionView::TestCase
79
80
  end
80
81
  end
81
82
 
83
+ test 'grouped collection finds default label methods on the group objects' do
84
+ option_list = %w[Jose Carlos]
85
+
86
+ GroupedClass = Struct.new(:to_label, :options)
87
+ group = GroupedClass.new("Authors", option_list)
88
+
89
+ with_input_for @user, :tag_ids, :grouped_select,
90
+ collection: [group],
91
+ group_method: :options
92
+
93
+ assert_select 'select.grouped_select#user_tag_ids' do
94
+ assert_select 'optgroup[label=Authors]' do
95
+ assert_select 'option', 'Jose'
96
+ assert_select 'option', 'Carlos'
97
+ end
98
+ end
99
+ end
100
+
101
+ test 'grouped collections finds the default label method from the first non-empty object' do
102
+ Agent = Struct.new(:id, :name)
103
+ agents = [["First", []], ["Second", [Agent.new(7, 'Bond'), Agent.new(47, 'Hitman')]]]
104
+
105
+ with_input_for @user, :tag_ids, :grouped_select,
106
+ collection: agents,
107
+ group_label_method: :first,
108
+ group_method: :last,
109
+ include_blank: false
110
+
111
+ assert_select 'select.grouped_select#user_tag_ids' do
112
+ assert_select 'optgroup[label=Second]' do
113
+ assert_select 'option[value="7"]', 'Bond'
114
+ assert_select 'option[value="47"]', 'Hitman'
115
+ end
116
+ end
117
+ end
118
+
82
119
  test 'grouped collection accepts label and value methods options' do
83
120
  with_input_for @user, :tag_ids, :grouped_select,
84
- :collection => { 'Authors' => ['Jose', 'Carlos'] },
85
- :group_method => :last,
86
- :label_method => :upcase,
87
- :value_method => :downcase
121
+ collection: { Authors: %w[Jose Carlos] },
122
+ group_method: :last,
123
+ label_method: :upcase,
124
+ value_method: :downcase
125
+
126
+ assert_select 'select.grouped_select#user_tag_ids' do
127
+ assert_select 'optgroup[label=Authors]' do
128
+ assert_select 'option[value=jose]', 'JOSE'
129
+ assert_select 'option[value=carlos]', 'CARLOS'
130
+ end
131
+ end
132
+ end
133
+
134
+ test 'grouped collection allows overriding label and value methods using a lambda' do
135
+ with_input_for @user, :tag_ids, :grouped_select,
136
+ collection: { Authors: %w[Jose Carlos] },
137
+ group_method: :last,
138
+ label_method: ->(i) { i.upcase },
139
+ value_method: ->(i) { i.downcase }
88
140
 
89
141
  assert_select 'select.grouped_select#user_tag_ids' do
90
142
  assert_select 'optgroup[label=Authors]' do
@@ -97,21 +149,34 @@ class GroupedCollectionSelectInputTest < ActionView::TestCase
97
149
  test 'grouped collection with associations' do
98
150
  tag_groups = [
99
151
  TagGroup.new(1, "Group of Tags", [Tag.new(1, "Tag 1"), Tag.new(2, "Tag 2")]),
100
- TagGroup.new(2, "Other group", [Tag.new(3, "Tag 3"), Tag.new(4,"Tag 4")])
152
+ TagGroup.new(2, "Other group", [Tag.new(3, "Tag 3"), Tag.new(4, "Tag 4")])
101
153
  ]
102
154
 
103
155
  with_input_for @user, :tag_ids, :grouped_select,
104
- :collection => tag_groups, :group_method => :tags
156
+ collection: tag_groups, group_method: :tags
105
157
 
106
158
  assert_select 'select.grouped_select#user_tag_ids' do
107
- assert_select 'optgroup[label=Group of Tags]' do
108
- assert_select 'option[value=1]', 'Tag 1'
109
- assert_select 'option[value=2]', 'Tag 2'
159
+ assert_select 'optgroup[label="Group of Tags"]' do
160
+ assert_select 'option[value="1"]', 'Tag 1'
161
+ assert_select 'option[value="2"]', 'Tag 2'
162
+ end
163
+
164
+ assert_select 'optgroup[label="Other group"]' do
165
+ assert_select 'option[value="3"]', 'Tag 3'
166
+ assert_select 'option[value="4"]', 'Tag 4'
110
167
  end
168
+ end
169
+ end
111
170
 
112
- assert_select 'optgroup[label=Other group]' do
113
- assert_select 'option[value=3]', 'Tag 3'
114
- assert_select 'option[value=4]', 'Tag 4'
171
+ test 'grouped collection accepts html options as the last element of collection' do
172
+ with_input_for @user, :tag_ids, :grouped_select,
173
+ collection: [['Authors', [['Jose', 'jose', class: 'foo'], ['Carlos', 'carlos', class: 'bar']]]],
174
+ group_method: :last
175
+
176
+ assert_select 'select.grouped_select#user_tag_ids' do
177
+ assert_select 'optgroup[label=Authors]' do
178
+ assert_select 'option.foo', 'Jose'
179
+ assert_select 'option.bar', 'Carlos'
115
180
  end
116
181
  end
117
182
  end
@@ -1,29 +1,31 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: UTF-8
2
3
  require 'test_helper'
3
4
 
4
5
  class HiddenInputTest < ActionView::TestCase
5
- test 'input should generate a hidden field' do
6
+ test 'input generates a hidden field' do
6
7
  with_input_for @user, :name, :hidden
7
8
  assert_no_select 'input[type=text]'
8
9
  assert_select 'input#user_name[type=hidden]'
9
10
  end
10
11
 
11
- test 'hint should not be generated for hidden fields' do
12
- store_translations(:en, :simple_form => { :hints => { :user => { :name => "text" } } }) do
12
+ test 'hint does not be generated for hidden fields' do
13
+ store_translations(:en, simple_form: { hints: { user: { name: "text" } } }) do
13
14
  with_input_for @user, :name, :hidden
14
15
  assert_no_select 'span.hint'
15
16
  end
16
17
  end
17
18
 
18
- test 'label should not be generated for hidden inputs' do
19
+ test 'label does not be generated for hidden inputs' do
19
20
  with_input_for @user, :name, :hidden
20
21
  assert_no_select 'label'
21
22
  end
22
23
 
23
- test 'required/optional options should not be generated for hidden inputs' do
24
+ test 'required/aria-required/optional options does not be generated for hidden inputs' do
24
25
  with_input_for @user, :name, :hidden
25
26
  assert_no_select 'input.required'
26
27
  assert_no_select 'input[required]'
28
+ assert_no_select 'input[aria-required]'
27
29
  assert_no_select 'input.optional'
28
30
  assert_select 'input.hidden#user_name'
29
31
  end
@@ -1,141 +1,145 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: UTF-8
2
3
  require 'test_helper'
3
4
 
4
5
  class NumericInputTest < ActionView::TestCase
5
- test 'input should generate an integer text field for integer attributes ' do
6
+ test 'input generates an integer text field for integer attributes ' do
6
7
  with_input_for @user, :age, :integer
7
8
  assert_select 'input[type=number].integer#user_age'
8
9
  end
9
10
 
10
- test 'input should generate a float text field for float attributes ' do
11
+ test 'input generates a float text field for float attributes ' do
11
12
  with_input_for @user, :age, :float
12
13
  assert_select 'input[type=number].float#user_age'
13
14
  end
14
15
 
15
- test 'input should generate a decimal text field for decimal attributes ' do
16
+ test 'input generates a decimal text field for decimal attributes ' do
16
17
  with_input_for @user, :age, :decimal
17
18
  assert_select 'input[type=number].decimal#user_age'
18
19
  end
19
20
 
20
- test 'input should not generate min attribute by default' do
21
+ test 'input does not generate min attribute by default' do
21
22
  with_input_for @user, :age, :integer
22
23
  assert_no_select 'input[min]'
23
24
  end
24
25
 
25
- test 'input should not generate max attribute by default' do
26
+ test 'input does not generate max attribute by default' do
26
27
  with_input_for @user, :age, :integer
27
28
  assert_no_select 'input[max]'
28
29
  end
29
30
 
30
- test 'input should infer min value from integer attributes with greater than validation' do
31
+ test 'input infers min value from integer attributes with greater than validation' do
31
32
  with_input_for @other_validating_user, :age, :float
32
33
  assert_no_select 'input[min]'
33
34
 
34
35
  with_input_for @other_validating_user, :age, :integer
35
- assert_select 'input[min=18]'
36
+ assert_select 'input[min="18"]'
36
37
  end
37
38
 
38
- test 'input should infer min value from integer attributes with greater than validation using symbol' do
39
+ test 'input infers min value from integer attributes with greater than validation using symbol' do
39
40
  with_input_for @validating_user, :amount, :float
40
41
  assert_no_select 'input[min]'
41
42
 
42
43
  with_input_for @validating_user, :amount, :integer
43
- assert_select 'input[min=11]'
44
+ assert_select 'input[min="11"]'
44
45
  end
45
46
 
46
- test 'input should infer min value from integer attributes with greater than or equal to validation using symbol' do
47
+ test 'input infers min value from integer attributes with greater than or equal to validation using symbol' do
47
48
  with_input_for @validating_user, :attempts, :float
48
- assert_select 'input[min=1]'
49
+ assert_select 'input[min="1"]'
49
50
 
50
51
  with_input_for @validating_user, :attempts, :integer
51
- assert_select 'input[min=1]'
52
+ assert_select 'input[min="1"]'
52
53
  end
53
54
 
54
- test 'input should infer min value from integer attributes with greater than validation using proc' do
55
+ test 'input infers min value from integer attributes with greater than validation using proc' do
55
56
  with_input_for @other_validating_user, :amount, :float
56
57
  assert_no_select 'input[min]'
57
58
 
58
59
  with_input_for @other_validating_user, :amount, :integer
59
- assert_select 'input[min=20]'
60
+ assert_select 'input[min="20"]'
60
61
  end
61
62
 
62
- test 'input should infer min value from integer attributes with greater than or equal to validation using proc' do
63
+ test 'input infers min value from integer attributes with greater than or equal to validation using proc' do
63
64
  with_input_for @other_validating_user, :attempts, :float
64
- assert_select 'input[min=19]'
65
+ assert_select 'input[min="19"]'
65
66
 
66
67
  with_input_for @other_validating_user, :attempts, :integer
67
- assert_select 'input[min=19]'
68
+ assert_select 'input[min="19"]'
68
69
  end
69
70
 
70
- test 'input should infer max value from attributes with less than validation' do
71
+ test 'input infers max value from attributes with less than validation' do
71
72
  with_input_for @other_validating_user, :age, :float
72
73
  assert_no_select 'input[max]'
73
74
 
74
75
  with_input_for @other_validating_user, :age, :integer
75
- assert_select 'input[max=99]'
76
+ assert_select 'input[max="99"]'
76
77
  end
77
78
 
78
- test 'input should infer max value from attributes with less than validation using symbol' do
79
+ test 'input infers max value from attributes with less than validation using symbol' do
79
80
  with_input_for @validating_user, :amount, :float
80
81
  assert_no_select 'input[max]'
81
82
 
82
83
  with_input_for @validating_user, :amount, :integer
83
- assert_select 'input[max=99]'
84
+ assert_select 'input[max="99"]'
84
85
  end
85
86
 
86
- test 'input should infer max value from attributes with less than or equal to validation using symbol' do
87
+ test 'input infers max value from attributes with less than or equal to validation using symbol' do
87
88
  with_input_for @validating_user, :attempts, :float
88
- assert_select 'input[max=100]'
89
+ assert_select 'input[max="100"]'
89
90
 
90
91
  with_input_for @validating_user, :attempts, :integer
91
- assert_select 'input[max=100]'
92
+ assert_select 'input[max="100"]'
92
93
  end
93
94
 
94
- test 'input should infer max value from attributes with less than validation using proc' do
95
+ test 'input infers max value from attributes with less than validation using proc' do
95
96
  with_input_for @other_validating_user, :amount, :float
96
97
  assert_no_select 'input[max]'
97
98
 
98
99
  with_input_for @other_validating_user, :amount, :integer
99
- assert_select 'input[max=118]'
100
+ assert_select 'input[max="118"]'
100
101
  end
101
102
 
102
- test 'input should infer max value from attributes with less than or equal to validation using proc' do
103
+ test 'input infers max value from attributes with less than or equal to validation using proc' do
103
104
  with_input_for @other_validating_user, :attempts, :float
104
- assert_select 'input[max=119]'
105
+ assert_select 'input[max="119"]'
105
106
 
106
107
  with_input_for @other_validating_user, :attempts, :integer
107
- assert_select 'input[max=119]'
108
+ assert_select 'input[max="119"]'
108
109
  end
109
110
 
110
- test 'input should have step value of any except for integer attribute' do
111
+ test 'input has step value of any except for integer attribute' do
111
112
  with_input_for @validating_user, :age, :float
112
113
  assert_select 'input[step="any"]'
113
114
 
114
115
  with_input_for @validating_user, :age, :integer
115
- assert_select 'input[step=1]'
116
+ assert_select 'input[step="1"]'
117
+
118
+ with_input_for @validating_user, :age, :integer, as: :decimal, input_html: { step: 0.5 }
119
+ assert_select 'input[step="0.5"]'
116
120
  end
117
121
 
118
- test 'numeric input should not generate placeholder by default' do
122
+ test 'numeric input does not generate placeholder by default' do
119
123
  with_input_for @user, :age, :integer
120
124
  assert_no_select 'input[placeholder]'
121
125
  end
122
126
 
123
- test 'numeric input should accept the placeholder option' do
124
- with_input_for @user, :age, :integer, :placeholder => 'Put in your age'
125
- assert_select 'input.integer[placeholder=Put in your age]'
127
+ test 'numeric input accepts the placeholder option' do
128
+ with_input_for @user, :age, :integer, placeholder: 'Put in your age'
129
+ assert_select 'input.integer[placeholder="Put in your age"]'
126
130
  end
127
131
 
128
- test 'numeric input should use i18n to translate placeholder text' do
129
- store_translations(:en, :simple_form => { :placeholders => { :user => {
130
- :age => 'Age goes here'
132
+ test 'numeric input uses i18n to translate placeholder text' do
133
+ store_translations(:en, simple_form: { placeholders: { user: {
134
+ age: 'Age goes here'
131
135
  } } }) do
132
136
  with_input_for @user, :age, :integer
133
- assert_select 'input.integer[placeholder=Age goes here]'
137
+ assert_select 'input.integer[placeholder="Age goes here"]'
134
138
  end
135
139
  end
136
140
 
137
141
  # Numeric input but HTML5 disabled
138
- test ' when not using HTML5 input should not generate field with type number and use text instead' do
142
+ test 'when not using HTML5 input does not generate field with type number and use text instead' do
139
143
  swap_wrapper do
140
144
  with_input_for @user, :age, :integer
141
145
  assert_no_select "input[type=number]"
@@ -143,7 +147,7 @@ class NumericInputTest < ActionView::TestCase
143
147
  end
144
148
  end
145
149
 
146
- test 'when not using HTML5 input should not use min or max or step attributes' do
150
+ test 'when not using HTML5 input does not use min or max or step attributes' do
147
151
  swap_wrapper do
148
152
  with_input_for @validating_user, :age, :integer
149
153
  assert_no_select "input[type=number]"
@@ -153,15 +157,21 @@ class NumericInputTest < ActionView::TestCase
153
157
  end
154
158
  end
155
159
 
156
- [:integer, :float, :decimal].each do |type|
157
- test "#{type} input should infer min value from attributes with greater than or equal validation" do
160
+ %i[integer float decimal].each do |type|
161
+ test "#{type} input infers min value from attributes with greater than or equal validation" do
158
162
  with_input_for @validating_user, :age, type
159
- assert_select 'input[min=18]'
163
+ assert_select 'input[min="18"]'
160
164
  end
161
165
 
162
- test "#{type} input should infer the max value from attributes with less than or equal to validation" do
166
+ test "#{type} input infers the max value from attributes with less than or equal to validation" do
163
167
  with_input_for @validating_user, :age, type
164
- assert_select 'input[max=99]'
168
+ assert_select 'input[max="99"]'
165
169
  end
166
170
  end
171
+
172
+ test 'min_max does not emit max value as bare string' do
173
+ with_input_for @other_validating_user, :age, :integer
174
+ assert_select 'input[max]'
175
+ assert_no_select 'div', %r{^99}
176
+ end
167
177
  end
@@ -1,43 +1,58 @@
1
+ # frozen_string_literal: true
1
2
  # encoding: UTF-8
2
3
  require 'test_helper'
3
4
 
4
5
  class PriorityInputTest < ActionView::TestCase
5
- test 'input should generate a country select field' do
6
+ test 'input generates a country select field' do
6
7
  with_input_for @user, :country, :country
7
8
  assert_select 'select#user_country'
8
- assert_select 'select option[value=Brazil]', 'Brazil'
9
- assert_no_select 'select option[value=][disabled=disabled]'
9
+ if ActionPack::VERSION::STRING >= '5'
10
+ assert_select 'select option[value=BR]', 'Brazil'
11
+ elsif ActionPack::VERSION::STRING < '5'
12
+ assert_select 'select option[value=Brazil]', 'Brazil'
13
+ end
14
+ assert_no_select 'select option[value=""][disabled=disabled]'
10
15
  end
11
16
 
12
- test 'input should generate a country select with SimpleForm default' do
13
- swap SimpleForm, :country_priority => [ 'Brazil' ] do
17
+ test 'input generates a country select with SimpleForm default' do
18
+ swap SimpleForm, country_priority: [ 'Brazil' ] do
14
19
  with_input_for @user, :country, :country
15
- assert_select 'select option[value=][disabled=disabled]'
20
+ if ActionPack::VERSION::STRING >= '5'
21
+ assert_select 'select option[value="---------------"][disabled=disabled]'
22
+ elsif ActionPack::VERSION::STRING < '5'
23
+ assert_select 'select option[value=""][disabled=disabled]'
24
+ end
16
25
  end
17
26
  end
18
27
 
19
- test 'input should generate a time zone select field' do
28
+ test 'input generates a time zone select field' do
20
29
  with_input_for @user, :time_zone, :time_zone
21
30
  assert_select 'select#user_time_zone'
22
31
  assert_select 'select option[value=Brasilia]', '(GMT-03:00) Brasilia'
23
- assert_no_select 'select option[value=][disabled=disabled]'
32
+ assert_no_select 'select option[value=""][disabled=disabled]'
24
33
  end
25
34
 
26
- test 'input should generate a time zone select field with default' do
27
- with_input_for @user, :time_zone, :time_zone, :default => 'Brasilia'
35
+ test 'input generates a time zone select field with default' do
36
+ with_input_for @user, :time_zone, :time_zone, default: 'Brasilia'
28
37
  assert_select 'select option[value=Brasilia][selected=selected]'
29
- assert_no_select 'select option[value=]'
38
+ assert_no_select 'select option[value=""]'
30
39
  end
31
40
 
32
- test 'input should generate a time zone select using options priority' do
33
- with_input_for @user, :time_zone, :time_zone, :priority => /Brasilia/
34
- assert_select 'select option[value=][disabled=disabled]'
35
- assert_no_select 'select option[value=]', /^$/
41
+ test 'input generates a time zone select using options priority' do
42
+ with_input_for @user, :time_zone, :time_zone, priority: /Brasilia/
43
+ assert_select 'select option[value=""][disabled=disabled]'
44
+ assert_no_select 'select option[value=""]', /^$/
36
45
  end
37
46
 
38
- test 'priority input should not generate invalid required html attribute' do
47
+ test 'priority input does not generate invalid required html attribute' do
39
48
  with_input_for @user, :country, :country
40
49
  assert_select 'select.required'
41
50
  assert_no_select 'select[required]'
42
51
  end
52
+
53
+ test 'priority input does not generate invalid aria-required html attribute' do
54
+ with_input_for @user, :country, :country
55
+ assert_select 'select.required'
56
+ assert_no_select 'select[aria-required]'
57
+ end
43
58
  end
@@ -1,58 +1,99 @@
1
+ # frozen_string_literal: true
1
2
  require 'test_helper'
2
3
 
3
4
  class ReadonlyTest < ActionView::TestCase
4
- test 'input should generate readonly elements based on the readonly option' do
5
- with_input_for @user, :name, :string, :readonly => true
5
+ test 'string input generates readonly elements when readonly option is true' do
6
+ with_input_for @user, :name, :string, readonly: true
6
7
  assert_select 'input.string.readonly[readonly]'
7
- with_input_for @user, :description, :text, :readonly => true
8
+ end
9
+
10
+ test 'text input generates readonly elements when readonly option is true' do
11
+ with_input_for @user, :description, :text, readonly: true
8
12
  assert_select 'textarea.text.readonly[readonly]'
9
- with_input_for @user, :age, :integer, :readonly => true
13
+ end
14
+
15
+ test 'numeric input generates readonly elements when readonly option is true' do
16
+ with_input_for @user, :age, :integer, readonly: true
10
17
  assert_select 'input.integer.readonly[readonly]'
11
- with_input_for @user, :born_at, :date, :readonly => true
18
+ end
19
+
20
+ test 'date input generates readonly elements when readonly option is true' do
21
+ with_input_for @user, :born_at, :date, readonly: true
12
22
  assert_select 'select.date.readonly[readonly]'
13
- with_input_for @user, :created_at, :datetime, :readonly => true
23
+ end
24
+
25
+ test 'datetime input generates readonly elements when readonly option is true' do
26
+ with_input_for @user, :created_at, :datetime, readonly: true
14
27
  assert_select 'select.datetime.readonly[readonly]'
28
+ end
29
+
30
+ test 'string input generates readonly elements when readonly option is false' do
31
+ with_input_for @user, :name, :string, readonly: false
32
+ assert_no_select 'input.string.readonly[readonly]'
33
+ end
34
+
35
+ test 'text input generates readonly elements when readonly option is false' do
36
+ with_input_for @user, :description, :text, readonly: false
37
+ assert_no_select 'textarea.text.readonly[readonly]'
38
+ end
15
39
 
16
- with_input_for @user, :name, :string, :readonly => false
17
- assert_select 'input.string:not(.readonly[readonly])'
18
- with_input_for @user, :description, :text, :readonly => false
19
- assert_select 'textarea.text:not(.readonly[readonly])'
20
- with_input_for @user, :age, :integer, :readonly => false
21
- assert_select 'input.integer:not(.readonly[readonly])'
22
- with_input_for @user, :born_at, :date, :readonly => false
23
- assert_select 'select.date:not(.readonly[readonly])'
24
- with_input_for @user, :created_at, :datetime, :readonly => false
25
- assert_select 'select.datetime:not(.readonly[readonly])'
40
+ test 'numeric input generates readonly elements when readonly option is false' do
41
+ with_input_for @user, :age, :integer, readonly: false
42
+ assert_no_select 'input.integer.readonly[readonly]'
43
+ end
44
+
45
+ test 'date input generates readonly elements when readonly option is false' do
46
+ with_input_for @user, :born_at, :date, readonly: false
47
+ assert_no_select 'select.date.readonly[readonly]'
48
+ end
26
49
 
50
+ test 'datetime input generates readonly elements when readonly option is false' do
51
+ with_input_for @user, :created_at, :datetime, readonly: false
52
+ assert_no_select 'select.datetime.readonly[readonly]'
53
+ end
54
+
55
+ test 'string input generates readonly elements when readonly option is not present' do
27
56
  with_input_for @user, :name, :string
28
- assert_select 'input.string:not(.readonly[readonly])'
57
+ assert_no_select 'input.string.readonly[readonly]'
58
+ end
59
+
60
+ test 'text input generates readonly elements when readonly option is not present' do
29
61
  with_input_for @user, :description, :text
30
- assert_select 'textarea.text:not(.readonly[readonly])'
62
+ assert_no_select 'textarea.text.readonly[readonly]'
63
+ end
64
+
65
+ test 'numeric input generates readonly elements when readonly option is not present' do
31
66
  with_input_for @user, :age, :integer
32
- assert_select 'input.integer:not(.readonly[readonly])'
67
+ assert_no_select 'input.integer.readonly[readonly]'
68
+ end
69
+
70
+ test 'date input generates readonly elements when readonly option is not present' do
33
71
  with_input_for @user, :born_at, :date
34
- assert_select 'select.date:not(.readonly[readonly])'
72
+ assert_no_select 'select.date.readonly[readonly]'
73
+ end
74
+
75
+ test 'datetime input generates readonly elements when readonly option is not present' do
35
76
  with_input_for @user, :created_at, :datetime
36
- assert_select 'select.datetime:not(.readonly[readonly])'
77
+ assert_no_select 'select.datetime.readonly[readonly]'
37
78
  end
38
79
 
39
- test 'input should generate readonly attribute when the field is readonly and the object is persisted' do
40
- with_input_for @user, :credit_card, :string, :readonly => :lookup
80
+ test 'input generates readonly attribute when the field is readonly and the object is persisted' do
81
+ with_input_for @user, :credit_card, :string, readonly: :lookup
41
82
  assert_select 'input.string.readonly[readonly]'
42
83
  end
43
84
 
44
- test 'input should not generate readonly attribute when the field is readonly and the object is not persisted' do
85
+ test 'input does not generate readonly attribute when the field is readonly and the object is not persisted' do
45
86
  @user.new_record!
46
- with_input_for @user, :credit_card, :string, :readonly => :lookup
87
+ with_input_for @user, :credit_card, :string, readonly: :lookup
47
88
  assert_no_select 'input.string.readonly[readonly]'
48
89
  end
49
90
 
50
- test 'input should not generate readonly attribute when the field is not readonly and the object is persisted' do
91
+ test 'input does not generate readonly attribute when the field is not readonly and the object is persisted' do
51
92
  with_input_for @user, :name, :string
52
93
  assert_no_select 'input.string.readonly[readonly]'
53
94
  end
54
95
 
55
- test 'input should not generate readonly attribute when the component is not used' do
96
+ test 'input does not generate readonly attribute when the component is not used' do
56
97
  swap_wrapper do
57
98
  with_input_for @user, :credit_card, :string
58
99
  assert_no_select 'input.string.readonly[readonly]'