Refactor User.total_users into a scope
This commit is contained in:
parent
6432bbe67b
commit
99ea156e1c
4 changed files with 18 additions and 22 deletions
|
|
@ -15,6 +15,7 @@ class User < ActiveRecord::Base
|
||||||
scope :daily_actives, ->(time = Time.now) { logged_in_since(time - 1.day) }
|
scope :daily_actives, ->(time = Time.now) { logged_in_since(time - 1.day) }
|
||||||
scope :yearly_actives, ->(time = Time.now) { logged_in_since(time - 1.year) }
|
scope :yearly_actives, ->(time = Time.now) { logged_in_since(time - 1.year) }
|
||||||
scope :halfyear_actives, ->(time = Time.now) { logged_in_since(time - 6.month) }
|
scope :halfyear_actives, ->(time = Time.now) { logged_in_since(time - 6.month) }
|
||||||
|
scope :active, -> { joins(:person).where(people: {closed_account: false}).where.not(username: nil) }
|
||||||
|
|
||||||
devise :token_authenticatable, :database_authenticatable, :registerable,
|
devise :token_authenticatable, :database_authenticatable, :registerable,
|
||||||
:recoverable, :rememberable, :trackable, :validatable,
|
:recoverable, :rememberable, :trackable, :validatable,
|
||||||
|
|
@ -517,7 +518,7 @@ class User < ActiveRecord::Base
|
||||||
self.save
|
self.save
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_database_authentication
|
def after_database_authentication
|
||||||
# remove any possible remove_after timestamp flag set by maintenance.remove_old_users
|
# remove any possible remove_after timestamp flag set by maintenance.remove_old_users
|
||||||
unless self.remove_after.nil?
|
unless self.remove_after.nil?
|
||||||
|
|
@ -526,12 +527,8 @@ class User < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def self.total_users
|
|
||||||
User.joins(:person).where(:people => {:closed_account => false}).where.not(:username => nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def clearable_fields
|
def clearable_fields
|
||||||
self.attributes.keys - ["id", "username", "encrypted_password",
|
self.attributes.keys - ["id", "username", "encrypted_password",
|
||||||
"created_at", "updated_at", "locked_at",
|
"created_at", "updated_at", "locked_at",
|
||||||
|
|
|
||||||
|
|
@ -48,10 +48,7 @@ class StatisticsPresenter
|
||||||
end
|
end
|
||||||
|
|
||||||
def total_users
|
def total_users
|
||||||
@total_users ||= User.joins(:person)
|
@total_users ||= User.active.count
|
||||||
.where(people: {closed_account: false})
|
|
||||||
.where.not(username: nil)
|
|
||||||
.count
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def monthly_users
|
def monthly_users
|
||||||
|
|
|
||||||
|
|
@ -1064,43 +1064,45 @@ describe User, :type => :model do
|
||||||
@user.sign_up
|
@user.sign_up
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "maintenance" do
|
describe "maintenance" do
|
||||||
before do
|
before do
|
||||||
@user = bob
|
@user = bob
|
||||||
AppConfig.settings.maintenance.remove_old_users.enable = true
|
AppConfig.settings.maintenance.remove_old_users.enable = true
|
||||||
end
|
end
|
||||||
|
|
||||||
it "#flags user for removal" do
|
it "#flags user for removal" do
|
||||||
remove_at = Time.now+5.days
|
remove_at = Time.now+5.days
|
||||||
@user.flag_for_removal(remove_at)
|
@user.flag_for_removal(remove_at)
|
||||||
expect(@user.remove_after).to eq(remove_at)
|
expect(@user.remove_after).to eq(remove_at)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#auth database auth maintenance" do
|
describe "#auth database auth maintenance" do
|
||||||
before do
|
before do
|
||||||
@user = bob
|
@user = bob
|
||||||
@user.remove_after = Time.now
|
@user.remove_after = Time.now
|
||||||
@user.save
|
@user.save
|
||||||
end
|
end
|
||||||
|
|
||||||
it "remove_after is cleared" do
|
it "remove_after is cleared" do
|
||||||
@user.after_database_authentication
|
@user.after_database_authentication
|
||||||
expect(@user.remove_after).to eq(nil)
|
expect(@user.remove_after).to eq(nil)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "total_users" do
|
describe "active" do
|
||||||
before do
|
before do
|
||||||
@user1 = FactoryGirl.build(:user, :username => nil)
|
invited_user = FactoryGirl.build(:user, username: nil)
|
||||||
@user1.save(:validate => false)
|
invited_user.save(validate: false)
|
||||||
@user2 = FactoryGirl.create(:user)
|
|
||||||
@user2.person.closed_account = true
|
closed_account = FactoryGirl.create(:user)
|
||||||
@user2.save
|
closed_account.person.closed_account = true
|
||||||
|
closed_account.save
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns total_users excluding closed accounts & users without usernames" do
|
it "returns total_users excluding closed accounts & users without usernames" do
|
||||||
expect(User.total_users.count).to eq 5 #5 users from fixtures
|
expect(User.active.count).to eq 6 # 6 users from fixtures
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ describe StatisticsPresenter do
|
||||||
"network" => "Diaspora",
|
"network" => "Diaspora",
|
||||||
"version" => AppConfig.version_string,
|
"version" => AppConfig.version_string,
|
||||||
"registrations_open" => AppConfig.settings.enable_registrations?,
|
"registrations_open" => AppConfig.settings.enable_registrations?,
|
||||||
"total_users" => User.total_users.count,
|
"total_users" => User.active.count,
|
||||||
"active_users_halfyear" => User.halfyear_actives.count,
|
"active_users_halfyear" => User.halfyear_actives.count,
|
||||||
"active_users_monthly" => User.monthly_actives.count,
|
"active_users_monthly" => User.monthly_actives.count,
|
||||||
"local_posts" => @presenter.local_posts,
|
"local_posts" => @presenter.local_posts,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue