首先是一些 FactoryBot 和 Rails 技巧。
Do not use Date.today in Rails, it is not aware of time zones。使用Time.zone.today。这些是时间,而不是日期,所以Time.current 更合适。最后,除非您的所有约会都已过去,否则请使用2.years.since。
时间戳的约定是以_at 结尾。 start_at 和 end_at。这也避免了混淆 start_date 是时间,而不是日期。
我们可以利用ActiveSupport::Duration 及其Numeric extensions 添加到开始日期。 end_date { start_date + 1.hour }.
我们可以使用trait 来明确假设,而不是将特定测试的假设硬编码到工厂中。
factory :appointment do
# These are the normal conditions.
# end_at will be 15 to 180 minutes after start_at.
start_at { Faker::Time.between(from: 2.years.ago, to: 2.years.since) }
end_at { start_at + rand(15..180).minutes }
# This is a specific trait putting end_at an hour after start_at.
trait :in_one_hour do
end_at { start_at + 1.hour }
end
end
# An appointment of 1 hour which started yesterday
appointment = build(:appointment, :in_one_hour, start_at: 1.day.ago)
我们可以做得更好。如果我们想要不同的持续时间怎么办?使用transient attribute 而不是特征。这使您可以向工厂发送不是对象属性的属性。喜欢持续时间。
factory :appointment do
transient do
duration { rand(15..180).minutes }
end
start_at { Faker::Time.between(from: 2.years.ago, to: 2.years.since) }
end_at { start_at + duration }
end
# An appointment with a random but reasonable duration.
p build(:appointment)
# An appointment with a duration of exactly 1 hour.
p build(:appointment, duration: 1.hour)
# An appointment lasting 30 minutes starting yesterday.
p build(:appointment, duration: 30.minutes, start_at: 1.day.ago)
有问题。如果调用者更改 end_at 怎么办?那么 start_at 应该以 end_at 为基础。但是如果他们设置 start_at,end_at 需要基于 start_at。这会导致循环定义。
factory :appointment do
transient do
duration { rand(15..180).minutes }
end
# Circular
start_at { end_at + duration }
end_at { start_at - duration }
end
我们需要使用callback 来避免循环依赖。
factory :appointment do
transient do
duration { rand(15..180).minutes }
end
after(:build) do |appointment, evaluator|
case
when appointment.start_at && appointment.end_at
# The user set both, leave them be.
when appointment.start_at
# The user set only the start_at.
appointment.end_at ||= appointment.start_at + evaluator.duration
when appointment.end_at
# The user set only the end_at.
appointment.start_at ||= appointment.end_at - evaluator.duration
else
# The user set neither.
appointment.start_at = Faker::Time.between(from: 2.years.ago, to: 2.years.since)
appointment.end_at = appointment.start_at + evaluator.duration
end
end
end
p build(:appointment)
p build(:appointment, duration: 1.hour, start_at: 1.year.ago)
p build(:appointment, duration: 1.hour, end_at: 1.year.since)
p build(:appointment, start_at: 1.year.ago, end_at: 1.year.since)
最后,如果您使用 Postgres,you can merge start_at and end_at into a single range column。这使用了Postgres's tstzrange type,Rails 将在两次之间变成Range。这比开始和结束时间戳更容易使用。
factory :appointment do
transient do
duration { rand(15..180).minutes }
end
timespan do
start_time = Faker::Time.between(from: 2.years.ago, to: 2.years.since)
end_time = start_time + duration
(start_time..end_time)
end
end
p FactoryBot.build(:appointment)
p FactoryBot.build(:appointment, duration: 1.hour)
p FactoryBot.build(:appointment, timespan: (1.year.ago..Time.current))