Skip to content

Commit 8218f40

Browse files
committed
remove include_blank, update README
1 parent d79ff0b commit 8218f40

5 files changed

Lines changed: 41 additions & 125 deletions

File tree

README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,10 @@ class SignupForm < Components::Form
362362
end
363363
end
364364

365-
# Select with include_blank: true adds a blank option at the start
365+
# Pass nil as first argument to add a blank option at the start
366366
div do
367367
field(:country).label { "Select your country" }
368-
field(:country).select([[1, "USA"], [2, "Canada"], [3, "Mexico"]], include_blank: true)
368+
field(:country).select(nil, [1, "USA"], [2, "Canada"], [3, "Mexico"])
369369
end
370370

371371
# Multiple select with multiple: true
@@ -380,13 +380,12 @@ class SignupForm < Components::Form
380380
)
381381
end
382382

383-
# Combine multiple: true with include_blank: true
383+
# Combine multiple: true with nil for blank option
384384
div do
385385
field(:tag_ids).label { "Select tags (optional)" }
386386
field(:tag_ids).select(
387-
[[1, "Ruby"], [2, "Rails"], [3, "Phlex"]],
388-
multiple: true,
389-
include_blank: true
387+
nil, [1, "Ruby"], [2, "Rails"], [3, "Phlex"],
388+
multiple: true
390389
)
391390
end
392391

lib/superform/rails/components/select.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@ module Components
44
class Select < Field
55
def initialize(
66
*,
7-
options: nil,
7+
options: [],
88
collection: nil,
99
multiple: false,
10-
include_blank: false,
1110
**,
1211
&
1312
)
1413
super(*, **, &)
1514

1615
# Handle deprecated collection parameter
17-
if collection && !options
16+
if collection && options.empty?
1817
warn "[DEPRECATION] Superform::Rails::Components::Select: " \
19-
"`collection:` parameter is deprecated. " \
20-
"Use `options:` instead."
18+
"`collection:` keyword is deprecated and will be removed. " \
19+
"Use positional arguments instead: field.select([1, 'A'], [2, 'B'])"
2120
options = collection
2221
end
2322

24-
@options = options || []
23+
@options = options
2524
@multiple = multiple
26-
@include_blank = include_blank
2725
end
2826

2927
def view_template(&block)
@@ -38,8 +36,12 @@ def view_template(&block)
3836
select(**attributes, &block)
3937
else
4038
select(**attributes) do
41-
blank_option if @include_blank
42-
options(*@options)
39+
# If first option is nil, render a blank option
40+
include_blank = @options.first.nil?
41+
filtered_options = include_blank ? @options[1..] : @options
42+
43+
blank_option if include_blank
44+
options(*filtered_options)
4345
end
4446
end
4547
end

lib/superform/rails/field.rb

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,14 @@ def textarea(**attributes)
4646
end
4747

4848
def select(*options, **attributes, &)
49-
# Extract select-specific parameters from attributes if passed as keyword arguments
50-
# Note: positional args are the preferred API - keyword form is for internal use
51-
52-
# Handle deprecated collection parameter
53-
if attributes.key?(:collection) && !attributes.key?(:options)
54-
warn "[DEPRECATION] Superform::Rails::Field#select: " \
55-
"`collection:` parameter is deprecated. " \
56-
"Use `options:` instead."
57-
attributes[:options] = attributes.delete(:collection)
58-
end
59-
60-
options = attributes.delete(:options) if options.empty? && attributes.key?(:options)
49+
# Extract select-specific parameters from attributes
6150
multiple = attributes.delete(:multiple) || false
62-
include_blank = attributes.delete(:include_blank) || false
51+
6352
Components::Select.new(
6453
field,
6554
attributes:,
6655
options:,
6756
multiple:,
68-
include_blank:,
6957
&
7058
)
7159
end

spec/superform/rails/components/select_collection_integration_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Order < ActiveRecord::Base
4444
html = render(form) do |f|
4545
orders_collection = f.collection(:orders)
4646
orders_collection.each do |order_namespace|
47-
f.render order_namespace.field(:item_id).select(options: item_options)
47+
f.render order_namespace.field(:item_id).select(*item_options)
4848
end
4949
end
5050

