views:

68

answers:

4

I have a message substitution called next_week which basically takes Date.today + 7.days.

However, although I still want to send emails on weekends, if the next_week falls on a weekend, I want it to know this and push to the Monday.

How do i do this?

+1  A: 
mail_date = Date.today + 7.days
if mail_date.wday == 0
  mail_date += 1.day
elsif mail_date.wday == 6
  mail_date += 2.days
end

# now send your email on mail_date

Is this helpful?

DJTripleThreat
+5  A: 

Like this:

sunday = 0
saturday = 6
weekend = [saturday, sunday]

mail_date += 1.days while weekend.include?(mail_date.wday)
Magnar
Hi, I think I get it, let me try it....
Angela
I get a nil outcome....?
Angela
seems to work, thanks!
Angela
i also +1 for clarity
Angela
A: 

You can use Action Mailer Queue. Your mails are added to a queue and whenever you call ActionMailer Queue's method, the emails will be sent. So, basically you can call that method every weekday. On weekends, your emails will be added to the queue but won't be sent. On monday when you make the call to the method, your mails will be sent. Of course you can schedule your Action mailer method calls , to be called automatically every week day using a rake task or Rufus Scheduler.

Shreyas Satish
A: 

You can Use this ,

def weekday?
(1..5).include?(wday)
end

check ..

d = Date.today
=> Mon, 04 Oct 2010
d.weekday?
=> true
d = Date.today - 1
=> Sun, 03 Oct 2010
d.weekday?
=> false

Srikanth