Applied Tomas Rich's patch (with some refactoring) to introduce a new api method

that allows task creation using the same syntax as the "Send to kGTD" quicksilver plugin:

Put an @ in front of context name and > in front of project name so "Call jim @calls > Fix house"
would create the "Call jim" todo in the calls context and the Fix house project.

If there aren't any exact matches, it selects the first context and project that starts with the given strings,
so "Call jim @cal >Fix h" would also work.

If no project and no context are give,
it works exactly like the NewTodo.

It also supports the ability to create a new project on the fly by prefacing
the project with "new:", for example "Call jim @calls > new:Clean the porch"

The new api method the new api method has the name NewRichTodo, which neither Tomas nor I like very much.
Perhaps you have an idea for a better name?

Closes #384.

Also, I removed duplication between context and project models by
introducing acts_as_namepart_finder and acts_as_todo_container.


git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@333 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
lukemelia 2006-10-27 07:19:24 +00:00
parent 4c2742f6f3
commit 4e0b459524
8 changed files with 200 additions and 61 deletions

View file

@ -6,15 +6,41 @@ class BackendController < ApplicationController
def new_todo(username, token, context_id, description)
check_token_against_user_word(username, token)
check_context_belongs_to_user(context_id)
item = @user.todos.build
item.description = description
item.context_id = context_id
item.save
raise item.errors.full_messages.to_s if item.new_record?
item = create_todo(description, context_id)
item.id
end
def new_rich_todo(username, token, default_context_id, description)
check_token_against_user_word(username,token)
description,context = split_by_char('@',description)
description,project = split_by_char('>',description)
if(!context.nil? && project.nil?)
context,project = split_by_char('>',context)
end
context_id = default_context_id
unless(context.nil?)
found_context = @user.contexts.find_by_namepart(context)
context_id = found_context.id unless found_context.nil?
end
check_context_belongs_to_user(context_id)
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.projects.find_by_namepart(project)
end
project_id = found_project.id unless found_project.nil?
end
todo = create_todo(description, context_id, project_id)
todo.id
end
def list_contexts(username, token)
check_token_against_user_word(username, token)
@ -42,7 +68,26 @@ class BackendController < ApplicationController
raise(CannotAccessContext, "Cannot access a context that does not belong to this user.")
end
end
def create_todo(description, context_id, project_id = nil)
item = @user.todos.build
item.description = description
item.context_id = context_id
item.project_id = project_id unless project_id.nil?
item.save
raise item.errors.full_messages.to_s if item.new_record?
item
end
def split_by_char(separator,string)
parts = string.split(separator)
return safe_strip(parts[0]), safe_strip(parts[1])
end
def safe_strip(s)
s.strip! unless s.nil?
s
end
end
class InvalidToken < RuntimeError; end