Create next tasks directly after creating a periodic task

This commit is contained in:
Patrick Gansterer 2016-02-24 20:47:09 +01:00 committed by wvengen
parent 6015d16cab
commit 9ae7c05a89
3 changed files with 17 additions and 7 deletions

View file

@ -22,6 +22,7 @@ class TasksController < ApplicationController
@task.periodic_task_group = PeriodicTaskGroup.new @task.periodic_task_group = PeriodicTaskGroup.new
end end
if @task.save 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') redirect_to tasks_url, :notice => I18n.t('tasks.create.notice')
else else
render :template => "tasks/new" render :template => "tasks/new"

View file

@ -19,6 +19,20 @@ class PeriodicTaskGroup < ActiveRecord::Base
self.save self.save
end 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) def exclude_tasks_before(task)
tasks.where("due_date < '#{task.due_date}'").each do |t| tasks.where("due_date < '#{task.due_date}'").each do |t|
t.update_attribute(:periodic_task_group, nil) t.update_attribute(:periodic_task_group, nil)

View file

@ -39,13 +39,8 @@ namespace :foodsoft do
desc "Create upcoming periodic tasks" desc "Create upcoming periodic tasks"
task :create_upcoming_periodic_tasks => :environment do task :create_upcoming_periodic_tasks => :environment do
for tg in PeriodicTaskGroup.all for tg in PeriodicTaskGroup.all
if tg.has_next_task? created_until tg.create_tasks_for_upfront_days
create_until = Date.today + FoodsoftConfig[:tasks_upfront_days].to_i + 1 rake_say "created until #{created_until}"
rake_say "creating until #{create_until}"
while tg.next_task_date.nil? || tg.next_task_date < create_until
tg.create_next_task
end
end
end end
end end
end end