tracks/tracks/app/controllers/contexts_controller.rb
lukemelia 38b2e336a8 Implemented a feature that give a project and optional default context. When set,
this context will be pre-populated when creating an action from the project's page.
When creating an action from the home page, the context will be auto-selected when
the project is selected if the context field has not yet been entered.

This implementation is a combination of the great patch submitted by James Kebinger
(thanks James!) and some of my modifications and additions.

Don't forget to rake db:migrate.

Fixes #162, originally suggested by Rolf one year ago!

Also in this commit:

 * Tweaked selenium tags test
 * Tweaked formatting of next/previous project HTML
 * Implemented Null Object pattern for context to support
   a Project having no default context
 * Removed tickler.rhtml, no longer in use
 * applying z-index values to project sortable list items (otherwise context
   autocomplete was appearing below next list item)
 * Swapped order of project and context in new action form (setting default context
   makes more sense this way)
 * Removed CSS width of for form elements, so form could be used in content area
   without being too narrow
   


git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@480 a4c988fc-2ded-0310-b66e-134b36920a42
2007-03-21 07:12:14 +00:00

168 lines
5.9 KiB
Ruby

class ContextsController < ApplicationController
helper :todos
before_filter :init, :except => [:create, :destroy, :order]
before_filter :init_todos, :only => :show
before_filter :check_user_set_context, :only => [:update, :destroy]
skip_before_filter :login_required, :only => [:index]
prepend_before_filter :login_or_feed_token_required, :only => [:index]
session :off, :only => :index, :if => Proc.new { |req| ['rss','atom','txt'].include?(req.parameters[:format]) }
def index
respond_to do |format|
format.html { @page_title = "TRACKS::List Contexts"; render }
format.xml { render :xml => @contexts.to_xml( :except => :user_id ) }
format.rss &render_contexts_rss_feed
format.atom &render_contexts_atom_feed
format.text { render :action => 'index_text', :layout => false, :content_type => Mime::TEXT }
end
end
def show
@page_title = "TRACKS::Context: #{@context.name}"
end
# 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/contexts
#
def create
if params[:format] == 'application/xml' && params['exception']
render_failure "Expected post format is valid xml like so: <request><context><name>context name</name></context></request>."
return
end
@context = @user.contexts.build
params_are_invalid = true
if (params['context'] || (params['request'] && params['request']['context']))
@context.attributes = params['context'] || params['request']['context']
params_are_invalid = false
end
@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 valid xml like so: <request><context><name>context name</name></context></request>."
elsif @context.new_record?
render_failure @context.errors.to_xml
else
render :xml => @context.to_xml( :except => :user_id )
end
end
end
end
# Edit the details of the context
#
def update
params['context'] ||= {}
success_text = if params['field'] == 'name' && params['value']
params['context']['id'] = params['id']
params['context']['name'] = params['value']
end
@context.attributes = params["context"]
if @context.save
if params['wants_render']
render
else
render :text => success_text || 'Success'
end
else
notify :warning, "Couldn't update new context"
render :text => ""
end
end
# Fairly self-explanatory; deletes the context
# If the context contains actions, you'll get a warning dialogue.
# If you choose to go ahead, any actions in the context will also be deleted.
def destroy
@context.destroy
respond_to do |format|
format.js
format.xml { render :text => "Deleted context #{@context.name}" }
end
end
# Methods for changing the sort order of the contexts in the list
#
def order
params["list-contexts"].each_with_index do |id, position|
if check_user_matches_context_user(id)
Context.update(id, :position => position + 1)
end
end
render :nothing => true
end
protected
def render_contexts_rss_feed
lambda do
render_rss_feed_for @contexts, :feed => Context.feed_options(@user),
:item => { :description => lambda { |c| c.summary(count_undone_todos_phrase(c)) } }
end
end
def render_contexts_atom_feed
lambda do
render_atom_feed_for @contexts, :feed => Context.feed_options(@user),
:item => { :description => lambda { |c| c.summary(count_undone_todos_phrase(c)) },
:author => lambda { |c| nil } }
end
end
def check_user_set_context
@context = @user.contexts.find_by_params(params)
if @context.nil?
render :text => "Context not found.", :status => 404
end
end
def check_user_matches_context_user(id)
@context = Context.find_by_id_and_user_id(id, @user.id)
if @user == @context.user
return @context
else
@context = nil
notify :warning, "Project and session user mis-match: #{@context.user_id} and #{@user.id}!"
render :text => ''
end
end
def check_user_return_item
item = Todo.find( params['id'] )
if @user == item.user
return item
else
notify :warning, "Item and session user mis-match: #{item.user.name} and #{@user.name}!"
render :text => ''
end
end
def init
@source_view = params['_source_view'] || 'context'
# If we exclude completed projects, then we can't display them in the sidebar
# if the user sets the preference for them to be shown
# @projects = @user.projects.reject { |x| x.completed? }
init_data_for_sidebar
@todos = @user.todos
@done = @user.todos.find_in_state(:all, :completed, :order => "todos.completed_at DESC")
end
def init_todos
check_user_set_context
@done = @context.done_todos
# @not_done_todos = @context.not_done_todos
# TODO: Temporarily doing this search manually until I can work out a way
# to do the same thing using not_done_todos acts_as_todo_container method
# Hides actions in hidden projects from context.
@not_done_todos = @context.todos.find(:all, :conditions => ['todos.state = ?', 'active'], :order => "todos.due IS NULL, todos.due ASC, todos.created_at ASC", :include => :project)
@count = @not_done_todos.size
@default_project_context_name_map = build_default_project_context_name_map(@projects).to_json
end
end