mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 04:11:33 +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)
|
def cast_value(value)
|
||||||
case value
|
case value
|
||||||
when String
|
when String
|
||||||
value.split(",")
|
cast_value(value.split(/,(?!.*\|)|\|(?!.*,)/))
|
||||||
when ::Array
|
when ::Array
|
||||||
value.map { |item| Integer(item, exception: false) || item }
|
value.map { |item| Integer(item, exception: false) || item }
|
||||||
else
|
else
|
||||||
|
|
|
@ -19,7 +19,7 @@ module Chat
|
||||||
include Service::Base
|
include Service::Base
|
||||||
|
|
||||||
policy :chat_enabled
|
policy :chat_enabled
|
||||||
step :cast_new_allowed_groups_to_array
|
contract { attribute :new_allowed_groups, :array }
|
||||||
policy :not_everyone_allowed
|
policy :not_everyone_allowed
|
||||||
model :users
|
model :users
|
||||||
model :memberships_to_remove
|
model :memberships_to_remove
|
||||||
|
@ -32,15 +32,11 @@ module Chat
|
||||||
SiteSetting.chat_enabled
|
SiteSetting.chat_enabled
|
||||||
end
|
end
|
||||||
|
|
||||||
def cast_new_allowed_groups_to_array(new_allowed_groups:)
|
def not_everyone_allowed(contract:)
|
||||||
context[:new_allowed_groups] = new_allowed_groups.to_s.split("|").map(&:to_i)
|
contract.new_allowed_groups.exclude?(Group::AUTO_GROUPS[:everyone])
|
||||||
end
|
end
|
||||||
|
|
||||||
def not_everyone_allowed(new_allowed_groups:)
|
def fetch_users(contract:)
|
||||||
!new_allowed_groups.include?(Group::AUTO_GROUPS[:everyone])
|
|
||||||
end
|
|
||||||
|
|
||||||
def fetch_users(new_allowed_groups:)
|
|
||||||
User
|
User
|
||||||
.real
|
.real
|
||||||
.activated
|
.activated
|
||||||
|
@ -50,8 +46,8 @@ module Chat
|
||||||
.joins(:user_chat_channel_memberships)
|
.joins(:user_chat_channel_memberships)
|
||||||
.distinct
|
.distinct
|
||||||
.then do |users|
|
.then do |users|
|
||||||
break users if new_allowed_groups.blank?
|
break users if contract.new_allowed_groups.blank?
|
||||||
users.where(<<~SQL, new_allowed_groups)
|
users.where(<<~SQL, contract.new_allowed_groups)
|
||||||
users.id NOT IN (
|
users.id NOT IN (
|
||||||
SELECT DISTINCT group_users.user_id
|
SELECT DISTINCT group_users.user_id
|
||||||
FROM group_users
|
FROM group_users
|
||||||
|
|
|
@ -14,6 +14,30 @@ RSpec.describe ActiveSupportTypeExtensions::Array do
|
||||||
end
|
end
|
||||||
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
|
context "when 'value' is an array" do
|
||||||
let(:value) { %w[existing array] }
|
let(:value) { %w[existing array] }
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user