From 43275e064e244e7345ce59483c90cf748b98b6f6 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Sun, 10 Mar 2013 13:46:22 -0500 Subject: [PATCH] Refactor the calculation for staleness Provide the number of days that the todo was stale instead of a class name to use in the view. We'll use the number of days stale in the helper to determine the class name --- lib/staleness.rb | 14 +++----------- test/unit/staleness_test.rb | 22 ++++++---------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/lib/staleness.rb b/lib/staleness.rb index 67cafdc0..22a7f5ad 100644 --- a/lib/staleness.rb +++ b/lib/staleness.rb @@ -1,18 +1,10 @@ require 'active_support/all' class Staleness + SECONDS_PER_DAY = 86400 def self.days_stale(item, current_user) - if item.due || item.completed? - return "" - elsif item.created_at < current_user.time - (current_user.prefs.staleness_starts * 3).days - return " stale_l3" - elsif item.created_at < current_user.time - (current_user.prefs.staleness_starts * 2).days - return " stale_l2" - elsif item.created_at < current_user.time - (current_user.prefs.staleness_starts).days - return " stale_l1" - else - return "" - end + return 0 if item.due || item.completed? + (current_user.time - item.created_at).to_i / SECONDS_PER_DAY end end diff --git a/test/unit/staleness_test.rb b/test/unit/staleness_test.rb index f5da5036..c96e2976 100644 --- a/test/unit/staleness_test.rb +++ b/test/unit/staleness_test.rb @@ -40,32 +40,22 @@ class StalenessTest < Test::Unit::TestCase end def test_item_with_due_date_is_not_stale_ever - todo = FakeTask.new(day24, false, now) - assert_equal "", Staleness.days_stale(todo, @current_user) + todo = FakeTask.new(now, false, day24) + assert_equal 0, Staleness.days_stale(todo, @current_user) end def test_complete_item_is_not_stale todo = FakeTask.new(day16, true, day24) - assert_equal "", Staleness.days_stale(todo, @current_user) + 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 "", Staleness.days_stale(todo, @current_user) + assert_equal 0, Staleness.days_stale(todo, @current_user) end - def test_staleness_level_one + def test_todo_staleness_calculation todo = FakeTask.new(nil, false, day8) - assert_equal " stale_l1", Staleness.days_stale(todo, @current_user) - end - - def test_staleness_level_two - todo = FakeTask.new(nil, false, day16) - assert_equal " stale_l2", Staleness.days_stale(todo, @current_user) - end - - def test_staleness_level_three - todo = FakeTask.new(nil, false, day24) - assert_equal " stale_l3", Staleness.days_stale(todo, @current_user) + assert_equal 8, Staleness.days_stale(todo, @current_user) end end