fix #922. You can now mark a todo complete from the tickler. Also fixed some small aasm corner cases found by this change

This commit is contained in:
Reinier Balt 2011-08-18 17:15:00 +02:00
parent 367907eab2
commit 07b05d01f7
16 changed files with 276 additions and 147 deletions

View file

@ -7,7 +7,7 @@ class Todo < ActiveRecord::Base
belongs_to :project
belongs_to :user
belongs_to :recurring_todo
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
has_many :predecessors, :through => :successor_dependencies
@ -27,7 +27,7 @@ class Todo < ActiveRecord::Base
named_scope :pending, :conditions => ['todos.state = ?', 'pending']
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"]
named_scope :hidden,
named_scope :hidden,
:joins => :context,
:conditions => ["todos.state = ? OR (contexts.hide = ? AND (todos.state = ? OR todos.state = ? OR todos.state = ?))",
'project_hidden', true, 'active', 'deferred', 'pending']
@ -41,7 +41,7 @@ class Todo < ActiveRecord::Base
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 :completed_after, lambda { |date| {:conditions => ["todos.completed_at > ? ", date] } }
named_scope :completed_before, lambda { |date| {:conditions => ["todos.completed_at < ? ", date] } }
named_scope :completed_before, lambda { |date| {:conditions => ["todos.completed_at < ? ", date] } }
STARRED_TAG_NAME = "starred"
DEFAULT_INCLUDES = [ :project, :context, :tags, :taggings, :pending_successors, :uncompleted_predecessors, :recurring_todo ]
@ -52,76 +52,82 @@ class Todo < ActiveRecord::Base
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
include AASM
aasm_column :state
aasm_initial_state Proc.new { |t| (t.show_from && t.user && (t.show_from > t.user.date)) ? :deferred : :active}
# when entering active state, also remove completed_at date. Looks like :exit
# of state completed is not run, see #679
aasm_state :active
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 }
aasm_state :completed, :enter => Proc.new { |t| t.completed_at = Time.zone.now }, :exit => Proc.new { |t| t.completed_at = nil}
aasm_state :deferred, :exit => Proc.new { |t| t[:show_from] = nil }
aasm_state :pending
aasm_state :pending
aasm_event :defer do
transitions :to => :deferred, :from => [:active]
end
aasm_event :complete do
transitions :to => :completed, :from => [:active, :project_hidden, :deferred]
transitions :to => :completed, :from => [:active, :project_hidden, :deferred, :pending]
end
aasm_event :activate do
transitions :to => :active, :from => [:project_hidden, :completed, :deferred]
transitions :to => :active, :from => [:pending], :guard => :no_uncompleted_predecessors_or_deferral?
transitions :to => :active, :from => [:project_hidden, :deferred]
transitions :to => :active, :from => [:completed], :guard => :no_uncompleted_predecessors?
transitions :to => :active, :from => [:pending], :guard => :no_uncompleted_predecessors_or_deferral?
transitions :to => :pending, :from => [:completed], :guard => :uncompleted_predecessors?
transitions :to => :deferred, :from => [:pending], :guard => :no_uncompleted_predecessors?
end
aasm_event :hide do
transitions :to => :project_hidden, :from => [:active, :deferred]
end
aasm_event :unhide do
transitions :to => :deferred, :from => [:project_hidden], :guard => Proc.new{|t| !t.show_from.blank? }
transitions :to => :active, :from => [:project_hidden]
end
aasm_event :block do
transitions :to => :pending, :from => [:active, :deferred]
end
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_length_of :notes, :maximum => 60000, :allow_nil => true
validates_presence_of :show_from, :if => :deferred?
validates_presence_of :context
def initialize(*args)
super(*args)
@predecessor_array = nil # Used for deferred save of predecessors
@removed_predecessors = nil
end
def no_uncompleted_predecessors_or_deferral?
return (show_from.blank? or Time.zone.now > show_from and uncompleted_predecessors.empty?)
no_deferral = show_from.blank? or Time.zone.now > show_from
no_uncompleted_predecessors = uncompleted_predecessors.all(true).empty?
return (no_deferral && no_uncompleted_predecessors)
end
def no_uncompleted_predecessors?
return uncompleted_predecessors.empty?
return uncompleted_predecessors.all(true).empty?
end
def uncompleted_predecessors?
return !uncompleted_predecessors.all(true).empty?
end
# Returns a string with description <context, project>
def specification
project_name = self.project.is_a?(NullProject) ? "(none)" : self.project.name
return "\'#{self.description}\' <\'#{self.context.title}\'; \'#{project_name}\'>"
end
def validate
if !show_from.blank? && show_from < user.date
errors.add("show_from", I18n.t('models.todo.error_date_must_be_future'))
@ -133,7 +139,7 @@ class Todo < ActiveRecord::Base
end
end
end
def save_predecessors
unless @predecessor_array.nil? # Only save predecessors if they changed
current_array = self.predecessors
@ -155,19 +161,19 @@ class Todo < ActiveRecord::Base
logger.error "Could not find #{todo.description}" # Unexpected since validation passed
end
end
end
end
end
def removed_predecessors
return @removed_predecessors
end
def remove_predecessor(predecessor)
# remove predecessor and activate myself
self.predecessors.delete(predecessor)
self.activate!
end
# Returns true if t is equal to self or a successor of self
def is_successor?(todo)
if self == todo
@ -183,11 +189,11 @@ class Todo < ActiveRecord::Base
end
return false
end
def has_pending_successors
return !pending_successors.empty?
end
def has_tag?(tag)
return self.tags.select{|t| t.name==tag }.size > 0
end
@ -208,23 +214,23 @@ class Todo < ActiveRecord::Base
end
self.save!
end
def toggle_completion!
return completed? ? activate! : complete!
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)
# show_from needs to be set before state_change because of "bug" in aasm.
# 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)
self[:show_from] = date
self[:show_from] = date
activate! if deferred? && date.blank?
defer! if active? && !date.blank? && date > user.date
@ -235,18 +241,18 @@ class Todo < ActiveRecord::Base
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!
self.starred= !starred?
end
@ -277,19 +283,19 @@ class Todo < ActiveRecord::Base
return @predecessor_array
end
def add_predecessor(t)
@predecessor_array = predecessors
@predecessor_array << t
end
# activate todos that should be activated if the current todo is completed
def activate_pending_todos
pending_todos = successors.find_all {|t| t.uncompleted_predecessors.empty?}
pending_todos.each {|t| t.activate! }
return pending_todos
end
# Return todos that should be blocked if the current todo is undone
def block_successors
active_successors = successors.find_all {|t| t.active? or t.deferred?}
@ -302,13 +308,13 @@ class Todo < ActiveRecord::Base
end
# 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 == ""
@ -318,11 +324,11 @@ class Todo < ActiveRecord::Base
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:")
@ -335,7 +341,7 @@ class Todo < ActiveRecord::Base
end
project_id = found_project.id unless found_project.nil?
end
todo = user.todos.build
todo.description = description
todo.raw_notes = notes
@ -343,5 +349,5 @@ class Todo < ActiveRecord::Base
todo.project_id = project_id unless project_id.nil?
return todo
end
end