這是我的第一篇文章,以便提前道歉任何新手的錯誤。我已經嘗試過研究這個問題的不同解決方案,並且迄今還沒有遇到似乎適合我的特殊情況的解決方案。發送提醒郵件,每三天特定日期後
我有一個應用程序,其中個人創建evaluations
,如果最低要求在created_at
日期後7天未滿足,我希望每3天發送一次提醒電子郵件,以提醒他們採取行動。
的reminders.rb
文件看起來像這樣,與Evaluation::ASSESSMENTS_COMPLETION_WAIT_TIME
設置爲7.days
和Reminders::LIMBO_EMAIL_INTERVAL_DAYS
設置爲3
:
def self.send_peer_shortage_notifications
time = Time.current - Evaluation::ASSESSMENTS_COMPLETION_WAIT_TIME
range = time..Time.current
today = Time.current.to_date
evaluations = Evaluation.arel_table
assessments = Assessment.arel_table
left_join = evaluations
.join(assessments, Arel::Nodes::OuterJoin)
.on(evaluations[:id].eq(assessments[:evaluation_id]),
assessments[:state].in([:pending, :complete]),
assessments[:assessor_id].not_in([evaluations[:user_id],
evaluations[:manager_id]]))
.join_sources
relation = Evaluation
.in_process
.joins(left_join)
.where(created_at: range)
.group(:user_id)
.having(evaluations[:user_id].count.lt(Evaluation::MINIMUM_NUM_PEERS))
relation.find_each do |evaluation|
days_in_limbo = (today - (evaluation.created_at + Evaluation::ASSESSMENTS_COMPLETION_WAIT_TIME).to_date).to_i
if days_in_limbo % Reminders::LIMBO_EMAIL_INTERVAL_DAYS == 0
EvaluationMailer.delay.limbo_notification(evaluation)
end
end
end
我reminders_rspec.rb
看起來像這樣(第一次測試失敗,我想不通爲什麼):
context 'minimum number of peer assessments not in pending/complete and limbo email interval day' do
limbo_interval_array = Array.new(10) { |i| i*Reminders::LIMBO_EMAIL_INTERVAL_DAYS }
let!(:evaluation) { create(:evaluation, created_at: (Evaluation::ASSESSMENTS_COMPLETION_WAIT_TIME + limbo_interval_array.sample.days).ago) }
let!(:assessments) do
create_list(:assessment,
Evaluation::MINIMUM_NUM_PEERS,
evaluation: evaluation,
state: [:expired, :declined].sample)
end
it 'sends limbo email' do
expect { subject }.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end
context 'on every non-third day since limbo' do
array = (1..20).to_a
limbo_interval_array = Array.new(10) { |i| i*Reminders::LIMBO_EMAIL_INTERVAL_DAYS }
non_limbo_interval_array = array - limbo_interval_array
let!(:evaluation) { create(:evaluation, created_at: (Evaluation::ASSESSMENTS_COMPLETION_WAIT_TIME + non_limbo_interval_array.sample.days).ago) }
let!(:assessments) do
create_list(:assessment,
Evaluation::MINIMUM_NUM_PEERS,
evaluation: evaluation,
state: [:expired, :declined].sample)
end
it 'sends nothing' do
expect { subject }.to change { ActionMailer::Base.deliveries.count }.by(0)
end
end
是否有更簡單的方法來編寫和測試它?這似乎爲我「米試圖做的過於複雜,但我一直沒能找到一個更簡單的方法。
我看到很多SH *** y個第一的職位。這個似乎很好。 +1爲你 – m02ph3u5