simple_form 3.0.1 → 4.0.0
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 +149 -25
- data/MIT-LICENSE +1 -1
- data/README.md +441 -107
- data/lib/generators/simple_form/install_generator.rb +4 -3
- data/lib/generators/simple_form/templates/README +4 -5
- data/lib/generators/simple_form/templates/_form.html.erb +2 -0
- data/lib/generators/simple_form/templates/_form.html.haml +2 -0
- data/lib/generators/simple_form/templates/_form.html.slim +1 -0
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +48 -11
- data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +417 -23
- data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +101 -5
- data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
- data/lib/simple_form/action_view_extensions/builder.rb +2 -0
- data/lib/simple_form/action_view_extensions/form_helper.rb +10 -3
- data/lib/simple_form/components/errors.rb +38 -3
- data/lib/simple_form/components/hints.rb +3 -2
- data/lib/simple_form/components/html5.rb +16 -5
- data/lib/simple_form/components/label_input.rb +21 -2
- data/lib/simple_form/components/labels.rb +22 -11
- data/lib/simple_form/components/maxlength.rb +9 -5
- data/lib/simple_form/components/min_max.rb +2 -1
- data/lib/simple_form/components/minlength.rb +38 -0
- data/lib/simple_form/components/pattern.rb +2 -1
- data/lib/simple_form/components/placeholders.rb +4 -3
- data/lib/simple_form/components/readonly.rb +2 -1
- data/lib/simple_form/components.rb +2 -0
- data/lib/simple_form/error_notification.rb +1 -0
- data/lib/simple_form/form_builder.rb +193 -80
- data/lib/simple_form/helpers/autofocus.rb +1 -0
- data/lib/simple_form/helpers/disabled.rb +1 -0
- data/lib/simple_form/helpers/readonly.rb +1 -0
- data/lib/simple_form/helpers/required.rb +1 -0
- data/lib/simple_form/helpers/validators.rb +2 -1
- data/lib/simple_form/helpers.rb +6 -5
- data/lib/simple_form/i18n_cache.rb +1 -0
- data/lib/simple_form/inputs/base.rb +57 -14
- data/lib/simple_form/inputs/block_input.rb +2 -1
- data/lib/simple_form/inputs/boolean_input.rb +39 -16
- data/lib/simple_form/inputs/collection_check_boxes_input.rb +2 -1
- data/lib/simple_form/inputs/collection_input.rb +37 -14
- data/lib/simple_form/inputs/collection_radio_buttons_input.rb +9 -13
- data/lib/simple_form/inputs/collection_select_input.rb +5 -2
- data/lib/simple_form/inputs/date_time_input.rb +24 -9
- data/lib/simple_form/inputs/file_input.rb +5 -2
- data/lib/simple_form/inputs/grouped_collection_select_input.rb +16 -3
- data/lib/simple_form/inputs/hidden_input.rb +5 -2
- data/lib/simple_form/inputs/numeric_input.rb +6 -4
- data/lib/simple_form/inputs/password_input.rb +6 -3
- data/lib/simple_form/inputs/priority_input.rb +5 -2
- data/lib/simple_form/inputs/range_input.rb +2 -1
- data/lib/simple_form/inputs/string_input.rb +6 -3
- data/lib/simple_form/inputs/text_input.rb +6 -3
- data/lib/simple_form/inputs.rb +1 -0
- data/lib/simple_form/map_type.rb +1 -0
- data/lib/simple_form/railtie.rb +8 -0
- data/lib/simple_form/tags.rb +7 -0
- data/lib/simple_form/version.rb +2 -1
- data/lib/simple_form/wrappers/builder.rb +7 -6
- data/lib/simple_form/wrappers/leaf.rb +29 -0
- data/lib/simple_form/wrappers/many.rb +7 -6
- data/lib/simple_form/wrappers/root.rb +3 -1
- data/lib/simple_form/wrappers/single.rb +7 -4
- data/lib/simple_form/wrappers.rb +2 -0
- data/lib/simple_form.rb +117 -18
- data/test/action_view_extensions/builder_test.rb +42 -41
- data/test/action_view_extensions/form_helper_test.rb +36 -16
- data/test/components/custom_components_test.rb +62 -0
- data/test/components/label_test.rb +70 -41
- data/test/form_builder/association_test.rb +79 -37
- data/test/form_builder/button_test.rb +11 -10
- data/test/form_builder/error_notification_test.rb +2 -1
- data/test/form_builder/error_test.rb +173 -24
- data/test/form_builder/general_test.rb +171 -73
- data/test/form_builder/hint_test.rb +24 -18
- data/test/form_builder/input_field_test.rb +132 -56
- data/test/form_builder/label_test.rb +68 -13
- data/test/form_builder/wrapper_test.rb +190 -22
- data/test/generators/simple_form_generator_test.rb +8 -7
- data/test/inputs/boolean_input_test.rb +89 -6
- data/test/inputs/collection_check_boxes_input_test.rb +109 -25
- data/test/inputs/collection_radio_buttons_input_test.rb +168 -54
- data/test/inputs/collection_select_input_test.rb +183 -77
- data/test/inputs/datetime_input_test.rb +131 -50
- data/test/inputs/disabled_test.rb +16 -15
- data/test/inputs/discovery_test.rb +58 -6
- data/test/inputs/file_input_test.rb +3 -2
- data/test/inputs/general_test.rb +23 -22
- data/test/inputs/grouped_collection_select_input_test.rb +54 -17
- data/test/inputs/hidden_input_test.rb +5 -4
- data/test/inputs/numeric_input_test.rb +48 -44
- data/test/inputs/priority_input_test.rb +23 -14
- data/test/inputs/readonly_test.rb +20 -19
- data/test/inputs/required_test.rb +58 -13
- data/test/inputs/string_input_test.rb +62 -33
- data/test/inputs/text_input_test.rb +20 -7
- data/test/simple_form_test.rb +9 -0
- data/test/support/discovery_inputs.rb +33 -2
- data/test/support/misc_helpers.rb +113 -5
- data/test/support/mock_controller.rb +7 -1
- data/test/support/models.rb +137 -37
- data/test/test_helper.rb +15 -1
- metadata +25 -33
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
1
2
|
require 'test_helper'
|
|
2
3
|
|
|
3
4
|
class FormHelperTest < ActionView::TestCase
|
|
@@ -8,17 +9,36 @@ class FormHelperTest < ActionView::TestCase
|
|
|
8
9
|
end
|
|
9
10
|
end
|
|
10
11
|
|
|
11
|
-
test 'SimpleForm
|
|
12
|
+
test 'SimpleForm adds default class to form' do
|
|
12
13
|
with_concat_form_for(:user)
|
|
13
14
|
assert_select 'form.simple_form'
|
|
14
15
|
end
|
|
15
16
|
|
|
16
|
-
test 'SimpleForm
|
|
17
|
+
test 'SimpleForm allows overriding default form class' do
|
|
18
|
+
swap SimpleForm, default_form_class: "my_custom_class" do
|
|
19
|
+
with_concat_form_for :user, html: { class: "override_class" }
|
|
20
|
+
assert_no_select 'form.my_custom_class'
|
|
21
|
+
assert_select 'form.override_class'
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Remove this test when SimpleForm.form_class is removed in 4.x
|
|
26
|
+
test 'SimpleForm allows overriding default form class, but not form class' do
|
|
27
|
+
ActiveSupport::Deprecation.silence do
|
|
28
|
+
swap SimpleForm, form_class: "fixed_class", default_form_class: "my_custom_class" do
|
|
29
|
+
with_concat_form_for :user, html: { class: "override_class" }
|
|
30
|
+
assert_no_select 'form.my_custom_class'
|
|
31
|
+
assert_select 'form.fixed_class.override_class'
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
test 'SimpleForm uses default browser validations by default' do
|
|
17
37
|
with_concat_form_for(:user)
|
|
18
38
|
assert_no_select 'form[novalidate]'
|
|
19
39
|
end
|
|
20
40
|
|
|
21
|
-
test 'SimpleForm
|
|
41
|
+
test 'SimpleForm does not use default browser validations if specified in the configuration options' do
|
|
22
42
|
swap SimpleForm, browser_validations: false do
|
|
23
43
|
with_concat_form_for(:user)
|
|
24
44
|
assert_select 'form[novalidate="novalidate"]'
|
|
@@ -37,57 +57,57 @@ class FormHelperTest < ActionView::TestCase
|
|
|
37
57
|
end
|
|
38
58
|
end
|
|
39
59
|
|
|
40
|
-
test 'SimpleForm
|
|
60
|
+
test 'SimpleForm adds object name as css class to form when object is not present' do
|
|
41
61
|
with_concat_form_for(:user, html: { novalidate: true })
|
|
42
62
|
assert_select 'form.simple_form.user'
|
|
43
63
|
end
|
|
44
64
|
|
|
45
|
-
test 'SimpleForm
|
|
65
|
+
test 'SimpleForm adds :as option as css class to form when object is not present' do
|
|
46
66
|
with_concat_form_for(:user, as: 'superuser')
|
|
47
67
|
assert_select 'form.simple_form.superuser'
|
|
48
68
|
end
|
|
49
69
|
|
|
50
|
-
test 'SimpleForm
|
|
70
|
+
test 'SimpleForm adds object class name with new prefix as css class to form if record is not persisted' do
|
|
51
71
|
@user.new_record!
|
|
52
72
|
with_concat_form_for(@user)
|
|
53
73
|
assert_select 'form.simple_form.new_user'
|
|
54
74
|
end
|
|
55
75
|
|
|
56
|
-
test 'SimpleForm
|
|
76
|
+
test 'SimpleForm adds :as option with new prefix as css class to form if record is not persisted' do
|
|
57
77
|
@user.new_record!
|
|
58
78
|
with_concat_form_for(@user, as: 'superuser')
|
|
59
79
|
assert_select 'form.simple_form.new_superuser'
|
|
60
80
|
end
|
|
61
81
|
|
|
62
|
-
test 'SimpleForm
|
|
82
|
+
test 'SimpleForm adds edit class prefix as css class to form if record is persisted' do
|
|
63
83
|
with_concat_form_for(@user)
|
|
64
84
|
assert_select 'form.simple_form.edit_user'
|
|
65
85
|
end
|
|
66
86
|
|
|
67
|
-
test 'SimpleForm
|
|
87
|
+
test 'SimpleForm adds :as options with edit prefix as css class to form if record is persisted' do
|
|
68
88
|
with_concat_form_for(@user, as: 'superuser')
|
|
69
89
|
assert_select 'form.simple_form.edit_superuser'
|
|
70
90
|
end
|
|
71
91
|
|
|
72
|
-
test 'SimpleForm
|
|
92
|
+
test 'SimpleForm adds last object name as css class to form when there is array of objects' do
|
|
73
93
|
with_concat_form_for([Company.new, @user])
|
|
74
94
|
assert_select 'form.simple_form.edit_user'
|
|
75
95
|
end
|
|
76
96
|
|
|
77
|
-
test 'SimpleForm
|
|
97
|
+
test 'SimpleForm does not add object class to form if css_class is specified' do
|
|
78
98
|
with_concat_form_for(:user, html: { class: nil })
|
|
79
99
|
assert_no_select 'form.user'
|
|
80
100
|
end
|
|
81
101
|
|
|
82
|
-
test 'SimpleForm
|
|
102
|
+
test 'SimpleForm adds custom class to form if css_class is specified' do
|
|
83
103
|
with_concat_form_for(:user, html: { class: 'my_class' })
|
|
84
104
|
assert_select 'form.my_class'
|
|
85
105
|
end
|
|
86
106
|
|
|
87
|
-
test '
|
|
107
|
+
test 'passes options to SimpleForm' do
|
|
88
108
|
with_concat_form_for(:user, url: '/account', html: { id: 'my_form' })
|
|
89
109
|
assert_select 'form#my_form'
|
|
90
|
-
assert_select 'form[action
|
|
110
|
+
assert_select 'form[action="/account"]'
|
|
91
111
|
end
|
|
92
112
|
|
|
93
113
|
test 'form_for yields an instance of FormBuilder' do
|
|
@@ -132,7 +152,7 @@ class FormHelperTest < ActionView::TestCase
|
|
|
132
152
|
end
|
|
133
153
|
|
|
134
154
|
test 'SimpleForm for swaps default action view field_error_proc' do
|
|
135
|
-
expected_error_proc =
|
|
155
|
+
expected_error_proc = -> {}
|
|
136
156
|
swap SimpleForm, field_error_proc: expected_error_proc do
|
|
137
157
|
simple_form_for :user do |f|
|
|
138
158
|
assert_equal expected_error_proc, ::ActionView::Base.field_error_proc
|
|
@@ -142,7 +162,7 @@ class FormHelperTest < ActionView::TestCase
|
|
|
142
162
|
|
|
143
163
|
private
|
|
144
164
|
|
|
145
|
-
def swap_field_error_proc(expected_error_proc =
|
|
165
|
+
def swap_field_error_proc(expected_error_proc = -> {})
|
|
146
166
|
swap ActionView::Base, field_error_proc: expected_error_proc do
|
|
147
167
|
yield
|
|
148
168
|
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'test_helper'
|
|
4
|
+
|
|
5
|
+
# Module that represents a custom component.
|
|
6
|
+
module Numbers
|
|
7
|
+
def number(wrapper_options = nil)
|
|
8
|
+
@number ||= options[:number].to_s.html_safe
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Module that represents a custom component.
|
|
13
|
+
module InputGroup
|
|
14
|
+
def prepend(wrapper_options = nil)
|
|
15
|
+
span_tag = content_tag(:span, options[:prepend], class: 'input-group-text')
|
|
16
|
+
template.content_tag(:div, span_tag, class: 'input-group-prepend')
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def append(wrapper_options = nil)
|
|
20
|
+
span_tag = content_tag(:span, options[:append], class: 'input-group-text')
|
|
21
|
+
template.content_tag(:div, span_tag, class: 'input-group-append')
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
class CustomComponentsTest < ActionView::TestCase
|
|
26
|
+
test 'includes the custom components' do
|
|
27
|
+
SimpleForm.include_component Numbers
|
|
28
|
+
|
|
29
|
+
custom_wrapper = SimpleForm.build tag: :div, class: "custom_wrapper" do |b|
|
|
30
|
+
b.use :number, wrap_with: { tag: 'div', class: 'number' }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
with_form_for @user, :name, number: 1, wrapper: custom_wrapper
|
|
34
|
+
|
|
35
|
+
assert_select 'div.number', text: '1'
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
test 'includes custom components and use it as optional in the wrapper' do
|
|
39
|
+
SimpleForm.include_component InputGroup
|
|
40
|
+
|
|
41
|
+
custom_wrapper = SimpleForm.build tag: :div, class: 'custom_wrapper' do |b|
|
|
42
|
+
b.use :label
|
|
43
|
+
b.optional :prepend
|
|
44
|
+
b.use :input
|
|
45
|
+
b.use :append
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
with_form_for @user, :name, prepend: true, wrapper: custom_wrapper
|
|
49
|
+
|
|
50
|
+
assert_select 'div.input-group-prepend > span.input-group-text'
|
|
51
|
+
assert_select 'div.input-group-append > span.input-group-text'
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
test 'raises a TypeError when the component is not a Module' do
|
|
55
|
+
component = 'MyComponent'
|
|
56
|
+
|
|
57
|
+
exception = assert_raises TypeError do
|
|
58
|
+
SimpleForm.include_component(component)
|
|
59
|
+
end
|
|
60
|
+
assert_equal exception.message, "SimpleForm.include_component expects a module but got: String"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
1
2
|
# encoding: UTF-8
|
|
2
3
|
require 'test_helper'
|
|
3
4
|
|
|
@@ -7,34 +8,34 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
7
8
|
SimpleForm::Inputs::Base.reset_i18n_cache :translate_required_html
|
|
8
9
|
end
|
|
9
10
|
|
|
10
|
-
def with_label_for(object, attribute_name, type, options={})
|
|
11
|
+
def with_label_for(object, attribute_name, type, options = {})
|
|
11
12
|
with_concat_form_for(object) do |f|
|
|
12
13
|
options[:reflection] = Association.new(Company, :company, {}) if options.delete(:setup_association)
|
|
13
14
|
SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).label
|
|
14
15
|
end
|
|
15
16
|
end
|
|
16
17
|
|
|
17
|
-
test 'label
|
|
18
|
+
test 'label generates a default humanized description' do
|
|
18
19
|
with_label_for @user, :name, :string
|
|
19
20
|
assert_select 'label[for=user_name]', /Name/
|
|
20
21
|
end
|
|
21
22
|
|
|
22
|
-
test 'label
|
|
23
|
+
test 'label allows a customized description' do
|
|
23
24
|
with_label_for @user, :name, :string, label: 'My label!'
|
|
24
25
|
assert_select 'label[for=user_name]', /My label!/
|
|
25
26
|
end
|
|
26
27
|
|
|
27
|
-
test 'label
|
|
28
|
+
test 'label uses human attribute name from object when available' do
|
|
28
29
|
with_label_for @user, :description, :text
|
|
29
30
|
assert_select 'label[for=user_description]', /User Description!/
|
|
30
31
|
end
|
|
31
32
|
|
|
32
|
-
test 'label
|
|
33
|
+
test 'label uses human attribute name based on association name' do
|
|
33
34
|
with_label_for @user, :company_id, :string, setup_association: true
|
|
34
35
|
assert_select 'label', /Company Human Name!/
|
|
35
36
|
end
|
|
36
37
|
|
|
37
|
-
test 'label
|
|
38
|
+
test 'label uses i18n based on model, action, and attribute to lookup translation' do
|
|
38
39
|
@controller.action_name = "new"
|
|
39
40
|
store_translations(:en, simple_form: { labels: { user: {
|
|
40
41
|
new: { description: 'Nova descrição' }
|
|
@@ -44,7 +45,7 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
44
45
|
end
|
|
45
46
|
end
|
|
46
47
|
|
|
47
|
-
test 'label
|
|
48
|
+
test 'label fallbacks to new when action is create' do
|
|
48
49
|
@controller.action_name = "create"
|
|
49
50
|
store_translations(:en, simple_form: { labels: { user: {
|
|
50
51
|
new: { description: 'Nova descrição' }
|
|
@@ -54,7 +55,7 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
54
55
|
end
|
|
55
56
|
end
|
|
56
57
|
|
|
57
|
-
test 'label
|
|
58
|
+
test 'label does not explode while looking for i18n translation when action is not set' do
|
|
58
59
|
def @controller.action_name; nil; end
|
|
59
60
|
|
|
60
61
|
assert_nothing_raised do
|
|
@@ -63,7 +64,7 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
63
64
|
assert_select 'label[for=user_description]'
|
|
64
65
|
end
|
|
65
66
|
|
|
66
|
-
test 'label
|
|
67
|
+
test 'label uses i18n based on model and attribute to lookup translation' do
|
|
67
68
|
store_translations(:en, simple_form: { labels: { user: {
|
|
68
69
|
description: 'Descrição'
|
|
69
70
|
} } }) do
|
|
@@ -72,14 +73,14 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
72
73
|
end
|
|
73
74
|
end
|
|
74
75
|
|
|
75
|
-
test 'label
|
|
76
|
+
test 'label uses i18n under defaults to lookup translation' do
|
|
76
77
|
store_translations(:en, simple_form: { labels: { defaults: { age: 'Idade' } } }) do
|
|
77
78
|
with_label_for @user, :age, :integer
|
|
78
79
|
assert_select 'label[for=user_age]', /Idade/
|
|
79
80
|
end
|
|
80
81
|
end
|
|
81
82
|
|
|
82
|
-
test 'label
|
|
83
|
+
test 'label does not use i18n label if translate is false' do
|
|
83
84
|
swap SimpleForm, translate_labels: false do
|
|
84
85
|
store_translations(:en, simple_form: { labels: { defaults: { age: 'Idade' } } }) do
|
|
85
86
|
with_label_for @user, :age, :integer
|
|
@@ -107,7 +108,7 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
107
108
|
end
|
|
108
109
|
end
|
|
109
110
|
|
|
110
|
-
test 'label
|
|
111
|
+
test 'label does correct i18n lookup for nested models with nested translation' do
|
|
111
112
|
@user.company = Company.new(1, 'Empresa')
|
|
112
113
|
|
|
113
114
|
store_translations(:en, simple_form: { labels: {
|
|
@@ -125,7 +126,7 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
125
126
|
end
|
|
126
127
|
end
|
|
127
128
|
|
|
128
|
-
test 'label
|
|
129
|
+
test 'label does correct i18n lookup for nested models with no nested translation' do
|
|
129
130
|
@user.company = Company.new(1, 'Empresa')
|
|
130
131
|
|
|
131
132
|
store_translations(:en, simple_form: { labels: {
|
|
@@ -144,7 +145,7 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
144
145
|
end
|
|
145
146
|
end
|
|
146
147
|
|
|
147
|
-
test 'label
|
|
148
|
+
test 'label does correct i18n lookup for nested has_many models with no nested translation' do
|
|
148
149
|
@user.tags = [Tag.new(1, 'Empresa')]
|
|
149
150
|
|
|
150
151
|
store_translations(:en, simple_form: { labels: {
|
|
@@ -163,7 +164,7 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
163
164
|
end
|
|
164
165
|
end
|
|
165
166
|
|
|
166
|
-
test 'label
|
|
167
|
+
test 'label has css class from type' do
|
|
167
168
|
with_label_for @user, :name, :string
|
|
168
169
|
assert_select 'label.string'
|
|
169
170
|
with_label_for @user, :description, :text
|
|
@@ -176,8 +177,8 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
176
177
|
assert_select 'label.datetime'
|
|
177
178
|
end
|
|
178
179
|
|
|
179
|
-
test 'label
|
|
180
|
-
swap SimpleForm, generate_additional_classes_for: [
|
|
180
|
+
test 'label does not have css class from type when generate_additional_classes_for does not include :label' do
|
|
181
|
+
swap SimpleForm, generate_additional_classes_for: %i[wrapper input] do
|
|
181
182
|
with_label_for @user, :name, :string
|
|
182
183
|
assert_no_select 'label.string'
|
|
183
184
|
with_label_for @user, :description, :text
|
|
@@ -191,22 +192,22 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
191
192
|
end
|
|
192
193
|
end
|
|
193
194
|
|
|
194
|
-
test 'label
|
|
195
|
-
swap SimpleForm, generate_additional_classes_for: [
|
|
195
|
+
test 'label does not generate empty css class' do
|
|
196
|
+
swap SimpleForm, generate_additional_classes_for: %i[wrapper input] do
|
|
196
197
|
with_label_for @user, :name, :string
|
|
197
198
|
assert_no_select 'label[class]'
|
|
198
199
|
end
|
|
199
200
|
end
|
|
200
201
|
|
|
201
|
-
test 'label
|
|
202
|
+
test 'label obtains required from ActiveModel::Validations when it is included' do
|
|
202
203
|
with_label_for @validating_user, :name, :string
|
|
203
204
|
assert_select 'label.required'
|
|
204
205
|
with_label_for @validating_user, :status, :string
|
|
205
206
|
assert_select 'label.optional'
|
|
206
207
|
end
|
|
207
208
|
|
|
208
|
-
test 'label
|
|
209
|
-
swap SimpleForm, generate_additional_classes_for: [
|
|
209
|
+
test 'label does not obtain required from ActiveModel::Validations when generate_additional_classes_for does not include :label' do
|
|
210
|
+
swap SimpleForm, generate_additional_classes_for: %i[wrapper input] do
|
|
210
211
|
with_label_for @validating_user, :name, :string
|
|
211
212
|
assert_no_select 'label.required'
|
|
212
213
|
with_label_for @validating_user, :status, :string
|
|
@@ -214,48 +215,66 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
214
215
|
end
|
|
215
216
|
end
|
|
216
217
|
|
|
217
|
-
test 'label
|
|
218
|
+
test 'label allows overriding required when ActiveModel::Validations is included' do
|
|
218
219
|
with_label_for @validating_user, :name, :string, required: false
|
|
219
220
|
assert_select 'label.optional'
|
|
220
221
|
with_label_for @validating_user, :status, :string, required: true
|
|
221
222
|
assert_select 'label.required'
|
|
222
223
|
end
|
|
223
224
|
|
|
224
|
-
test 'label
|
|
225
|
+
test 'label is required by default when ActiveModel::Validations is not included' do
|
|
225
226
|
with_label_for @user, :name, :string
|
|
226
227
|
assert_select 'label.required'
|
|
227
228
|
end
|
|
228
229
|
|
|
229
|
-
test 'label
|
|
230
|
+
test 'label is able to disable required when ActiveModel::Validations is not included' do
|
|
230
231
|
with_label_for @user, :name, :string, required: false
|
|
231
232
|
assert_no_select 'label.required'
|
|
232
233
|
end
|
|
233
234
|
|
|
234
|
-
test 'label
|
|
235
|
+
test 'label adds required text when required' do
|
|
235
236
|
with_label_for @user, :name, :string
|
|
236
237
|
assert_select 'label.required abbr[title=required]', '*'
|
|
237
238
|
end
|
|
238
239
|
|
|
239
|
-
test 'label
|
|
240
|
+
test 'label does not have required text in no required inputs' do
|
|
240
241
|
with_label_for @user, :name, :string, required: false
|
|
241
242
|
assert_no_select 'form label abbr'
|
|
242
243
|
end
|
|
243
244
|
|
|
244
|
-
test 'label
|
|
245
|
+
test 'label uses i18n to find required text' do
|
|
245
246
|
store_translations(:en, simple_form: { required: { text: 'campo requerido' } }) do
|
|
246
247
|
with_label_for @user, :name, :string
|
|
247
|
-
assert_select 'form label abbr[title=campo requerido]', '*'
|
|
248
|
+
assert_select 'form label abbr[title="campo requerido"]', '*'
|
|
248
249
|
end
|
|
249
250
|
end
|
|
250
251
|
|
|
251
|
-
test 'label
|
|
252
|
+
test 'label uses custom i18n scope to find required text' do
|
|
253
|
+
store_translations(:en, my_scope: { required: { text: 'Pflichtfeld' } }) do
|
|
254
|
+
swap SimpleForm, i18n_scope: :my_scope do
|
|
255
|
+
with_label_for @user, :name, :string
|
|
256
|
+
assert_select 'form label abbr[title="Pflichtfeld"]', '*'
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
test 'label uses i18n to find required mark' do
|
|
252
262
|
store_translations(:en, simple_form: { required: { mark: '*-*' } }) do
|
|
253
263
|
with_label_for @user, :name, :string
|
|
254
264
|
assert_select 'form label abbr', '*-*'
|
|
255
265
|
end
|
|
256
266
|
end
|
|
257
267
|
|
|
258
|
-
test 'label
|
|
268
|
+
test 'label uses custom i18n scope to find required mark' do
|
|
269
|
+
store_translations(:en, my_scope: { required: { mark: '!!' } }) do
|
|
270
|
+
swap SimpleForm, i18n_scope: :my_scope do
|
|
271
|
+
with_label_for @user, :name, :string
|
|
272
|
+
assert_select 'form label abbr', '!!'
|
|
273
|
+
end
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
test 'label uses i18n to find required string tag' do
|
|
259
278
|
store_translations(:en, simple_form: { required: { html: '<span class="required" title="requerido">*</span>' } }) do
|
|
260
279
|
with_label_for @user, :name, :string
|
|
261
280
|
assert_no_select 'form label abbr'
|
|
@@ -263,37 +282,47 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
263
282
|
end
|
|
264
283
|
end
|
|
265
284
|
|
|
266
|
-
test 'label
|
|
285
|
+
test 'label uses custom i18n scope to find required string tag' do
|
|
286
|
+
store_translations(:en, my_scope: { required: { html: '<span class="mandatory" title="Pflichtfeld">!!</span>' } }) do
|
|
287
|
+
swap SimpleForm, i18n_scope: :my_scope do
|
|
288
|
+
with_label_for @user, :name, :string
|
|
289
|
+
assert_no_select 'form label abbr'
|
|
290
|
+
assert_select 'form label span.mandatory[title=Pflichtfeld]', '!!'
|
|
291
|
+
end
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
test 'label allows overwriting input id' do
|
|
267
296
|
with_label_for @user, :name, :string, input_html: { id: 'my_new_id' }
|
|
268
297
|
assert_select 'label[for=my_new_id]'
|
|
269
298
|
end
|
|
270
299
|
|
|
271
|
-
test 'label
|
|
300
|
+
test 'label allows overwriting of for attribute' do
|
|
272
301
|
with_label_for @user, :name, :string, label_html: { for: 'my_new_id' }
|
|
273
302
|
assert_select 'label[for=my_new_id]'
|
|
274
303
|
end
|
|
275
304
|
|
|
276
|
-
test 'label
|
|
305
|
+
test 'label allows overwriting of for attribute with input_html not containing id' do
|
|
277
306
|
with_label_for @user, :name, :string, label_html: { for: 'my_new_id' }, input_html: { class: 'foo' }
|
|
278
307
|
assert_select 'label[for=my_new_id]'
|
|
279
308
|
end
|
|
280
309
|
|
|
281
|
-
test 'label
|
|
310
|
+
test 'label uses default input id when it was not overridden' do
|
|
282
311
|
with_label_for @user, :name, :string, input_html: { class: 'my_new_id' }
|
|
283
312
|
assert_select 'label[for=user_name]'
|
|
284
313
|
end
|
|
285
314
|
|
|
286
|
-
test 'label
|
|
315
|
+
test 'label is generated properly when object is not present' do
|
|
287
316
|
with_label_for :project, :name, :string
|
|
288
317
|
assert_select 'label[for=project_name]', /Name/
|
|
289
318
|
end
|
|
290
319
|
|
|
291
|
-
test 'label
|
|
292
|
-
with_label_for @user, :sex, :select, collection: [
|
|
320
|
+
test 'label includes for attribute for select collection' do
|
|
321
|
+
with_label_for @user, :sex, :select, collection: %i[male female]
|
|
293
322
|
assert_select 'label[for=user_sex]'
|
|
294
323
|
end
|
|
295
324
|
|
|
296
|
-
test 'label
|
|
325
|
+
test 'label uses i18n properly when object is not present' do
|
|
297
326
|
store_translations(:en, simple_form: { labels: {
|
|
298
327
|
project: { name: 'Nome' }
|
|
299
328
|
} }) do
|
|
@@ -302,14 +331,14 @@ class IsolatedLabelTest < ActionView::TestCase
|
|
|
302
331
|
end
|
|
303
332
|
end
|
|
304
333
|
|
|
305
|
-
test 'label
|
|
334
|
+
test 'label adds required by default when object is not present' do
|
|
306
335
|
with_label_for :project, :name, :string
|
|
307
336
|
assert_select 'label.required[for=project_name]'
|
|
308
337
|
with_label_for :project, :description, :string, required: false
|
|
309
338
|
assert_no_select 'label.required[for=project_description]'
|
|
310
339
|
end
|
|
311
340
|
|
|
312
|
-
test 'label
|
|
341
|
+
test 'label adds chosen label class' do
|
|
313
342
|
swap SimpleForm, label_class: :my_custom_class do
|
|
314
343
|
with_label_for @user, :name, :string
|
|
315
344
|
assert_select 'label.my_custom_class'
|