Commit 9d391d58 authored by Florian Staudacher's avatar Florian Staudacher

Merge pull request #3597 from devendram/diaspora-refactoring-1

refactored user model (using scope instead of class methods) + rspec tests
for daily, monthly, yearly activities
parents 0a1be140 a2fa083c
......@@ -11,6 +11,11 @@ class User < ActiveRecord::Base
include Querying
include SocialActions
scope :logged_in_since, lambda { |time| where('last_sign_in_at > ?', time) }
scope :monthly_actives, lambda { |time = Time.now| logged_in_since(time - 1.month) }
scope :daily_actives, lambda { |time = Time.now| logged_in_since(time - 1.day) }
scope :yearly_actives, lambda { |time = Time.now| logged_in_since(time - 1.year) }
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:timeoutable, :token_authenticatable, :lockable,
......@@ -82,22 +87,6 @@ class User < ActiveRecord::Base
User.joins(:contacts).where(:contacts => {:person_id => person.id})
end
def self.monthly_actives(start_day = Time.now)
logged_in_since(start_day - 1.month)
end
def self.yearly_actives(start_day = Time.now)
logged_in_since(start_day - 1.year)
end
def self.daily_actives(start_day = Time.now)
logged_in_since(start_day - 1.day)
end
def self.logged_in_since(time)
where('last_sign_in_at > ?', time)
end
def unread_notifications
notifications.where(:unread => true)
end
......
......@@ -22,6 +22,54 @@ describe User do
end
end
describe 'yearly_actives' do
it 'returns list which includes users who latest signed in within last year' do
user = FactoryGirl.build(:user)
user.last_sign_in_at = Time.now - 1.month
user.save
User.yearly_actives.should include user
end
it 'returns list which does not include users who did not sign in within last year' do
user = FactoryGirl.build(:user)
user.last_sign_in_at = Time.now - 2.year
user.save
User.yearly_actives.should_not include user
end
end
describe 'monthly_actives' do
it 'returns list which includes users who latest signed in within last month' do
user = FactoryGirl.build(:user)
user.last_sign_in_at = Time.now - 1.day
user.save
User.monthly_actives.should include user
end
it 'returns list which does not include users who did not sign in within last month' do
user = FactoryGirl.build(:user)
user.last_sign_in_at = Time.now - 2.month
user.save
User.monthly_actives.should_not include user
end
end
describe 'daily_actives' do
it 'returns list which includes users who latest signed in within last day' do
user = FactoryGirl.build(:user)
user.last_sign_in_at = Time.now - 1.hour
user.save
User.daily_actives.should include(user)
end
it 'returns list which does not include users who did not sign in within last day' do
user = FactoryGirl.build(:user)
user.last_sign_in_at = Time.now - 2.day
user.save
User.daily_actives.should_not include(user)
end
end
context 'callbacks' do
describe '#save_person!' do
it 'saves the corresponding user if it has changed' do
......
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