simple_form 2.1.0 → 3.2.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.

Potentially problematic release.


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

Files changed (93) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +77 -33
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +387 -187
  5. data/lib/generators/simple_form/install_generator.rb +4 -4
  6. data/lib/generators/simple_form/templates/README +3 -4
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +45 -22
  8. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +128 -24
  9. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +87 -6
  10. data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
  11. data/lib/simple_form/action_view_extensions/builder.rb +2 -319
  12. data/lib/simple_form/action_view_extensions/form_helper.rb +8 -11
  13. data/lib/simple_form/components/errors.rb +28 -2
  14. data/lib/simple_form/components/hints.rb +8 -3
  15. data/lib/simple_form/components/html5.rb +6 -3
  16. data/lib/simple_form/components/label_input.rb +20 -2
  17. data/lib/simple_form/components/labels.rb +14 -10
  18. data/lib/simple_form/components/maxlength.rb +2 -9
  19. data/lib/simple_form/components/min_max.rb +1 -1
  20. data/lib/simple_form/components/pattern.rb +3 -3
  21. data/lib/simple_form/components/placeholders.rb +2 -2
  22. data/lib/simple_form/components/readonly.rb +1 -1
  23. data/lib/simple_form/components.rb +1 -1
  24. data/lib/simple_form/error_notification.rb +2 -2
  25. data/lib/simple_form/form_builder.rb +262 -107
  26. data/lib/simple_form/helpers.rb +6 -6
  27. data/lib/simple_form/inputs/base.rb +37 -16
  28. data/lib/simple_form/inputs/block_input.rb +2 -2
  29. data/lib/simple_form/inputs/boolean_input.rb +33 -18
  30. data/lib/simple_form/inputs/collection_input.rb +34 -13
  31. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +7 -12
  32. data/lib/simple_form/inputs/collection_select_input.rb +4 -2
  33. data/lib/simple_form/inputs/date_time_input.rb +23 -9
  34. data/lib/simple_form/inputs/file_input.rb +4 -2
  35. data/lib/simple_form/inputs/grouped_collection_select_input.rb +15 -3
  36. data/lib/simple_form/inputs/hidden_input.rb +4 -2
  37. data/lib/simple_form/inputs/numeric_input.rb +3 -8
  38. data/lib/simple_form/inputs/password_input.rb +4 -3
  39. data/lib/simple_form/inputs/priority_input.rb +4 -2
  40. data/lib/simple_form/inputs/range_input.rb +1 -1
  41. data/lib/simple_form/inputs/string_input.rb +4 -3
  42. data/lib/simple_form/inputs/text_input.rb +4 -2
  43. data/lib/simple_form/railtie.rb +14 -0
  44. data/lib/simple_form/tags.rb +68 -0
  45. data/lib/simple_form/version.rb +1 -1
  46. data/lib/simple_form/wrappers/builder.rb +11 -35
  47. data/lib/simple_form/wrappers/leaf.rb +28 -0
  48. data/lib/simple_form/wrappers/many.rb +7 -7
  49. data/lib/simple_form/wrappers/root.rb +2 -2
  50. data/lib/simple_form/wrappers/single.rb +5 -3
  51. data/lib/simple_form/wrappers.rb +2 -1
  52. data/lib/simple_form.rb +99 -52
  53. data/test/action_view_extensions/builder_test.rb +113 -127
  54. data/test/action_view_extensions/form_helper_test.rb +58 -30
  55. data/test/components/label_test.rb +83 -83
  56. data/test/form_builder/association_test.rb +96 -61
  57. data/test/form_builder/button_test.rb +14 -14
  58. data/test/form_builder/error_notification_test.rb +9 -9
  59. data/test/form_builder/error_test.rb +159 -34
  60. data/test/form_builder/general_test.rb +176 -121
  61. data/test/form_builder/hint_test.rb +43 -37
  62. data/test/form_builder/input_field_test.rb +99 -52
  63. data/test/form_builder/label_test.rb +67 -15
  64. data/test/form_builder/wrapper_test.rb +157 -41
  65. data/test/generators/simple_form_generator_test.rb +4 -4
  66. data/test/inputs/boolean_input_test.rb +92 -24
  67. data/test/inputs/collection_check_boxes_input_test.rb +150 -71
  68. data/test/inputs/collection_radio_buttons_input_test.rb +213 -113
  69. data/test/inputs/collection_select_input_test.rb +221 -85
  70. data/test/inputs/datetime_input_test.rb +125 -47
  71. data/test/inputs/disabled_test.rb +25 -25
  72. data/test/inputs/discovery_test.rb +60 -10
  73. data/test/inputs/file_input_test.rb +3 -3
  74. data/test/inputs/general_test.rb +48 -32
  75. data/test/inputs/grouped_collection_select_input_test.rb +76 -27
  76. data/test/inputs/hidden_input_test.rb +6 -5
  77. data/test/inputs/numeric_input_test.rb +46 -46
  78. data/test/inputs/priority_input_test.rb +21 -15
  79. data/test/inputs/readonly_test.rb +31 -31
  80. data/test/inputs/required_test.rb +30 -18
  81. data/test/inputs/string_input_test.rb +53 -52
  82. data/test/inputs/text_input_test.rb +15 -8
  83. data/test/simple_form_test.rb +8 -0
  84. data/test/support/discovery_inputs.rb +32 -2
  85. data/test/support/misc_helpers.rb +130 -29
  86. data/test/support/mock_controller.rb +6 -6
  87. data/test/support/models.rb +125 -71
  88. data/test/test_helper.rb +28 -35
  89. metadata +17 -29
  90. data/lib/simple_form/action_view_extensions/builder.rb.orig +0 -247
  91. data/lib/simple_form/core_ext/hash.rb +0 -16
  92. data/lib/simple_form/form_builder.rb.orig +0 -486
  93. data/lib/simple_form/version.rb.orig +0 -7
@@ -8,43 +8,49 @@ class HintTest < ActionView::TestCase
8
8
  end
9
9
  end
10
10
 
11
- test 'hint should not be generated by default' do
11
+ test 'hint does not be generated by default' do
12
12
  with_hint_for @user, :name
13
13
  assert_no_select 'span.hint'
14
14
  end
15
15
 
16
- test 'hint should be generated with optional text' do
17
- with_hint_for @user, :name, :hint => 'Use with care...'
16
+ test 'hint is generated with optional text' do
17
+ with_hint_for @user, :name, hint: 'Use with care...'
18
18
  assert_select 'span.hint', 'Use with care...'
19
19
  end
20
20
 
21
- test 'hint should not modify the options hash' do
22
- options = { :hint => 'Use with care...' }
21
+ test 'hint does not modify the options hash' do
22
+ options = { hint: 'Use with care...' }
23
23
  with_hint_for @user, :name, options
24
24
  assert_select 'span.hint', 'Use with care...'
25
- assert_equal({ :hint => 'Use with care...' }, options)
25
+ assert_equal({ hint: 'Use with care...' }, options)
26
26
  end
27
27
 
28
- test 'hint should be generated cleanly with optional text' do
29
- with_hint_for @user, :name, :hint => 'Use with care...', :hint_tag => :span
28
+ test 'hint is generated cleanly with optional text' do
29
+ with_hint_for @user, :name, hint: 'Use with care...', hint_tag: :span
30
30
  assert_no_select 'span.hint[hint]'
31
31
  assert_no_select 'span.hint[hint_tag]'
32
32
  assert_no_select 'span.hint[hint_html]'
33
33
  end
34
34
 
35
35
  test 'hint uses the current component tag set' do
36
- with_hint_for @user, :name, :hint => 'Use with care...', :hint_tag => :p
36
+ with_hint_for @user, :name, hint: 'Use with care...', hint_tag: :p
37
37
  assert_select 'p.hint', 'Use with care...'
38
38
  end
39
39
 
40
- test 'hint should be able to pass html options' do
41
- with_hint_for @user, :name, :hint => 'Yay!', :id => 'hint', :class => 'yay'
40
+ test 'hint is able to pass html options' do
41
+ with_hint_for @user, :name, hint: 'Yay!', id: 'hint', class: 'yay'
42
42
  assert_select 'span#hint.hint.yay'
43
43
  end
44
44
 
45
- test 'hint should be output as html_safe' do
46
- with_hint_for @user, :name, :hint => '<b>Bold</b> and not...'
45
+ test 'hint is output as html_safe' do
46
+ with_hint_for @user, :name, hint: '<b>Bold</b> and not...'.html_safe
47
47
  assert_select 'span.hint', 'Bold and not...'
48
+ assert_select 'span.hint b', 'Bold'
49
+ end
50
+
51
+ test 'builder escapes hint text' do
52
+ with_hint_for @user, :name, hint: '<script>alert(1337)</script>'
53
+ assert_no_select 'span.hint script'
48
54
  end
49
55
 
50
56
  # Without attribute name
