Rename new_context and new_project actions to create to fit into the CRUD model, add tests for these methods, too.

Add a plugin to allow executing tests using an in-memory sqlite database. Faster!



git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@318 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
lukemelia 2006-09-16 06:50:22 +00:00
parent 3e22381187
commit ba3757f29e
29 changed files with 416 additions and 114 deletions

View file

@ -66,6 +66,10 @@ class ApplicationController < ActionController::Base
redirect_with_flash message, options
end
def render_failure message, status = 404
render :text => message, :status => status
end
private
def get_current_user

View file

@ -32,14 +32,14 @@ class BackendController < ApplicationController
# Check whether the token in the URL matches the word in the User's table
def check_token_against_user_word(username, token)
@user = User.find_by_login( username )
unless ( token == @user.word)
raise (InvalidToken, "Sorry, you don't have permission to perform this action.")
unless (token == @user.word)
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.")
raise(CannotAccessContext, "Cannot access a context that does not belong to this user.")
end
end

View file

@ -32,14 +32,33 @@ class ContextController < ApplicationController
@page_title = "TRACKS::Context: #{@context.name}"
end
# Creates a new context via Ajax helpers
# Example XML usage: curl -H 'Accept: application/xml' -H 'Content-Type: application/xml'
# -u username:password
# -d '<request><context><name>new context_name</name></context></request>'
# http://our.tracks.host/context/create
#
def new_context
def create
@context = @user.contexts.build
@context.attributes = params['context']
params_are_invalid = true
if (params['context'] || (params['request'] && params['request']['context']))
@context.attributes = params['context'] || params['request']['context']
params_are_invalid = false
end
@context.name = deurlize(@context.name)
@saved = @context.save
@context_not_done_counts = { @context.id => 0 }
respond_to do |wants|
wants.js
wants.xml do
if @context.new_record? && params_are_invalid
render_failure "Expected post format is xml like so: <request><context><name>context name</name></context></request>."
elsif @context.new_record?
render_failure @context.errors.full_messages.join(', ')
else
render :xml => @context.to_xml( :except => :user_id )
end
end
end
end
# Called by a form button

View file

@ -56,12 +56,33 @@ class ProjectController < ApplicationController
end
end
def new_project
# Example XML usage: curl -H 'Accept: application/xml' -H 'Content-Type: application/xml'
# -u username:password
# -d '<request><project><name>new project_name</name></project></request>'
# http://our.tracks.host/project/create
#
def create
@project = @user.projects.build
@project.attributes = params['project']
params_are_invalid = true
if (params['project'] || (params['request'] && params['request']['project']))
@project.attributes = params['project'] || params['request']['project']
params_are_invalid = false
end
@project.name = deurlize(@project.name)
@saved = @project.save
@project_not_done_counts = { @project.id => 0 }
respond_to do |wants|
wants.js
wants.xml do
if @project.new_record? && params_are_invalid
render_failure "Expected post format is xml like so: <request><project><name>project name</name></project></request>."
elsif @project.new_record?
render_failure @project.errors.full_messages.join(', ')
else
render :xml => @project.to_xml( :except => :user_id )
end
end
end
end
# Called by a form button

View file

@ -47,16 +47,14 @@ class TodoController < ApplicationController
end
end
# Called by a form button
# Parameters from form fields are passed to create new action
# in the selected context.
def add_item
def create
init
@item = @user.todos.build
@item.attributes = params["todo"]
p = params['todo'] || params['request']['todo']
@item.attributes = p
if @item.due?
@item.due = parse_date_per_user_prefs(params["todo"]["due"])
@item.due = parse_date_per_user_prefs(p["due"])
else
@item.due = ""
end
@ -70,7 +68,7 @@ class TodoController < ApplicationController
init_todos
@up_count = @todos.reject { |x| x.done? or x.context.hide? }.size.to_s
end
render
render :action => 'create'
end
wants.xml { render :xml => @item.to_xml( :root => 'todo', :except => :user_id ) }
end
@ -86,6 +84,10 @@ class TodoController < ApplicationController
wants.xml { render :text => 'An error occurred on the server.' + $! }
end
end
def add_item
create
end
def edit
init

View file

@ -18,7 +18,7 @@ class UserController < ApplicationController
# Example usage: curl -H 'Accept: application/xml' -H 'Content-Type: application/xml'
# -u admin:up2n0g00d
# -d '<request><login>username</login><password>abc123</password></request>'
# http://our.tracks.host/cpa/create_user
# http://our.tracks.host/user/create
#
def create
admin = User.find_admin
@ -108,11 +108,7 @@ class UserController < ApplicationController
end
private
def render_failure message, status = 404
render :text => message, :status => status
end
def check_create_user_params
return false unless params.has_key?(:request)
return false unless params[:request].has_key?(:login)