From 990397a7f06f68e054a459c8c638d6573670fa0a Mon Sep 17 00:00:00 2001 From: Robert Waltemath Date: Fri, 21 Jun 2013 22:04:36 +0200 Subject: [PATCH] Delete only the FOLLOWING tasks. --- .../periodic_task_groups_controller.rb | 8 -------- app/controllers/tasks_controller.rb | 7 ++++++- app/helpers/periodic_task_groups_helper.rb | 2 -- app/models/periodic_task_group.rb | 16 +++++++++++----- app/views/tasks/show.haml | 4 ++-- config/routes.rb | 2 -- 6 files changed, 19 insertions(+), 20 deletions(-) delete mode 100644 app/controllers/periodic_task_groups_controller.rb delete mode 100644 app/helpers/periodic_task_groups_helper.rb diff --git a/app/controllers/periodic_task_groups_controller.rb b/app/controllers/periodic_task_groups_controller.rb deleted file mode 100644 index 3642cc97..00000000 --- a/app/controllers/periodic_task_groups_controller.rb +++ /dev/null @@ -1,8 +0,0 @@ -class PeriodicTaskGroupsController < ApplicationController - def destroy - @task_group = PeriodicTaskGroup.find(params[:id]) - @task_group.destroy - - redirect_to tasks_url, notice: I18n.t('periodic_task_groups.destroy.notice') - end -end diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index c8fbcada..017a9a8a 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -56,7 +56,12 @@ class TasksController < ApplicationController task = Task.find(params[:id]) # Save user_ids to update apple statistics after destroy user_ids = task.user_ids - task.destroy + if params[:periodic] + task.periodic_task_group.exclude_tasks_before(task) + task.periodic_task_group.destroy + else + task.destroy + end task.update_ordergroup_stats(user_ids) redirect_to tasks_url, :notice => I18n.t('tasks.destroy.notice') diff --git a/app/helpers/periodic_task_groups_helper.rb b/app/helpers/periodic_task_groups_helper.rb deleted file mode 100644 index cf0d9631..00000000 --- a/app/helpers/periodic_task_groups_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module PeriodicTaskGroupsHelper -end diff --git a/app/models/periodic_task_group.rb b/app/models/periodic_task_group.rb index 3e11de9e..b92de76c 100644 --- a/app/models/periodic_task_group.rb +++ b/app/models/periodic_task_group.rb @@ -1,23 +1,29 @@ class PeriodicTaskGroup < ActiveRecord::Base - has_many :tasks, :dependent => :destroy + has_many :tasks, dependent: :destroy PeriodDays = 7 def has_next_task? - return false if self.tasks.empty? - return false if self.tasks.first.due_date.nil? + return false if tasks.empty? + return false if tasks.first.due_date.nil? return true end def create_next_task - template_task = self.tasks.first + template_task = tasks.first self.next_task_date ||= template_task.due_date + PeriodDays next_task = template_task.dup - next_task.due_date = self.next_task_date + next_task.due_date = next_task_date next_task.save self.next_task_date += PeriodDays self.save end + + def exclude_tasks_before(task) + tasks.where("due_date < '#{task.due_date}'").each do |t| + t.update_attribute(:periodic_task_group, nil) + end + end end diff --git a/app/views/tasks/show.haml b/app/views/tasks/show.haml index 2d8fa063..f2e147c8 100644 --- a/app/views/tasks/show.haml +++ b/app/views/tasks/show.haml @@ -30,5 +30,5 @@ = link_to t('ui.delete'), task_path(@task), :method => :delete, :confirm => "Die Aufgabe wirklich löschen?", class: 'btn btn-danger' - if @task.periodic? - = link_to t('ui.delete_all'), periodic_task_group_path(@task.periodic_task_group), :method => :delete, - :confirm => "Alle Aufgaben dieser wöchentlichen Aufgabe wirklich löschen?", class: 'btn btn-danger' + = link_to 'Aufgabe und folgende löschen', task_path(@task, periodic: true), method: :delete, + confirm: "Diese und alle folgenden wöchentlichen Aufgaben wirklich löschen?", class: 'btn btn-danger' diff --git a/config/routes.rb b/config/routes.rb index 19855380..cbcf2828 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -70,8 +70,6 @@ Foodsoft::Application.routes.draw do end end - resources :periodic_task_groups, only: [:destroy] - resources :messages, :only => [:index, :show, :new, :create] namespace :foodcoop do