simple_form 3.0.1 → 3.1.0.rc1

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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -27
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +146 -71
  5. data/lib/generators/simple_form/install_generator.rb +2 -2
  6. data/lib/generators/simple_form/templates/README +3 -4
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +19 -3
  8. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +83 -22
  9. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +1 -1
  10. data/lib/generators/simple_form/templates/config/locales/simple_form.en.yml +7 -2
  11. data/lib/simple_form/action_view_extensions/form_helper.rb +1 -1
  12. data/lib/simple_form/components/errors.rb +30 -2
  13. data/lib/simple_form/components/hints.rb +2 -2
  14. data/lib/simple_form/components/html5.rb +1 -1
  15. data/lib/simple_form/components/label_input.rb +20 -2
  16. data/lib/simple_form/components/labels.rb +9 -5
  17. data/lib/simple_form/components/maxlength.rb +1 -1
  18. data/lib/simple_form/components/min_max.rb +1 -1
  19. data/lib/simple_form/components/pattern.rb +1 -1
  20. data/lib/simple_form/components/placeholders.rb +2 -2
  21. data/lib/simple_form/components/readonly.rb +1 -1
  22. data/lib/simple_form/form_builder.rb +92 -57
  23. data/lib/simple_form/helpers.rb +5 -5
  24. data/lib/simple_form/inputs/base.rb +33 -11
  25. data/lib/simple_form/inputs/block_input.rb +1 -1
  26. data/lib/simple_form/inputs/boolean_input.rb +23 -13
  27. data/lib/simple_form/inputs/collection_input.rb +32 -9
  28. data/lib/simple_form/inputs/collection_radio_buttons_input.rb +6 -11
  29. data/lib/simple_form/inputs/collection_select_input.rb +4 -2
  30. data/lib/simple_form/inputs/date_time_input.rb +12 -2
  31. data/lib/simple_form/inputs/file_input.rb +4 -2
  32. data/lib/simple_form/inputs/grouped_collection_select_input.rb +15 -3
  33. data/lib/simple_form/inputs/hidden_input.rb +4 -2
  34. data/lib/simple_form/inputs/numeric_input.rb +5 -4
  35. data/lib/simple_form/inputs/password_input.rb +4 -2
  36. data/lib/simple_form/inputs/priority_input.rb +4 -2
  37. data/lib/simple_form/inputs/range_input.rb +1 -1
  38. data/lib/simple_form/inputs/string_input.rb +4 -2
  39. data/lib/simple_form/inputs/text_input.rb +4 -2
  40. data/lib/simple_form/railtie.rb +7 -0
  41. data/lib/simple_form/tags.rb +7 -0
  42. data/lib/simple_form/version.rb +1 -1
  43. data/lib/simple_form/wrappers/builder.rb +5 -5
  44. data/lib/simple_form/wrappers/leaf.rb +28 -0
  45. data/lib/simple_form/wrappers/many.rb +5 -6
  46. data/lib/simple_form/wrappers/root.rb +1 -1
  47. data/lib/simple_form/wrappers/single.rb +5 -3
  48. data/lib/simple_form/wrappers.rb +1 -0
  49. data/lib/simple_form.rb +38 -6
  50. data/test/action_view_extensions/builder_test.rb +2 -2
  51. data/test/components/label_test.rb +1 -1
  52. data/test/form_builder/association_test.rb +17 -0
  53. data/test/form_builder/error_notification_test.rb +1 -1
  54. data/test/form_builder/error_test.rb +61 -0
  55. data/test/form_builder/input_field_test.rb +25 -1
  56. data/test/form_builder/label_test.rb +24 -1
  57. data/test/form_builder/wrapper_test.rb +67 -0
  58. data/test/generators/simple_form_generator_test.rb +2 -2
  59. data/test/inputs/boolean_input_test.rb +50 -2
  60. data/test/inputs/collection_check_boxes_input_test.rb +40 -11
  61. data/test/inputs/collection_radio_buttons_input_test.rb +76 -17
  62. data/test/inputs/collection_select_input_test.rb +108 -3
  63. data/test/inputs/datetime_input_test.rb +105 -38
  64. data/test/inputs/discovery_test.rb +12 -1
  65. data/test/inputs/grouped_collection_select_input_test.rb +36 -0
  66. data/test/inputs/string_input_test.rb +20 -0
  67. data/test/simple_form_test.rb +8 -0
  68. data/test/support/discovery_inputs.rb +12 -2
  69. data/test/support/misc_helpers.rb +49 -5
  70. data/test/support/models.rb +49 -24
  71. data/test/test_helper.rb +2 -0
  72. metadata +23 -34
