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 :set_context_from_params, :only => [:update, :destroy]
|
|
|
|
skip_before_filter :login_required, :only => [:index]
|
|
|
|
prepend_before_filter :login_or_feed_token_required, :only => [:index]
|
|
|
|
|
|
|
|
def index
|
2013-05-27 12:44:31 +02:00
|
|
|
@all_contexts = current_user.contexts
|
2012-04-30 13:51:42 +02:00
|
|
|
@active_contexts = current_user.contexts.active
|
|
|
|
@hidden_contexts = current_user.contexts.hidden
|
2013-03-01 16:20:15 +01:00
|
|
|
@closed_contexts = current_user.contexts.closed
|
2013-02-18 16:39:43 +01:00
|
|
|
init_not_done_counts(['context']) unless request.format == :autocomplete
|
2011-02-11 16:18:59 +01:00
|
|
|
|
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
|
2011-02-11 16:18:59 +01:00
|
|
|
format.xml { render :xml => @all_contexts.to_xml( :except => :user_id ) }
|
2013-09-13 14:58:28 +03:00
|
|
|
format.any(:rss, :atom) do
|
2012-04-19 15:31:18 +02:00
|
|
|
@feed_title = 'Tracks Contexts'
|
|
|
|
@feed_description = "Lists all the contexts for #{current_user.display_name}"
|
|
|
|
end
|
2010-11-09 19:57:53 +01:00
|
|
|
format.text do
|
2012-05-20 05:46:52 +02:00
|
|
|
# somehow passing Mime::TEXT using content_type to render does not work
|
|
|
|
headers['Content-Type']=Mime::TEXT.to_s
|
2010-11-09 19:57:53 +01:00
|
|
|
render :action => 'index', :layout => false, :content_type => Mime::TEXT
|
|
|
|
end
|
2012-04-09 12:48:40 +02:00
|
|
|
format.autocomplete &render_autocomplete
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
end
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def show
|
2012-08-12 11:27:37 +02:00
|
|
|
set_context_from_params
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2012-08-12 11:27:37 +02:00
|
|
|
unless @context.nil?
|
|
|
|
@max_completed = current_user.prefs.show_number_completed
|
|
|
|
@done = @context.todos.completed.limit(@max_completed).reorder("todos.completed_at DESC, todos.created_at DESC").includes(Todo::DEFAULT_INCLUDES)
|
|
|
|
@not_done_todos = @context.todos.active.reorder("todos.due IS NULL, todos.due ASC, todos.created_at ASC").includes(Todo::DEFAULT_INCLUDES)
|
2013-06-11 11:12:21 +02:00
|
|
|
@todos_without_project = @not_done_todos.select{|t| t.project.nil?}
|
2012-08-12 11:27:37 +02:00
|
|
|
|
2013-03-10 20:38:10 +01:00
|
|
|
@deferred_todos = @context.todos.deferred.includes(Todo::DEFAULT_INCLUDES)
|
|
|
|
@pending_todos = @context.todos.pending.includes(Todo::DEFAULT_INCLUDES)
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2012-08-12 11:27:37 +02:00
|
|
|
@projects = current_user.projects
|
2012-08-27 21:00:51 +02:00
|
|
|
@contexts = current_user.contexts
|
2012-08-12 11:27:37 +02:00
|
|
|
|
2013-06-11 11:12:21 +02:00
|
|
|
@projects_to_show = @projects.active
|
|
|
|
@contexts_to_show = [@context]
|
|
|
|
|
2013-03-10 20:38:10 +01:00
|
|
|
@count = @not_done_todos.count + @deferred_todos.count + @pending_todos.count
|
2007-05-21 06:12:55 +00:00
|
|
|
@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
|
2012-08-27 21:00:51 +02:00
|
|
|
else
|
|
|
|
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
|
2007-05-21 06:12:55 +00:00
|
|
|
end
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
def create
|
|
|
|
if params[:format] == 'application/xml' && params['exception']
|
2012-04-19 15:31:18 +02:00
|
|
|
render_failure "Expected post format is valid xml like so: <context><name>context name</name></context>.", 400
|
2007-03-30 04:36:52 +00:00
|
|
|
return
|
|
|
|
end
|
2013-05-27 12:44:31 +02:00
|
|
|
@context = current_user.contexts.build(context_params)
|
2013-02-28 12:23:22 +01:00
|
|
|
@context.hide! if params['context_state'] && params['context_state']['hide'] == '1'
|
2007-03-30 04:36:52 +00:00
|
|
|
@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
|
2012-06-29 16:48:30 +02:00
|
|
|
init_not_done_counts
|
2007-11-16 14:28:52 +00:00
|
|
|
end
|
2007-04-02 04:18:19 +00:00
|
|
|
format.xml do
|
2012-04-19 15:31:18 +02:00
|
|
|
if @context.new_record?
|
2012-04-27 14:22:16 +02:00
|
|
|
render_failure @context.errors.to_xml.html_safe, 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
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
# Edit the details of the context
|
2009-04-07 21:18:23 +02:00
|
|
|
#
|
2007-03-30 04:36:52 +00:00
|
|
|
def update
|
2013-03-02 14:52:03 +01:00
|
|
|
process_params_for_update
|
2009-04-07 21:18:23 +02:00
|
|
|
|
2013-05-27 12:44:31 +02:00
|
|
|
@context.attributes = context_params
|
2010-10-24 22:31:57 +02:00
|
|
|
@saved = @context.save
|
2014-08-14 21:05:05 -05:00
|
|
|
@state_saved = set_state_for_update(@new_state)
|
2013-03-02 14:52:03 +01:00
|
|
|
@saved = @saved && @state_saved
|
2010-10-24 22:31:57 +02:00
|
|
|
|
|
|
|
if @saved
|
2013-03-02 14:52:03 +01:00
|
|
|
@state_changed = (@original_context_state != @context.state)
|
|
|
|
@new_state = @context.state if @state_changed
|
|
|
|
@active_contexts = current_user.contexts.active
|
|
|
|
@hidden_contexts = current_user.contexts.hidden
|
|
|
|
@closed_contexts = current_user.contexts.closed
|
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2013-03-02 14:52:03 +01:00
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
format.xml {
|
2011-06-10 23:29:42 +02:00
|
|
|
if @saved
|
2011-06-26 23:05:33 +02:00
|
|
|
render :xml => @context.to_xml( :except => :user_id )
|
2011-06-10 23:29:42 +02:00
|
|
|
else
|
|
|
|
render :text => "Error on update: #{@context.errors.full_messages.inject("") {|v, e| v + e + " " }}", :status => 409
|
|
|
|
end
|
|
|
|
}
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-02 19:45:49 -04:00
|
|
|
def edit
|
|
|
|
@context = Context.find(params[:id])
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
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
|
2010-08-19 11:25:04 +02:00
|
|
|
# make sure the deleted recurring patterns are removed from associated todos
|
|
|
|
@context.recurring_todos.each { |rt| rt.clear_todos_association } unless @context.recurring_todos.nil?
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
@context.destroy
|
|
|
|
respond_to do |format|
|
2010-10-23 17:52:50 +02:00
|
|
|
format.js do
|
|
|
|
@down_count = current_user.contexts.size
|
|
|
|
update_state_counts
|
|
|
|
end
|
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
|
2009-04-07 21:18:23 +02:00
|
|
|
#
|
2007-03-30 04:36:52 +00:00
|
|
|
def order
|
2009-10-02 19:45:49 -04:00
|
|
|
context_ids = params["container_context"]
|
|
|
|
@projects = current_user.contexts.update_positions( context_ids )
|
2007-03-30 04:36:52 +00:00
|
|
|
render :nothing => true
|
2009-10-02 19:45:49 -04:00
|
|
|
rescue
|
|
|
|
notify :error, $!
|
|
|
|
redirect_to :action => 'index'
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2011-06-17 14:30:32 +02:00
|
|
|
def done_todos
|
2013-05-11 10:38:34 +02:00
|
|
|
done_todos_for current_user.contexts.find(params[:id])
|
2011-06-17 14:30:32 +02:00
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2011-06-17 14:30:32 +02:00
|
|
|
def all_done_todos
|
2013-05-11 10:38:34 +02:00
|
|
|
all_done_todos_for current_user.contexts.find(params[:id])
|
2011-06-17 14:30:32 +02:00
|
|
|
end
|
|
|
|
|
2013-05-27 12:44:31 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def context_params
|
|
|
|
params.require(:context).permit(:name, :position, :state)
|
|
|
|
end
|
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
protected
|
|
|
|
|
2010-10-23 17:52:50 +02:00
|
|
|
def update_state_counts
|
|
|
|
@active_contexts_count = current_user.contexts.active.count
|
|
|
|
@hidden_contexts_count = current_user.contexts.hidden.count
|
2013-03-01 16:20:15 +01:00
|
|
|
@closed_contexts_count = current_user.contexts.closed.count
|
2010-10-23 17:52:50 +02:00
|
|
|
@show_active_contexts = @active_contexts_count > 0
|
|
|
|
@show_hidden_contexts = @hidden_contexts_count > 0
|
2013-03-01 16:20:15 +01:00
|
|
|
@show_closed_contexts = @closed_contexts_count > 0
|
2010-10-23 17:52:50 +02:00
|
|
|
end
|
|
|
|
|
2008-01-18 21:02:54 +00:00
|
|
|
def render_contexts_html
|
|
|
|
lambda do
|
|
|
|
@page_title = "TRACKS::List Contexts"
|
2009-04-07 21:18:23 +02:00
|
|
|
@no_active_contexts = @active_contexts.empty?
|
|
|
|
@no_hidden_contexts = @hidden_contexts.empty?
|
2013-03-01 16:20:15 +01:00
|
|
|
@no_closed_contexts = @closed_contexts.empty?
|
2009-04-07 21:18:23 +02:00
|
|
|
@active_count = @active_contexts.size
|
|
|
|
@hidden_count = @hidden_contexts.size
|
2013-03-01 16:20:15 +01:00
|
|
|
@closed_count = @closed_contexts.size
|
|
|
|
@count = @active_count + @hidden_count + @closed_count
|
2013-02-18 16:39:43 +01:00
|
|
|
@new_context = current_user.contexts.build
|
|
|
|
|
2008-01-18 21:02:54 +00:00
|
|
|
render
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|
2008-01-18 21:02:54 +00:00
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
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"
|
2009-04-14 21:49:43 +02:00
|
|
|
@active_contexts = current_user.contexts.active
|
|
|
|
@hidden_contexts = current_user.contexts.hidden
|
2011-06-26 23:05:33 +02:00
|
|
|
@down_count = @active_contexts.size + @hidden_contexts.size
|
2012-05-12 13:37:36 +02:00
|
|
|
cookies[:mobile_url]= {:value => request.fullpath, :secure => SITE_CONFIG['secure_cookies']}
|
|
|
|
render
|
2008-04-19 19:15:07 +00:00
|
|
|
end
|
|
|
|
end
|
2011-06-26 23:05:33 +02:00
|
|
|
|
2008-04-19 19:15:07 +00:00
|
|
|
def render_context_mobile
|
|
|
|
lambda do
|
2008-04-27 19:23:19 +00:00
|
|
|
@page_title = "TRACKS::List actions in "+@context.name
|
2011-06-26 23:05:33 +02:00
|
|
|
@not_done = @not_done_todos.select {|t| t.context_id == @context.id }
|
|
|
|
@down_count = @not_done.size
|
2012-05-12 13:37:36 +02:00
|
|
|
cookies[:mobile_url]= {:value => request.fullpath, :secure => SITE_CONFIG['secure_cookies']}
|
2008-08-04 16:13:51 +02:00
|
|
|
@mobile_from_context = @context.id
|
2012-05-12 13:37:36 +02:00
|
|
|
render
|
2008-04-19 19:15:07 +00:00
|
|
|
end
|
|
|
|
end
|
2014-08-14 21:05:05 -05:00
|
|
|
|
2012-04-09 12:48:40 +02:00
|
|
|
def render_autocomplete
|
|
|
|
lambda do
|
2013-03-09 22:21:54 +02:00
|
|
|
render :text => for_autocomplete(current_user.contexts, params[:term])
|
2012-04-09 12:48:40 +02:00
|
|
|
end
|
|
|
|
end
|
2011-06-26 23:05:33 +02: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
|
2012-04-12 17:30:05 +02:00
|
|
|
@context = current_user.contexts.find(params[:id])
|
2008-01-18 21:02:54 +00:00
|
|
|
rescue
|
|
|
|
@context = nil
|
|
|
|
end
|
2011-06-26 23:05:33 +02: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
|
|
|
|
2013-03-02 14:52:03 +01:00
|
|
|
def process_params_for_update
|
|
|
|
params['context'] ||= {}
|
|
|
|
@success_text = if params['field'] == 'name' && params['value']
|
|
|
|
params['context']['id'] = params['id']
|
|
|
|
params['context']['name'] = params['value']
|
|
|
|
end
|
|
|
|
|
|
|
|
@original_context_state = @context.state
|
|
|
|
@new_state = params['context']['state']
|
|
|
|
params['context'].delete('state')
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_state_for_update(new_state)
|
|
|
|
begin
|
|
|
|
unless @original_context_state == new_state
|
|
|
|
if new_state == 'active'
|
|
|
|
@context.activate!
|
|
|
|
elsif new_state == 'hidden'
|
|
|
|
@context.hide!
|
|
|
|
elsif new_state == 'closed'
|
|
|
|
@context.close!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
rescue AASM::InvalidTransition
|
|
|
|
@context.errors.add(:state, "cannot be changed. The context cannot be closed if you have uncompleted actions in this context")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-30 04:36:52 +00:00
|
|
|
end
|