开发者

Recommendations on proper refactoring in a When statement?

I'm trying to call two lengthy commands in a when statement, but for some reason, because of its syntax, it performs two of the commands twice when it is called :

  @email = Email.find(params[:id])
  delivery = case @email.mail_type
    # when "magic_email" these two delayed_jobs perform 2x instead of 1x. Why is that?
    when "magic_email"      then Delayed::Job.enqueue MagicEmail.new(@email.subject, @email.body)
                                 Delayed::Job.enqueue ReferredEmail.new(@email.subject, @email.body)
    when "org_magic_email"  then Delayed::Job.enqueue OrgMagicEmail.new(@email.subject, @email.body)
    when "all_orgs"         then Delayed::Job.enqueue OrgBlast.new(@email.subject, @email.body)
    when "all_card_holders" then Delayed::Job.enqueue MassEmail.new(@email.subject, @email.body)
  end
  return delivery

How can I make 开发者_运维知识库it so that when I hit when "magic_email", it only renders both those delayed jobs once ?


I have tried this with following example:


  q = []
   a = case 1
   when 1 then  q.push 'ashish'
                q.push 'kumar'
   when 2 then  q.push 'test'
   when 4 then  q.push 'another test'
   end
   puts a.inspect #######["ashish", "kumar"]


This is working fine. It means your case-when syntax is ok. It might be you have aome other problem.


You are calling return delivery and delivery varible may be having the value to call the delayed job again. It depends on what the then statement returns, so try not to return anything if possible. I believe you want to do the delayed job and not return anything by using the function.

Perhaps you should just have the case and dont store it in any variable. I mean delivery variable has no purpose here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