@@ -17,7 +17,7 @@ module SimpleForm
17
17
 
18
18
  # Provide a fallback if name cannot be found.
19
19
  def find(name)
20
- super || SimpleForm::Wrappers::Many.new(name, [name])
20
+ super || SimpleForm::Wrappers::Many.new(name, [Leaf.new(name)])
21
21
  end
22
22
 
23
23
  private
@@ -2,14 +2,16 @@ module SimpleForm
2
2
  module Wrappers
3
3
  # `Single` is an optimization for a wrapper that has only one component.
4
4
  class Single < Many
5
- def initialize(name, options={})
6
- super(name, [name], options)
5
+ def initialize(name, wrapper_options = {}, options = {})
6
+ @component = Leaf.new(name, options)
7
+
8
+ super(name, [@component], wrapper_options)
7
9
  end
8
10
 
9
11
  def render(input)
10
12
  options = input.options
11
13
  if options[namespace] != false
12
- content = input.send(namespace)
14
+ content = @component.render(input)
13
15
  wrap(input, options, content) if content
14
16
  end
15
17
  end
@@ -4,5 +4,6 @@ module SimpleForm
4
4
  autoload :Many, 'simple_form/wrappers/many'
5
5
  autoload :Root, 'simple_form/wrappers/root'
6
6
  autoload :Single, 'simple_form/wrappers/single'
7
+ autoload :Leaf, 'simple_form/wrappers/leaf'
7
8
  end
8
9
  end
data/lib/simple_form.rb CHANGED
@@ -24,6 +24,24 @@ module SimpleForm
24
24
  SimpleForm::Components.eager_load!
25
25
  end
26
26
 
27
+ CUSTOM_INPUT_DEPRECATION_WARN = <<-WARN
28
+ %{name} method now accepts a `wrapper_options` argument. The method definition without the argument is deprecated and will be removed in the next Simple Form version. Change your code from:
29
+
30
+ def %{name}
31
+
32
+ to
33
+
34
+ def %{name}(wrapper_options)
35
+
36
+ See https://github.com/plataformatec/simple_form/pull/997 for more information.
37
+ WARN
38
+
39
+ @@configured = false
40
+
41
+ def self.configured? #:nodoc:
42
+ @@configured
43
+ end
44
+
27
45
  ## CONFIGURATION OPTIONS
28
46
 
29
47
  # Method used to tidy up errors.
@@ -66,7 +84,7 @@ module SimpleForm
66
84
 
67
85
  # How the label text should be generated altogether with the required text.
68
86
  mattr_accessor :label_text
69
- @@label_text = lambda { |label, required| "#{required} #{label}" }
87
+ @@label_text = lambda { |label, required, explicit_label| "#{required} #{label}" }
70
88
 
71
89
  # You can define the class to be used on all labels. Defaults to none.
72
90
  mattr_accessor :label_class
@@ -107,6 +125,8 @@ module SimpleForm
107
125
  # Custom wrappers for input types. This should be a hash containing an input
108
126
  # type as key and the wrapper that will be used for all inputs with specified type.
109
127
  # e.g { string: :string_wrapper, boolean: :boolean_wrapper }
128
+ # You can also set a wrapper mapping per form basis.
129
+ # e.g simple_form_for(@foo, wrapper_mappings: { check_boxes: :bootstrap_checkbox })
110
130
  mattr_accessor :wrapper_mappings
111
131
  @@wrapper_mappings = nil
112
132
 
@@ -151,15 +171,26 @@ module SimpleForm
151
171
  mattr_accessor :input_class
152
172
  @@input_class = nil
153
173
 
174
+ # Defines if an input wrapper class should be included or not
175
+ mattr_accessor :include_default_input_wrapper_class
176
+ @@include_default_input_wrapper_class = true
177
+
178
+ # Define the default class of the input wrapper of the boolean input.
179
+ mattr_accessor :boolean_label_class
180
+ @@boolean_label_class = 'checkbox'
181
+
154
182
  ## WRAPPER CONFIGURATION
155
183
  # The default wrapper to be used by the FormBuilder.
156
184
  mattr_accessor :default_wrapper
157
185
  @@default_wrapper = :default
158
- @@wrappers = {}
186
+ @@wrappers = {} #:nodoc:
187
+
188
+ mattr_accessor :i18n_scope
189
+ @@i18n_scope = 'simple_form'
159
190
 
160
191
  # Retrieves a given wrapper
161
192
  def self.wrapper(name)
162
- @@wrappers[name.to_sym] or raise WrapperNotFound, "Couldn't find wrapper with name #{name}"
193
+ @@wrappers[name.to_s] or raise WrapperNotFound, "Couldn't find wrapper with name #{name}"
163
194
  end
164
195
 
165
196
  # Raised when fails to find a given wrapper name
@@ -172,14 +203,14 @@ module SimpleForm
172
203
  if block_given?
173
204
  options = args.extract_options!
174
205
  name = args.first || :default
175
- @@wrappers[name.to_sym] = build(options, &block)
206
+ @@wrappers[name.to_s] = build(options, &block)
176
207
  else
177
208
  @@wrappers
178
209
  end
179
210
  end
180
211
 
181
212
  # Builds a new wrapper using SimpleForm::Wrappers::Builder.
182
- def self.build(options={})
213
+ def self.build(options = {})
183
214
  options[:tag] = :div if options[:tag].nil?
184
215
  builder = SimpleForm::Wrappers::Builder.new(options)
185
216
  yield builder
@@ -210,9 +241,10 @@ module SimpleForm
210
241
  ActiveSupport::Deprecation.warn "[SIMPLE_FORM] SimpleForm.default_input_size= is deprecated and has no effect", caller
211
242
  end
212
243
 
213
- # Default way to setup SimpleForm. Run rails generate simple_form:install
244
+ # Default way to setup Simple Form. Run rails generate simple_form:install
214
245
  # to create a fresh initializer with all configuration values.
215
246
  def self.setup
247
+ @@configured = true
216
248
  yield self
217
249
  end
218
250
  end
@@ -8,13 +8,13 @@ class BuilderTest < ActionView::TestCase
8
8
  end
9
9
  end
10
10
 
11
- def with_collection_radio_buttons(object, attribute, collection, value_method, text_method, options={}, html_options={}, &block)
11
+ def with_collection_radio_buttons(object, attribute, collection, value_method, text_method, options = {}, html_options = {}, &block)
12
12
  with_concat_form_for(object) do |f|
13
13
  f.collection_radio_buttons attribute, collection, value_method, text_method, options, html_options, &block
14
14
  end
15
15
  end
16
16
 
17
- def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options={}, html_options={}, &block)
17
+ def with_collection_check_boxes(object, attribute, collection, value_method, text_method, options = {}, html_options = {}, &block)
18
18
  with_concat_form_for(object) do |f|
19
19
  f.collection_check_boxes attribute, collection, value_method, text_method, options, html_options, &block
20
20
  end
@@ -7,7 +7,7 @@ class IsolatedLabelTest < ActionView::TestCase
7
7
  SimpleForm::Inputs::Base.reset_i18n_cache :translate_required_html
8
8
  end
9
9
 
10
- def with_label_for(object, attribute_name, type, options={})
10
+ def with_label_for(object, attribute_name, type, options = {})
11
11
  with_concat_form_for(object) do |f|
12
12
  options[:reflection] = Association.new(Company, :company, {}) if options.delete(:setup_association)
13
13
  SimpleForm::Inputs::Base.new(f, attribute_name, nil, type, options).label
@@ -113,6 +113,14 @@ class AssociationTest < ActionView::TestCase
113
113
  assert_no_select 'form select option[value=3]'
114
114
  end
115
115
 
116
+ test 'builder allows collection to have a scope' do
117
+ with_association_for @user, :special_pictures
118
+ assert_select 'form select.select#user_special_picture_ids'
119
+ assert_select 'form select option[value=3]', '3'
120
+ assert_no_select 'form select option[value=1]'
121
+ assert_no_select 'form select option[value=2]'
122
+ end
123
+
116
124
  test 'builder marks the record which already belongs to the user' do
117
125
  @user.company_id = 2
118
126
  with_association_for @user, :company, as: :radio_buttons
@@ -146,6 +154,15 @@ class AssociationTest < ActionView::TestCase
146
154
  end