@@ -57,7 +57,7 @@ class Order < ActiveRecord::Base
5757
html = render(form) do |f|
5858
orders_collection = f.collection(:orders)
5959
orders_collection.each do |order_namespace|
60-
f.render order_namespace.field(:item_id).select(options: item_options)
60+
f.render order_namespace.field(:item_id).select(*item_options)
6161
end
6262
end
6363

@@ -85,7 +85,7 @@ class Order < ActiveRecord::Base
8585
html = render(submitted_form) do |f|
8686
orders_collection = f.collection(:orders)
8787
orders_collection.each do |order_namespace|
88-
f.render order_namespace.field(:item_id).select(options: item_options)
88+
f.render order_namespace.field(:item_id).select(*item_options)
8989
end
9090
end
9191

@@ -121,7 +121,7 @@ class Order < ActiveRecord::Base
121121
orders_collection = f.collection(:orders)
122122
orders_collection.each do |order_namespace|
123123
f.render order_namespace.field(:tag_ids).select(
124-
options: tag_options,
124+
*tag_options,
125125
multiple: true
126126
)
127127
end
@@ -139,7 +139,7 @@ class Order < ActiveRecord::Base
139139
orders_collection = f.collection(:orders)
140140
orders_collection.each do |order_namespace|
141141
f.render order_namespace.field(:tag_ids).select(
142-
options: tag_options,
142+
*tag_options,
143143
multiple: true
144144
)
145145
end
@@ -155,7 +155,7 @@ class Order < ActiveRecord::Base
155155
orders_collection = f.collection(:orders)
156156
orders_collection.each do |order_namespace|
157157
f.render order_namespace.field(:tag_ids).select(
158-
options: tag_options,
158+
*tag_options,
159159
multiple: true
160160
)
161161
end
@@ -191,7 +191,7 @@ class Order < ActiveRecord::Base
191191
orders_collection = f.collection(:orders)
192192
orders_collection.each do |order_namespace|
193193
f.render order_namespace.field(:tag_ids).select(
194-
options: tag_options,
194+
*tag_options,
195195
multiple: true
196196
)
197197
end

spec/superform/rails/components/select_spec.rb

Lines changed: 16 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
end
1010
let(:options) { [[1, 'Admin'], [2, 'Editor'], [3, 'Viewer']] }
1111
let(:component) do
12-
described_class.new(field, attributes: attributes, options: options)
12+
described_class.new(field, attributes: attributes, options:)
1313
end
1414
let(:attributes) { {} }
1515

@@ -81,7 +81,7 @@
8181
described_class.new(
8282
field,
8383
attributes: attributes,
84-
options: options,
84+
options:,
8585
multiple: true
8686
)
8787
end
@@ -124,7 +124,7 @@
124124
described_class.new(
125125
field,
126126
attributes: attributes,
127-
options: options,
127+
options:,
128128
multiple: true
129129
)
130130
end
@@ -158,13 +158,13 @@
158158
end
159159
end
160160

161-
describe 'with include_blank: true' do
161+
describe 'with nil as first option (blank option)' do
162+
let(:options_with_blank) { [nil, *options] }
162163
let(:component) do
163164
described_class.new(
164165
field,
165166
attributes: attributes,
166-
options: options,
167-
include_blank: true
167+
options: options_with_blank
168168
)
169169
end
170170

@@ -212,7 +212,7 @@
212212
described_class.new(
213213
role_ids_field,
214214
attributes: attributes,
215-
options: options,
215+
options:,
216216
multiple: true
217217
)
218218
end
@@ -246,14 +246,14 @@
246246
end
247247
end
248248

249-
describe 'with both multiple and include_blank' do
249+
describe 'with both multiple and nil first option (blank)' do
250+
let(:options_with_blank) { [nil, *options] }
250251
let(:component) do
251252
described_class.new(
252253
field,
253254
attributes: attributes,
254-
options: options,
255-
multiple: true,
256-
include_blank: true
255+
options: options_with_blank,
256+
multiple: true
257257
)
258258
end
259259

@@ -283,16 +283,16 @@
283283
end
284284
end
285285

286-
context 'with options keyword argument' do
286+
context 'with positional arguments' do
287287
subject do
288288
render(
289289
form_field.select(
290-
options: [[1, 'Admin'], [2, 'Editor'], [3, 'Viewer']]
290+
[1, 'Admin'], [2, 'Editor'], [3, 'Viewer']
291291
)
292292
)
293293
end
294294

295-
it 'renders select with options from options kwarg' do
295+
it 'renders select with options from positional args' do
296296
expect(subject).to include('>Admin</option>')
297297
expect(subject).to include('>Editor</option>')
298298
expect(subject).to include('>Viewer</option>')
@@ -303,13 +303,13 @@
303303
subject do
304304
render(
305305
form_field.select(
306-
options: [[1, 'Admin'], [2, 'Editor']],
306+
[1, 'Admin'], [2, 'Editor'],
307307
multiple: true
308308
)
309309
)
310310
end
311311

312-
it 'renders multiple select with options kwarg' do
312+
it 'renders multiple select with positional args' do
313313
expect(subject).to include('multiple')
314314
expect(subject).to include('name="role_ids[]"')
315315
expect(subject).to include('>Admin</option>')
@@ -365,78 +365,5 @@
365365
expect(subject).to match(/<option value="\d+">Bob<\/option>/)
366366
end
367367
end
368-
369-
describe 'backwards compatibility with collection parameter' do
370-
context 'using deprecated collection keyword in component' do
371-
let(:component) do
372-
described_class.new(field, attributes: attributes, collection: options)
373-
end
374-
375-
it 'shows deprecation warning' do
376-
expect_any_instance_of(described_class).to receive(:warn).with(
377-
"[DEPRECATION] Superform::Rails::Components::Select: " \
378-
"`collection:` parameter is deprecated. " \
379-
"Use `options:` instead."
380-
)
381-
component
382-
end
383-
384-
it 'still renders select correctly' do
385-
# Suppress deprecation warning for this test
386-
allow_any_instance_of(described_class).to receive(:warn)
387-
result = render(component)
388-
expect(result).to include('>Admin</option>')
389-
expect(result).to include('>Editor</option>')
390-
expect(result).to include('>Viewer</option>')
391-
end
392-
end
393-
394-
context 'using deprecated collection keyword in field helper' do
395-
let(:form_field) do
396-
Superform::Rails::Field.new(:role_ids, parent: nil, object: object)
397-
end
398-
399-
it 'shows deprecation warning' do
400-
expect(form_field).to receive(:warn).with(
401-
"[DEPRECATION] Superform::Rails::Field#select: " \
402-
"`collection:` parameter is deprecated. " \
403-
"Use `options:` instead."
404-
)
405-
form_field.select(collection: [[1, 'Admin'], [2, 'Editor']])
406-
end
407-
408-
it 'still renders select correctly' do
409-
# Suppress deprecation warning for this test
410-
allow(form_field).to receive(:warn)
411-
result = render(
412-
form_field.select(collection: [[1, 'Admin'], [2, 'Editor']])
413-
)
414-
expect(result).to include('>Admin</option>')
415-
expect(result).to include('>Editor</option>')
416-
end
417-
end
418-
419-
context 'when both options and collection are provided' do
420-
let(:component) do
421-
described_class.new(
422-
field,
423-
attributes: attributes,
424-
options: [[1, 'Admin']],
425-
collection: [[2, 'Editor']]
426-
)
427-
end
428-
429-
it 'does not show deprecation warning' do
430-
expect_any_instance_of(described_class).not_to receive(:warn)
431-
component
432-
end
433-
434-
it 'uses options parameter (takes precedence)' do
435-
result = render(component)
436-
expect(result).to include('>Admin</option>')
437-
expect(result).not_to include('>Editor</option>')
438-
end
439-
end
440-
end
441368
end
442369
# rubocop:enable Metrics/BlockLength

0 commit comments

Comments
 (0)