mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 21:16:08 +08:00
DEV: Improve array
type in service contracts
This patch improves the custom `array` type available in contracts. It’s now able to split strings on `|` on top of `,`, and to be more consistent, it also tries to cast the resulting items to integers.
This commit is contained in:
parent
2ee63e4164
commit
23c486799f
|
@ -9,7 +9,7 @@ module ActiveSupportTypeExtensions
|
|||
def cast_value(value)
|
||||
case value
|
||||
when String
|
||||
value.split(",")
|
||||
cast_value(value.split(/,(?!.*\|)|\|(?!.*,)/))
|
||||
when ::Array
|
||||
value.map { |item| Integer(item, exception: false) || item }
|
||||
else
|
||||
|
|
|
@ -19,7 +19,7 @@ module Chat
|
|||
include Service::Base
|
||||
|
||||
policy :chat_enabled
|
||||
step :cast_new_allowed_groups_to_array
|
||||
contract { attribute :new_allowed_groups, :array }
|
||||
policy :not_everyone_allowed
|
||||
model :users
|
||||
model :memberships_to_remove
|
||||
|
@ -32,15 +32,11 @@ module Chat
|
|||
SiteSetting.chat_enabled
|
||||
end
|
||||
|
||||
def cast_new_allowed_groups_to_array(new_allowed_groups:)
|
||||
context[:new_allowed_groups] = new_allowed_groups.to_s.split("|").map(&:to_i)
|
||||
def not_everyone_allowed(contract:)
|
||||
contract.new_allowed_groups.exclude?(Group::AUTO_GROUPS[:everyone])
|
||||
end
|
||||
|
||||
def not_everyone_allowed(new_allowed_groups:)
|
||||
!new_allowed_groups.include?(Group::AUTO_GROUPS[:everyone])
|
||||
end
|
||||
|
||||
def fetch_users(new_allowed_groups:)
|
||||
def fetch_users(contract:)
|
||||
User
|
||||
.real
|
||||
.activated
|
||||
|
@ -50,8 +46,8 @@ module Chat
|
|||
.joins(:user_chat_channel_memberships)
|
||||
.distinct
|
||||
.then do |users|
|
||||
break users if new_allowed_groups.blank?
|
||||
users.where(<<~SQL, new_allowed_groups)
|
||||
break users if contract.new_allowed_groups.blank?
|
||||
users.where(<<~SQL, contract.new_allowed_groups)
|
||||
users.id NOT IN (
|
||||
SELECT DISTINCT group_users.user_id
|
||||
FROM group_users
|
||||
|
|
|
@ -14,6 +14,30 @@ RSpec.describe ActiveSupportTypeExtensions::Array do
|
|||
end
|
||||
end
|
||||
|
||||
context "when 'value' is a string of numbers" do
|
||||
let(:value) { "1,2,3" }
|
||||
|
||||
it "splits it with strings casted as integers" do
|
||||
expect(casted_value).to eq([1, 2, 3])
|
||||
end
|
||||
end
|
||||
|
||||
context "when 'value' is a string of numbers separated by '|'" do
|
||||
let(:value) { "1|2|3" }
|
||||
|
||||
it "splits it with strings casted as integers" do
|
||||
expect(casted_value).to eq([1, 2, 3])
|
||||
end
|
||||
end
|
||||
|
||||
context "when 'value' has mixed separators" do
|
||||
let(:value) { "1,2,3|4" }
|
||||
|
||||
it "splits only on one of the separators" do
|
||||
expect(casted_value).to eq(["1,2,3", 4])
|
||||
end
|
||||
end
|
||||
|
||||
context "when 'value' is an array" do
|
||||
let(:value) { %w[existing array] }
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user