147
155
  end
148
156
 
157
+ test 'builder does not call order if the given association does not respond to it' do
158
+ with_association_for @user, :pictures
159
+ assert_select 'form select.select#user_picture_ids'
160
+ assert_select 'form select[multiple=multiple]'
161
+ assert_select 'form select option[value=1]', 'Picture 1'
162
+ assert_select 'form select option[value=2]', 'Picture 2'
163
+ assert_select 'form select option[value=3]', 'Picture 3'
164
+ end
165
+
149
166
  test 'builder creates a select with multiple options for collection associations' do
150
167
  with_association_for @user, :tags
151
168
  assert_select 'form select.select#user_tag_ids'
@@ -3,7 +3,7 @@ require 'test_helper'
3
3
 
4
4
  # Tests for f.error_notification
5
5
  class ErrorNotificationTest < ActionView::TestCase
6
- def with_error_notification_for(object, options={}, &block)
6
+ def with_error_notification_for(object, options = {}, &block)
7
7
  with_concat_form_for(object) do |f|
8
8
  f.error_notification(options)
9
9
  end
@@ -123,4 +123,65 @@ class ErrorTest < ActionView::TestCase
123
123
  assert_select 'span.omg_error', "can't be blank"
124
124
  end
125
125
  end
126
+
127
+ # FULL_ERROR_WRAPPER
128
+
129
+ test 'full error should find errors on association' do
130
+ swap_wrapper :default, self.custom_wrapper_with_full_error do
131
+ with_form_for @user, :company_id, as: :select
132
+ assert_select 'span.error', 'Company must be valid'
133
+ end
134
+ end
135
+
136
+ test 'full error can be disabled' do
137
+ swap_wrapper :default, self.custom_wrapper_with_full_error do
138
+ with_form_for @user, :company_id, as: :select, full_error: false
139
+ assert_no_select 'span.error'
140
+ end
141
+ end
142
+
143
+ test 'full error can be disabled setting error to false' do
144
+ swap_wrapper :default, self.custom_wrapper_with_full_error do
145
+ with_form_for @user, :company_id, as: :select, error: false
146
+ assert_no_select 'span.error'
147
+ end
148
+ end
149
+
150
+ # CUSTOM ERRORS
151
+
152
+ test 'input with custom error works' do
153
+ error_text = "Super User Name! can't be blank"
154
+ with_form_for @user, :name, error: error_text
155
+
156
+ assert_select 'span.error', error_text
157
+ end
158
+
159
+ test 'input with error option as true does not use custom error' do
160
+ with_form_for @user, :name, error: true
161
+
162
+ assert_select 'span.error', "can't be blank"
163
+ end
164
+
165
+ test 'input with custom error does not generate the error if there is no error on the attribute' do
166
+ with_form_for @user, :active, error: "Super User Active! can't be blank"
167
+
168
+ assert_no_select 'span.error'
169
+ end
170
+
171
+ test 'input with custom error works when using full_error component' do
172
+ swap_wrapper :default, self.custom_wrapper_with_full_error do
173
+ error_text = "Super User Name! can't be blank"
174
+ with_form_for @user, :name, error: error_text
175
+
176
+ assert_select 'span.error', error_text
177
+ end
178
+ end
179
+
180
+ test 'input with custom error when using full_error component does not generate the error if there is no error on the attribute' do
181
+ swap_wrapper :default, self.custom_wrapper_with_full_error do
182
+ with_form_for @user, :active, error: "Super User Active! can't be blank"
183
+
184
+ assert_no_select 'span.error'
185
+ end
186
+ end
126
187
  end
@@ -88,14 +88,30 @@ class InputFieldTest < ActionView::TestCase
88
88
  assert_select 'input[min=18]'
89
89
  end
90
90
 
91
- test 'builder input_field should use pattern component' do
91
+ test 'builder input_field should not use pattern component by default' do
92
92
  with_concat_form_for(@other_validating_user) do |f|
93
93
  f.input_field :country, as: :string
94
94
  end
95
95
 
96
+ assert_no_select 'input[pattern="\w+"]'
97
+ end
98
+
99
+ test 'builder input_field should infer pattern from attributes' do
100
+ with_concat_form_for(@other_validating_user) do |f|
101
+ f.input_field :country, as: :string, pattern: true
102
+ end
103
+
96
104
  assert_select 'input[pattern="\w+"]'
