diff --git a/app/models/todo.rb b/app/models/todo.rb index 012da634..a8258a64 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -51,6 +51,16 @@ class Todo < ActiveRecord::Base scope :created_after, lambda { |date| where("todos.created_at > ?", date) } scope :created_before, lambda { |date| where("todos.created_at < ?", date) } + scope :due_today, lambda { where("todos.due <= ?", Time.zone.now) } + + def self.due_after(date) + where('todos.due > ?', date) + end + + def self.due_between(start_date, end_date) + where('todos.due > ? AND todos.due <= ?', start_date, end_date) + end + STARRED_TAG_NAME = "starred" DEFAULT_INCLUDES = [ :project, :context, :tags, :taggings, :pending_successors, :uncompleted_predecessors, :recurring_todo ] diff --git a/app/models/todos/calendar.rb b/app/models/todos/calendar.rb index ba727c8f..d208f660 100644 --- a/app/models/todos/calendar.rb +++ b/app/models/todos/calendar.rb @@ -13,38 +13,23 @@ module Todos end def due_today - user.todos.not_completed. - where('todos.due <= ?', today). - includes(included_tables). - reorder("due") + actions.due_today end def due_this_week - user.todos.not_completed. - where('todos.due > ? AND todos.due <= ?', today, Time.zone.now.end_of_week). - includes(included_tables). - reorder("due") + actions.due_between(today, end_of_the_week) end def due_next_week - user.todos.not_completed. - where('todos.due > ? AND todos.due <= ?', end_of_the_week, end_of_next_week). - includes(included_tables). - reorder("due") + actions.due_between(end_of_the_week, end_of_next_week) end def due_this_month - user.todos.not_completed. - where('todos.due > ? AND todos.due <= ?', end_of_next_week, end_of_the_month). - includes(included_tables). - reorder("due") + actions.due_between(end_of_next_week, end_of_the_month) end def due_after_this_month - user.todos.not_completed. - where('todos.due > ?', end_of_the_month). - includes(included_tables). - reorder("due") + actions.due_after(end_of_the_month) end def today @@ -63,5 +48,11 @@ module Todos @end_of_the_month ||= today.end_of_month end + private + + def actions + user.todos.not_completed.includes(included_tables).reorder("due") + end + end end