From a9a05289ece257836cb1b87bb4d19447cc5703db Mon Sep 17 00:00:00 2001 From: lrbalt Date: Fri, 25 Jan 2008 20:39:44 +0000 Subject: [PATCH] fixes rich_api to handle contexts that contain @ like @home git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@702 a4c988fc-2ded-0310-b66e-134b36920a42 --- tracks/app/controllers/backend_controller.rb | 62 +++++++++++--------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/tracks/app/controllers/backend_controller.rb b/tracks/app/controllers/backend_controller.rb index bb9c1258..87781bdb 100644 --- a/tracks/app/controllers/backend_controller.rb +++ b/tracks/app/controllers/backend_controller.rb @@ -57,40 +57,48 @@ class BackendController < ApplicationController private - # Check whether the token in the URL matches the token in the User's table - def check_token(username, token) - @user = User.find_by_login( username ) - unless (token == @user.token) - raise(InvalidToken, "Sorry, you don't have permission to perform this action.") - end + # Check whether the token in the URL matches the token in the User's table + def check_token(username, token) + @user = User.find_by_login( username ) + unless (token == @user.token) + raise(InvalidToken, "Sorry, you don't have permission to perform this action.") end + end - def check_context_belongs_to_user(context_id) - unless @user.contexts.exists? context_id - raise(CannotAccessContext, "Cannot access a context that does not belong to this user.") - end + def check_context_belongs_to_user(context_id) + unless @user.contexts.exists? context_id + raise(CannotAccessContext, "Cannot access a context that does not belong to this user.") end + end - def create_todo(description, context_id, project_id = nil, notes="") - item = @user.todos.build - item.description = description - item.notes = notes - 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 create_todo(description, context_id, project_id = nil, notes="") + item = @user.todos.build + item.description = description + item.notes = notes + 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]) + 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] += parts[i] } end + + return safe_strip(parts[0]), safe_strip(parts[1]) + end - def safe_strip(s) - s.strip! unless s.nil? - s - end + def safe_strip(s) + s.strip! unless s.nil? + s + end end class InvalidToken < RuntimeError; end