From 32f4dfd2fb72d5d82d51f97b7bea225059a0dbc5 Mon Sep 17 00:00:00 2001 From: Leonard Garvey Date: Sat, 18 May 2013 16:00:35 +1000 Subject: [PATCH 1/3] DRY up the user model slightly regular? is just the inverse of staff? --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 572a1e708..f2d94f0b6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -307,7 +307,7 @@ class User < ActiveRecord::Base end def regular? - !(admin? || moderator?) + !staff? end def password=(password) From b16705be0d67260c17e4e9789b530c8d512883e4 Mon Sep 17 00:00:00 2001 From: Leonard Garvey Date: Sat, 18 May 2013 16:02:06 +1000 Subject: [PATCH 2/3] Clean up update_visit_record! update_visit_record! appears to add user_visit record if one doesn't exist and days_visited is simply a counter for the number of visit records. --- app/models/user.rb | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index f2d94f0b6..1d572fc68 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -325,10 +325,6 @@ class User < ActiveRecord::Base self.password_hash == hash_password(password, salt) end - def seen?(date) - last_seen_at.to_date >= date if last_seen_at.present? - end - def seen_before? last_seen_at.present? end @@ -338,14 +334,9 @@ class User < ActiveRecord::Base end 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) - 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 From 3de632f8929dc421b22c3b0dc9f2d6b95de5f72c Mon Sep 17 00:00:00 2001 From: Leonard Garvey Date: Sat, 18 May 2013 18:07:07 +1000 Subject: [PATCH 3/3] Added a spec for staff? and regular? --- spec/models/user_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 061f87b1f..464b7d158 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -321,6 +321,46 @@ describe User do 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 let(:user) { Fabricate(:user) }