Class: PeriodicTaskGroup
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- PeriodicTaskGroup
- Defined in:
- app/models/periodic_task_group.rb
Instance Method Summary collapse
- #create_next_task ⇒ Object
- #create_tasks_for_upfront_days ⇒ Object
- #create_tasks_until(create_until) ⇒ Object
- #exclude_tasks_before(task) ⇒ Object
- #has_next_task? ⇒ Boolean
-
#period_days ⇒ Number
protected
Number of days between two periodic tasks.
- #update_tasks_including(template_task, prev_due_date) ⇒ Object
Instance Method Details
#create_next_task ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/models/periodic_task_group.rb', line 11 def create_next_task template_task = tasks.first self.next_task_date ||= template_task.due_date + period_days next_task = template_task.dup next_task.due_date = next_task_date next_task.done = false next_task.save self.next_task_date += period_days save end |
#create_tasks_for_upfront_days ⇒ Object
30 31 32 33 34 |
# File 'app/models/periodic_task_group.rb', line 30 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 |
#create_tasks_until(create_until) ⇒ Object
24 25 26 27 28 |
# File 'app/models/periodic_task_group.rb', line 24 def create_tasks_until(create_until) return unless has_next_task? create_next_task while next_task_date.nil? || next_task_date < create_until end |
#exclude_tasks_before(task) ⇒ Object
36 37 38 39 40 |
# File 'app/models/periodic_task_group.rb', line 36 def exclude_tasks_before(task) tasks.where("due_date < '#{task.due_date}'").find_each do |t| t.update_attribute(:periodic_task_group, nil) end end |
#has_next_task? ⇒ Boolean
4 5 6 7 8 9 |
# File 'app/models/periodic_task_group.rb', line 4 def has_next_task? return false if tasks.empty? return false if tasks.first.due_date.nil? true end |
#period_days ⇒ Number (protected)
Returns Number of days between two periodic tasks.
61 62 63 64 |
# File 'app/models/periodic_task_group.rb', line 61 def period_days # minimum of one to avoid inifite loop when value is invalid [FoodsoftConfig[:tasks_period_days].to_i, 1].max end |
#update_tasks_including(template_task, prev_due_date) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/models/periodic_task_group.rb', line 42 def update_tasks_including(template_task, prev_due_date) group_tasks = tasks + [template_task] due_date_delta = template_task.due_date - prev_due_date tasks.each do |task| task.update!(name: template_task.name, description: template_task.description, duration: template_task.duration, required_users: template_task.required_users, workgroup: template_task.workgroup, due_date: task.due_date + due_date_delta) end group_tasks.each do |task| task.update_columns(periodic_task_group_id: id) end end |