Merge pull request #142 from foodcoop-rostock/multiple-recurring-tasks
Great!
This commit is contained in:
commit
db8a929f0b
22 changed files with 223 additions and 174 deletions
29
app/models/periodic_task_group.rb
Normal file
29
app/models/periodic_task_group.rb
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
class PeriodicTaskGroup < ActiveRecord::Base
|
||||
has_many :tasks, dependent: :destroy
|
||||
|
||||
PeriodDays = 7
|
||||
|
||||
def has_next_task?
|
||||
return false if tasks.empty?
|
||||
return false if tasks.first.due_date.nil?
|
||||
return true
|
||||
end
|
||||
|
||||
def create_next_task
|
||||
template_task = tasks.first
|
||||
self.next_task_date ||= template_task.due_date + PeriodDays
|
||||
|
||||
next_task = template_task.dup
|
||||
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
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
class Task < ActiveRecord::Base
|
||||
has_many :assignments, :dependent => :destroy
|
||||
has_many :users, :through => :assignments
|
||||
belongs_to :workgroup
|
||||
belongs_to :periodic_task_group
|
||||
|
||||
scope :non_group, where(workgroup_id: nil, done: false)
|
||||
scope :done, where(done: true)
|
||||
|
|
@ -16,7 +18,9 @@ class Task < ActiveRecord::Base
|
|||
validates :required_users, :presence => true
|
||||
validates_numericality_of :duration, :required_users, :only_integer => true, :greater_than => 0
|
||||
validates_length_of :description, maximum: 250
|
||||
validates :done, exclusion: { in: [true] }, if: :periodic?, on: :create
|
||||
|
||||
before_save :exclude_from_periodic_task_group, if: :changed?, unless: :new_record?
|
||||
after_save :update_ordergroup_stats
|
||||
|
||||
# Find all tasks, for which the current user should be responsible
|
||||
|
|
@ -46,6 +50,10 @@ class Task < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def periodic?
|
||||
not periodic_task_group.nil?
|
||||
end
|
||||
|
||||
def is_assigned?(user)
|
||||
self.assignments.detect {|ass| ass.user_id == user.id }
|
||||
end
|
||||
|
|
@ -100,5 +108,10 @@ class Task < ActiveRecord::Base
|
|||
def update_ordergroup_stats(user_ids = self.user_ids)
|
||||
Ordergroup.joins(:users).where(users: {id: user_ids}).each(&:update_stats!)
|
||||
end
|
||||
|
||||
def exclude_from_periodic_task_group
|
||||
self.periodic_task_group = nil
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -6,51 +6,9 @@ class Workgroup < Group
|
|||
has_many :open_tasks, :class_name => 'Task', :conditions => ['done = ?', false], :order => 'due_date ASC'
|
||||
|
||||
validates_uniqueness_of :name
|
||||
validates_presence_of :task_name, :weekday, :task_required_users, :next_weekly_tasks_number,
|
||||
:if => :weekly_task
|
||||
validates_numericality_of :next_weekly_tasks_number, :greater_than => 0, :less_than => 21, :only_integer => true,
|
||||
:if => :weekly_task
|
||||
validates_length_of :task_description, maximum: 250
|
||||
validate :last_admin_on_earth, :on => :update
|
||||
before_destroy :check_last_admin_group
|
||||
|
||||
def self.weekdays
|
||||
days = I18n.t('date.day_names')
|
||||
(0..days.length-1).map {|i| [days[i], i.to_s]}
|
||||
end
|
||||
|
||||
# Returns an Array with date-objects to represent the next weekly-tasks
|
||||
def next_weekly_tasks
|
||||
# our system starts from 0 (sunday) to 6 (saturday)
|
||||
# get difference between groups weekday and now
|
||||
diff = self.weekday - Time.now.wday
|
||||
if diff >= 0
|
||||
# weektask is in current week
|
||||
nextTask = diff.day.from_now
|
||||
else
|
||||
# weektask is in the next week
|
||||
nextTask = (diff + 7).day.from_now
|
||||
end
|
||||
# now generate the Array
|
||||
nextTasks = Array.new
|
||||
next_weekly_tasks_number.times do
|
||||
nextTasks << nextTask.to_date
|
||||
nextTask = 1.week.from_now(nextTask)
|
||||
end
|
||||
return nextTasks
|
||||
end
|
||||
|
||||
def task_attributes(date)
|
||||
{
|
||||
:name => task_name,
|
||||
:description => task_description,
|
||||
:due_date => date,
|
||||
:required_users => task_required_users,
|
||||
:duration => task_duration,
|
||||
:weekly => true
|
||||
}
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Check before destroy a group, if this is the last group with admin role
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue