Class: ProjectsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/projects_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_login_required, #all_done_todos_for, #boolean_param, cas_enabled?, #cas_enabled?, #count_deferred_todos, #count_undone_todos, #count_undone_todos_phrase, #done_todos_for, #enable_mobile_content_negotiation, #for_autocomplete, #format_date, #format_dependencies_as_json_for_auto_complete, #handle_unverified_request, #init_data_for_sidebar, #init_not_done_counts, #init_project_hidden_todo_counts, #mobile?, #notify, openid_enabled?, #openid_enabled?, #parse_date_per_user_prefs, prefered_auth?, #prefered_auth?, #redirect_back_or_home, #render_failure, #sanitize, #set_charset, #set_group_view_by, #set_locale, #set_session_expiration, #set_time_zone, #set_zindex_counter, #todo_xml_params

Methods included from LoginSystem

#access_denied, #authorize?, #basic_auth_denied, #current_user, #get_basic_auth_data, #get_current_user, #logged_in?, #login_from_cookie, #login_optional, #login_or_feed_token_required, #login_required, #logout_user, #prefs, #protect?, #redirect_back_or_default, #redirect_to_login, #set_current_user, #store_location

Instance Method Details

#actionizeObject



295
296
297
298
299
300
301
# File 'app/controllers/projects_controller.rb', line 295

def actionize
  @state = params['state']
  @projects = current_user.projects.actionize(:state => @state) if @state
  @contexts = current_user.contexts
  init_not_done_counts(['project'])
  init_project_hidden_todo_counts(['project']) if @state == 'hidden'
end

#all_done_todosObject



307
308
309
# File 'app/controllers/projects_controller.rb', line 307

def all_done_todos
  all_done_todos_for current_user.projects.find(params[:id])
end

#alphabetizeObject



287
288
289
290
291
292
293
# File 'app/controllers/projects_controller.rb', line 287

def alphabetize
  @state = params['state']
  @projects = current_user.projects.alphabetize(:state => @state) if @state
  @contexts = current_user.contexts
  init_not_done_counts(['project'])
  init_project_hidden_todo_counts(['project']) if @state == 'hidden'
end

#createObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/controllers/projects_controller.rb', line 169

def create
  if params[:format] == 'application/xml' && params['exception']
    render_failure "Expected post format is valid xml like so: <project><name>project name</name></project>.", 400
    return
  end
  @project = current_user.projects.build(project_params)
  @go_to_project = params['go_to_project']
  @saved = @project.save
  @project_not_done_counts = { @project.id => 0 }
  @active_projects_count = current_user.projects.active.count
  @contexts = current_user.contexts

  respond_to do |format|
    format.js do
      @down_count = current_user.projects.size
      init_not_done_counts
    end
    format.xml do
      if @project.new_record?
        render_failure @project.errors.to_xml.html_safe, 409
      else
        head :created, :location => project_url(@project), :text => @project.id
      end
    end
    format.html {redirect_to :action => 'index'}
  end
end

#default_context_filterObject (protected)



330
331
332
333
334
335
336
337
338
339
340
341
# File 'app/controllers/projects_controller.rb', line 330

def default_context_filter
  p = params['project']
  p = params['request']['project'] if p.nil? && params['request']
  p = {} if p.nil?
  default_context_name = p['default_context_name']
  p.delete('default_context_name')

  if default_context_name.present?
    default_context = current_user.contexts.where(:name => default_context_name).first_or_create
    p['default_context_id'] = default_context.id
  end
end

#destroyObject



265
266
267
268
269
270
271
272
273
274
275
276
# File 'app/controllers/projects_controller.rb', line 265

def destroy
  @project.recurring_todos.each {|rt| rt.remove_from_project!}
  @project.destroy

  respond_to do |format|
    format.js {
      @down_count = current_user.projects.size
      update_state_counts
    }
    format.xml { render :text => "Deleted project #{@project.name}" }
  end
end

#doneObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/controllers/projects_controller.rb', line 78

def done
  @source_view = params['_source_view'] || 'project_list'
  @page_title = t('projects.list_completed_projects')

  page = params[:page] || 1
  projects_per_page = 20
  @projects = current_user.projects.completed.paginate :page => page, :per_page => projects_per_page
  @count = @projects.count
  @total = current_user.projects.completed.count
  @no_projects = @projects.empty?

  @range_low = (page.to_i-1) * projects_per_page + 1
  @range_high = @range_low + @projects.size - 1

  init_not_done_counts(['project'])
end

#done_todosObject



303
304
305
# File 'app/controllers/projects_controller.rb', line 303

def done_todos
  done_todos_for current_user.projects.find(params[:id])
end

#editObject



259
260
261
262
263
# File 'app/controllers/projects_controller.rb', line 259

def edit
  respond_to do |format|
    format.js
  end
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/projects_controller.rb', line 10

def index
  @source_view = params['_source_view'] || 'project_list'
  if params[:projects_and_actions]
    projects_and_actions
  else
    @contexts = current_user.contexts
    init_not_done_counts(['project'])
    init_project_hidden_todo_counts(['project'])
    if params[:only_active_with_no_next_actions]
      @projects = current_user.projects.active.select { |p| count_undone_todos(p) == 0  }
    else
      @projects = current_user.projects
    end
    @active_projects = current_user.projects.active
    @hidden_projects = current_user.projects.hidden
    respond_to do |format|
      format.html  do
        @page_title = t('projects.list_projects')
        @count = current_user.projects.count
        @completed_projects = current_user.projects.completed.limit(10)
        @completed_count = current_user.projects.completed.count
        @no_projects = current_user.projects.empty?
        current_user.projects.cache_note_counts
        @new_project = current_user.projects.build
      end
      format.m     do
        @completed_projects = current_user.projects.completed
        @down_count = @active_projects.size + @hidden_projects.size + @completed_projects.size
        cookies[:mobile_url]= {:value => request.fullpath, :secure => SITE_CONFIG['secure_cookies']}
      end
      format.xml   { render :xml => @projects.to_xml( :except => :user_id )  }
      format.any(:rss, :atom) do
        @feed_title = I18n.t('models.project.feed_title')
        @feed_description = I18n.t('models.project.feed_description', :username => current_user.display_name)
      end
      format.text do
        # somehow passing Mime::TEXT using content_type to render does not work
        headers['Content-Type']=Mime::TEXT.to_s
      end
      format.autocomplete do
        projects = current_user.projects.active + current_user.projects.hidden
        render :text => for_autocomplete(projects, params[:term])
      end
    end
  end
end

#orderObject



278
279
280
281
282
283
284
285
# File 'app/controllers/projects_controller.rb', line 278

def order
  project_ids = params["container_project"]
  @projects = current_user.projects.update_positions( project_ids )
  render :nothing => true
rescue
  notify :error, $!
  redirect_to :action => 'index'
end

#project_paramsObject (private)



345
346
347
# File 'app/controllers/projects_controller.rb', line 345

def project_params
  params.require(:project).permit(:name, :position, :user_id, :description, :state, :default_context_id, :default_tags)
end

#projects_and_actionsObject



111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/projects_controller.rb', line 111

def projects_and_actions
  @projects = current_user.projects.active
  respond_to do |format|
    format.text  {
      # somehow passing Mime::TEXT using content_type to render does not work
      headers['Content-Type']=Mime::TEXT.to_s
      render :action => 'index_text_projects_and_actions', :layout => false, :content_type => Mime::TEXT
    }
  end
end

#reviewObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/controllers/projects_controller.rb', line 57

def review
  @source_view = params['_source_view'] || 'review'
  @page_title = t('projects.list_reviews')
  @projects = current_user.projects.load
  @contexts = current_user.contexts.load
  @projects_to_review = current_user.projects.select  {|p| p.needs_review?(current_user)}
  @stalled_projects = current_user.projects.select  {|p| p.stalled?}
  @blocked_projects = current_user.projects.select  {|p| p.blocked?}
  @current_projects = current_user.projects.uncompleted.select  {|p| not(p.needs_review?(current_user))}

  init_not_done_counts(['project'])
  init_project_hidden_todo_counts(['project'])
  current_user.projects.cache_note_counts

  @page_title = t('projects.list_reviews')
  @count = @projects_to_review.count + @blocked_projects.count + @stalled_projects.count + @current_projects.count

  @no_projects = current_user.projects.empty?
  @new_project = current_user.projects.build
end

#set_project_from_paramsObject (protected)



322
323
324
# File 'app/controllers/projects_controller.rb', line 322

def set_project_from_params
  @project = current_user.projects.find_by_params(params)
end

#set_reviewedObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/projects_controller.rb', line 95

def set_reviewed
  @project.last_reviewed = Time.zone.now
  @project.save

  case @source_view
  when "project"
    redirect_to :action => 'show'
  when "project_list"
    redirect_to :action => 'index'
  when "review"
    redirect_to :action => 'review'
  else
    redirect_to :action => 'index'
  end
end

#set_source_viewObject (protected)



326
327
328
# File 'app/controllers/projects_controller.rb', line 326

def set_source_view
  @source_view = params['_source_view'] || 'project'
end

#showObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/projects_controller.rb', line 122

def show
  @max_completed = current_user.prefs.show_number_completed
  init_data_for_sidebar unless mobile?
  @page_title = t('projects.page_title', :project => @project.name)

  @not_done_todos = @project.todos.active_or_hidden.includes(Todo::DEFAULT_INCLUDES)
  @deferred_todos = @project.todos.deferred.includes(Todo::DEFAULT_INCLUDES)
  @pending_todos = @project.todos.pending.includes(Todo::DEFAULT_INCLUDES)
  @contexts_to_show = current_user.contexts.active
  @projects_to_show = [@project]

  @done = {}
  @done = @project.todos.completed.
    reorder("todos.completed_at DESC").
    limit(current_user.prefs.show_number_completed).
    includes(Todo::DEFAULT_INCLUDES) unless @max_completed == 0

  @down_count = @not_done_todos.size + @deferred_todos.size + @pending_todos.size
  @count=@down_count
  @next_project = current_user.projects.next_from(@project)
  @previous_project = current_user.projects.previous_from(@project)
  @default_tags = @project.default_tags
  @new_note = current_user.notes.new
  @new_note.project_id = @project.id
  @contexts = current_user.contexts
  respond_to do |format|
    format.html
    format.m     do
      if @project.default_context.nil?
        @project_default_context = t('projects.no_default_context')
      else
        @project_default_context = t('projects.default_context', :context => @project.default_context.name)
      end
      cookies[:mobile_url]= {:value => request.fullpath, :secure => SITE_CONFIG['secure_cookies']}
      @mobile_from_project = @project.id
    end
    format.xml   do
      render :xml => @project.to_xml(:except => :user_id) { |xml|
        xml.not_done { @not_done_todos.each { |child| child.to_xml(:builder => xml, :skip_instruct => true) } }
        xml.deferred { @deferred_todos.each { |child| child.to_xml(:builder => xml, :skip_instruct => true) } }
        xml.pending { @pending_todos.each { |child| child.to_xml(:builder => xml, :skip_instruct => true) } }
        xml.done { @done.each { |child| child.to_xml(:builder => xml, :skip_instruct => true) } }
      }
    end
  end
end

#updateObject

Edit the details of the project



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/controllers/projects_controller.rb', line 199

def update
  template = ""

  params['project'] ||= {}
  if params['project']['state']
    @new_state = params['project']['state']
    @state_changed = @project.state != @new_state
    params['project'].delete('state')
  end
  success_text = if params['field'] == 'name' && params['value']
    params['project']['id'] = params['id']
    params['project']['name'] = params['value']
  end

  @project.attributes = project_params
  @saved = @project.save
  if @saved
    @project.transition_to(@new_state) if @state_changed
    if boolean_param('wants_render')
      @contexts = current_user.contexts
      update_state_counts
      init_data_for_sidebar
      init_project_hidden_todo_counts(['project'])

      template = 'projects/update'

    # TODO: are these params ever set? or is this dead code?
    elsif boolean_param('update_status')
      template = 'projects/update_status'
    elsif boolean_param('update_default_context')
      @initial_context_name = @project.default_context.name
      template = 'projects/update_default_context'
    elsif boolean_param('update_default_tags')
      template = 'projects/update_default_tags'
    elsif boolean_param('update_project_name')
      @projects = current_user.projects
      template = 'projects/update_project_name'
    else
      render :text => success_text || 'Success'
      return
    end
  else
    init_data_for_sidebar
    template = 'projects/update'
  end

  respond_to do |format|
    format.js { render :template => template }
    format.html { redirect_to :action => 'index'}
    format.xml {
      if @saved
        render :xml => @project.to_xml( :except => :user_id )
      else
        render :text => "Error on update: #{@project.errors.full_messages.inject("") {|v, e| v + e + " " }}", :status => 409
      end
    }
  end

end

#update_state_countsObject (protected)



313
314
315
316
317
318
319
320
# File 'app/controllers/projects_controller.rb', line 313

def update_state_counts
  @active_projects_count = current_user.projects.active.count
  @hidden_projects_count = current_user.projects.hidden.count
  @completed_projects_count = current_user.projects.completed.count
  @show_active_projects = @active_projects_count > 0
  @show_hidden_projects = @hidden_projects_count > 0
  @show_completed_projects = @completed_projects_count > 0
end