97
105
  end
98
106
 
107
+ test 'builder input_field should accept custom patter' do
108
+ with_concat_form_for(@other_validating_user) do |f|
109
+ f.input_field :country, as: :string, pattern: '\d+'
110
+ end
111
+
112
+ assert_select 'input[pattern="\d+"]'
113
+ end
114
+
99
115
  test 'builder input_field should use readonly component' do
100
116
  with_concat_form_for(@other_validating_user) do |f|
101
117
  f.input_field :age, as: :integer, readonly: true
@@ -122,4 +138,12 @@ class InputFieldTest < ActionView::TestCase
122
138
  assert_no_select 'select.status[label_method]'
123
139
  assert_no_select 'select.status[value_method]'
124
140
  end
141
+
142
+ test 'build input_field does not treat "boolean_style" as an HTML attribute' do
143
+ with_concat_form_for(@user) do |f|
144
+ f.input_field :active, boolean_style: :nested
145
+ end
146
+
147
+ assert_no_select 'input.boolean[boolean_style]'
148
+ end
125
149
  end
@@ -73,9 +73,32 @@ class LabelTest < ActionView::TestCase
73
73
  end
74
74
 
75
75
  test 'builder allows label order to be changed' do
76
- swap SimpleForm, label_text: lambda { |l, r| "#{l}:" } do
76
+ swap SimpleForm, label_text: proc { |l, r| "#{l}:" } do
77
77
  with_label_for @user, :age
78
78
  assert_select 'label.integer[for=user_age]', "Age:"
79
79
  end
80
80
  end
81
+
82
+ test 'configuration allow set label text for wrappers' do
83
+ swap_wrapper :default, self.custom_wrapper_with_label_text do
84
+ with_concat_form_for(@user) do |f|
85
+ concat f.input :age
86
+ end
87
+ assert_select "label.integer[for=user_age]", "**Age**"
88
+ end
89
+ end
90
+
91
+ test 'builder should allow custom formatting when label is explicitly specified' do
92
+ swap SimpleForm, label_text: lambda { |l, r, explicit_label| explicit_label ? l : "#{l.titleize}:" } do
93
+ with_label_for @user, :time_zone, 'What is your home time zone?'
94
+ assert_select 'label[for=user_time_zone]', 'What is your home time zone?'
95
+ end
96
+ end
97
+
98
+ test 'builder should allow custom formatting when label is generated' do
99
+ swap SimpleForm, label_text: lambda { |l, r, explicit_label| explicit_label ? l : "#{l.titleize}:" } do
100
+ with_label_for @user, :time_zone
101
+ assert_select 'label[for=user_time_zone]', 'Time Zone:'
102
+ end
103
+ end
81
104
  end
@@ -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, self.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, self.custom_wrapper_with_full_error do
141
+ with_form_for @user, :name
142
+ assert_select 'span.error', "Name can't 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|
@@ -200,4 +215,56 @@ class WrapperTest < ActionView::TestCase
200
215
  end
201
216
  end
202
217
  end
218
+
219
+ test 'use 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 'input accepts attributes in the DSL' do
231
+ swap_wrapper :default, self.custom_wrapper_with_input_class do
232
+ with_concat_form_for @user do |f|
233
+ concat f.input :name
234
+ end
235
+ end
236
+
237
+ assert_select "div.custom_wrapper input.string.inline-class"
238
+ end
239
+
240
+ test 'label accepts attributes in the DSL' do
241
+ swap_wrapper :default, self.custom_wrapper_with_label_class do
242
+ with_concat_form_for @user do |f|
243
+ concat f.input :name
244
+ end
245
+ end
246
+
247
+ assert_select "div.custom_wrapper label.string.inline-class"
248
+ end
249
+
250
+ test 'label_input accepts attributes in the DSL' do
251
+ swap_wrapper :default, self.custom_wrapper_with_label_input_class do
252
+ with_concat_form_for @user do |f|
253
+ concat f.input :name
254
+ end
255
+ end
256
+
257
+ assert_select "div.custom_wrapper label.string.inline-class"
258
+ assert_select "div.custom_wrapper input.string.inline-class"
259
+ end
260
+
261
+ test 'input accepts data attributes in the DSL' do
262
+ swap_wrapper :default, self.custom_wrapper_with_input_attributes do
263
+ with_concat_form_for @user do |f|
264
+ concat f.input :name
265
+ end
266
+ end
267
+
268
+ assert_select "div.custom_wrapper input.string[data-modal=true]"
269
+ end
203
270
  end
@@ -21,8 +21,8 @@ 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 :bootstrap/,
25
- /config\.default_wrapper = :bootstrap/
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
@@ -50,14 +50,42 @@ class BooleanInputTest < ActionView::TestCase
50
50
  test 'input boolean with nested allows :inline_label' do
51
51
  swap SimpleForm, boolean_style: :nested do
52
52
  with_input_for @user, :active, :boolean, label: false, inline_label: 'I am so inline.'
53
- assert_select 'label.checkbox', text: 'I am so inline.'
53
+ assert_select 'label.checkbox', text: ' I am so inline.'
54
+ end
55
+ end
56
+
57
+ test 'input boolean with nested escapes :inline_label with HTML' do
58
+ swap SimpleForm, boolean_style: :nested do
59
+ with_input_for @user, :active, :boolean, label: false, inline_label: '<b>I am so inline.</b>'
60
+ assert_select 'label.checkbox', text: ' &lt;b&gt;I am so inline.&lt;/b&gt;'
61
+ end
62
+ end
63
+
64
+ test 'input boolean with nested allows :inline_label with HTML when safe' do
65
+ swap SimpleForm, boolean_style: :nested do
66
+ with_input_for @user, :active, :boolean, label: false, inline_label: '<b>I am so inline.</b>'.html_safe
67
+ assert_select 'label.checkbox b', text: 'I am so inline.'
54
68
  end
55
69
  end
56
70
 
57
71
  test 'input boolean with nested style creates an inline label using the default label text when inline_label option set to true' do
58
72
  swap SimpleForm, boolean_style: :nested do
59
73
  with_input_for @user, :active, :boolean, label: false, inline_label: true
60
- assert_select 'label.checkbox', text: 'Active'
74
+ assert_select 'label.checkbox', text: ' Active'
75
+ end
76
+ end
77
+
78
+ test 'input boolean with nested style creates an inline label using the label text when inline_label option set to true' do
79
+ swap SimpleForm, boolean_style: :nested do
80
+ with_input_for @user, :active, :boolean, label: false, inline_label: true, label_text: proc { 'New Active' }
81
+ assert_select 'label.checkbox', text: ' New Active'
82
+ end
83
+ end
84
+
85
+ test 'input boolean with nested style creates an inline label using the label html when inline_label option set to true' do
86
+ swap SimpleForm, boolean_style: :nested do
87
+ with_input_for @user, :active, :boolean, label: false, inline_label: true, label_text: proc { '<b>New Active</b>' }
88
+ assert_select 'label.checkbox', text: ' New Active'
61
89
  end
62
90
  end
63
91
 
@@ -128,6 +156,16 @@ class BooleanInputTest < ActionView::TestCase
128
156
  end
129
157
  end
130
158
 
159
+ test 'input boolean with nested style works using :input only in wrapper config (no label_input), adding the extra label wrapper with custom class' do
160
+ swap_wrapper do
161
+ swap SimpleForm, boolean_style: :nested, boolean_label_class: 'foo' do
162
+ with_input_for @user, :active, :boolean
163
+
164
+ assert_select 'label.boolean + input[type=hidden] + label.foo > input.boolean'
165
+ end
166
+ end
167
+ end
168
+
131
169
  test 'input boolean with nested style works using :label_input in wrapper config, adding "checkbox" class to label' do
132
170
  swap_wrapper :default, self.custom_wrapper_without_top_level do
133
171
  swap SimpleForm, boolean_style: :nested do
@@ -138,6 +176,16 @@ class BooleanInputTest < ActionView::TestCase
138
176
  end
139
177
  end
140
178
 
179
+ test 'input boolean with nested style works using :label_input in wrapper config, adding custom class to label' do
180
+ swap_wrapper :default, self.custom_wrapper_without_top_level do
181
+ swap SimpleForm, boolean_style: :nested, boolean_label_class: 'foo' do
182
+ with_input_for @user, :active, :boolean
183
+
184
+ assert_select 'input[type=hidden] + label.boolean.foo > input.boolean'
185
+ end
186
+ end
187
+ end
188
+
141
189
  test 'input boolean without additional classes should add "checkbox" class to label' do
142
190
  swap_wrapper :default, self.custom_wrapper_without_top_level do
143
191
  swap SimpleForm, boolean_style: :nested, generate_additional_classes_for: [:input] do
@@ -53,6 +53,20 @@ class CollectionCheckBoxesInputTest < ActionView::TestCase
53
53
  end
54
54
  end
55
55
 
56
+ test 'input that uses automatic collection translation for check_boxes should properly set checked values' do
57
+ store_translations(:en, simple_form: { options: { defaults: {
58
+ gender: { male: 'Male', female: 'Female'}
59
+ } } } ) do
60
+ @user.gender = 'male'
61
+
62
+ with_input_for @user, :gender, :check_boxes, collection: [:male, :female]
63
+ assert_select 'input[type=checkbox][value=male][checked=checked]'
64
+ assert_select 'input[type=checkbox][value=female]'
65
+ assert_select 'label.collection_check_boxes', 'Male'
66
+ assert_select 'label.collection_check_boxes', 'Female'
67
+ end
68
+ end
69
+
56
70
  test 'input check boxes does not wrap the collection by default' do
57
71
  with_input_for @user, :active, :check_boxes
58
72
 
@@ -199,29 +213,27 @@ class CollectionCheckBoxesInputTest < ActionView::TestCase
199
213
  swap SimpleForm, boolean_style: :nested do
200
214
  with_input_for @user, :active, :check_boxes
201
215
 
202
- assert_select 'label.checkbox > input#user_active_true[type=checkbox]'
203
- assert_select 'label.checkbox', 'Yes'
204
- assert_select 'label.checkbox > input#user_active_false[type=checkbox]'
205
- assert_select 'label.checkbox', 'No'
216
+ assert_select 'span.checkbox > label > input#user_active_true[type=checkbox]'
217
+ assert_select 'span.checkbox > label', 'Yes'
218
+ assert_select 'span.checkbox > label > input#user_active_false[type=checkbox]'
219
+ assert_select 'span.checkbox > label', 'No'
206
220
  assert_no_select 'label.collection_radio_buttons'
207
221
  end
208
222
  end
209
223
 
210
- test 'input check boxes with nested style overrides configured item wrapper tag, forcing the :label' do
224
+ test 'input check boxes with nested style does not overrides configured item wrapper tag' do
211
225
  swap SimpleForm, boolean_style: :nested, item_wrapper_tag: :li do
212
226
  with_input_for @user, :active, :check_boxes
213
227
 
214
- assert_select 'label.checkbox > input'
215
- assert_no_select 'li'
228
+ assert_select 'li.checkbox > label > input'
216
229
  end
217
230
  end
218
231
 
219
- test 'input check boxes with nested style overrides given item wrapper tag, forcing the :label' do
232
+ test 'input check boxes with nested style does not overrides given item wrapper tag' do
220
233
  swap SimpleForm, boolean_style: :nested do
221
234
  with_input_for @user, :active, :check_boxes, item_wrapper_tag: :li
222
235
 
223
- assert_select 'label.checkbox > input'
224
- assert_no_select 'li'
236
+ assert_select 'li.checkbox > label > input'
225
237
  end
226
238
  end
227
239
 
@@ -229,7 +241,24 @@ class CollectionCheckBoxesInputTest < ActionView::TestCase
229
241
  swap SimpleForm, boolean_style: :nested do
230
242
  with_input_for @user, :active, :check_boxes, item_wrapper_class: "inline"
231
243
 
232
- assert_select 'label.checkbox.inline > input'
244
+ assert_select 'span.checkbox.inline > label > input'
245
+ end
246
+ end
247
+
248
+ test 'input check boxes wrapper class are not included when set to falsey' do
249
+ swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do
250
+ with_input_for @user, :gender, :check_boxes, collection: [:male, :female]
251
+
252
+ assert_no_select 'label.checkbox'
253
+ end
254
+ end
255
+
256
+ test 'input check boxes custom wrapper class is included when include input wrapper class is falsey' do
257
+ swap SimpleForm, include_default_input_wrapper_class: false, boolean_style: :nested do
258
+ with_input_for @user, :gender, :check_boxes, collection: [:male, :female], item_wrapper_class: 'custom'
259
+
260
+ assert_no_select 'label.checkbox'
261
+ assert_select 'span.custom'
233
262
  end
234
263
  end
235
264
  end