From 4d4a1a1552444ff1d6cb47d019950ad01e72547f Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Sat, 11 Mar 2017 14:25:09 +0800 Subject: [PATCH] Add scope for human users. --- app/models/about.rb | 4 ++-- app/models/user.rb | 4 +++- spec/models/user_spec.rb | 8 ++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/models/about.rb b/app/models/about.rb index a59a73a019f..0d670183ba7 100644 --- a/app/models/about.rb +++ b/app/models/about.rb @@ -35,12 +35,12 @@ class About def moderators @moderators ||= User.where(moderator: true, admin: false) - .where("id > 0") + .human_users .order(:username_lower) end def admins - @admins ||= User.where(admin: true).where("id > 0").order(:username_lower) + @admins ||= User.where(admin: true).human_users.order(:username_lower) end def stats diff --git a/app/models/user.rb b/app/models/user.rb index f6c297da9b4..de36555acde 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -124,8 +124,10 @@ class User < ActiveRecord::Base # set to true to optimize creation and save for imports attr_accessor :import_mode + scope :human_users, -> { where('users.id > 0') } + # 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 FROM user_custom_fields ucf WHERE diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index f52089126b7..dfee88c7fbb 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1485,4 +1485,12 @@ describe User do 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