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




119
120
121
122
123
124
125
126
127
128
# File 'app/controllers/tasks_controller.rb', line 119

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

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

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

#createObject

POST /tasks




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

def create
  @view = view
  @task = Task.new(task_params) # 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




105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/tasks_controller.rb', line 105

def destroy
  @view = view
  @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.
  @empty_bucket = params[:bucket] if Task.bucket_empty?(params[:bucket], current_user, @view)

  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
# File 'app/controllers/tasks_controller.rb', line 54

def edit
  @view = view
  @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?

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

  respond_with(@task)
end

#filterObject

Ajax request to filter out a list of tasks. AJAX




149
150
151
152
153
154
155
156
157
158
159
# File 'app/controllers/tasks_controller.rb', line 149

def filter
  @view = view

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

#indexObject

GET /tasks




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

def index
  @view = view
  @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 = view
  @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(current_user).find_by_id(id)
      instance_variable_set("@asset", related)
    else
      respond_to_related_not_found(model) && return
    end
  end

  respond_with(@task)
end

#showObject

GET /tasks/1




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

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

#uncompleteObject

PUT /tasks/1/uncomplete




132
133
134
135
136
137
138
139
140
141
# File 'app/controllers/tasks_controller.rb', line 132

def uncomplete
  @task = Task.tracked_by(current_user).find(params[:id])
  @task&.update(completed_at: nil, completed_by: nil)

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

  update_sidebar
  respond_with(@task)
end

#updateObject

PUT /tasks/1




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

def update
  @view = view
  @task = Task.tracked_by(current_user).find(params[:id])
  @task_before_update = @task.dup

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

  respond_with(@task) do |_format|
    if @task.update(task_params)
      @task.bucket = @task.computed_bucket
      if called_from_index_page?
        @empty_bucket = @task_before_update.bucket if Task.bucket_empty?(@task_before_update.bucket, current_user, @view)
        update_sidebar
      end
    end
  end
end