From 335362601649185462a749a4496b0a4ffcf391c3 Mon Sep 17 00:00:00 2001 From: Eric Allen Date: Sun, 21 Sep 2008 09:35:34 -0700 Subject: [PATCH] Move Rich Todo API into Todo class and clean it up a bit --- app/controllers/application.rb | 2 + app/controllers/backend_controller.rb | 53 ++------------------------- app/models/todo.rb | 44 +++++++++++++++++++++- 3 files changed, 49 insertions(+), 50 deletions(-) diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 55c89a60..71e74fb5 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -16,6 +16,8 @@ require 'time' # # Tag +class CannotAccessContext < RuntimeError; end + class ApplicationController < ActionController::Base protect_from_forgery :secret => SALT diff --git a/app/controllers/backend_controller.rb b/app/controllers/backend_controller.rb index cc4c605e..206f2e81 100644 --- a/app/controllers/backend_controller.rb +++ b/app/controllers/backend_controller.rb @@ -14,36 +14,10 @@ class BackendController < ApplicationController def new_rich_todo(username, token, default_context_id, description, notes) check_token(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 -# logger.info("context='#{context}' project='#{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 - 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.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 = create_todo(description, context_id, project_id, notes) - todo.id + item = Todo.from_rich_message(@user, default_context_id, description, notes) + item.save + raise item.errors.full_messages.to_s if item.new_record? + item.id end def list_contexts(username, token) @@ -84,25 +58,6 @@ class BackendController < ApplicationController raise item.errors.full_messages.to_s if item.new_record? item end - - def split_by_char(separator,string) - parts = string.split(separator) - - # if the separator is used more than once, concat the last parts this is - # needed to get 'description @ @home > project' working for contexts - # starting with @ - if parts.length > 2 - 2.upto(parts.length-1) { |i| parts[1] += (separator +parts[i]) } - end - - 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 -class CannotAccessContext < RuntimeError; end diff --git a/app/models/todo.rb b/app/models/todo.rb index 7a6f3892..4d9c2675 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -125,4 +125,46 @@ class Todo < ActiveRecord::Base return self.recurring_todo_id != nil end -end \ No newline at end of file + # 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 + todo.notes = notes + todo.context_id = context_id + todo.project_id = project_id unless project_id.nil? + return todo + end +end