Merge pull request #859 from lengarvey/user_refactoring

Light refactoring of the User model
This commit is contained in:
Sam 2013-05-19 16:41:20 -07:00
commit 18b00d01d0
2 changed files with 43 additions and 12 deletions

View File

@ -307,7 +307,7 @@ class User < ActiveRecord::Base
end end
def regular? def regular?
!(admin? || moderator?) !staff?
end end
def password=(password) def password=(password)
@ -325,10 +325,6 @@ class User < ActiveRecord::Base
self.password_hash == hash_password(password, salt) self.password_hash == hash_password(password, salt)
end end
def seen?(date)
last_seen_at.to_date >= date if last_seen_at.present?
end
def seen_before? def seen_before?
last_seen_at.present? last_seen_at.present?
end end
@ -338,14 +334,9 @@ class User < ActiveRecord::Base
end end
def update_visit_record!(date) def update_visit_record!(date)
unless seen_before? unless has_visit_record?(date)
update_column(:days_visited, days_visited + 1)
user_visits.create!(visited_at: date) user_visits.create!(visited_at: date)
update_column(:days_visited, 1)
end
unless seen?(date) || has_visit_record?(date)
user_visits.create!(visited_at: date)
User.update_all('days_visited = days_visited + 1', id: self.id)
end end
end end

View File

@ -321,6 +321,46 @@ describe User do
end end
describe 'staff and regular users' do
let(:user) { Fabricate.build(:user) }
describe '#staff?' do
subject { user.staff? }
it { should be_false }
context 'for a moderator user' do
before { user.moderator = true }
it { should be_true }
end
context 'for an admin user' do
before { user.admin = true }
it { should be_true }
end
end
describe '#regular?' do
subject { user.regular? }
it { should be_true }
context 'for a moderator user' do
before { user.moderator = true }
it { should be_false }
end
context 'for an admin user' do
before { user.admin = true }
it { should be_false }
end
end
end
describe 'temporary_key' do describe 'temporary_key' do
let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }