Skip to content
Browse files

add a way to fix user progress

  • Loading branch information...
1 parent 2cb9869 commit e157a915e971b4ec0a161af5f6580d2d437ba937 @technoweenie technoweenie committed Dec 23, 2011
Showing with 61 additions and 8 deletions.
  1. +9 −0 Rakefile
  2. +29 −1 lib/seinfeld/user.rb
  3. +23 −7 test/user_test.rb
View
9 Rakefile
@@ -54,6 +54,15 @@ namespace :seinfeld do
end
end
+ desc "Fix USER progress."
+ task :fix_progress => :init do
+ raise "Need USER=" if ENV['USER'].blank?
+ user = Seinfeld::User.find_by_login(ENV['USER'])
+ Time.zone = user.time_zone || 'UTC'
+ user.fix_progress
+ puts user.inspect
+ end
+
desc "Sets USER's timezone to ZONE."
task :tz => :init do
raise "Need USER=" if ENV['USER'].to_s.size.zero?
View
30 lib/seinfeld/user.rb
@@ -71,6 +71,34 @@ def update_progress(days, today = Date.today)
end
end
+ # Public: Sets the latest streak from the current data.
+ #
+ # today - Optional Date instance representing today.
+ #
+ # Returns nothing.
+ def fix_progress(today = Date.today)
+ date = nil
+ streak = Streak.new
+ progs = progressions.limit(100).to_a.reverse
+ progs.each do |prog|
+ prog_date = prog.created_at.to_date
+ if date
+ date = date - 1
+ if prog_date == date
+ streak.started = date
+ else
+ return
+ end
+ else
+ streak.started = streak.ended = date = prog_date
+ end
+ end
+ ensure
+ if streak.days > 0
+ update_from_streaks([streak], today)
+ end
+ end
+
# Public: Clears all progression data for a user.
#
# Returns nothing.
@@ -131,7 +159,7 @@ def scan_days_for_streaks(days)
# streaks - Array of Streak objects. This is used to set the various
# streak related attributes on User.
# today - A Date instance representing today.
- def update_from_streaks(streaks, today)
+ def update_from_streaks(streaks, today = Date.today)
highest_streak = streaks.empty? ? 0 : streaks.max { |a, b| a.days <=> b.days }
if latest_streak = streaks.last
self.streak_start = latest_streak.started
View
30 test/user_test.rb
@@ -7,12 +7,28 @@ class UserTest < ActiveSupport::TestCase
@user = Seinfeld::User.create! :login => 'user',
:streak_start => Date.civil(2007, 12, 30), :streak_end => Date.civil(2007, 12, 31), :current_streak => 2,
:longest_streak_start => Date.civil(2007, 12, 30), :longest_streak_end => Date.civil(2007, 12, 31), :longest_streak => 2
+ @user.progressions.create!(:created_at => Date.civil(2006, 12, 30))
@user.progressions.create!(:created_at => Date.civil(2007, 12, 30))
@user.progressions.create!(:created_at => Date.civil(2007, 12, 31))
Seinfeld::Progression.create! :user_id => @user.id+1
@today = Date.civil(2008, 1, 3)
end
+ test "fixes progress" do
+ @user.update_attributes \
+ :streak_start => nil, :streak_end => nil, :current_streak => nil,
+ :longest_streak => nil, :longest_streak_start => nil, :longest_streak_end => nil
+ @user.fix_progress(@today)
+ assert_equal Date.civil(2007, 12, 30), @user.streak_start
+ assert_equal Date.civil(2007, 12, 31), @user.streak_end
+ assert_equal 0, @user.current_streak
+
+ @user.fix_progress(Date.civil(2007, 12, 31))
+ assert_equal Date.civil(2007, 12, 30), @user.streak_start
+ assert_equal Date.civil(2007, 12, 31), @user.streak_end
+ assert_equal 2, @user.current_streak
+ end
+
test "downcases login" do
assert_equal 'bob', Seinfeld::User.new(:login => 'BoB').login
end
@@ -75,9 +91,9 @@ class UserTest < ActiveSupport::TestCase
test "#update_progress with user, keeping the streak" do
parsed_dates = [Date.civil(2007, 12, 31), Date.civil(2008, 1, 1), Date.civil(2008, 1, 2)]
- assert_equal 2, @user.progressions.count
+ assert_equal 3, @user.progressions.count
@user.update_progress parsed_dates, @today
- assert_equal 4, @user.progressions.count
+ assert_equal 5, @user.progressions.count
assert_equal parsed_dates, @user.progress_for(2008, 1, 1)
assert_equal 4, @user.longest_streak
@@ -91,9 +107,9 @@ class UserTest < ActiveSupport::TestCase
test "#update_progress with user, breaking the streak" do
parsed_dates = [Date.civil(2007, 12, 31), Date.civil(2008, 1, 1)]
- assert_equal 2, @user.progressions.count
- @user.update_progress parsed_dates, @today
assert_equal 3, @user.progressions.count
+ @user.update_progress parsed_dates, @today
+ assert_equal 4, @user.progressions.count
assert_equal parsed_dates, @user.progress_for(2008, 1, 1)
assert_equal 3, @user.longest_streak
@@ -107,9 +123,9 @@ class UserTest < ActiveSupport::TestCase
test "#update_progress with user, starting new streak" do
parsed_dates = [Date.civil(2007, 12, 31), Date.civil(2008, 1, 2)]
- assert_equal 2, @user.progressions.count
- @user.update_progress parsed_dates, @today
assert_equal 3, @user.progressions.count
+ @user.update_progress parsed_dates, @today
+ assert_equal 4, @user.progressions.count
assert_equal parsed_dates, @user.progress_for(2008, 1, 1)
assert_equal 2, @user.longest_streak
@@ -121,7 +137,7 @@ class UserTest < ActiveSupport::TestCase
end
test "#clear_progress clears all user data" do
- assert_equal 3, Seinfeld::Progression.count
+ assert_equal 4, Seinfeld::Progression.count
@user.clear_progress
assert_equal 1, Seinfeld::Progression.count

0 comments on commit e157a91

Please sign in to comment.
Something went wrong with that request. Please try again.