From 1cdb9e85017a17e4dbee276cd87feaa2aab75932 Mon Sep 17 00:00:00 2001 From: Robert Waltemath Date: Wed, 28 Nov 2012 10:13:54 +0100 Subject: [PATCH] Outlined new concept for multiple periodically recurring tasks (#3). --- app/models/periodic_task_group.rb | 23 +++++++++++++++++++ app/models/task.rb | 9 ++++++++ app/views/tasks/_form.html.haml | 1 + config/locales/de.yml | 1 + ...21115073715_create_periodic_task_groups.rb | 13 +++++++++++ db/schema.rb | 23 ++++++++++++------- lib/tasks/foodsoft.rake | 11 +++++++++ 7 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 app/models/periodic_task_group.rb create mode 100644 db/migrate/20121115073715_create_periodic_task_groups.rb diff --git a/app/models/periodic_task_group.rb b/app/models/periodic_task_group.rb new file mode 100644 index 00000000..7f0efa1a --- /dev/null +++ b/app/models/periodic_task_group.rb @@ -0,0 +1,23 @@ +class PeriodicTaskGroup < ActiveRecord::Base + has_many :tasks, :inverse_of => :periodic_task_group, :dependent => :destroy + + PeriodDays = 7 + + def has_next_task? + return false if self.tasks.empty? + return false if self.tasks.first.due_date.nil? + return true + end + + def create_next_task + template_task = self.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.save + + self.next_task_date += PeriodDays + self.save + end +end diff --git a/app/models/task.rb b/app/models/task.rb index a16ada32..69dbb860 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -2,6 +2,7 @@ class Task < ActiveRecord::Base has_many :assignments, :dependent => :destroy has_many :users, :through => :assignments belongs_to :workgroup + belongs_to :periodic_task_group, :inverse_of => :tasks scope :non_group, where(workgroup_id: nil, done: false) scope :done, where(done: true) @@ -45,6 +46,14 @@ class Task < ActiveRecord::Base end end + def periodic + not periodic_task_group.nil? + end + + def periodic=(p) + self.periodic_task_group = PeriodicTaskGroup.new if p == "1" + end + def is_assigned?(user) self.assignments.detect {|ass| ass.user_id == user.id } end diff --git a/app/views/tasks/_form.html.haml b/app/views/tasks/_form.html.haml index 77e96aad..29875af1 100644 --- a/app/views/tasks/_form.html.haml +++ b/app/views/tasks/_form.html.haml @@ -23,6 +23,7 @@ = f.input :required_users = f.association :workgroup = f.input :due_date, as: :date_picker + = f.input :periodic, as: :boolean = f.input :done .form-actions = f.submit class: 'btn' diff --git a/config/locales/de.yml b/config/locales/de.yml index d53e9593..0e487c4d 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -312,6 +312,7 @@ de: required_users: 'Anzahl' due_date: 'Wann erledigen?' workgroup: 'Arbeitsgruppe' + periodic: 'Wöchentlich wiederholen' done: Erledigt? message: sent_to_all: 'An alle Mitglieder schicken' diff --git a/db/migrate/20121115073715_create_periodic_task_groups.rb b/db/migrate/20121115073715_create_periodic_task_groups.rb new file mode 100644 index 00000000..87cf560b --- /dev/null +++ b/db/migrate/20121115073715_create_periodic_task_groups.rb @@ -0,0 +1,13 @@ +class CreatePeriodicTaskGroups < ActiveRecord::Migration + def change + create_table :periodic_task_groups do |t| + t.date :next_task_date + + t.timestamps + end + + change_table :tasks do |t| + t.references :periodic_task_group + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 2e795b70..63ab6801 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120929155541) do +ActiveRecord::Schema.define(:version => 20121115073715) do create_table "article_categories", :force => true do |t| t.string "name", :default => "", :null => false @@ -284,6 +284,12 @@ ActiveRecord::Schema.define(:version => 20120929155541) do add_index "pages", ["permalink"], :name => "index_pages_on_permalink" add_index "pages", ["title"], :name => "index_pages_on_title" + create_table "periodic_task_groups", :force => true do |t| + t.date "next_task_date" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "stock_changes", :force => true do |t| t.integer "delivery_id" t.integer "order_id" @@ -324,17 +330,18 @@ ActiveRecord::Schema.define(:version => 20120929155541) do add_index "suppliers", ["name"], :name => "index_suppliers_on_name", :unique => true create_table "tasks", :force => true do |t| - t.string "name", :default => "", :null => false + t.string "name", :default => "", :null => false t.string "description" t.date "due_date" - t.boolean "done", :default => false + t.boolean "done", :default => false t.integer "workgroup_id" - t.boolean "assigned", :default => false - t.datetime "created_on", :null => false - t.datetime "updated_on", :null => false - t.integer "required_users", :default => 1 + t.boolean "assigned", :default => false + t.datetime "created_on", :null => false + t.datetime "updated_on", :null => false + t.integer "required_users", :default => 1 t.boolean "weekly" - t.integer "duration", :default => 1 + t.integer "duration", :default => 1 + t.integer "periodic_task_group_id" end add_index "tasks", ["due_date"], :name => "index_tasks_on_due_date" diff --git a/lib/tasks/foodsoft.rake b/lib/tasks/foodsoft.rake index ebfecbc1..8ef1e739 100644 --- a/lib/tasks/foodsoft.rake +++ b/lib/tasks/foodsoft.rake @@ -49,4 +49,15 @@ namespace :foodsoft do end end end + + desc "Create upcoming periodic tasks" + task :create_upcoming_periodic_tasks => :environment do + for tg in PeriodicTaskGroup.all + if tg.has_next_task? + while tg.next_task_date.nil? or tg.next_task_date < Date.today + 30 + tg.create_next_task + end + end + end + end end