-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtasks_controller.rb
More file actions
227 lines (191 loc) · 7.26 KB
/
tasks_controller.rb
File metadata and controls
227 lines (191 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
168
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
196
197
198
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
# frozen_string_literal: true
# Copyright (c) 2008-2013 Michael Dvorkin and contributors.
#
# Fat Free CRM is freely distributable under the terms of MIT license.
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------
class TasksController < ApplicationController
before_action :set_current_tab, only: %i[index show]
before_action :update_sidebar, only: :index
# GET /tasks
#----------------------------------------------------------------------------
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
# GET /tasks/1
#----------------------------------------------------------------------------
def show
@task = Task.tracked_by(current_user).find(params[:id])
respond_with(@task)
end
# GET /tasks/new
#----------------------------------------------------------------------------
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
# GET /tasks/1/edit AJAX
#----------------------------------------------------------------------------
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
# POST /tasks
#----------------------------------------------------------------------------
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
# PUT /tasks/1
#----------------------------------------------------------------------------
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
# DELETE /tasks/1
#----------------------------------------------------------------------------
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
# PUT /tasks/1/complete
#----------------------------------------------------------------------------
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
# PUT /tasks/1/uncomplete
#----------------------------------------------------------------------------
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
# POST /tasks/auto_complete/query AJAX
#----------------------------------------------------------------------------
# Handled by ApplicationController :auto_complete
# Ajax request to filter out a list of tasks. AJAX
#----------------------------------------------------------------------------
def filter
@view = view
update_session do |filters|
if params[:checked].true?
filters << params[:filter]
else
filters.delete(params[:filter])
end
end
end
protected
def task_params
return {} unless params[:task]
params.require(:task).permit(
:user_id,
:assigned_to,
:completed_by,
:name,
:asset_id,
:asset_type,
:priority,
:category,
:bucket,
:due_at,
:completed_at,
:deleted_at,
:background_info,
:calendar
)
end
private
# Yields array of current filters and updates the session using new values.
#----------------------------------------------------------------------------
def update_session
name = "filter_by_task_#{@view}"
filters = (session[name].nil? ? [] : session[name].split(","))
yield filters
session[name] = filters.uniq.join(",")
end
# Collect data necessary to render filters sidebar.
#----------------------------------------------------------------------------
def update_sidebar
@view = view
@task_total = Task.totals(current_user, @view)
# Update filters session if we added, deleted, or completed a task.
if @task
update_session do |filters|
if @empty_bucket # deleted, completed, rescheduled, or reassigned and need to hide a bucket
filters.delete(@empty_bucket)
elsif !@task.deleted_at && !@task.completed_at # created new task
filters << @task.computed_bucket
end
end
end
# Create default filters if filters session is empty.
name = "filter_by_task_#{@view}"
unless session[name]
filters = @task_total.keys.select { |key| key != :all && @task_total[key] != 0 }.join(",")
session[name] = filters unless filters.blank?
end
end
# Ensure view is allowed
#----------------------------------------------------------------------------
def view
view = params[:view]
views = Task::ALLOWED_VIEWS
views.include?(view) ? view : views.first
end
end