2012-11-27 23:04:07 -06:00
|
|
|
class DoneTodos
|
2014-01-09 11:18:33 +01:00
|
|
|
def self.done_todos_for_container(todos)
|
|
|
|
completed_todos = todos.completed
|
2013-03-18 12:24:42 +01:00
|
|
|
return done_today(completed_todos), done_rest_of_week(completed_todos), done_rest_of_month(completed_todos)
|
2012-11-27 23:04:07 -06:00
|
|
|
end
|
|
|
|
|
2020-10-27 21:39:19 +02:00
|
|
|
def self.done_today(todos, includes = { :include => Todo::DEFAULT_INCLUDES })
|
2013-09-13 16:44:59 +02:00
|
|
|
# TODO: refactor to remove outer hash from includes param
|
2013-09-25 15:08:25 +02:00
|
|
|
todos.completed_after(beginning_of_day).includes(includes[:include])
|
2012-11-27 23:04:07 -06:00
|
|
|
end
|
|
|
|
|
2020-10-27 21:39:19 +02:00
|
|
|
def self.done_rest_of_week(todos, includes = { :include => Todo::DEFAULT_INCLUDES })
|
2013-09-25 15:08:25 +02:00
|
|
|
done_between(todos, includes, beginning_of_day, beginning_of_week)
|
2012-11-27 23:04:07 -06:00
|
|
|
end
|
|
|
|
|
2020-10-27 21:39:19 +02:00
|
|
|
def self.done_rest_of_month(todos, includes = { :include => Todo::DEFAULT_INCLUDES })
|
2013-09-25 15:08:25 +02:00
|
|
|
done_between(todos, includes, beginning_of_week, beginning_of_month)
|
2013-01-01 17:38:59 +01:00
|
|
|
end
|
|
|
|
|
2013-03-18 12:24:42 +01:00
|
|
|
def self.completed_period(date)
|
2020-10-10 13:58:13 +03:00
|
|
|
return nil if date.nil?
|
2013-03-18 12:24:42 +01:00
|
|
|
|
2020-10-27 21:39:19 +02:00
|
|
|
return "today" if date >= end_of_day # Treat todos with completed_at in future as done today (happens in tests)
|
2013-09-25 15:08:25 +02:00
|
|
|
return "today" if date.between?(beginning_of_day, end_of_day)
|
|
|
|
return "rest_of_week" if date >= beginning_of_week
|
|
|
|
return "rest_of_month" if date >= beginning_of_month
|
|
|
|
return nil
|
2013-03-18 12:24:42 +01:00
|
|
|
end
|
|
|
|
|
2014-01-09 11:18:33 +01:00
|
|
|
def self.remaining_in_container(todos, period)
|
2020-10-27 21:39:19 +02:00
|
|
|
count = send("done_#{period}", todos.completed, {}).count
|
2013-03-18 12:24:42 +01:00
|
|
|
return nil if period.nil?
|
|
|
|
return count
|
|
|
|
end
|
|
|
|
|
2013-01-01 17:38:59 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def self.done_between(todos, includes, start_date, end_date)
|
2013-09-13 16:44:59 +02:00
|
|
|
# TODO: refactor to remove outer hash from includes param
|
|
|
|
todos.completed_before(start_date).completed_after(end_date).includes(includes[:include])
|
2012-11-27 23:04:07 -06:00
|
|
|
end
|
2013-09-25 15:08:25 +02:00
|
|
|
|
|
|
|
def self.beginning_of_day
|
|
|
|
Time.zone.now.beginning_of_day
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.end_of_day
|
|
|
|
Time.zone.now.end_of_day
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.beginning_of_week
|
|
|
|
Time.zone.now.beginning_of_week
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.beginning_of_month
|
|
|
|
Time.zone.now.beginning_of_month
|
|
|
|
end
|
2012-11-27 23:04:07 -06:00
|
|
|
end
|