@@ -54,65 +60,65 @@ class HintTest < ActionView::TestCase
54
60
  assert_select 'span.hint', 'Hello World!'
55
61
  end
56
62
 
57
- test 'hint without attribute name should generate component tag with a clean HTML' do
63
+ test 'hint without attribute name generates component tag with a clean HTML' do
58
64
  with_hint_for @validating_user, 'Hello World!'
59
65
  assert_no_select 'span.hint[hint]'
60
66
  assert_no_select 'span.hint[hint_html]'
61
67
  end
62
68
 
63
69
  test 'hint without attribute name uses the current component tag set' do
64
- with_hint_for @user, 'Hello World!', :hint_tag => :p
70
+ with_hint_for @user, 'Hello World!', hint_tag: :p
65
71
  assert_no_select 'p.hint[hint]'
66
72
  assert_no_select 'p.hint[hint_html]'
67
73
  assert_no_select 'p.hint[hint_tag]'
68
74
  end
69
75
 
70
- test 'hint without attribute name should be able to pass html options' do
71
- with_hint_for @user, 'Yay', :id => 'hint', :class => 'yay'
76
+ test 'hint without attribute name is able to pass html options' do
77
+ with_hint_for @user, 'Yay', id: 'hint', class: 'yay'
72
78
  assert_select 'span#hint.hint.yay', 'Yay'
73
79
  end
74
80
 
75
81
  # I18n
76
82
 
77
- test 'hint should use i18n based on model, action, and attribute to lookup translation' do
78
- store_translations(:en, :simple_form => { :hints => { :user => {
79
- :edit => { :name => 'Content of this input will be truncated...' }
83
+ test 'hint uses i18n based on model, action, and attribute to lookup translation' do
84
+ store_translations(:en, simple_form: { hints: { user: {
85
+ edit: { name: 'Content of this input will be truncated...' }
80
86
  } } }) do
81
87
  with_hint_for @user, :name
82
88
  assert_select 'span.hint', 'Content of this input will be truncated...'
83
89
  end
84
90
  end
85
91
 
86
- test 'hint should use i18n with model and attribute to lookup translation' do
87
- store_translations(:en, :simple_form => { :hints => { :user => {
88
- :name => 'Content of this input will be capitalized...'
92
+ test 'hint uses i18n with model and attribute to lookup translation' do
93
+ store_translations(:en, simple_form: { hints: { user: {
94
+ name: 'Content of this input will be capitalized...'
89
95
  } } }) do
90
96
  with_hint_for @user, :name
91
97
  assert_select 'span.hint', 'Content of this input will be capitalized...'
92
98
  end
93
99
  end
94
100
 
95
- test 'hint should use i18n under defaults namespace to lookup translation' do
96
- store_translations(:en, :simple_form => {
97
- :hints => {:defaults => {:name => 'Content of this input will be downcased...' } }
101
+ test 'hint uses i18n under defaults namespace to lookup translation' do
102
+ store_translations(:en, simple_form: {
103
+ hints: { defaults: { name: 'Content of this input will be downcased...' } }
98
104
  }) do
99
105
  with_hint_for @user, :name
100
106
  assert_select 'span.hint', 'Content of this input will be downcased...'
101
107
  end
102
108
  end
103
109
 
104
- test 'hint should use i18n with lookup for association name' do
105
- store_translations(:en, :simple_form => { :hints => {
106
- :user => { :company => 'My company!' }
110
+ test 'hint uses i18n with lookup for association name' do
111
+ store_translations(:en, simple_form: { hints: {
112
+ user: { company: 'My company!' }
107
113
  } } ) do
108
- with_hint_for @user, :company_id, :as => :string, :reflection => Association.new(Company, :company, {})
114
+ with_hint_for @user, :company_id, as: :string, reflection: Association.new(Company, :company, {})
109
115
  assert_select 'span.hint', /My company!/
110
116
  end
111
117
  end
112
118
 
113
- test 'hint should output translations as html_safe' do
114
- store_translations(:en, :simple_form => { :hints => { :user => {
115
- :edit => { :name => '<b>This is bold</b> and this is not...' }
119
+ test 'hint outputs translations as html_safe' do
120
+ store_translations(:en, simple_form: { hints: { user: {
121
+ edit: { name: '<b>This is bold</b> and this is not...' }
116
122
  } } }) do
117
123
  with_hint_for @user, :name
118
124
  assert_select 'span.hint', 'This is bold and this is not...'
@@ -122,8 +128,8 @@ class HintTest < ActionView::TestCase
122
128
 
123
129
  # No object
124
130
 
125
- test 'hint should generate properly when object is not present' do
126
- with_hint_for :project, :name, :hint => 'Test without object'
131
+ test 'hint generates properly when object is not present' do
132
+ with_hint_for :project, :name, hint: 'Test without object'
127
133
  assert_select 'span.hint', 'Test without object'
128
134
  end
129
135
 
@@ -131,8 +137,8 @@ class HintTest < ActionView::TestCase
131
137
 
132
138
  test 'hint with custom wrappers works' do
133
139
  swap_wrapper do
134
- with_hint_for @user, :name, :hint => "can't be blank"
135
- assert_select 'div.omg_hint', "can't be blank"
140
+ with_hint_for @user, :name, hint: "cannot be blank"
141
+ assert_select 'div.omg_hint', "cannot be blank"
136
142
  end
137
143
  end
138
144
  end
@@ -2,107 +2,154 @@ require 'test_helper'
2
2
 
3
3
  # Tests for f.input_field
4
4
  class InputFieldTest < ActionView::TestCase
5
- test "builder input_field should only render the input tag, nothing else" do
6
- with_concat_form_for(@user) do |f|
7
- f.input_field :name
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 should allow overriding default input type' do
16
- with_concat_form_for(@user) do |f|
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 should allow passing options to input tag' do
25
- with_concat_form_for(@user) do |f|
26
- f.input_field :name, :id => 'name_input', :class => 'name'
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
- assert_select 'input.string.name#name_input'
30
+ assert_select 'input[type=number].integer#user_age'
30
31
  end
31
32
 
32
- test 'builder input_field should not modify the options hash' do
33
- options = { :id => 'name_input', :class => 'name' }
33
+ test 'builder input_field is able to disable any component' do
34
+ with_input_field_for @user, :age, html5: false
34
35
 
35
- with_concat_form_for(@user) do |f|
36
- f.input_field :name, options
37
- end
36
+ assert_no_select 'input[html5=false]#user_age'
37
+ assert_select 'input[type=text].integer#user_age'
38
+ end
39
+
40
+ test 'builder input_field allows passing options to input tag' do
41
+ with_input_field_for @user, :name, id: 'name_input', class: 'name'
38
42
 
39
43
  assert_select 'input.string.name#name_input'
40
- assert_equal({ :id => 'name_input', :class => 'name' }, options)
41
44
  end
42
45
 
46
+ test 'builder input_field does not modify the options hash' do
47
+ options = { id: 'name_input', class: 'name' }
48
+ with_input_field_for @user, :name, options
43
49
 
44
- test 'builder input_field should generate an input tag with a clean HTML' do
45
- with_concat_form_for(@user) do |f|
46
- f.input_field :name, :as => :integer, :class => 'name'
47
- end
50
+ assert_select 'input.string.name#name_input'
51
+ assert_equal({ id: 'name_input', class: 'name' }, options)
52
+ end
53
+
54
+
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'
48
57
 
49
58
  assert_no_select 'input.integer[input_html]'
50
59
  assert_no_select 'input.integer[as]'
51
60
  end
52
61
 
53
- test 'builder input_field should use i18n to translate placeholder text' do
54
- store_translations(:en, :simple_form => { :placeholders => { :user => {
55
- :name => 'Name goes here'
62
+ test 'builder input_field uses i18n to translate placeholder text' do
63
+ store_translations(:en, simple_form: { placeholders: { user: {
64
+ name: 'Name goes here'
56
65
  } } }) do
66
+ with_input_field_for @user, :name
57
67
 
58
- with_concat_form_for(@user) do |f|
59
- f.input_field :name
60
- end
61
-
62
- assert_select 'input.string[placeholder=Name goes here]'
68
+ assert_select 'input.string[placeholder="Name goes here"]'
63
69
  end
64
70
  end
65
71
 
66
- test 'builder input_field should use min_max component' do
67
- with_concat_form_for(@other_validating_user) do |f|
68
- f.input_field :age, :as => :integer
69
- end
72
+ test 'builder input_field uses min_max component' do
73
+ with_input_field_for @other_validating_user, :age, as: :integer
70
74
 
71
- assert_select 'input[min=18]'
75
+ assert_select 'input[min="18"]'
72
76
  end
73
77
 
74
- test 'builder input_field should use pattern component' do
75
- with_concat_form_for(@other_validating_user) do |f|
76
- f.input_field :country, :as => :string
77
- end
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
78
86
 
79
87
  assert_select 'input[pattern="\w+"]'
80
88
  end
81
89
 
82
- test 'builder input_field should use readonly component' do
83
- with_concat_form_for(@other_validating_user) do |f|
84
- f.input_field :age, :as => :integer, :readonly => true
85
- end
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
86
98
 
87
99
  assert_select 'input.integer.readonly[readonly]'
88
100
  end
89
101
 
90
- test 'builder input_field should use maxlength component' do
91
- with_concat_form_for(@validating_user) do |f|
92
- f.input_field :name, :as => :string
93
- end
102
+ test 'builder input_field uses maxlength component' do
103
+ with_input_field_for @validating_user, :name, as: :string
94
104
 
95
- assert_select 'input.string[maxlength=25]'
105
+ assert_select 'input.string[maxlength="25"]'
96
106
  end
97
107
 
98
- test 'builder collection input_field should generate input tag with a clean HTML' do
99
- with_concat_form_for(@user) do |f|
100
- f.input_field :status, :collection => ['Open', 'Closed'], :class => 'status', :label_method => :to_s, :value_method => :to_s
101
- 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
102
111
 
103
112
  assert_no_select 'select.status[input_html]'
104
113
  assert_no_select 'select.status[collection]'
105
114
  assert_no_select 'select.status[label_method]'
106
115
  assert_no_select 'select.status[value_method]'
107
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
108
155
  end
@@ -8,53 +8,73 @@ class LabelTest < ActionView::TestCase
8
8
  end
9
9
  end
10
10
 
11
- test 'builder should generate a label for the attribute' do
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 should generate a label for the boolean attrbiute' do
17
- with_label_for @user, :name, :as => :boolean
16
+ test 'builder generates a label for the boolean attrbiute' do
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 should generate a label componet tag with a clean HTML' do
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 should add a required class to label if the attribute is required' do
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 should allow passing options to label tag' do
33
- with_label_for @user, :name, :label => 'My label', :id => 'name_label'
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
43
+ with_label_for @user, :name, label: '<script>alert(1337)</script>', required: false
44
+ assert_no_select 'label.string script'
45
+ end
46
+
47
+ test 'builder does not escape label text if it is safe' do
48
+ with_label_for @user, :name, label: '<script>alert(1337)</script>'.html_safe, required: false
49
+ assert_select 'label.string script', "alert(1337)"
50
+ end
51
+
52
+ test 'builder allows passing options to label tag' do
53
+ with_label_for @user, :name, label: 'My label', id: 'name_label'
34
54
  assert_select 'label.string#name_label', /My label/
35
55
  end
36
56
 
37
- test 'builder label should generate label tag with clean HTML' do
38
- with_label_for @user, :name, :label => 'My label', :required => true, :id => 'name_label'
57
+ test 'builder label generates label tag with clean HTML' do
58
+ with_label_for @user, :name, label: 'My label', required: true, id: 'name_label'
39
59
  assert_select 'label.string#name_label', /My label/
40
60
  assert_no_select 'label[label]'
41
61
  assert_no_select 'label[required]'
42
62
  end
43
63
 
44
- test 'builder should not modify the options hash' do
45
- options = { :label => 'My label', :id => 'name_label' }
64
+ test 'builder does not modify the options hash' do
65
+ options = { label: 'My label', id: 'name_label' }
46
66
  with_label_for @user, :name, options
47
67
  assert_select 'label.string#name_label', /My label/
48
- assert_equal({ :label => 'My label', :id => 'name_label' }, options)
68
+ assert_equal({ label: 'My label', id: 'name_label' }, options)
49
69
  end
50
70
 
51
- test 'builder should fallback to default label when string is given' do
71
+ test 'builder fallbacks to default label when string is given' do
52
72
  with_label_for @user, :name, 'Nome do usuário'
53
73
  assert_select 'label', 'Nome do usuário'
54
74
  assert_no_select 'label.string'
55
75
  end
56
76
 
57
- test 'builder should fallback to default label when block is given' do
77
+ test 'builder fallbacks to default label when block is given' do
58
78
  with_label_for @user, :name do
59
79
  'Nome do usuário'
60
80
  end
@@ -63,9 +83,41 @@ class LabelTest < ActionView::TestCase
63
83
  end
64
84
 
65
85
  test 'builder allows label order to be changed' do
66
- swap SimpleForm, :label_text => lambda { |l, r| "#{l}:" } do
86
+ swap SimpleForm, label_text: proc { |l, r| "#{l}:" } do
67
87
  with_label_for @user, :age
68
88
  assert_select 'label.integer[for=user_age]', "Age:"
69
89
  end
70
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
71
123
  end