From cf32952cd1f761b0e116f551a6040371a3ff0c48 Mon Sep 17 00:00:00 2001 From: Roland Jesse Date: Thu, 19 Jul 2012 22:49:36 +0200 Subject: [PATCH] FogBugz #135: add controller methods in preparation of sending birthday mails only in 7 days + 2 days in advance. --- app/controllers/birthdays_controller.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/controllers/birthdays_controller.rb b/app/controllers/birthdays_controller.rb index d67a7a9..fb9be8d 100644 --- a/app/controllers/birthdays_controller.rb +++ b/app/controllers/birthdays_controller.rb @@ -3,7 +3,7 @@ class BirthdaysController < ApplicationController before_filter :authenticate_user!, :except => [:do_mailing] def index - @birthdays = get_next_weeks_birthdays + @birthdays = get_birthdays_next_7d @nav_active = "birthdays" end @@ -12,7 +12,7 @@ class BirthdaysController < ApplicationController # crontab: # 30 3 * * * wget --post-data '' http://contact-o-rama.de/birthdays/do_mailing def do_mailing - birthdays = get_next_weeks_birthdays + birthdays = get_birthdays_next_7d logger.debug "birthdays.nil? = " + birthdays.nil?.to_s + "......" unless birthdays.nil? birthdays_by_email = group_birthdays_by_email(birthdays) @@ -37,11 +37,19 @@ class BirthdaysController < ApplicationController private - def get_next_weeks_birthdays + def get_birthdays_next_7d + get_birthdays_next_n_days(7) + end + + def get_birthdays_next_2d + get_birthdays_next_n_days(2) + end + + def get_birthdays_next_n_days(num_days) n = Date.today - e = Date.today + 7.days + e = Date.today + num_days.days - # two cases here: now + 7days is still the same month. or it's not. + # two cases here: now + x days is still the same month. or it's not. if n.month == e.month birthdays = Contact.where("birth_month = ? AND birth_day <= ? AND birth_day >= ?", e.month, e.day, n.day) @@ -49,7 +57,7 @@ class BirthdaysController < ApplicationController birthdays = Contact.where("(birth_month = ? AND birth_day <= ?) OR (birth_month = ? AND birth_day >= ?)", e.month, e.day, n.month, n.day) end - birthdays + birthdays end # in: Array [contact,contact,contact,...]