2009-01-21 13:36:26 +01:00
|
|
|
class Todo < ActiveRecord::Base
|
|
|
|
|
|
|
|
belongs_to :context
|
|
|
|
belongs_to :project
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :recurring_todo
|
2009-05-20 02:05:49 +02:00
|
|
|
|
2009-06-08 23:43:40 +02:00
|
|
|
has_many :predecessor_dependencies, :foreign_key => 'predecessor_id', :class_name => 'Dependency', :dependent => :destroy
|
|
|
|
has_many :successor_dependencies, :foreign_key => 'successor_id', :class_name => 'Dependency', :dependent => :destroy
|
2009-06-09 13:45:39 +02:00
|
|
|
has_many :predecessors, :through => :successor_dependencies
|
|
|
|
has_many :successors, :through => :predecessor_dependencies
|
2009-05-20 13:00:14 +02:00
|
|
|
has_many :uncompleted_predecessors, :through => :successor_dependencies,
|
2011-02-04 20:11:42 +01:00
|
|
|
:source => :predecessor, :conditions => ['NOT (todos.state = ?)', 'completed']
|
2009-06-08 23:43:40 +02:00
|
|
|
has_many :pending_successors, :through => :predecessor_dependencies,
|
2011-02-04 20:11:42 +01:00
|
|
|
:source => :successor, :conditions => ['todos.state = ?', 'pending']
|
2009-06-08 23:43:40 +02:00
|
|
|
|
2009-06-30 23:17:33 +02:00
|
|
|
after_save :save_predecessors
|
2009-01-21 13:36:26 +01:00
|
|
|
|
|
|
|
named_scope :active, :conditions => { :state => 'active' }
|
2011-01-06 23:01:17 +01:00
|
|
|
named_scope :active_or_hidden, :conditions => ["todos.state = ? OR todos.state = ?", 'active', 'project_hidden']
|
2009-12-08 13:13:21 -05:00
|
|
|
named_scope :not_completed, :conditions => ['NOT (todos.state = ? )', 'completed']
|
2011-05-03 11:43:02 +02:00
|
|
|
named_scope :completed, :conditions => ["NOT(todos.completed_at IS NULL)"]
|
2009-02-08 20:38:27 +01:00
|
|
|
named_scope :are_due, :conditions => ['NOT (todos.due IS NULL)']
|
2011-05-03 11:43:02 +02:00
|
|
|
named_scope :deferred, :conditions => ["todos.completed_at IS NULL AND NOT(todos.show_from IS NULL)"]
|
2010-05-01 17:19:28 +02:00
|
|
|
named_scope :blocked, :conditions => ['todos.state = ?', 'pending']
|
2011-05-03 11:43:02 +02:00
|
|
|
named_scope :deferred_or_blocked, :conditions => ["(todos.completed_at IS NULL AND NOT(todos.show_from IS NULL)) OR (todos.state = ?)", "pending"]
|
|
|
|
named_scope :not_deferred_or_blocked, :conditions => ["todos.completed_at IS NULL AND todos.show_from IS NULL AND NOT(todos.state = ?)", "pending"]
|
2011-01-01 18:55:53 +01:00
|
|
|
named_scope :with_tag, lambda { |tag| {:joins => :taggings, :conditions => ["taggings.tag_id = ? ", tag.id] } }
|
|
|
|
named_scope :of_user, lambda { |user_id| {:conditions => ["todos.user_id = ? ", user_id] } }
|
|
|
|
named_scope :hidden,
|
|
|
|
:joins => :context,
|
2011-01-06 10:51:58 +01:00
|
|
|
:conditions => ["todos.state = ? OR (contexts.hide = ? AND (todos.state = ? OR todos.state = ? OR todos.state = ?))",
|
|
|
|
'project_hidden', true, 'active', 'deferred', 'pending']
|
2011-01-01 18:55:53 +01:00
|
|
|
named_scope :not_hidden,
|
2011-01-08 09:12:37 +01:00
|
|
|
:joins => [:context],
|
2011-01-06 10:51:58 +01:00
|
|
|
:conditions => ['NOT(todos.state = ? OR (contexts.hide = ? AND (todos.state = ? OR todos.state = ? OR todos.state = ?)))',
|
|
|
|
'project_hidden', true, 'active', 'deferred', 'pending']
|
2009-01-21 13:36:26 +01:00
|
|
|
|
|
|
|
STARRED_TAG_NAME = "starred"
|
2010-08-12 14:39:58 +02:00
|
|
|
|
|
|
|
# regular expressions for dependencies
|
2011-02-03 16:59:59 +01:00
|
|
|
RE_TODO = /[^']+/
|
|
|
|
RE_CONTEXT = /[^']+/
|
|
|
|
RE_PROJECT = /[^']+/
|
|
|
|
RE_PARTS = /'(#{RE_TODO})'\s<'(#{RE_CONTEXT})';\s'(#{RE_PROJECT})'>/ # results in array
|
|
|
|
RE_SPEC = /'#{RE_TODO}'\s<'#{RE_CONTEXT}';\s'#{RE_PROJECT}'>/ # results in string
|
2009-01-21 13:36:26 +01:00
|
|
|
|
2011-05-16 15:42:47 +08:00
|
|
|
include AASM
|
|
|
|
aasm_column :state
|
|
|
|
aasm_initial_state Proc.new { |todo| (todo.show_from && (todo.show_from > todo.user.date)) ? :deferred : :active}
|
2009-01-21 13:36:26 +01:00
|
|
|
|
|
|
|
# when entering active state, also remove completed_at date. Looks like :exit
|
|
|
|
# of state completed is not run, see #679
|
2011-06-10 14:28:42 +02:00
|
|
|
aasm_state :active
|
2011-06-12 00:11:29 +02:00
|
|
|
aasm_state :project_hidden
|
|
|
|
aasm_state :completed, :enter => Proc.new { |t| t.completed_at = Time.zone.now }, :exit => Proc.new { |t| t.completed_at = nil }
|
2011-06-10 14:28:42 +02:00
|
|
|
aasm_state :deferred, :exit => Proc.new { |t| t[:show_from] = nil }
|
|
|
|
aasm_state :pending
|
2009-01-21 13:36:26 +01:00
|
|
|
|
2011-05-16 15:42:47 +08:00
|
|
|
aasm_event :defer do
|
2009-01-21 13:36:26 +01:00
|
|
|
transitions :to => :deferred, :from => [:active]
|
|
|
|
end
|
|
|
|
|
2011-05-16 15:42:47 +08:00
|
|
|
aasm_event :complete do
|
2009-01-21 13:36:26 +01:00
|
|
|
transitions :to => :completed, :from => [:active, :project_hidden, :deferred]
|
|
|
|
end
|
|
|
|
|
2011-05-16 15:42:47 +08:00
|
|
|
aasm_event :activate do
|
2009-01-21 13:36:26 +01:00
|
|
|
transitions :to => :active, :from => [:project_hidden, :completed, :deferred]
|
2009-08-16 22:00:20 +02:00
|
|
|
transitions :to => :active, :from => [:pending], :guard => :no_uncompleted_predecessors_or_deferral?
|
|
|
|
transitions :to => :deferred, :from => [:pending], :guard => :no_uncompleted_predecessors?
|
2009-01-21 13:36:26 +01:00
|
|
|
end
|
|
|
|
|
2011-05-16 15:42:47 +08:00
|
|
|
aasm_event :hide do
|
2009-01-21 13:36:26 +01:00
|
|
|
transitions :to => :project_hidden, :from => [:active, :deferred]
|
|
|
|
end
|
|
|
|
|
2011-05-16 15:42:47 +08:00
|
|
|
aasm_event :unhide do
|
2009-01-21 13:36:26 +01:00
|
|
|
transitions :to => :deferred, :from => [:project_hidden], :guard => Proc.new{|t| !t.show_from.blank? }
|
|
|
|
transitions :to => :active, :from => [:project_hidden]
|
|
|
|
end
|
2009-05-20 02:05:49 +02:00
|
|
|
|
2011-05-16 15:42:47 +08:00
|
|
|
aasm_event :block do
|
2009-08-16 22:00:20 +02:00
|
|
|
transitions :to => :pending, :from => [:active, :deferred]
|
2009-05-20 02:05:49 +02:00
|
|
|
end
|
2009-01-21 13:36:26 +01:00
|
|
|
|
|
|
|
attr_protected :user
|
|
|
|
|
|
|
|
# Description field can't be empty, and must be < 100 bytes Notes must be <
|
|
|
|
# 60,000 bytes (65,000 actually, but I'm being cautious)
|
|
|
|
validates_presence_of :description
|
|
|
|
validates_length_of :description, :maximum => 100
|
|
|
|
validates_length_of :notes, :maximum => 60000, :allow_nil => true
|
|
|
|
validates_presence_of :show_from, :if => :deferred?
|
|
|
|
validates_presence_of :context
|
|
|
|
|
2009-06-30 23:17:33 +02:00
|
|
|
def initialize(*args)
|
|
|
|
super(*args)
|
2009-07-19 21:46:45 +02:00
|
|
|
@predecessor_array = nil # Used for deferred save of predecessors
|
2011-02-03 16:59:59 +01:00
|
|
|
@removed_predecessors = nil
|
2009-06-30 23:17:33 +02:00
|
|
|
end
|
2011-06-10 14:28:42 +02:00
|
|
|
|
2009-08-16 22:00:20 +02:00
|
|
|
def no_uncompleted_predecessors_or_deferral?
|
2009-08-19 22:15:38 +02:00
|
|
|
return (show_from.blank? or Time.zone.now > show_from and uncompleted_predecessors.empty?)
|
2009-08-16 22:00:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def no_uncompleted_predecessors?
|
2009-08-19 22:15:38 +02:00
|
|
|
return uncompleted_predecessors.empty?
|
|
|
|
end
|
|
|
|
|
2009-08-20 09:54:22 +02:00
|
|
|
# Returns a string with description <context, project>
|
|
|
|
def specification
|
2011-02-12 10:38:15 +01:00
|
|
|
project_name = self.project.is_a?(NullProject) ? "(none)" : self.project.name
|
|
|
|
return "\'#{self.description}\' <\'#{self.context.title}\'; \'#{project_name}\'>"
|
2009-08-20 09:54:22 +02:00
|
|
|
end
|
2011-02-12 10:38:15 +01:00
|
|
|
|
2009-01-21 13:36:26 +01:00
|
|
|
def validate
|
|
|
|
if !show_from.blank? && show_from < user.date
|
2010-11-09 10:47:09 +01:00
|
|
|
errors.add("show_from", I18n.t('models.todo.error_date_must_be_future'))
|
2009-01-21 13:36:26 +01:00
|
|
|
end
|
2011-02-12 10:38:15 +01:00
|
|
|
errors.add(:description, "may not contain \" characters") if /\"/.match(self.description)
|
2009-07-19 21:46:45 +02:00
|
|
|
unless @predecessor_array.nil? # Only validate predecessors if they changed
|
2011-02-12 10:38:15 +01:00
|
|
|
@predecessor_array.each do |todo|
|
|
|
|
errors.add("Depends on:", "Adding '#{h(todo.specification)}' would create a circular dependency") if is_successor?(todo)
|
2009-06-30 23:17:33 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_predecessors
|
2009-07-19 21:46:45 +02:00
|
|
|
unless @predecessor_array.nil? # Only save predecessors if they changed
|
2011-02-12 10:38:15 +01:00
|
|
|
current_array = self.predecessors
|
2009-07-19 21:46:45 +02:00
|
|
|
remove_array = current_array - @predecessor_array
|
|
|
|
add_array = @predecessor_array - current_array
|
2011-02-03 16:59:59 +01:00
|
|
|
|
|
|
|
@removed_predecessors = []
|
2011-02-12 10:38:15 +01:00
|
|
|
remove_array.each do |todo|
|
|
|
|
unless todo.nil?
|
|
|
|
@removed_predecessors << todo
|
|
|
|
self.predecessors.delete(todo)
|
2011-02-03 16:59:59 +01:00
|
|
|
end
|
2009-07-19 21:46:45 +02:00
|
|
|
end
|
2011-02-12 10:38:15 +01:00
|
|
|
|
|
|
|
add_array.each do |todo|
|
|
|
|
unless todo.nil?
|
|
|
|
self.predecessors << todo unless self.predecessors.include?(todo)
|
2009-07-19 21:46:45 +02:00
|
|
|
else
|
2011-02-12 10:38:15 +01:00
|
|
|
logger.error "Could not find #{todo.description}" # Unexpected since validation passed
|
2009-07-19 21:46:45 +02:00
|
|
|
end
|
2009-06-30 23:17:33 +02:00
|
|
|
end
|
2011-02-03 16:59:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def removed_predecessors
|
|
|
|
return @removed_predecessors
|
2009-06-30 23:17:33 +02:00
|
|
|
end
|
|
|
|
|
2009-11-10 22:15:16 -05:00
|
|
|
def remove_predecessor(predecessor)
|
|
|
|
# remove predecessor and activate myself
|
2011-02-12 10:38:15 +01:00
|
|
|
self.predecessors.delete(predecessor)
|
2009-11-10 22:15:16 -05:00
|
|
|
self.activate!
|
|
|
|
end
|
|
|
|
|
2009-06-30 23:17:33 +02:00
|
|
|
# Returns true if t is equal to self or a successor of self
|
2011-02-12 10:38:15 +01:00
|
|
|
def is_successor?(todo)
|
|
|
|
if self == todo
|
2009-06-30 23:17:33 +02:00
|
|
|
return true
|
|
|
|
elsif self.successors.empty?
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
self.successors.each do |item|
|
2011-02-12 10:38:15 +01:00
|
|
|
if item.is_successor?(todo)
|
2009-06-30 23:17:33 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
2009-01-21 13:36:26 +01:00
|
|
|
end
|
|
|
|
|
2011-01-01 18:55:53 +01:00
|
|
|
def has_tag?(tag)
|
|
|
|
return self.tags.select{|t| t.name==tag }.size > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def hidden?
|
|
|
|
return self.state == 'project_hidden' || ( self.context.hidden? && (self.state == 'active' || self.state == 'deferred'))
|
|
|
|
end
|
|
|
|
|
2009-01-21 13:36:26 +01:00
|
|
|
def update_state_from_project
|
2011-01-06 23:01:17 +01:00
|
|
|
if self.state == 'project_hidden' and !self.project.hidden?
|
2009-11-04 22:45:38 -05:00
|
|
|
if self.uncompleted_predecessors.empty?
|
|
|
|
self.state = 'active'
|
2009-11-25 16:13:52 -05:00
|
|
|
else
|
|
|
|
self.state = 'pending'
|
2009-11-04 22:45:38 -05:00
|
|
|
end
|
2011-01-01 18:55:53 +01:00
|
|
|
elsif self.state == 'active' and self.project.hidden?
|
2009-01-21 13:36:26 +01:00
|
|
|
self.state = 'project_hidden'
|
|
|
|
end
|
2011-01-06 23:01:17 +01:00
|
|
|
self.save!
|
2009-01-21 13:36:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def toggle_completion!
|
|
|
|
saved = false
|
|
|
|
if completed?
|
|
|
|
saved = activate!
|
|
|
|
else
|
|
|
|
saved = complete!
|
|
|
|
end
|
|
|
|
return saved
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_from
|
|
|
|
self[:show_from]
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_from=(date)
|
|
|
|
# parse Date objects into the proper timezone
|
|
|
|
date = user.at_midnight(date) if (date.is_a? Date)
|
2011-05-16 15:42:47 +08:00
|
|
|
|
2011-06-10 14:28:42 +02:00
|
|
|
# show_from needs to be set before state_change because of "bug" in aasm.
|
|
|
|
# If show_From is not set, the todo will not validate and thus aasm will not save
|
|
|
|
# (see http://stackoverflow.com/questions/682920/persisting-the-state-column-on-transition-using-rubyist-aasm-acts-as-state-machi)
|
2009-01-21 13:36:26 +01:00
|
|
|
self[:show_from] = date
|
2011-06-10 14:28:42 +02:00
|
|
|
|
|
|
|
activate! if deferred? && date.blank?
|
|
|
|
defer! if active? && !date.blank? && date > user.date
|
2009-01-21 13:36:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :original_project, :project
|
|
|
|
|
|
|
|
def project
|
|
|
|
original_project.nil? ? Project.null_object : original_project
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.feed_options(user)
|
|
|
|
{
|
|
|
|
:title => 'Tracks Actions',
|
|
|
|
:description => "Actions for #{user.display_name}"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def starred?
|
|
|
|
tags.any? {|tag| tag.name == STARRED_TAG_NAME}
|
|
|
|
end
|
|
|
|
|
|
|
|
def toggle_star!
|
|
|
|
if starred?
|
|
|
|
_remove_tags STARRED_TAG_NAME
|
|
|
|
tags.reload
|
|
|
|
else
|
|
|
|
_add_tags(STARRED_TAG_NAME)
|
|
|
|
tags.reload
|
|
|
|
end
|
|
|
|
starred?
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_recurring_todo?
|
|
|
|
return self.recurring_todo_id != nil
|
|
|
|
end
|
2009-08-20 09:54:22 +02:00
|
|
|
|
2009-11-04 22:39:19 -05:00
|
|
|
def add_predecessor_list(predecessor_list)
|
2009-08-21 14:59:03 +02:00
|
|
|
return unless predecessor_list.kind_of? String
|
2011-02-12 10:38:15 +01:00
|
|
|
|
|
|
|
@predecessor_array=[]
|
|
|
|
|
2011-02-25 15:24:43 +01:00
|
|
|
predecessor_ids_array = predecessor_list.split(",")
|
2011-02-12 10:38:15 +01:00
|
|
|
predecessor_ids_array.each do |todo_id|
|
|
|
|
predecessor = self.user.todos.find_by_id( todo_id.to_i ) unless todo_id.blank?
|
|
|
|
@predecessor_array << predecessor unless predecessor.nil?
|
|
|
|
end
|
|
|
|
|
2010-08-12 14:39:58 +02:00
|
|
|
return @predecessor_array
|
2009-11-04 22:39:19 -05:00
|
|
|
end
|
|
|
|
|
2009-06-30 23:51:41 +02:00
|
|
|
def add_predecessor(t)
|
2011-02-12 10:38:15 +01:00
|
|
|
@predecessor_array = predecessors
|
2011-02-12 23:18:00 +01:00
|
|
|
@predecessor_array << t
|
2009-06-30 23:51:41 +02:00
|
|
|
end
|
|
|
|
|
2009-11-10 22:10:52 -05:00
|
|
|
# Return todos that should be activated if the current todo is completed
|
|
|
|
def pending_to_activate
|
|
|
|
return successors.find_all {|t| t.uncompleted_predecessors.empty?}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return todos that should be blocked if the current todo is undone
|
|
|
|
def active_to_block
|
2009-08-16 22:00:20 +02:00
|
|
|
return successors.find_all {|t| t.active? or t.deferred?}
|
2009-11-10 22:10:52 -05:00
|
|
|
end
|
2010-04-07 10:06:46 -04:00
|
|
|
|
|
|
|
def raw_notes=(value)
|
|
|
|
self[:notes] = value
|
|
|
|
end
|
|
|
|
|
2009-01-21 13:36:26 +01:00
|
|
|
# Rich Todo API
|
|
|
|
|
|
|
|
def self.from_rich_message(user, default_context_id, description, notes)
|
|
|
|
fields = description.match(/([^>@]*)@?([^>]*)>?(.*)/)
|
|
|
|
description = fields[1].strip
|
|
|
|
context = fields[2].strip
|
|
|
|
project = fields[3].strip
|
|
|
|
|
|
|
|
context = nil if context == ""
|
|
|
|
project = nil if project == ""
|
|
|
|
|
|
|
|
context_id = default_context_id
|
|
|
|
unless(context.nil?)
|
|
|
|
found_context = user.active_contexts.find_by_namepart(context)
|
|
|
|
found_context = user.contexts.find_by_namepart(context) if found_context.nil?
|
|
|
|
context_id = found_context.id unless found_context.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
unless user.contexts.exists? context_id
|
|
|
|
raise(CannotAccessContext, "Cannot access a context that does not belong to this user.")
|
|
|
|
end
|
|
|
|
|
|
|
|
project_id = nil
|
|
|
|
unless(project.blank?)
|
|
|
|
if(project[0..3].downcase == "new:")
|
|
|
|
found_project = user.projects.build
|
|
|
|
found_project.name = project[4..255+4].strip
|
|
|
|
found_project.save!
|
|
|
|
else
|
|
|
|
found_project = user.active_projects.find_by_namepart(project)
|
|
|
|
found_project = user.projects.find_by_namepart(project) if found_project.nil?
|
|
|
|
end
|
|
|
|
project_id = found_project.id unless found_project.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
todo = user.todos.build
|
|
|
|
todo.description = description
|
2010-04-07 10:06:46 -04:00
|
|
|
todo.raw_notes = notes
|
2009-01-21 13:36:26 +01:00
|
|
|
todo.context_id = context_id
|
|
|
|
todo.project_id = project_id unless project_id.nil?
|
|
|
|
return todo
|
|
|
|
end
|
|
|
|
end
|