bsag / tracks

Tracks is a GTD(TM) web application, built with Ruby on Rails

This URL has Read+Write access

tracks / app / controllers / contexts_controller.rb
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 1 class ContextsController < ApplicationController
2
3 helper :todos
4
2ff03c57 » lukemelia 2007-04-10 Cleaned up some more unnece... 5 before_filter :init, :except => [:index, :create, :destroy, :order]
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 6 before_filter :init_todos, :only => :show
7 before_filter :set_context_from_params, :only => [:update, :destroy]
8 skip_before_filter :login_required, :only => [:index]
9 prepend_before_filter :login_or_feed_token_required, :only => [:index]
10 session :off, :only => :index, :if => Proc.new { |req| ['rss','atom','txt'].include?(req.parameters[:format]) }
11
12 def index
1516d7ae » lukemelia 2007-11-26 Learned a better way to for... 13 @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
2ff03c57 » lukemelia 2007-04-10 Cleaned up some more unnece... 14 init_not_done_counts(['context'])
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 15 respond_to do |format|
16 format.html &render_contexts_html
39a64989 » lrbalt 2008-04-19 merge new mobile interface ... 17 format.m &render_contexts_mobile
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 18 format.xml { render :xml => @contexts.to_xml( :except => :user_id ) }
19 format.rss &render_contexts_rss_feed
20 format.atom &render_contexts_atom_feed
76aba636 » lukemelia 2008-04-27 Merged rails2-branch back i... 21 format.text { render :action => 'index', :layout => false, :content_type => Mime::TEXT }
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 22 end
23 end
24
25 def show
bd519165 » lukemelia 2007-05-20 Added an XML representation... 26 if (@context.nil?)
27 respond_to do |format|
28 format.html { render :text => 'Context not found', :status => 404 }
29 format.xml { render :xml => '<error>Context not found</error>', :status => 404 }
30 end
31 else
32 @page_title = "TRACKS::Context: #{@context.name}"
33 respond_to do |format|
34 format.html
39a64989 » lrbalt 2008-04-19 merge new mobile interface ... 35 format.m &render_context_mobile
bd519165 » lukemelia 2007-05-20 Added an XML representation... 36 format.xml { render :xml => @context.to_xml( :except => :user_id ) }
37 end
38 end
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 39 end
40
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 41 # Example XML usage: curl -H 'Accept: application/xml' -H 'Content-Type:
42 # application/xml'
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 43 # -u username:password
44 # -d '<request><context><name>new context_name</name></context></request>'
45 # http://our.tracks.host/contexts
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 46 #
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 47 def create
48 if params[:format] == 'application/xml' && params['exception']
bd519165 » lukemelia 2007-05-20 Added an XML representation... 49 render_failure "Expected post format is valid xml like so: <request><context><name>context name</name></context></request>.", 400
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 50 return
51 end
30c23fc5 » lukemelia 2007-07-29 Introduce current_user and ... 52 @context = current_user.contexts.build
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 53 params_are_invalid = true
54 if (params['context'] || (params['request'] && params['request']['context']))
55 @context.attributes = params['context'] || params['request']['context']
56 params_are_invalid = false
57 end
58 @saved = @context.save
59 @context_not_done_counts = { @context.id => 0 }
ba0b52ff » lukemelia 2007-04-01 Merged mobile_controller in... 60 respond_to do |format|
8a53096f » lrbalt 2007-11-16 Added totals of contexts an... 61 format.js do
62 @down_count = current_user.contexts.size
63 end
ba0b52ff » lukemelia 2007-04-01 Merged mobile_controller in... 64 format.xml do
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 65 if @context.new_record? && params_are_invalid
bd519165 » lukemelia 2007-05-20 Added an XML representation... 66 render_failure "Expected post format is valid xml like so: <request><context><name>context name</name></context></request>.", 400
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 67 elsif @context.new_record?
bd519165 » lukemelia 2007-05-20 Added an XML representation... 68 render_failure @context.errors.to_xml, 409
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 69 else
36c35a7a » lukemelia 2007-12-03 Tweaked the REST API for Ac... 70 head :created, :location => context_url(@context)
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 71 end
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 72 end
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 73 end
74 end
75
76 # Edit the details of the context
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 77 #
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 78 def update
79 params['context'] ||= {}
80 success_text = if params['field'] == 'name' && params['value']
81 params['context']['id'] = params['id']
82 params['context']['name'] = params['value']
83 end
84 @context.attributes = params["context"]
85 if @context.save
b2e82ea6 » lrbalt 2008-08-18 fix for #749 86 if boolean_param('wants_render')
76aba636 » lukemelia 2008-04-27 Merged rails2-branch back i... 87 respond_to do |format|
88 format.js
89 end
b2e82ea6 » lrbalt 2008-08-18 fix for #749 90 elsif boolean_param('update_context_name')
91 @contexts = current_user.projects
92 render :template => 'contexts/update_context_name.js.rjs'
93 return
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 94 else
95 render :text => success_text || 'Success'
96 end
97 else
98 notify :warning, "Couldn't update new context"
99 render :text => ""
100 end
101 end
102
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 103 # Fairly self-explanatory; deletes the context If the context contains
104 # actions, you'll get a warning dialogue. If you choose to go ahead, any
105 # actions in the context will also be deleted.
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 106 def destroy
107 @context.destroy
108 respond_to do |format|
8a53096f » lrbalt 2007-11-16 Added totals of contexts an... 109 format.js { @down_count = current_user.contexts.size }
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 110 format.xml { render :text => "Deleted context #{@context.name}" }
111 end
112 end
113
114 # Methods for changing the sort order of the contexts in the list
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 115 #
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 116 def order
117 params["list-contexts"].each_with_index do |id, position|
30c23fc5 » lukemelia 2007-07-29 Introduce current_user and ... 118 current_user.contexts.update(id, :position => position + 1)
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 119 end
120 render :nothing => true
121 end
122
123 protected
124
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 125 def render_contexts_html
126 lambda do
127 @page_title = "TRACKS::List Contexts"
128 @no_contexts = @contexts.empty?
129 @count = @contexts.size
130 render
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 131 end
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 132 end
39a64989 » lrbalt 2008-04-19 merge new mobile interface ... 133
134 def render_contexts_mobile
135 lambda do
5d2023d1 » lrbalt 2008-04-27 keep tracks of last url in ... 136 @page_title = "TRACKS::List Contexts"
9147fb88 » lrbalt 2008-11-29 refactor conditional finds ... 137 @active_contexts = @contexts.active
138 @hidden_contexts = @contexts.hidden
5d2023d1 » lrbalt 2008-04-27 keep tracks of last url in ... 139 @down_count = @active_contexts.size + @hidden_contexts.size
2a6b8f1c » lrbalt 2008-08-18 fixes #753 by adding a glob... 140 cookies[:mobile_url]= {:value => request.request_uri, :secure => TRACKS_COOKIES_SECURE}
39a64989 » lrbalt 2008-04-19 merge new mobile interface ... 141 render :action => 'index_mobile'
142 end
143 end
144
145 def render_context_mobile
146 lambda do
5d2023d1 » lrbalt 2008-04-27 keep tracks of last url in ... 147 @page_title = "TRACKS::List actions in "+@context.name
148 @not_done = @not_done_todos.select {|t| t.context_id == @context.id }
149 @down_count = @not_done.size
2a6b8f1c » lrbalt 2008-08-18 fixes #753 by adding a glob... 150 cookies[:mobile_url]= {:value => request.request_uri, :secure => TRACKS_COOKIES_SECURE}
db86df54 » lrbalt 2008-08-04 fixes for mobile view and f... 151 @mobile_from_context = @context.id
39a64989 » lrbalt 2008-04-19 merge new mobile interface ... 152 render :action => 'mobile_show_context'
153 end
154 end
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 155
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 156 def render_contexts_rss_feed
157 lambda do
158 render_rss_feed_for @contexts, :feed => feed_options,
159 :item => { :description => lambda { |c| c.summary(count_undone_todos_phrase(c)) } }
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 160 end
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 161 end
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 162
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 163 def render_contexts_atom_feed
164 lambda do
165 render_atom_feed_for @contexts, :feed => feed_options,
166 :item => { :description => lambda { |c| c.summary(count_undone_todos_phrase(c)) },
167 :author => lambda { |c| nil } }
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 168 end
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 169 end
30c23fc5 » lukemelia 2007-07-29 Introduce current_user and ... 170
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 171 def feed_options
172 Context.feed_options(current_user)
173 end
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 174
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 175 def set_context_from_params
176 @context = current_user.contexts.find_by_params(params)
177 rescue
178 @context = nil
179 end
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 180
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 181 def init
182 @source_view = params['_source_view'] || 'context'
183 init_data_for_sidebar
184 end
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 185
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 186 def init_todos
187 set_context_from_params
188 unless @context.nil?
76aba636 » lukemelia 2008-04-27 Merged rails2-branch back i... 189 @context.todos.send :with_scope, :find => { :include => [:project, :tags] } do
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 190 @done = @context.done_todos
bd519165 » lukemelia 2007-05-20 Added an XML representation... 191 end
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 192
193 @max_completed = current_user.prefs.show_number_completed
194
195 # @not_done_todos = @context.not_done_todos TODO: Temporarily doing this
196 # search manually until I can work out a way to do the same thing using
197 # not_done_todos acts_as_todo_container method Hides actions in hidden
198 # projects from context.
0e7a85be » lrbalt 2008-03-20 fixes #678. When viewing a ... 199 @not_done_todos = @context.todos.find(
200 :all,
201 :conditions => ['todos.state = ? AND (todos.project_id IS ? OR projects.state = ?)', 'active', nil, 'active'],
202 :order => "todos.due IS NULL, todos.due ASC, todos.created_at ASC",
203 :include => [:project, :tags])
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 204 @count = @not_done_todos.size
205 @default_project_context_name_map = build_default_project_context_name_map(@projects).to_json
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 206 end
327b2e11 » lrbalt 2008-01-18 Fixes #634 and #631. The pr... 207 end
06f14dd3 » lukemelia 2007-03-29 And here's the copy step. 208
209 end