【发布时间】:2017-09-09 21:31:54
【问题描述】:
我正在构建一个具有 Workflows 和 Tasks 的 Rails 4.2+ 应用程序。 task.due_date 可以是指定的日期(例如“01/01/2017”),也可以是相对于父 workflow 记录上的日期。
class Workflow < ApplicationRecord
# closing_date:date
after_save :update_relative_tasks
has_many :tasks
# Production code would make sure closing_date changed before updating associations
def update_relative_tasks
tasks.where(has_relative_date: true).each do |task|
task.update_relative_due_date(closing_date)
end
end
end
class Task < ApplicationRecord
# due_date:date
# has_relative_date:boolean (default: false)
# number:integer (default: 0)
# days_or_weeks:string (default: "days")
# before_or_after:string (default: "after")
# workflow:references
belongs_to :workflow
def number_of_days
@number_of_days ||= if days_or_weeks == "weeks"
number * 7 # relative date is in "weeks"
else
number # assume relative date is in "days"
end
end
# Pseudo-ish code, not tested, but shows concept
def update_relative_due_date(date)
new_due_date = if before_or_after == "before"
date - number_of_days.days
else
date + number_of_days.days
end
update_attribute(:due_date, new_due_date)
end
end
在task 上分配特定日期很简单,我只使用日历小部件并设置日期,没问题。
棘手的部分是根据workflow.closing_date 分配一个相对的task.due_date。
例如,假设workflow.closing_date 是01/01/2017,我想在该日期之前(或之后)5 天完成任务。
我可以添加一些表单字段,其中的括号代表输入字段:
Due Date: [x] number of [days|weeks] [before|after] workflow.closing_date
然后我可以在 Ruby 代码中解析它并使用正确的 due_date 更新 task。
但是,如果workflow.closing_date 发生变化,我将需要查找所有关联的tasks 并重新计算它们的相对日期。此外,如果 workflow.closing_date 最初不为人所知,后来被添加,则需要重新计算所有相关任务的截止日期。
最终,我还将向tasks 添加提醒,这些提醒也将与task.due_date 相关。与 Google 日历的通知工作方式非常相似。
这样的问题是如何解决的?我知道我需要添加一个后台处理器来处理发送提醒。但我不确定如何解决在另一条记录上相对于某个日期设置task.due_date 的问题。
【问题讨论】:
标签: ruby-on-rails notifications relative-date