Class: DoneTodos

Inherits:
Object
  • Object
show all
Defined in:
lib/tracks/done_todos.rb

Class Method Summary collapse

Class Method Details

.beginning_of_dayObject (private)


45
46
47
# File 'lib/tracks/done_todos.rb', line 45

def self.beginning_of_day
  Time.zone.now.beginning_of_day
end

.beginning_of_monthObject (private)


57
58
59
# File 'lib/tracks/done_todos.rb', line 57

def self.beginning_of_month
  Time.zone.now.beginning_of_month
end

.beginning_of_weekObject (private)


53
54
55
# File 'lib/tracks/done_todos.rb', line 53

def self.beginning_of_week
  Time.zone.now.beginning_of_week
end

.completed_period(date) ⇒ Object


22
23
24
25
26
27
28
29
30
# File 'lib/tracks/done_todos.rb', line 22

def self.completed_period(date)
  return nil if date.nil? 

  return "today"         if date >= end_of_day  # treat todos with completed_at in future as done today (happens in tests)
  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
end

.done_between(todos, includes, start_date, end_date) ⇒ Object (private)


40
41
42
43
# File 'lib/tracks/done_todos.rb', line 40

def self.done_between(todos, includes, start_date, end_date)
  # TODO: refactor to remove outer hash from includes param
  todos.completed_before(start_date).completed_after(end_date).includes(includes[:include])
end

.done_rest_of_month(todos, includes = {:include => Todo::DEFAULT_INCLUDES}) ⇒ Object


18
19
20
# File 'lib/tracks/done_todos.rb', line 18

def self.done_rest_of_month(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
  done_between(todos, includes, beginning_of_week, beginning_of_month)
end

.done_rest_of_week(todos, includes = {:include => Todo::DEFAULT_INCLUDES}) ⇒ Object


14
15
16
# File 'lib/tracks/done_todos.rb', line 14

def self.done_rest_of_week(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
  done_between(todos, includes, beginning_of_day, beginning_of_week)
end

.done_today(todos, includes = {:include => Todo::DEFAULT_INCLUDES}) ⇒ Object


9
10
11
12
# File 'lib/tracks/done_todos.rb', line 9

def self.done_today(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
  # TODO: refactor to remove outer hash from includes param
  todos.completed_after(beginning_of_day).includes(includes[:include])
end

.done_todos_for_container(todos) ⇒ Object


4
5
6
7
# File 'lib/tracks/done_todos.rb', line 4

def self.done_todos_for_container(todos)
  completed_todos = todos.completed
  return done_today(completed_todos), done_rest_of_week(completed_todos), done_rest_of_month(completed_todos)
end

.end_of_dayObject (private)


49
50
51
# File 'lib/tracks/done_todos.rb', line 49

def self.end_of_day
  Time.zone.now.end_of_day
end

.remaining_in_container(todos, period) ⇒ Object


32
33
34
35
36
# File 'lib/tracks/done_todos.rb', line 32

def self.remaining_in_container(todos, period)
  count = self.send("done_#{period}", todos.completed, {}).count
  return nil if period.nil?
  return count
end