2007-03-30 04:36:52 +00:00
class ContextsController < ApplicationController
helper :todos
2007-04-11 06:04:17 +00:00
before_filter :init , :except = > [ :index , :create , :destroy , :order ]
2007-03-30 04:36:52 +00:00
before_filter :init_todos , :only = > :show
before_filter :set_context_from_params , :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
2007-11-27 03:00:38 +00:00
@contexts = current_user . contexts ( true ) #true is passed here to force an immediate load so that size and empty? checks later don't result in separate SQL queries
2007-04-11 06:04:17 +00:00
init_not_done_counts ( [ 'context' ] )
2007-03-30 04:36:52 +00:00
respond_to do | format |
format . html & render_contexts_html
2008-04-19 19:15:07 +00:00
format . m & render_contexts_mobile
2007-03-30 04:36:52 +00:00
format . xml { render :xml = > @contexts . to_xml ( :except = > :user_id ) }
format . rss & render_contexts_rss_feed
format . atom & render_contexts_atom_feed
2008-04-28 05:53:24 +00:00
format . text { render :action = > 'index' , :layout = > false , :content_type = > Mime :: TEXT }
2007-03-30 04:36:52 +00:00
end
end
def show
2008-12-17 17:15:04 +01:00
@contexts = current_user . contexts ( true )
2007-05-21 06:12:55 +00:00
if ( @context . nil? )
respond_to do | format |
format . html { render :text = > 'Context not found' , :status = > 404 }
format . xml { render :xml = > '<error>Context not found</error>' , :status = > 404 }
end
else
@page_title = " TRACKS::Context: #{ @context . name } "
respond_to do | format |
format . html
2008-04-19 19:15:07 +00:00
format . m & render_context_mobile
2007-05-21 06:12:55 +00:00
format . xml { render :xml = > @context . to_xml ( :except = > :user_id ) }
end
end
2007-03-30 04:36:52 +00:00
end
2008-01-18 21:02:54 +00:00
# Example XML usage: curl -H 'Accept: application/xml' -H 'Content-Type:
# application/xml'
2007-03-30 04:36:52 +00:00
# -u username:password
# -d '<request><context><name>new context_name</name></context></request>'
# http://our.tracks.host/contexts
2008-01-18 21:02:54 +00:00
#
2007-03-30 04:36:52 +00:00
def create
if params [ :format ] == 'application/xml' && params [ 'exception' ]
2007-05-21 06:12:55 +00:00
render_failure " Expected post format is valid xml like so: <request><context><name>context name</name></context></request>. " , 400
2007-03-30 04:36:52 +00:00
return
end
2007-07-30 05:29:18 +00:00
@context = current_user . contexts . build
2007-03-30 04:36:52 +00:00
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 }
2007-04-02 04:18:19 +00:00
respond_to do | format |
2007-11-16 14:28:52 +00:00
format . js do
@down_count = current_user . contexts . size
end
2007-04-02 04:18:19 +00:00
format . xml do
2007-03-30 04:36:52 +00:00
if @context . new_record? && params_are_invalid
2007-05-21 06:12:55 +00:00
render_failure " Expected post format is valid xml like so: <request><context><name>context name</name></context></request>. " , 400
2007-03-30 04:36:52 +00:00
elsif @context . new_record?
2007-05-21 06:12:55 +00:00
render_failure @context . errors . to_xml , 409
2007-03-30 04:36:52 +00:00
else
2007-12-04 06:24:23 +00:00
head :created , :location = > context_url ( @context )
2007-03-30 04:36:52 +00:00
end
2008-01-18 21:02:54 +00:00
end
2007-03-30 04:36:52 +00:00
end
end
# Edit the details of the context
2008-01-18 21:02:54 +00:00
#
2007-03-30 04:36:52 +00:00
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
2008-08-18 16:02:13 +02:00
if boolean_param ( 'wants_render' )
2008-04-28 05:53:24 +00:00
respond_to do | format |
format . js
end
2008-08-18 16:02:13 +02:00
elsif boolean_param ( 'update_context_name' )
@contexts = current_user . projects
render :template = > 'contexts/update_context_name.js.rjs'
return
2007-03-30 04:36:52 +00:00
else
render :text = > success_text || 'Success'
end
else
notify :warning , " Couldn't update new context "
render :text = > " "
end
end
2008-01-18 21:02:54 +00:00
# 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.
2007-03-30 04:36:52 +00:00
def destroy
@context . destroy
respond_to do | format |
2007-11-16 14:28:52 +00:00
format . js { @down_count = current_user . contexts . size }
2007-03-30 04:36:52 +00:00
format . xml { render :text = > " Deleted context #{ @context . name } " }
end
end
# Methods for changing the sort order of the contexts in the list
2008-01-18 21:02:54 +00:00
#
2007-03-30 04:36:52 +00:00
def order
params [ " list-contexts " ] . each_with_index do | id , position |
2007-07-30 05:29:18 +00:00
current_user . contexts . update ( id , :position = > position + 1 )
2007-03-30 04:36:52 +00:00
end
render :nothing = > true
end
protected
2008-01-18 21:02:54 +00:00
def render_contexts_html
lambda do
@page_title = " TRACKS::List Contexts "
@no_contexts = @contexts . empty?
@count = @contexts . size
render
2007-03-30 04:36:52 +00:00
end
2008-01-18 21:02:54 +00:00
end
2008-04-19 19:15:07 +00:00
def render_contexts_mobile
lambda do
2008-04-27 19:23:19 +00:00
@page_title = " TRACKS::List Contexts "
2008-11-29 15:35:17 +01:00
@active_contexts = @contexts . active
@hidden_contexts = @contexts . hidden
2008-04-27 19:23:19 +00:00
@down_count = @active_contexts . size + @hidden_contexts . size
2008-08-18 14:11:27 +02:00
cookies [ :mobile_url ] = { :value = > request . request_uri , :secure = > TRACKS_COOKIES_SECURE }
2008-04-19 19:15:07 +00:00
render :action = > 'index_mobile'
end
end
def render_context_mobile
lambda do
2008-04-27 19:23:19 +00:00
@page_title = " TRACKS::List actions in " + @context . name
@not_done = @not_done_todos . select { | t | t . context_id == @context . id }
@down_count = @not_done . size
2008-08-18 14:11:27 +02:00
cookies [ :mobile_url ] = { :value = > request . request_uri , :secure = > TRACKS_COOKIES_SECURE }
2008-08-04 16:13:51 +02:00
@mobile_from_context = @context . id
2008-04-19 19:15:07 +00:00
render :action = > 'mobile_show_context'
end
end
2007-03-30 04:36:52 +00:00
2008-01-18 21:02:54 +00:00
def render_contexts_rss_feed
lambda do
render_rss_feed_for @contexts , :feed = > feed_options ,
:item = > { :description = > lambda { | c | c . summary ( count_undone_todos_phrase ( c ) ) } }
2007-03-30 04:36:52 +00:00
end
2008-01-18 21:02:54 +00:00
end
2007-03-30 04:36:52 +00:00
2008-01-18 21:02:54 +00:00
def render_contexts_atom_feed
lambda do
render_atom_feed_for @contexts , :feed = > feed_options ,
:item = > { :description = > lambda { | c | c . summary ( count_undone_todos_phrase ( c ) ) } ,
:author = > lambda { | c | nil } }
2007-03-30 04:36:52 +00:00
end
2008-01-18 21:02:54 +00:00
end
2007-07-30 05:29:18 +00:00
2008-01-18 21:02:54 +00:00
def feed_options
Context . feed_options ( current_user )
end
2007-03-30 04:36:52 +00:00
2008-01-18 21:02:54 +00:00
def set_context_from_params
@context = current_user . contexts . find_by_params ( params )
rescue
@context = nil
end
2007-03-30 04:36:52 +00:00
2008-01-18 21:02:54 +00:00
def init
@source_view = params [ '_source_view' ] || 'context'
init_data_for_sidebar
end
2007-03-30 04:36:52 +00:00
2008-01-18 21:02:54 +00:00
def init_todos
set_context_from_params
unless @context . nil?
2008-04-28 05:53:24 +00:00
@context . todos . send :with_scope , :find = > { :include = > [ :project , :tags ] } do
2008-01-18 21:02:54 +00:00
@done = @context . done_todos
2007-05-21 06:12:55 +00:00
end
2008-01-18 21:02:54 +00:00
@max_completed = current_user . prefs . show_number_completed
# @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.
2008-03-20 14:15:17 +00:00
@not_done_todos = @context . todos . find (
:all ,
:conditions = > [ 'todos.state = ? AND (todos.project_id IS ? OR projects.state = ?)' , 'active' , nil , 'active' ] ,
:order = > " todos.due IS NULL, todos.due ASC, todos.created_at ASC " ,
:include = > [ :project , :tags ] )
2008-12-03 14:03:59 +01:00
@projects = current_user . projects
2008-01-18 21:02:54 +00:00
@count = @not_done_todos . size
@default_project_context_name_map = build_default_project_context_name_map ( @projects ) . to_json
2007-03-30 04:36:52 +00:00
end
2008-01-18 21:02:54 +00:00
end
2007-03-30 04:36:52 +00:00
end