Move due date according to delta

This commit is contained in:
Robert 2015-05-01 21:31:06 +02:00
parent fd51b07e21
commit a8a434ef7b
2 changed files with 6 additions and 3 deletions

View file

@ -44,9 +44,10 @@ class TasksController < ApplicationController
@task = Task.find(params[:id]) @task = Task.find(params[:id])
task_group = @task.periodic_task_group task_group = @task.periodic_task_group
was_periodic = @task.periodic? was_periodic = @task.periodic?
prev_due_date = @task.due_date
@task.attributes=(params[:task]) @task.attributes=(params[:task])
if @task.errors.empty? && @task.save if @task.errors.empty? && @task.save
task_group.update_tasks_including(@task) if params[:periodic] task_group.update_tasks_including(@task, prev_due_date) if params[:periodic]
flash[:notice] = I18n.t('tasks.update.notice') flash[:notice] = I18n.t('tasks.update.notice')
if was_periodic && !@task.periodic? if was_periodic && !@task.periodic?
flash[:notice] = I18n.t('tasks.update.notice_converted') flash[:notice] = I18n.t('tasks.update.notice_converted')

View file

@ -25,14 +25,16 @@ class PeriodicTaskGroup < ActiveRecord::Base
end end
end end
def update_tasks_including(template_task) def update_tasks_including(template_task, prev_due_date)
group_tasks = tasks + [template_task] group_tasks = tasks + [template_task]
due_date_delta = template_task.due_date - prev_due_date
tasks.each do |task| tasks.each do |task|
task.update!(name: template_task.name, task.update!(name: template_task.name,
description: template_task.description, description: template_task.description,
duration: template_task.duration, duration: template_task.duration,
required_users: template_task.required_users, required_users: template_task.required_users,
workgroup: template_task.workgroup) workgroup: template_task.workgroup,
due_date: task.due_date + due_date_delta)
end end
group_tasks.each do |task| group_tasks.each do |task|
task.update_columns(periodic_task_group_id: self.id) task.update_columns(periodic_task_group_id: self.id)