From 32e2a1fd4a926625547a841bfad668e765a4392d Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Wed, 24 Jan 2024 13:33:05 +0800 Subject: [PATCH] DEV: Add delegated Group#human_users scope (#25398) Some preparatory refactoring as we're working on TL groups for the system user. On User we have a scope #human_users to exclude the system user, DiscoBot, etc. This PR adds the same scope (delegated to User) on Group. --- app/models/group.rb | 1 + spec/models/group_spec.rb | 44 ++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/app/models/group.rb b/app/models/group.rb index e1d47537fc7..0357e98695c 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -24,6 +24,7 @@ class Group < ActiveRecord::Base has_many :categories, through: :category_groups has_many :users, through: :group_users + has_many :human_users, -> { human_users }, through: :group_users, source: :user has_many :requesters, through: :group_requests, source: :user has_many :group_histories, dependent: :destroy has_many :category_reviews, diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb index 83611eb8ee7..13cebcdb879 100644 --- a/spec/models/group_spec.rb +++ b/spec/models/group_spec.rb @@ -57,6 +57,14 @@ RSpec.describe Group do end end + describe ".human_users" do + before { group.users << user << Discourse.system_user } + + it "returns only human users" do + expect(group.human_users).to contain_exactly(user) + end + end + describe "#posts_for" do it "returns the post in the group" do p = Fabricate(:post) @@ -197,18 +205,6 @@ RSpec.describe Group do end end - def real_admins - Group[:admins].user_ids.reject { |id| id < 0 } - end - - def real_moderators - Group[:moderators].user_ids.reject { |id| id < 0 } - end - - def real_staff - Group[:staff].user_ids.reject { |id| id < 0 } - end - describe "#primary_group=" do before { group.add(user) } @@ -424,33 +420,33 @@ RSpec.describe Group do Group.refresh_automatic_groups!(:admins, :staff, :moderators) - expect(real_admins).to eq [admin.id] - expect(real_moderators).to eq [moderator.id] - expect(real_staff.sort).to eq [moderator.id, admin.id].sort + expect(Group[:admins].human_users).to contain_exactly(admin) + expect(Group[:moderators].human_users).to contain_exactly(moderator) + expect(Group[:staff].human_users).to contain_exactly(moderator, admin) admin.admin = false admin.save Group.refresh_automatic_group!(:admins) - expect(real_admins).to be_empty + expect(Group[:admins].human_users).to be_empty moderator.revoke_moderation! admin.grant_admin! - expect(real_admins).to eq [admin.id] - expect(real_staff).to eq [admin.id] + expect(Group[:admins].human_users).to contain_exactly(admin) + expect(Group[:staff].human_users).to contain_exactly(admin) admin.revoke_admin! - expect(real_admins).to be_empty - expect(real_staff).to be_empty + expect(Group[:admins].human_users).to be_empty + expect(Group[:staff].human_users).to be_empty admin.grant_moderation! - expect(real_moderators).to eq [admin.id] - expect(real_staff).to eq [admin.id] + expect(Group[:moderators].human_users).to contain_exactly(admin) + expect(Group[:staff].human_users).to contain_exactly(admin) admin.revoke_moderation! - expect(real_admins).to be_empty - expect(real_staff).to eq [] + expect(Group[:admins].human_users).to be_empty + expect(Group[:staff].human_users).to be_empty # we need some work to set min username to 6