Commit 7e3c5aa3 authored by Adam Hegyi's avatar Adam Hegyi

Optimize participants list

parent 156c3306
......@@ -16,9 +16,16 @@ class GroupMember < Member
default_scope { where(source_type: SOURCE_TYPE) }
scope :of_groups, ->(groups) { where(source_id: groups.select(:id)) }
scope :count_users_by_group_id, -> { joins(:user).group(:source_id).count }
scope :of_ldap_type, -> { where(ldap: true) }
scope :count_users_by_group_id, -> do
if Feature.enabled?(:optimized_count_users_by_group_id)
group(:source_id).count
else
joins(:user).group(:source_id).count
end
end
after_create :update_two_factor_requirement, unless: :invite?
after_destroy :update_two_factor_requirement, unless: :invite?
......
......@@ -4,7 +4,7 @@ require 'spec_helper'
describe GroupMember do
context 'scopes' do
describe '.count_users_by_group_id' do
shared_examples '.count_users_by_group_id' do
it 'counts users by group ID' do
user_1 = create(:user)
user_2 = create(:user)
......@@ -20,6 +20,36 @@ describe GroupMember do
end
end
describe '.count_users_by_group_id with optimized_count_users_by_group_id feature flag on' do
before do
stub_feature_flags(optimized_count_users_by_group_id: true)
end
it_behaves_like '.count_users_by_group_id'
it 'does not JOIN users' do
scope = described_class.all
expect(scope).not_to receive(:joins).with(:user)
scope.count_users_by_group_id
end
end
describe '.count_users_by_group_id with optimized_count_users_by_group_id feature flag off' do
before do
stub_feature_flags(optimized_count_users_by_group_id: false)
end
it_behaves_like '.count_users_by_group_id'
it 'does JOIN users' do
scope = described_class.all
expect(scope).to receive(:joins).with(:user).and_call_original
scope.count_users_by_group_id
end
end
describe '.of_ldap_type' do
it 'returns ldap type users' do
group_member = create(:group_member, :ldap)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment