Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: Check if today is sunday, 15th of month or last day of month

A sidekiq worker sending PDF attachment in emails to clients. The worker runs daily checking if its Sunday then sends weekly report, if today is 15th of month then Bi-monthly report and if last day of month then monthly report. The condition looks something like this:

if sunday?
  # send weekly
elsif 15th of month
  # send Bi-monthly
elsif last_day_of_month
  # send Monthly
end

How to check if its sunday, 15th of month and last day of month ?

like image 590
Arif Avatar asked Sep 19 '16 14:09

Arif


1 Answers

Rails has many interesting methods for this kind of date calculations:

today = Date.today

if today.sunday?
  # send weekly
elsif today.day == 15
  # send bi-monthly
elsif today == today.end_of_month
  # send monthly
end
like image 113
spickermann Avatar answered Oct 20 '22 16:10

spickermann