Class: TasksController

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

Overview

Copyright © 2008-2013 Michael Dvorkin and contributors.

Fat Free CRM is freely distributable under the terms of MIT license. See MIT-LICENSE file or www.opensource.org/licenses/mit-license.php


Instance Method Summary collapse

Methods inherited from ApplicationController

#auto_complete

Instance Method Details

#completeObject

PUT /tasks/1/complete




125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/controllers/tasks_controller.rb', line 125

def complete
  @task = Task.tracked_by(current_user).find(params[:id])
  @task.update_attributes(:completed_at => Time.now, :completed_by => current_user.id) if @task

  # Make sure bucket's div gets hidden if it's the last completed task in the bucket.
  if Task.bucket_empty?(params[:bucket], current_user)
    @empty_bucket = params[:bucket]
  end

  update_sidebar unless params[:bucket].blank?
  respond_with(@task)
end

#createObject

POST /tasks




70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/tasks_controller.rb', line 70

def create
  @view = params[:view] || "pending"
  @task = Task.new(params[:task]) # NOTE: we don't display validation messages for tasks.

  respond_with(@task) do |format|
    if @task.save
      update_sidebar if called_from_index_page?
    end
  end
end

#destroyObject

DELETE /tasks/1




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

def destroy
  @view = params[:view] || "pending"
  @task = Task.tracked_by(current_user).find(params[:id])
  @task.destroy

  # Make sure bucket's div gets hidden if we're deleting last task in the bucket.
  if Task.bucket_empty?(params[:bucket], current_user, @view)
    @empty_bucket = params[:bucket]
  end

  update_sidebar if called_from_index_page?
  respond_with(@task)
end

#editObject

GET /tasks/1/edit AJAX




54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/tasks_controller.rb', line 54

def edit
  @view = params[:view] || "pending"
  @task = Task.tracked_by(current_user).find(params[:id])
  @bucket = Setting.unroll(:task_bucket)[1..-1] << [ t(:due_specific_date, :default => 'On Specific Date...'), :specific_time ]
  @category = Setting.unroll(:task_category)
  @asset = @task.asset if @task.asset_id?

  if params[:previous].to_s =~ /(\d+)\z/
    @previous = Task.tracked_by(current_user).find_by_id($1) || $1.to_i
  end

  respond_with(@task)
end

#filterObject

Ajax request to filter out a list of tasks. AJAX




144
145
146
147
148
149
150
151
152
153
154
# File 'app/controllers/tasks_controller.rb', line 144

def filter
  @view = params[:view] || "pending"

  update_session do |filters|
    if params[:checked].true?
      filters << params[:filter]
    else
      filters.delete(params[:filter])
    end
  end
end

#indexObject

GET /tasks




13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/tasks_controller.rb', line 13

def index
  @view = params[:view] || "pending"
  @tasks = Task.find_all_grouped(current_user, @view)

  respond_with @tasks do |format|
    format.xls { render :layout => 'header' }
    format.csv { render :csv => @tasks.map(&:second).flatten }
    format.xml { render :xml => @tasks, :except => [:subscribed_users] }
  end
end

#newObject

GET /tasks/new




34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/tasks_controller.rb', line 34

def new
  @view = params[:view] || "pending"
  @task = Task.new
  @bucket = Setting.unroll(:task_bucket)[1..-1] << [ t(:due_specific_date, :default => 'On Specific Date...'), :specific_time ]
  @category = Setting.unroll(:task_category)

  if params[:related]
    model, id = params[:related].split(/_(\d+)/)
    if related = model.classify.constantize.my.find_by_id(id)
      instance_variable_set("@asset", related)
    else
      respond_to_related_not_found(model) and return
    end
  end

  respond_with(@task)
end

#showObject

GET /tasks/1




26
27
28
29
30
# File 'app/controllers/tasks_controller.rb', line 26

def show
  @task = Task.tracked_by(current_user).find(params[:id])

  respond_with(@task)
end

#updateObject

PUT /tasks/1




83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/tasks_controller.rb', line 83

def update
  @view = params[:view] || "pending"
  @task = Task.tracked_by(current_user).find(params[:id])
  @task_before_update = @task.clone

  if @task.due_at && (@task.due_at < Date.today.to_time)
    @task_before_update.bucket = "overdue"
  else
    @task_before_update.bucket = @task.computed_bucket
  end

  respond_with(@task) do |format|
    if @task.update_attributes(params[:task])
      @task.bucket = @task.computed_bucket
      if called_from_index_page?
        if Task.bucket_empty?(@task_before_update.bucket, current_user, @view)
          @empty_bucket = @task_before_update.bucket
        end
        update_sidebar
      end
    end
  end
end