contactorama/app/controllers/birthdays_controller.rb
2012-01-15 21:28:46 +01:00

48 lines
1.7 KiB
Ruby

# -*- coding: utf-8 -*-
class BirthdaysController < ApplicationController
before_filter :authenticate_user!, :except => [:do_mailing]
def index
@birthdays = get_next_weeks_birthdays
end
# curl --verbose --header "Content-type: application/html" --request POST --data "" http://contactorama.dev/birthdays/do_mailing
# wget --post-data '' http://contact-o-rama.de/birthdays/do_mailing
# crontab:
# 30 3 * * * wget --post-data '' http://contact-o-rama.de/birthdays/do_mailing
def do_mailing
birthdays = get_next_weeks_birthdays
logger.debug "birthdays.nil? = " + birthdays.nil?.to_s + "......"
unless birthdays.nil?
logger.debug "..... birthdays: " + birthdays.count.to_s + "......"
birthdays.each do |bday|
logger.info "............< do_mailing: " + bday.firstname + " " + bday.lastname + " >.............."
UserMailer.next_weeks_birthday_notification(birthdays).deliver
end
end
logger.debug "........... Mailings done (or had none to do)"
respond_to do |format|
logger.debug ".... rendering...."
format.html { render :layout => false }
logger.debug ".... rendered ...."
end
end
private
def get_next_weeks_birthdays
n = Date.today
e = Date.today + 7.days
# two cases here: now + 7days iis 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)
else
birthdays = Contact.where("(birth_month = ? AND birth_day <= ?) OR (birth_month = ? AND birth_day >= ?)",
e.month, e.day, n.month, n.day)
end
birthdays
end
end