Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add a `compact_blank` option to the `ActiveModel` array type (#35476)

Loïc Guitaut695533b99cd9
diff --git a/app/services/user/bulk_destroy.rb b/app/services/user/bulk_destroy.rb
index 1c20b2b737d..d411bda2f48 100644
--- a/app/services/user/bulk_destroy.rb
+++ b/app/services/user/bulk_destroy.rb
@@ -4,12 +4,10 @@ class User::BulkDestroy
include Service::Base
params do
- attribute :user_ids, :array
+ attribute :user_ids, :array, compact_blank: true
attribute :block_ip_and_email, :boolean, default: false
validates :user_ids, length: { minimum: 1, maximum: 100 }
-
- after_validation { user_ids&.compact_blank! }
end
model :users
diff --git a/lib/active_support_type_extensions/array.rb b/lib/active_support_type_extensions/array.rb
index 843624c824e..7955b26cba5 100644
--- a/lib/active_support_type_extensions/array.rb
+++ b/lib/active_support_type_extensions/array.rb
@@ -2,6 +2,13 @@
module ActiveSupportTypeExtensions
class Array < ActiveModel::Type::Value
+ attr_reader :compact_blank
+
+ def initialize(compact_blank: false)
+ super()
+ @compact_blank = compact_blank
+ end
+
def serializable?(_)
false
end
@@ -14,7 +21,7 @@ module ActiveSupportTypeExtensions
value.map { |item| Integer(item, exception: false) || item }
else
::Array.wrap(value)
- end
+ end.tap { _1.compact_blank! if compact_blank }
end
end
end
diff --git a/spec/lib/active_support_type_extensions/array_spec.rb b/spec/lib/active_support_type_extensions/array_spec.rb
index 1cb522bdbc0..d3d299e7778 100644
--- a/spec/lib/active_support_type_extensions/array_spec.rb
+++ b/spec/lib/active_support_type_extensions/array_spec.rb
@@ -1,7 +1,9 @@
# frozen_string_literal: true
RSpec.describe ActiveSupportTypeExtensions::Array do
- subject(:type) { described_class.new }
+ subject(:type) { described_class.new(compact_blank:) }
+
+ let(:compact_blank) { false }
describe "#cast" do
subject(:casted_value) { type.cast(value) }
@@ -69,5 +71,23 @@ RSpec.describe ActiveSupportTypeExtensions::Array do
expect(casted_value).to eq([value])
end
end
+
+ context "when 'value' is an array containing falsy or empty values" do
+ let(:value) { [nil, "", false, []] }
+
+ context "when the `compact_blank` option is false" do
+ it "returns the original array" do
+ expect(casted_value).to eq(value)
+ end
+ end
+
+ context "when the `compact_blank` option is true" do
+ let(:compact_blank) { true }
+
+ it "cleans the array" do
+ expect(casted_value).to eq([])
+ end
+ end
+ end
end
end
diff --git a/spec/requests/admin/users_controller_spec.rb b/spec/requests/admin/users_controller_spec.rb
index 3d036345b5c..20257607c4c 100644
--- a/spec/requests/admin/users_controller_spec.rb
+++ b/spec/requests/admin/users_controller_spec.rb
@@ -1523,8 +1523,8 @@ RSpec.describe Admin::UsersController do
expect(User.where(id: deleted_users.map(&:id)).count).to eq(0)
end
- it "responds with 404 when sending an empty user_ids list" do
- delete "/admin/users/destroy-bulk.json", params: { user_ids: [] }
+ it "responds with 404 when sending non-existent user ids" do
+ delete "/admin/users/destroy-bulk.json", params: { user_ids: [0] }
expect(response.status).to eq(404)
end