refactor done todos view and tests

This commit is contained in:
Reinier Balt 2013-03-18 12:24:42 +01:00
parent aac744e411
commit 85fc82d494
16 changed files with 141 additions and 116 deletions

View file

@ -1,7 +1,7 @@
class DoneTodos
def self.done_todos_for_container(container)
completed_todos = container.todos.completed
return done_today(completed_todos), done_this_week(completed_todos), done_this_month(completed_todos)
def self.done_todos_for_container(user)
completed_todos = user.todos.completed
return done_today(completed_todos), done_rest_of_week(completed_todos), done_rest_of_month(completed_todos)
end
def self.done_today(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
@ -9,14 +9,31 @@ class DoneTodos
todos.completed_after(start_of_this_day).all(includes)
end
def self.done_this_week(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
def self.done_rest_of_week(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
done_between(todos, includes, Time.zone.now.beginning_of_day, Time.zone.now.beginning_of_week)
end
def self.done_this_month(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
def self.done_rest_of_month(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
done_between(todos, includes, Time.zone.now.beginning_of_week, Time.zone.now.beginning_of_month)
end
def self.completed_period(date)
return nil if date.nil?
period = nil
period = "rest_of_month" if date > Time.zone.now.beginning_of_month
period = "rest_of_week" if date > Time.zone.now.beginning_of_week
period = "today" if date > Time.zone.now.beginning_of_day
return period
end
def self.remaining_in_container(user, period)
count = self.send("done_#{period}", user.todos.completed, {}).count
return nil if period.nil?
return count
end
private
def self.done_between(todos, includes, start_date, end_date)