8

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?

Satchel
  • 16,414
  • 23
  • 106
  • 192

6 Answers6

18

Rails 5:

date.on_weekend?
date.on_weekday?

Rails 4:

date.saturday? || date.sunday?
Marcin Urbanski
  • 2,493
  • 1
  • 16
  • 17
  • I like these! it looks like the saturday? and any *weekday*? method is implemented in Ruby 1.9+, maybe earlier, on the Time and Date classes. Man, that would have been useful to know a couple of years ago :) Thanks @marcin – alassiter Aug 24 '16 at 12:59
  • Oh I assume this is in active support? I am not using rails. – Satchel Jul 28 '17 at 20:25
13

Like this:

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

mail_date += 1.days while weekend.include?(mail_date.wday)
Magnar
  • 28,550
  • 8
  • 60
  • 65
2

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 Jeeva
  • 3,005
  • 4
  • 38
  • 58
2

Generally, use the business_time gem (https://github.com/bokmann/business_time), which will solve this issue in a complete way. This library will allow you to adapt for different work weeks (Sundays to Thursdays for instance) and even check if the hour is out of working hours.

Your case would be

def next_week
  0.business_days.after(7.days.from_now)
end
rewritten
  • 16,280
  • 2
  • 47
  • 50
1
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?

aarona
  • 35,986
  • 41
  • 138
  • 186
0

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
  • 8,737
  • 7
  • 44
  • 56