simple_form 3.0.1 → 3.1.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +75 -25
- data/MIT-LICENSE +1 -1
- data/README.md +251 -93
- data/lib/generators/simple_form/install_generator.rb +3 -3
- data/lib/generators/simple_form/templates/README +3 -4
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +29 -9
- data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +128 -24
- data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +86 -5
- data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
- data/lib/simple_form/action_view_extensions/builder.rb +1 -0
- data/lib/simple_form/action_view_extensions/form_helper.rb +6 -2
- data/lib/simple_form/components/errors.rb +28 -2
- data/lib/simple_form/components/hints.rb +2 -2
- data/lib/simple_form/components/html5.rb +5 -5
- data/lib/simple_form/components/label_input.rb +20 -2
- data/lib/simple_form/components/labels.rb +11 -7
- data/lib/simple_form/components/maxlength.rb +1 -1
- data/lib/simple_form/components/min_max.rb +1 -1
- data/lib/simple_form/components/pattern.rb +1 -1
- data/lib/simple_form/components/placeholders.rb +2 -2
- data/lib/simple_form/components/readonly.rb +1 -1
- data/lib/simple_form/form_builder.rb +127 -76
- data/lib/simple_form/helpers.rb +5 -5
- data/lib/simple_form/inputs/base.rb +33 -11
- data/lib/simple_form/inputs/block_input.rb +1 -1
- data/lib/simple_form/inputs/boolean_input.rb +28 -15
- data/lib/simple_form/inputs/collection_input.rb +30 -9
- data/lib/simple_form/inputs/collection_radio_buttons_input.rb +7 -12
- data/lib/simple_form/inputs/collection_select_input.rb +4 -2
- data/lib/simple_form/inputs/date_time_input.rb +23 -9
- data/lib/simple_form/inputs/file_input.rb +4 -2
- data/lib/simple_form/inputs/grouped_collection_select_input.rb +15 -3
- data/lib/simple_form/inputs/hidden_input.rb +4 -2
- data/lib/simple_form/inputs/numeric_input.rb +5 -4
- data/lib/simple_form/inputs/password_input.rb +4 -2
- data/lib/simple_form/inputs/priority_input.rb +4 -2
- data/lib/simple_form/inputs/range_input.rb +1 -1
- data/lib/simple_form/inputs/string_input.rb +4 -2
- data/lib/simple_form/inputs/text_input.rb +4 -2
- data/lib/simple_form/railtie.rb +7 -0
- data/lib/simple_form/tags.rb +6 -0
- data/lib/simple_form/version.rb +1 -1
- data/lib/simple_form/wrappers/builder.rb +6 -6
- data/lib/simple_form/wrappers/leaf.rb +28 -0
- data/lib/simple_form/wrappers/many.rb +6 -6
- data/lib/simple_form/wrappers/root.rb +1 -1
- data/lib/simple_form/wrappers/single.rb +5 -3
- data/lib/simple_form/wrappers.rb +1 -0
- data/lib/simple_form.rb +59 -8
- data/test/action_view_extensions/builder_test.rb +36 -36
- data/test/action_view_extensions/form_helper_test.rb +33 -14
- data/test/components/label_test.rb +37 -37
- data/test/form_builder/association_test.rb +52 -35
- data/test/form_builder/button_test.rb +10 -10
- data/test/form_builder/error_notification_test.rb +1 -1
- data/test/form_builder/error_test.rb +144 -24
- data/test/form_builder/general_test.rb +107 -71
- data/test/form_builder/hint_test.rb +18 -18
- data/test/form_builder/input_field_test.rb +86 -56
- data/test/form_builder/label_test.rb +55 -13
- data/test/form_builder/wrapper_test.rb +136 -20
- data/test/generators/simple_form_generator_test.rb +4 -4
- data/test/inputs/boolean_input_test.rb +62 -6
- data/test/inputs/collection_check_boxes_input_test.rb +85 -17
- data/test/inputs/collection_radio_buttons_input_test.rb +134 -36
- data/test/inputs/collection_select_input_test.rb +146 -41
- data/test/inputs/datetime_input_test.rb +120 -48
- data/test/inputs/disabled_test.rb +15 -15
- data/test/inputs/discovery_test.rb +56 -6
- data/test/inputs/file_input_test.rb +2 -2
- data/test/inputs/general_test.rb +20 -20
- data/test/inputs/grouped_collection_select_input_test.rb +44 -8
- data/test/inputs/hidden_input_test.rb +4 -4
- data/test/inputs/numeric_input_test.rb +43 -43
- data/test/inputs/priority_input_test.rb +13 -13
- data/test/inputs/readonly_test.rb +19 -19
- data/test/inputs/required_test.rb +25 -13
- data/test/inputs/string_input_test.rb +50 -30
- data/test/inputs/text_input_test.rb +14 -7
- data/test/simple_form_test.rb +8 -0
- data/test/support/discovery_inputs.rb +32 -2
- data/test/support/misc_helpers.rb +84 -5
- data/test/support/models.rb +60 -24
- data/test/test_helper.rb +11 -1
- metadata +22 -33
|
@@ -2,124 +2,154 @@ require 'test_helper'
|
|
|
2
2
|
|
|
3
3
|
# Tests for f.input_field
|
|
4
4
|
class InputFieldTest < ActionView::TestCase
|
|
5
|
-
|
|
6
|
-
with_concat_form_for(
|
|
7
|
-
f.input_field
|
|
5
|
+
def with_input_field_for(object, *args)
|
|
6
|
+
with_concat_form_for(object) do |f|
|
|
7
|
+
f.input_field(*args)
|
|
8
8
|
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
test "builder input_field only renders the input tag, nothing else" do
|
|
12
|
+
with_input_field_for @user, :name
|
|
13
|
+
|
|
9
14
|
assert_select 'form > input.required.string'
|
|
10
15
|
assert_no_select 'div.string'
|
|
11
16
|
assert_no_select 'label'
|
|
12
17
|
assert_no_select '.hint'
|
|
13
18
|
end
|
|
14
19
|
|
|
15
|
-
test 'builder input_field
|
|
16
|
-
|
|
17
|
-
f.input_field :name, as: :text
|
|
18
|
-
end
|
|
20
|
+
test 'builder input_field allows overriding default input type' do
|
|
21
|
+
with_input_field_for @user, :name, as: :text
|
|
19
22
|
|
|
20
23
|
assert_no_select 'input#user_name'
|
|
21
24
|
assert_select 'textarea#user_name.text'
|
|
22
25
|
end
|
|
23
26
|
|
|
24
|
-
test 'builder input_field
|
|
25
|
-
|
|
26
|
-
f.input_field :age
|
|
27
|
-
end
|
|
27
|
+
test 'builder input_field generates input type based on column type' do
|
|
28
|
+
with_input_field_for @user, :age
|
|
28
29
|
|
|
29
30
|
assert_select 'input[type=number].integer#user_age'
|
|
30
31
|
end
|
|
31
32
|
|
|
32
|
-
test 'builder input_field
|
|
33
|
-
|
|
34
|
-
f.input_field :age, html5: false
|
|
35
|
-
end
|
|
33
|
+
test 'builder input_field is able to disable any component' do
|
|
34
|
+
with_input_field_for @user, :age, html5: false
|
|
36
35
|
|
|
37
36
|
assert_no_select 'input[html5=false]#user_age'
|
|
38
37
|
assert_select 'input[type=text].integer#user_age'
|
|
39
38
|
end
|
|
40
39
|
|
|
41
|
-
test 'builder input_field
|
|
42
|
-
|
|
43
|
-
f.input_field :name, id: 'name_input', class: 'name'
|
|
44
|
-
end
|
|
40
|
+
test 'builder input_field allows passing options to input tag' do
|
|
41
|
+
with_input_field_for @user, :name, id: 'name_input', class: 'name'
|
|
45
42
|
|
|
46
43
|
assert_select 'input.string.name#name_input'
|
|
47
44
|
end
|
|
48
45
|
|
|
49
|
-
test 'builder input_field
|
|
46
|
+
test 'builder input_field does not modify the options hash' do
|
|
50
47
|
options = { id: 'name_input', class: 'name' }
|
|
51
|
-
|
|
52
|
-
with_concat_form_for(@user) do |f|
|
|
53
|
-
f.input_field :name, options
|
|
54
|
-
end
|
|
48
|
+
with_input_field_for @user, :name, options
|
|
55
49
|
|
|
56
50
|
assert_select 'input.string.name#name_input'
|
|
57
51
|
assert_equal({ id: 'name_input', class: 'name' }, options)
|
|
58
52
|
end
|
|
59
53
|
|
|
60
54
|
|
|
61
|
-
test 'builder input_field
|
|
62
|
-
|
|
63
|
-
f.input_field :name, as: :integer, class: 'name'
|
|
64
|
-
end
|
|
55
|
+
test 'builder input_field generates an input tag with a clean HTML' do
|
|
56
|
+
with_input_field_for @user, :name, as: :integer, class: 'name'
|
|
65
57
|
|
|
66
58
|
assert_no_select 'input.integer[input_html]'
|
|
67
59
|
assert_no_select 'input.integer[as]'
|
|
68
60
|
end
|
|
69
61
|
|
|
70
|
-
test 'builder input_field
|
|
62
|
+
test 'builder input_field uses i18n to translate placeholder text' do
|
|
71
63
|
store_translations(:en, simple_form: { placeholders: { user: {
|
|
72
64
|
name: 'Name goes here'
|
|
73
65
|
} } }) do
|
|
66
|
+
with_input_field_for @user, :name
|
|
74
67
|
|
|
75
|
-
|
|
76
|
-
f.input_field :name
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
assert_select 'input.string[placeholder=Name goes here]'
|
|
68
|
+
assert_select 'input.string[placeholder="Name goes here"]'
|
|
80
69
|
end
|
|
81
70
|
end
|
|
82
71
|
|
|
83
|
-
test 'builder input_field
|
|
84
|
-
|
|
85
|
-
f.input_field :age, as: :integer
|
|
86
|
-
end
|
|
72
|
+
test 'builder input_field uses min_max component' do
|
|
73
|
+
with_input_field_for @other_validating_user, :age, as: :integer
|
|
87
74
|
|
|
88
|
-
assert_select 'input[min=18]'
|
|
75
|
+
assert_select 'input[min="18"]'
|
|
89
76
|
end
|
|
90
77
|
|
|
91
|
-
test 'builder input_field
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
78
|
+
test 'builder input_field does not use pattern component by default' do
|
|
79
|
+
with_input_field_for @other_validating_user, :country, as: :string
|
|
80
|
+
|
|
81
|
+
assert_no_select 'input[pattern="\w+"]'
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
test 'builder input_field infers pattern from attributes' do
|
|
85
|
+
with_input_field_for @other_validating_user, :country, as: :string, pattern: true
|
|
95
86
|
|
|
96
87
|
assert_select 'input[pattern="\w+"]'
|
|
97
88
|
end
|
|
98
89
|
|
|
99
|
-
test 'builder input_field
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
90
|
+
test 'builder input_field accepts custom patter' do
|
|
91
|
+
with_input_field_for @other_validating_user, :country, as: :string, pattern: '\d+'
|
|
92
|
+
|
|
93
|
+
assert_select 'input[pattern="\d+"]'
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
test 'builder input_field uses readonly component' do
|
|
97
|
+
with_input_field_for @other_validating_user, :age, as: :integer, readonly: true
|
|
103
98
|
|
|
104
99
|
assert_select 'input.integer.readonly[readonly]'
|
|
105
100
|
end
|
|
106
101
|
|
|
107
|
-
test 'builder input_field
|
|
108
|
-
|
|
109
|
-
f.input_field :name, as: :string
|
|
110
|
-
end
|
|
102
|
+
test 'builder input_field uses maxlength component' do
|
|
103
|
+
with_input_field_for @validating_user, :name, as: :string
|
|
111
104
|
|
|
112
|
-
assert_select 'input.string[maxlength=25]'
|
|
105
|
+
assert_select 'input.string[maxlength="25"]'
|
|
113
106
|
end
|
|
114
107
|
|
|
115
|
-
test 'builder collection input_field
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
end
|
|
108
|
+
test 'builder collection input_field generates input tag with a clean HTML' do
|
|
109
|
+
with_input_field_for @user, :status, collection: ['Open', 'Closed'],
|
|
110
|
+
class: 'status', label_method: :to_s, value_method: :to_s
|
|
119
111
|
|
|
120
112
|
assert_no_select 'select.status[input_html]'
|
|
121
113
|
assert_no_select 'select.status[collection]'
|
|
122
114
|
assert_no_select 'select.status[label_method]'
|
|
123
115
|
assert_no_select 'select.status[value_method]'
|
|
124
116
|
end
|
|
117
|
+
|
|
118
|
+
test 'build input_field does not treat "boolean_style" as a HTML attribute' do
|
|
119
|
+
with_input_field_for @user, :active, boolean_style: :nested
|
|
120
|
+
|
|
121
|
+
assert_no_select 'input.boolean[boolean_style]'
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
test 'build input_field without pattern component use the pattern string' do
|
|
125
|
+
swap_wrapper :default, custom_wrapper_with_html5_components do
|
|
126
|
+
with_input_field_for @user, :name, pattern: '\w+'
|
|
127
|
+
|
|
128
|
+
assert_select 'input[pattern="\w+"]'
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
test 'build input_field without placeholder component use the placeholder string' do
|
|
133
|
+
swap_wrapper :default, custom_wrapper_with_html5_components do
|
|
134
|
+
with_input_field_for @user, :name, placeholder: 'Placeholder'
|
|
135
|
+
|
|
136
|
+
assert_select 'input[placeholder="Placeholder"]'
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
test 'build input_field without maxlength component use the maxlength string' do
|
|
141
|
+
swap_wrapper :default, custom_wrapper_with_html5_components do
|
|
142
|
+
with_input_field_for @user, :name, maxlength: 5
|
|
143
|
+
|
|
144
|
+
assert_select 'input[maxlength="5"]'
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
test 'build input_field without readonly component use the readonly string' do
|
|
149
|
+
swap_wrapper :default, custom_wrapper_with_html5_components do
|
|
150
|
+
with_input_field_for @user, :name, readonly: true
|
|
151
|
+
|
|
152
|
+
assert_select 'input[readonly="readonly"]'
|
|
153
|
+
end
|
|
154
|
+
end
|
|
125
155
|
end
|
|
@@ -8,63 +8,73 @@ class LabelTest < ActionView::TestCase
|
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
test 'builder
|
|
11
|
+
test 'builder generates a label for the attribute' do
|
|
12
12
|
with_label_for @user, :name
|
|
13
13
|
assert_select 'label.string[for=user_name]', /Name/
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
test 'builder
|
|
16
|
+
test 'builder generates a label for the boolean attrbiute' do
|
|
17
17
|
with_label_for @user, :name, as: :boolean
|
|
18
18
|
assert_select 'label.boolean[for=user_name]', /Name/
|
|
19
19
|
assert_no_select 'label[as=boolean]'
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
test 'builder
|
|
22
|
+
test 'builder generates a label component tag with a clean HTML' do
|
|
23
23
|
with_label_for @user, :name
|
|
24
24
|
assert_no_select 'label.string[label_html]'
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
-
test 'builder
|
|
27
|
+
test 'builder adds a required class to label if the attribute is required' do
|
|
28
28
|
with_label_for @validating_user, :name
|
|
29
29
|
assert_select 'label.string.required[for=validating_user_name]', /Name/
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
test 'builder
|
|
32
|
+
test 'builder adds a disabled class to label if the attribute is disabled' do
|
|
33
|
+
with_label_for @validating_user, :name, disabled: true
|
|
34
|
+
assert_select 'label.string.disabled[for=validating_user_name]', /Name/
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
test 'builder does not add a disabled class to label if the attribute is not disabled' do
|
|
38
|
+
with_label_for @validating_user, :name, disabled: false
|
|
39
|
+
assert_no_select 'label.string.disabled[for=validating_user_name]', /Name/
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
test 'builder escapes label text' do
|
|
33
43
|
with_label_for @user, :name, label: '<script>alert(1337)</script>', required: false
|
|
34
|
-
|
|
44
|
+
assert_no_select 'label.string script'
|
|
35
45
|
end
|
|
36
46
|
|
|
37
|
-
test 'builder
|
|
47
|
+
test 'builder does not escape label text if it is safe' do
|
|
38
48
|
with_label_for @user, :name, label: '<script>alert(1337)</script>'.html_safe, required: false
|
|
39
49
|
assert_select 'label.string script', "alert(1337)"
|
|
40
50
|
end
|
|
41
51
|
|
|
42
|
-
test 'builder
|
|
52
|
+
test 'builder allows passing options to label tag' do
|
|
43
53
|
with_label_for @user, :name, label: 'My label', id: 'name_label'
|
|
44
54
|
assert_select 'label.string#name_label', /My label/
|
|
45
55
|
end
|
|
46
56
|
|
|
47
|
-
test 'builder label
|
|
57
|
+
test 'builder label generates label tag with clean HTML' do
|
|
48
58
|
with_label_for @user, :name, label: 'My label', required: true, id: 'name_label'
|
|
49
59
|
assert_select 'label.string#name_label', /My label/
|
|
50
60
|
assert_no_select 'label[label]'
|
|
51
61
|
assert_no_select 'label[required]'
|
|
52
62
|
end
|
|
53
63
|
|
|
54
|
-
test 'builder
|
|
64
|
+
test 'builder does not modify the options hash' do
|
|
55
65
|
options = { label: 'My label', id: 'name_label' }
|
|
56
66
|
with_label_for @user, :name, options
|
|
57
67
|
assert_select 'label.string#name_label', /My label/
|
|
58
68
|
assert_equal({ label: 'My label', id: 'name_label' }, options)
|
|
59
69
|
end
|
|
60
70
|
|
|
61
|
-
test 'builder
|
|
71
|
+
test 'builder fallbacks to default label when string is given' do
|
|
62
72
|
with_label_for @user, :name, 'Nome do usuário'
|
|
63
73
|
assert_select 'label', 'Nome do usuário'
|
|
64
74
|
assert_no_select 'label.string'
|
|
65
75
|
end
|
|
66
76
|
|
|
67
|
-
test 'builder
|
|
77
|
+
test 'builder fallbacks to default label when block is given' do
|
|
68
78
|
with_label_for @user, :name do
|
|
69
79
|
'Nome do usuário'
|
|
70
80
|
end
|
|
@@ -73,9 +83,41 @@ class LabelTest < ActionView::TestCase
|
|
|
73
83
|
end
|
|
74
84
|
|
|
75
85
|
test 'builder allows label order to be changed' do
|
|
76
|
-
swap SimpleForm, label_text:
|
|
86
|
+
swap SimpleForm, label_text: proc { |l, r| "#{l}:" } do
|
|
77
87
|
with_label_for @user, :age
|
|
78
88
|
assert_select 'label.integer[for=user_age]', "Age:"
|
|
79
89
|
end
|
|
80
90
|
end
|
|
91
|
+
|
|
92
|
+
test 'configuration allow set label text for wrappers' do
|
|
93
|
+
swap_wrapper :default, custom_wrapper_with_label_text do
|
|
94
|
+
with_concat_form_for(@user) do |f|
|
|
95
|
+
concat f.input :age
|
|
96
|
+
end
|
|
97
|
+
assert_select "label.integer[for=user_age]", "**Age**"
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
test 'configuration allow set rewrited label tag for wrappers' do
|
|
102
|
+
swap_wrapper :default, custom_wrapper_with_custom_label_component do
|
|
103
|
+
with_concat_form_for(@user) do |f|
|
|
104
|
+
concat f.input :age
|
|
105
|
+
end
|
|
106
|
+
assert_select "span.integer.user_age", /Age/
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
test 'builder allows custom formatting when label is explicitly specified' do
|
|
111
|
+
swap SimpleForm, label_text: lambda { |l, r, explicit_label| explicit_label ? l : "#{l.titleize}:" } do
|
|
112
|
+
with_label_for @user, :time_zone, 'What is your home time zone?'
|
|
113
|
+
assert_select 'label[for=user_time_zone]', 'What is your home time zone?'
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
test 'builder allows custom formatting when label is generated' do
|
|
118
|
+
swap SimpleForm, label_text: lambda { |l, r, explicit_label| explicit_label ? l : "#{l.titleize}:" } do
|
|
119
|
+
with_label_for @user, :time_zone
|
|
120
|
+
assert_select 'label[for=user_time_zone]', 'Time Zone:'
|
|
121
|
+
end
|
|
122
|
+
end
|
|
81
123
|
end
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
require 'test_helper'
|
|
2
2
|
|
|
3
3
|
class WrapperTest < ActionView::TestCase
|
|
4
|
-
test 'wrapper
|
|
4
|
+
test 'wrapper does not have error class for attribute without errors' do
|
|
5
5
|
with_form_for @user, :active
|
|
6
6
|
assert_no_select 'div.field_with_errors'
|
|
7
7
|
end
|
|
8
8
|
|
|
9
|
-
test 'wrapper
|
|
9
|
+
test 'wrapper does not have error class when object is not present' do
|
|
10
10
|
with_form_for :project, :name
|
|
11
11
|
assert_no_select 'div.field_with_errors'
|
|
12
12
|
end
|
|
13
13
|
|
|
14
|
-
test 'wrapper
|
|
14
|
+
test 'wrapper adds the attribute name class' do
|
|
15
15
|
with_form_for @user, :name
|
|
16
16
|
assert_select 'div.user_name'
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
test 'wrapper
|
|
19
|
+
test 'wrapper adds the attribute name class for nested forms' do
|
|
20
20
|
@user.company = Company.new(1, 'Empresa')
|
|
21
21
|
with_concat_form_for @user do |f|
|
|
22
22
|
concat(f.simple_fields_for(:company) do |company_form|
|
|
@@ -27,44 +27,44 @@ class WrapperTest < ActionView::TestCase
|
|
|
27
27
|
assert_select 'div.user_company_name'
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
test 'wrapper
|
|
30
|
+
test 'wrapper adds the association name class' do
|
|
31
31
|
with_form_for @user, :company
|
|
32
32
|
assert_select 'div.user_company'
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
test 'wrapper
|
|
35
|
+
test 'wrapper adds error class for attribute with errors' do
|
|
36
36
|
with_form_for @user, :name
|
|
37
37
|
assert_select 'div.field_with_errors'
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
test 'wrapper
|
|
40
|
+
test 'wrapper adds hint class for attribute with a hint' do
|
|
41
41
|
with_form_for @user, :name, hint: 'hint'
|
|
42
42
|
assert_select 'div.field_with_hint'
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
-
test 'wrapper
|
|
45
|
+
test 'wrapper does not have disabled class by default' do
|
|
46
46
|
with_form_for @user, :active
|
|
47
47
|
assert_no_select 'div.disabled'
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
-
test 'wrapper
|
|
50
|
+
test 'wrapper has disabled class when input is disabled' do
|
|
51
51
|
with_form_for @user, :active, disabled: true
|
|
52
52
|
assert_select 'div.disabled'
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
-
test 'wrapper
|
|
55
|
+
test 'wrapper supports no wrapping when wrapper is false' do
|
|
56
56
|
with_form_for @user, :name, wrapper: false
|
|
57
57
|
assert_select 'form > label[for=user_name]'
|
|
58
58
|
assert_select 'form > input#user_name.string'
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
-
test 'wrapper
|
|
61
|
+
test 'wrapper supports no wrapping when wrapper tag is false' do
|
|
62
62
|
with_form_for @user, :name, wrapper: custom_wrapper_without_top_level
|
|
63
63
|
assert_select 'form > label[for=user_name]'
|
|
64
64
|
assert_select 'form > input#user_name.string'
|
|
65
65
|
end
|
|
66
66
|
|
|
67
|
-
test 'wrapper
|
|
67
|
+
test 'wrapper wraps tag adds required/optional css classes' do
|
|
68
68
|
with_form_for @user, :name
|
|
69
69
|
assert_select 'form div.input.required.string'
|
|
70
70
|
|
|
@@ -72,22 +72,22 @@ class WrapperTest < ActionView::TestCase
|
|
|
72
72
|
assert_select 'form div.input.optional.integer'
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
-
test 'wrapper
|
|
75
|
+
test 'wrapper allows custom options to be given' do
|
|
76
76
|
with_form_for @user, :name, wrapper_html: { id: "super_cool", class: 'yay' }
|
|
77
77
|
assert_select 'form #super_cool.required.string.yay'
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
-
test 'wrapper
|
|
80
|
+
test 'wrapper allows tag to be given on demand' do
|
|
81
81
|
with_form_for @user, :name, wrapper_tag: :b
|
|
82
82
|
assert_select 'form b.required.string'
|
|
83
83
|
end
|
|
84
84
|
|
|
85
|
-
test 'wrapper
|
|
85
|
+
test 'wrapper allows wrapper class to be given on demand' do
|
|
86
86
|
with_form_for @user, :name, wrapper_class: :wrapper
|
|
87
87
|
assert_select 'form div.wrapper.required.string'
|
|
88
88
|
end
|
|
89
89
|
|
|
90
|
-
test 'wrapper
|
|
90
|
+
test 'wrapper skips additional classes when configured' do
|
|
91
91
|
swap SimpleForm, generate_additional_classes_for: [:input, :label] do
|
|
92
92
|
with_form_for @user, :name, wrapper_class: :wrapper
|
|
93
93
|
assert_select 'form div.wrapper'
|
|
@@ -97,7 +97,7 @@ class WrapperTest < ActionView::TestCase
|
|
|
97
97
|
end
|
|
98
98
|
end
|
|
99
99
|
|
|
100
|
-
test 'wrapper
|
|
100
|
+
test 'wrapper does not generate empty css class' do
|
|
101
101
|
swap SimpleForm, generate_additional_classes_for: [:input, :label] do
|
|
102
102
|
swap_wrapper :default, custom_wrapper_without_class do
|
|
103
103
|
with_form_for @user, :name
|
|
@@ -128,6 +128,21 @@ class WrapperTest < ActionView::TestCase
|
|
|
128
128
|
end
|
|
129
129
|
end
|
|
130
130
|
|
|
131
|
+
test 'custom wrappers can have additional attributes' do
|
|
132
|
+
swap_wrapper :default, custom_wrapper_with_additional_attributes do
|
|
133
|
+
with_form_for @user, :name
|
|
134
|
+
|
|
135
|
+
assert_select "div.custom_wrapper[title='some title'][data-wrapper='test']"
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
test 'custom wrappers can have full error message on attributes' do
|
|
140
|
+
swap_wrapper :default, custom_wrapper_with_full_error do
|
|
141
|
+
with_form_for @user, :name
|
|
142
|
+
assert_select 'span.error', "Name cannot be blank"
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
131
146
|
test 'custom wrappers on a form basis' do
|
|
132
147
|
swap_wrapper :another do
|
|
133
148
|
with_concat_form_for(@user) do |f|
|
|
@@ -172,8 +187,8 @@ class WrapperTest < ActionView::TestCase
|
|
|
172
187
|
end
|
|
173
188
|
end
|
|
174
189
|
|
|
175
|
-
test '
|
|
176
|
-
swap_wrapper :default,
|
|
190
|
+
test 'does not duplicate label classes for different inputs' do
|
|
191
|
+
swap_wrapper :default, custom_wrapper_with_label_html_option do
|
|
177
192
|
with_concat_form_for(@user) do |f|
|
|
178
193
|
concat f.input :name, required: false
|
|
179
194
|
concat f.input :email, as: :email, required: true
|
|
@@ -191,7 +206,7 @@ class WrapperTest < ActionView::TestCase
|
|
|
191
206
|
end
|
|
192
207
|
end
|
|
193
208
|
|
|
194
|
-
test '
|
|
209
|
+
test 'uses wrapper for specified in config mapping' do
|
|
195
210
|
swap_wrapper :another do
|
|
196
211
|
swap SimpleForm, wrapper_mappings: { string: :another } do
|
|
197
212
|
with_form_for @user, :name
|
|
@@ -200,4 +215,105 @@ class WrapperTest < ActionView::TestCase
|
|
|
200
215
|
end
|
|
201
216
|
end
|
|
202
217
|
end
|
|
218
|
+
|
|
219
|
+
test 'uses custom wrapper mapping per form basis' do
|
|
220
|
+
swap_wrapper :another do
|
|
221
|
+
with_concat_form_for @user, wrapper_mappings: { string: :another } do |f|
|
|
222
|
+
concat f.input :name
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
227
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
test 'simple_fields_form reuses custom wrapper mapping per form basis' do
|
|
231
|
+
@user.company = Company.new(1, 'Empresa')
|
|
232
|
+
|
|
233
|
+
swap_wrapper :another do
|
|
234
|
+
with_concat_form_for @user, wrapper_mappings: { string: :another } do |f|
|
|
235
|
+
concat(f.simple_fields_for(:company) do |company_form|
|
|
236
|
+
concat(company_form.input(:name))
|
|
237
|
+
end)
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
assert_select "section.custom_wrapper div.another_wrapper label"
|
|
242
|
+
assert_select "section.custom_wrapper div.another_wrapper input.string"
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
test 'input accepts attributes in the DSL' do
|
|
246
|
+
swap_wrapper :default, custom_wrapper_with_input_class do
|
|
247
|
+
with_concat_form_for @user do |f|
|
|
248
|
+
concat f.input :name
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
assert_select "div.custom_wrapper input.string.inline-class"
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
test 'label accepts attributes in the DSL' do
|
|
256
|
+
swap_wrapper :default, custom_wrapper_with_label_class do
|
|
257
|
+
with_concat_form_for @user do |f|
|
|
258
|
+
concat f.input :name
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
assert_select "div.custom_wrapper label.string.inline-class"
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
test 'label_input accepts attributes in the DSL' do
|
|
266
|
+
swap_wrapper :default, custom_wrapper_with_label_input_class do
|
|
267
|
+
with_concat_form_for @user do |f|
|
|
268
|
+
concat f.input :name
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
assert_select "div.custom_wrapper label.string.inline-class"
|
|
273
|
+
assert_select "div.custom_wrapper input.string.inline-class"
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
test 'input accepts data attributes in the DSL' do
|
|
277
|
+
swap_wrapper :default, custom_wrapper_with_input_attributes do
|
|
278
|
+
with_concat_form_for @user do |f|
|
|
279
|
+
concat f.input :name
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
assert_select "div.custom_wrapper input.string[data-modal=true]"
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
test 'inline wrapper displays when there is content' do
|
|
287
|
+
swap_wrapper :default, custom_wrapper_with_wrapped_optional_component do
|
|
288
|
+
with_form_for @user, :name, hint: "cannot be blank"
|
|
289
|
+
assert_select 'section.custom_wrapper div.no_output_wrapper p.omg_hint', "cannot be blank"
|
|
290
|
+
assert_select 'p.omg_hint'
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
test 'inline wrapper does not display when there is no content' do
|
|
295
|
+
swap_wrapper :default, custom_wrapper_with_wrapped_optional_component do
|
|
296
|
+
with_form_for @user, :name
|
|
297
|
+
assert_select 'section.custom_wrapper div.no_output_wrapper'
|
|
298
|
+
assert_no_select 'p.omg_hint'
|
|
299
|
+
end
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
test 'optional wrapper does not display when there is content' do
|
|
303
|
+
swap_wrapper :default, custom_wrapper_with_unless_blank do
|
|
304
|
+
with_form_for @user, :name, hint: "can't be blank"
|
|
305
|
+
assert_select 'section.custom_wrapper div.no_output_wrapper'
|
|
306
|
+
assert_select 'div.no_output_wrapper'
|
|
307
|
+
assert_select 'p.omg_hint'
|
|
308
|
+
end
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
test 'optional wrapper does not display when there is no content' do
|
|
312
|
+
swap_wrapper :default, custom_wrapper_with_unless_blank do
|
|
313
|
+
with_form_for @user, :name
|
|
314
|
+
assert_no_select 'section.custom_wrapper div.no_output_wrapper'
|
|
315
|
+
assert_no_select 'div.no_output_wrapper'
|
|
316
|
+
assert_no_select 'p.omg_hint'
|
|
317
|
+
end
|
|
318
|
+
end
|
|
203
319
|
end
|
|
@@ -21,16 +21,16 @@ class SimpleFormGeneratorTest < Rails::Generators::TestCase
|
|
|
21
21
|
run_generator %w(--bootstrap)
|
|
22
22
|
assert_file 'config/initializers/simple_form.rb',
|
|
23
23
|
/config\.default_wrapper = :default/, /config\.boolean_style = :nested/
|
|
24
|
-
assert_file 'config/initializers/simple_form_bootstrap.rb', /config\.wrappers :
|
|
25
|
-
/config\.default_wrapper = :
|
|
24
|
+
assert_file 'config/initializers/simple_form_bootstrap.rb', /config\.wrappers :vertical_form/,
|
|
25
|
+
/config\.wrappers :horizontal_form/, /config\.default_wrapper = :vertical_form/
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
test 'generates the simple_form initializer with the foundation wrappers' do
|
|
29
29
|
run_generator %w(--foundation)
|
|
30
30
|
assert_file 'config/initializers/simple_form.rb',
|
|
31
31
|
/config\.default_wrapper = :default/, /config\.boolean_style = :nested/
|
|
32
|
-
assert_file 'config/initializers/simple_form_foundation.rb', /config\.wrappers :
|
|
33
|
-
/config\.default_wrapper = :
|
|
32
|
+
assert_file 'config/initializers/simple_form_foundation.rb', /config\.wrappers :vertical_form/,
|
|
33
|
+
/config\.default_wrapper = :vertical_form/, /config\.item_wrapper_tag = :div/
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
%W(erb haml slim).each do |engine|
|