mirror of
https://github.com/TracksApp/tracks.git
synced 2025-12-21 17:50:13 +01:00
Move done_todos.rb to the proper place
Since this is a bare class without a module, it needs to be in the `lib` directory rather than in `lib/tracks` since newer versions of Rails are more strict about file paths and autoloading.
This commit is contained in:
parent
555ae30bef
commit
d95bd49b68
1 changed files with 0 additions and 0 deletions
61
lib/done_todos.rb
Normal file
61
lib/done_todos.rb
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
class DoneTodos
|
||||
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
def self.done_rest_of_week(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
|
||||
done_between(todos, includes, beginning_of_day, beginning_of_week)
|
||||
end
|
||||
|
||||
def self.done_rest_of_month(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
|
||||
done_between(todos, includes, beginning_of_week, beginning_of_month)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def self.remaining_in_container(todos, period)
|
||||
count = self.send("done_#{period}", todos.completed, {}).count
|
||||
return nil if period.nil?
|
||||
return count
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue