Class: TasksController

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

Constant Summary

Constants included from Localization

Localization::LOCALIZED_STRINGS

Instance Method Summary collapse

Methods inherited from ApplicationController

in_place_edit_for

Methods included from ApplicationHelper

#back_or_link_to, #detour_to, #h, #image_button_to, #image_detour_to, #image_link_to, #t, #with_detour

Methods included from Localization

#l, load_localized_strings, #valid_language?

Instance Method Details

#abortObject



187
188
189
190
191
# File 'app/controllers/tasks_controller.rb', line 187

def abort
  task = Task.find params[:id]
  task.abort(user)
  back_or_redirect_to(:controller => "periods", :action => "show", :id => task.period)
end

#createObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/tasks_controller.rb', line 21

def create
  if params[:task] && params[:task][:period_id] && params[:task][:period_id][0..0] == 'G'
    detour_to :controller => 'periods', :action => :new, :group_id => params[:task][:period_id][1..-1].to_i
    return
  end
  @task = Task.new(params[:task])
  if @task.save
    flash[:notice] = 'Task was successfully created.'
    @task.move_to_top
    if @task.period
      back_or_redirect_to :controller => 'periods', :action => 'show', :id => @task.period, :task => @task.id
    else
      back_or_redirect_to :controller => 'backlogs', :action => :edit, :id => @task.backlog, :task => @task.id
    end
  else
    populate_layout
    @backlogs = Backlog.find(:all, :order => 'name')
    @periods = Period.find_active_or_future
    render :action => 'new'
  end
end

#destroyObject



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

def destroy
  task = Task.find(params[:id])
  lower_item = task.lower_item
  task.destroy
  flash[:notice] = 'Task was successfully deleted.'
  redirect_to :controller => 'periods', :action => :show, :id => task.period, :task => lower_item ? lower_item.id : nil
end

#editObject



43
44
45
46
47
48
# File 'app/controllers/tasks_controller.rb', line 43

def edit
  @task = Task.find(params[:id])
  @task.period_id = params[:task][:period_id] if params[:task] && params[:task][:period_id]
  @periods = Period.find_active_or_future
  @backlogs = Backlog.find(:all, :order => 'name')
end

#end_workObject



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

def end_work
  @task = Task.find(params[:id])
  next_quarter = Time.next_quarter
  if started_work = @task.started_work
    redirect_to({
      :controller => 'works',
      :action => :edit,
      :id => started_work,
          'work[task_id]' => @task.id,
          'work[started_at]' => started_work.started_at.iso8601,
          'work[completed_at]' => [next_quarter, started_work.started_at].max.iso8601,
          'work[hours]' => "%0.2f" % ((next_quarter - started_work.started_at).to_f / 3600),
          'estimate[todo]' => @task.todo
    })
  else
    @task.update_attributes(params[:task])
    redirect_to({
      :controller => 'works',
      :action => :new, 
          'work[task_id]' => @task.id,
          'work[started_at]' => Time.previous_quarter.iso8601,
          'work[completed_at]' => next_quarter.iso8601,
          'work[hours]' => "0.25",
          'estimate[todo]' => @task.todo
    })
  end
end

#finishObject



181
182
183
184
185
# File 'app/controllers/tasks_controller.rb', line 181

def finish
  @task = Task.find(params[:id])
  @task.finish(Task::COMPLETED, true)
  back_or_redirect_to :controller => 'periods', :action => :show, :id => @task.period, :task_id => @task.id
end

#list_startedObject



7
8
9
10
11
# File 'app/controllers/tasks_controller.rb', line 7

def list_started
  @tasks = Task.find_started
  @selected_task = Task.find_by_id(params[:id])
  back_or_redirect_to(:controller => '', :action => '') if @tasks.empty?
end

#move_downObject



120
121
122
123
124
# File 'app/controllers/tasks_controller.rb', line 120

def move_down
  task = Task.find(params[:id])
  task.move_lower
  redirect_to :controller => 'periods', :action => :show, :id => task.period, :task => task.id
end

#move_toObject



138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/tasks_controller.rb', line 138

def move_to
  task = Task.find(params[:id][5..-1])
  if params[:period_id] && params[:period_id].to_i != task.period_id
    task.remove_from_list
    task.period_id = params[:period_id]
  end
  task.insert_at params[:position]
  task.save!
  back_or_redirect_to :controller => 'periods', :action => :show_no_layout, :id => task.period, :task => task.id
end

#move_to_bottomObject



132
133
134
135
136
# File 'app/controllers/tasks_controller.rb', line 132

def move_to_bottom
  task = Task.find(params[:id])
  task.move_to_bottom
  redirect_to :action => :list, :id => task
end

#move_to_next_periodObject



170
171
172
173
174
175
176
177
178
179
# File 'app/controllers/tasks_controller.rb', line 170

def move_to_next_period
  task = Task.find(params[:id])
  if task.period
    params[:period_id] = task.period.lower_item && task.period.lower_item.id
    params[:id] = task.id
    move_to_period(true)
  else
    redirect_to :action => :edit, :id => task
  end
end

#move_to_period(with_layout = false) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/tasks_controller.rb', line 149

def move_to_period(with_layout = false)
  params[:id] = $1 if params[:id] =~ /^task_(\d*)$/
  task = Task.find(params[:id])
  if request.post?
    period = params[:period_id] && Period.find(params[:period_id])
    if period && period.active_or_future?
      next_task = task.higher_item ? task.higher_item : task.lower_item ? task.lower_item : task
      task.move_to_period period
      if with_layout
        back_or_redirect_to :controller => 'periods', :action => :show, :id => next_task.period, :task_id => next_task.id
      else
        back_or_redirect_to :controller => 'periods', :action => :show_no_layout, :id => next_task.period, :task_id => next_task.id
      end
    else
      detour_to :controller => 'periods', :action => :new, :period => {:party_id => task.period.party_id}, :layout => with_layout && determine_layout
    end
  else
    redirect_to :controller => 'periods', :action => :show, :id => task.period, :task_id => task.id, :layout => with_layout && determine_layout
  end
end

#move_to_topObject



126
127
128
129
130
# File 'app/controllers/tasks_controller.rb', line 126

def move_to_top
  task = Task.find(params[:id])
  task.move_to_top
  redirect_to :action => :list, :id => task
end

#move_upObject



113
114
115
116
117
118
# File 'app/controllers/tasks_controller.rb', line 113

def move_up
  task = Task.find(params[:id])
  task.move_higher
  flash[:notice] = "Task #{@focus_id} was successfully moved."
  redirect_to :controller => 'periods', :action => :show, :id => task.period, :task => task.id
end

#newObject



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

def new
  redirect_to params if request.post?
  @task = Task.new params[:task]
  @task.backlog_id ||= @task.period && @task.period.most_frequent_backlog
  @backlogs = Backlog.find(:all, :order => 'name')
  @periods = Period.find_active_or_future
end

#reopenObject



193
194
195
196
197
# File 'app/controllers/tasks_controller.rb', line 193

def reopen
  task = Task.find(params[:id])
  task.reopen
  back_or_redirect_to :controller => 'periods', :action => :show, :id => task.period, :task => task.id
end

#set_task_descriptionObject



262
263
264
265
266
267
# File 'app/controllers/tasks_controller.rb', line 262

def set_task_description
  task = Task.find(params[:id])
  task.description = params[:value]
  task.save!
  render :text => task.description
end

#specifyObject



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
258
259
260
# File 'app/controllers/tasks_controller.rb', line 233

def specify
  parent_task = Task.find(params[:id])
  raise "Task is not open" unless parent_task.active?
  if parent_task.children.empty?
    subtask_estimate = (parent_task.todo / 4).ceil
    no_of_subtasks = (parent_task.todo / subtask_estimate).to_i
    subtask_estimate = parent_task.todo / no_of_subtasks
     (1..no_of_subtasks).each do |index|
      subtask = parent_task.children.build
      subtask.description = "#{parent_task.description} ##{parent_task.children.size}"
      subtask.initial_estimate = subtask_estimate
      subtask.save!
    end
    redirect_to :controller => 'periods', :action => :show, :id => parent_task.period, :task => parent_task.children.first
  else
    average_estimate = parent_task.children.reduce {|value, task, size| value + (task.initial_estimate / size)}
    subtask = parent_task.children.build
    subtask.description = "#{parent_task.description} ##{parent_task.children.size}"
    subtask.initial_estimate = average_estimate
    subtask.save!
    redirect_to :controller => 'periods', :action => :show, :id => parent_task.period, :task => parent_task.children.first
  end
  if started_work = parent_task.started_work(user)
    flash[:notice] = "Pågående arbeid er flyttet til subtask"
    started_work.task = parent_task.children.last
    started_work.save!
  end
end

#start_workObject



199
200
201
202
203
# File 'app/controllers/tasks_controller.rb', line 199

def start_work
  task = Task.find(params[:id])
  task.start_work
  back_or_redirect_to :controller => 'periods', :action => :show, :id => task.period, :task => task.id
end

#updateObject



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

def update
  redirect_to(:controller => 'backlogs', :action => :index) unless params[:id]
  if params[:task] && params[:task][:description] == ''
    destroy
    return
  end
  @task = Task.find(params[:id])
  unless (@task.period && @task.period.active?) || (params[:task] && params[:task][:period_id] && Period.find(params[:task][:period_id]).active?)
    @task.errors.add(:period_id, "You may not update a task in an inactive period.  Move the task to an active period first.")
    back_or_redirect_to :controller => 'periods', :action => :show, :id => @task.period, :task => @task.id
    return
  end

  # TODO (uwe): This should be moved to the Task model: Task#period=
  if params[:task] && params[:task][:period_id]
    if params[:task][:period_id] != ''
      if params[:task][:period_id].to_i != @task.period_id
        @task.remove_from_list
        params[:task][:position] = Period.find(params[:task][:period_id]).tasks.size
      end
    else
      if @task.period_id != nil
        @task.remove_from_list
        @task.period = nil
        new_pos = @task.backlog.tasks.count(:conditions => 'period_id IS NULL') + 1
        params[:task][:position] = new_pos
      end
    end
  end

  if params[:task] && @task.update_attributes(params[:task])
    flash[:notice] = 'Task was successfully updated.'
    if params[:task][:todo] && params[:task][:todo].to_i == 0
      unless @task.finished_at
        next_task = @task.lower_item || @task.higher_item
        @task.finish Task::COMPLETED, true, user
        @task.save
        back_or_redirect_to :controller => 'periods', :action => :show, :id => @task.period, :task => next_task ? next_task.id : nil
      else
        back_or_redirect_to :controller => 'periods', :action => :show, :id => @task.period, :task => @task.id
      end
    else
      if @task.finished_at
        reopen
      else
        back_or_redirect_to :controller => 'periods', :action => 'show', :id => @task.period, :task => @task.id
      end
    end
  else
    populate_layout
    edit
    render :action => 'edit'
  end
end