Add scope for human users.

This commit is contained in:
Guo Xiang Tan 2017-03-11 14:25:09 +08:00
parent 6ebddc42d1
commit 4d4a1a1552
3 changed files with 13 additions and 3 deletions

View File

@ -35,12 +35,12 @@ class About
def moderators def moderators
@moderators ||= User.where(moderator: true, admin: false) @moderators ||= User.where(moderator: true, admin: false)
.where("id > 0") .human_users
.order(:username_lower) .order(:username_lower)
end end
def admins def admins
@admins ||= User.where(admin: true).where("id > 0").order(:username_lower) @admins ||= User.where(admin: true).human_users.order(:username_lower)
end end
def stats def stats

View File

@ -124,8 +124,10 @@ class User < ActiveRecord::Base
# set to true to optimize creation and save for imports # set to true to optimize creation and save for imports
attr_accessor :import_mode attr_accessor :import_mode
scope :human_users, -> { where('users.id > 0') }
# excluding fake users like the system user or anonymous users # excluding fake users like the system user or anonymous users
scope :real, -> { where('users.id > 0').where('NOT EXISTS( scope :real, -> { human_users.where('NOT EXISTS(
SELECT 1 SELECT 1
FROM user_custom_fields ucf FROM user_custom_fields ucf
WHERE WHERE

View File

@ -1485,4 +1485,12 @@ describe User do
end end
describe '.human_users' do
it 'should only return users with a positive primary key' do
Fabricate(:user, id: -2)
user = Fabricate(:user)
expect(User.human_users).to eq([user])
end
end
end end