Class: DoneTodos

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

Class Method Summary collapse

Class Method Details

.beginning_of_dayObject (private)



43
44
45
# File 'lib/done_todos.rb', line 43

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

.beginning_of_monthObject (private)



55
56
57
# File 'lib/done_todos.rb', line 55

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

.beginning_of_weekObject (private)



51
52
53
# File 'lib/done_todos.rb', line 51

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

.completed_period(date) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/done_todos.rb', line 20

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)



38
39
40
41
# File 'lib/done_todos.rb', line 38

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



16
17
18
# File 'lib/done_todos.rb', line 16

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



12
13
14
# File 'lib/done_todos.rb', line 12

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



7
8
9
10
# File 'lib/done_todos.rb', line 7

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



2
3
4
5
# File 'lib/done_todos.rb', line 2

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)



47
48
49
# File 'lib/done_todos.rb', line 47

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

.remaining_in_container(todos, period) ⇒ Object



30
31
32
33
34
# File 'lib/done_todos.rb', line 30

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