2016-04-04 70 views
1

這是我的第一篇文章,以便提前道歉任何新手的錯誤。我已經嘗試過研究這個問題的不同解決方案,並且迄今還沒有遇到似乎適合我的特殊情況的解決方案。發送提醒郵件,每三天特定日期後

我有一個應用程序,其中個人創建evaluations,如果最低要求在created_at日期後7天未滿足,我希望每3天發送一次提醒電子郵件,以提醒他們採取行動。

reminders.rb文件看起來像這樣,與Evaluation::ASSESSMENTS_COMPLETION_WAIT_TIME設置爲7.daysReminders::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 

是否有更簡單的方法來編寫和測試它?這似乎爲我「米試圖做的過於複雜,但我一直沒能找到一個更簡單的方法。

+0

我看到很多SH *** y個第一的職位。這個似乎很好。 +1爲你 – m02ph3u5

回答

0

事實上,看來你是做這方面比它需要更加複雜。這裏的關鍵是,你要使用排隊後端異步分派電子郵件通知由於提醒要創建評估後7天內發出,延遲的電子郵件將被排隊的EvaluationsController#create行動:

class EvaluationsController < ApplicationController 

def create 
    # Do whatever it is you do to create an evaluation 

    if @evaluation.valid? 
    EvaluationMailer.delay_for(7.days).limbo_notification(evaluation.id) # Pass an ID rather than a model object, and use `find_by` within `limbo_notification` 
    # redirect or whatever... 
    end 
end 

現在,所有的硬工作將通過您的排隊後端ActiveJob這將在7天后自動發送電子郵件給你做。

排隊後端是一個大課題而非細說他們是如何工作的,我會指出你的文檔在這裏:http://edgeguides.rubyonrails.org/active_job_basics.html。對於特定的排隊後端,我建議使用Redis的Sidekiq。

+0

謝謝,很好的信息! – jnapolitan