From 9ae7c05a8904aaf50d2ed8bff325161860b325bf Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Wed, 24 Feb 2016 20:47:09 +0100 Subject: [PATCH] Create next tasks directly after creating a periodic task --- app/controllers/tasks_controller.rb | 1 + app/models/periodic_task_group.rb | 14 ++++++++++++++ lib/tasks/foodsoft.rake | 9 ++------- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 9fd7211e..78f5c261 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -22,6 +22,7 @@ class TasksController < ApplicationController @task.periodic_task_group = PeriodicTaskGroup.new end if @task.save + @task.periodic_task_group.create_tasks_for_upfront_days if params[:periodic] redirect_to tasks_url, :notice => I18n.t('tasks.create.notice') else render :template => "tasks/new" diff --git a/app/models/periodic_task_group.rb b/app/models/periodic_task_group.rb index b7ccceeb..962012c1 100644 --- a/app/models/periodic_task_group.rb +++ b/app/models/periodic_task_group.rb @@ -19,6 +19,20 @@ class PeriodicTaskGroup < ActiveRecord::Base self.save end + def create_tasks_until(create_until) + if has_next_task? + while next_task_date.nil? || next_task_date < create_until + create_next_task + end + end + end + + def create_tasks_for_upfront_days + create_until = Date.today + FoodsoftConfig[:tasks_upfront_days].to_i + 1 + create_tasks_until create_until + create_until + end + def exclude_tasks_before(task) tasks.where("due_date < '#{task.due_date}'").each do |t| t.update_attribute(:periodic_task_group, nil) diff --git a/lib/tasks/foodsoft.rake b/lib/tasks/foodsoft.rake index bfe94319..8efd9e28 100644 --- a/lib/tasks/foodsoft.rake +++ b/lib/tasks/foodsoft.rake @@ -39,13 +39,8 @@ namespace :foodsoft do desc "Create upcoming periodic tasks" task :create_upcoming_periodic_tasks => :environment do for tg in PeriodicTaskGroup.all - if tg.has_next_task? - create_until = Date.today + FoodsoftConfig[:tasks_upfront_days].to_i + 1 - rake_say "creating until #{create_until}" - while tg.next_task_date.nil? || tg.next_task_date < create_until - tg.create_next_task - end - end + created_until tg.create_tasks_for_upfront_days + rake_say "created until #{created_until}" end end end