Guard against the case days stale can be negative

This commit is contained in:
Matt Rogers 2013-03-10 22:48:27 -05:00
parent d699359648
commit 053e3fc8d6
2 changed files with 16 additions and 1 deletions

View file

@ -3,8 +3,14 @@ require 'active_support/all'
class Staleness
SECONDS_PER_DAY = 86400
def self.days_stale(item, current_user)
return 0 if item.due || item.completed?
return 0 if cannot_be_stale(item, current_user)
(current_user.time - item.created_at).to_i / SECONDS_PER_DAY
end
def self.cannot_be_stale(item, current_user)
return true if item.due || item.completed?
return true if item.created_at > current_user.time
false
end
end

View file

@ -14,6 +14,10 @@ class StalenessTest < Test::Unit::TestCase
@now ||= Time.utc(2013, 2, 28, 0, 0, 0)
end
def after_now
@after_now ||= Time.utc(2013, 3, 1, 0, 0, 0)
end
def day16
@day16 ||= Time.utc(2013, 2, 12, 0, 0, 0)
end
@ -36,6 +40,11 @@ class StalenessTest < Test::Unit::TestCase
assert_equal 0, Staleness.days_stale(todo, @current_user)
end
def test_created_at_after_current_time_is_not_stale
todo = FakeTask.new(nil, false, after_now)
assert_equal 0, Staleness.days_stale(todo, @current_user)
end
def test_young_item_is_not_stale
todo = FakeTask.new(nil, false, now)
assert_equal 0, Staleness.days_stale(todo, @current_user)