Updated actionmailer classes and API.

This commit is contained in:
benni 2011-05-11 10:53:18 +02:00
parent 8679356ccf
commit 2d7d0d3b94
8 changed files with 98 additions and 92 deletions

View File

@ -9,7 +9,7 @@ class FeedbackController < ApplicationController
def create
unless params[:message].blank?
Mailer.deliver_feedback(current_user, params[:message])
Mailer.feedback(current_user, params[:message]).deliver
end
render :update do |page|

View File

@ -53,7 +53,7 @@ class LoginController < ApplicationController
user.reset_password_token = user.new_random_password(16)
user.reset_password_expires = Time.now.advance(:days => 2)
if user.save
email = Mailer.deliver_reset_password(user)
email = Mailer.reset_password(user).deliver
logger.debug("Sent password reset email to #{user.email}.")
end
end

90
app/mailers/mailer.rb Normal file
View File

@ -0,0 +1,90 @@
# ActionMailer class that handles all emails for the FoodSoft.
class Mailer < ActionMailer::Base
layout 'email' # Use views/layouts/email.html.erb
default :from => "FoodSoft <#{Foodsoft.config[:email_sender]}>"
# Sends an email copy of the given internal foodsoft message.
def message(message, recipient)
@body = message.body
@sender = message.sender.nick
@recipients = recipient.nick
@reply = url_for(:controller => "messages", :action => "reply", :id => message.id)
@link = url_for(:controller => "messages", :action => "show", :id => message.id)
@profile = url_for(:controller => "home", :action => "profile")
mail :sender => Foodsoft.config[:email_sender],
:errors_to => Foodsoft.config[:email_sender],
:subject => "[#{Foodsoft.config[:name]}] " + message.subject,
:to => recipient.email,
:from => "#{message.sender.nick} <#{message.sender.email}>"
end
# Sends an email with instructions on how to reset the password.
# Assumes user.setResetPasswordToken has been successfully called already.
def reset_password(user)
@user = user
@link = url_for(:controller => "login", :action => "password", :id => user.id, :token => user.reset_password_token)
mail :to => user.email,
:subject => "[#{Foodsoft.config[:name]}] Neues Passwort für/ New password for #{user.nick}"
end
# Sends an invite email.
def invite(invite)
@invite = invite
@link = url_for(:controller => "login", :action => "invite", :id => invite.token)
mail :to => invite.email,
:subject => "Einladung in die Foodcoop #{Foodsoft.config[:name]} - Invitation to the Foodcoop"
end
# Notify user of upcoming task.
def upcoming_tasks(user, task)
@user = user
@task = task
mail :to => user.email,
:subject => "[#{Foodsoft.config[:name]}] Aufgaben werden fällig!"
end
# Sends order result for specific Ordergroup
def order_result(user, group_order)
@order = group_order.order
@group_order = group_order
mail :to => user.email,
:subject => "[#{Foodsoft.config[:name]}] Bestellung beendet: #{group_order.order.name}"
end
# Notify user if account balance is less than zero
def negative_balance(user,transaction)
@group = user.ordergroup
@transaction = transaction
mail :to => user.email,
:subject => "[#{Foodsoft.config[:name]}] Gruppenkonto im Minus"
end
def feedback(user, message)
@user = user
@message = message
mail :to => Foodsoft.config[:notification]["error_recipients"],
:from => "#{user.nick} <#{user.email}>",
:sender => Foodsoft.config[:notification]["sender_address"],
:errors_to => Foodsoft.config[:notification]["sender_address"],
:subject => "[Foodsoft] Feeback von #{user.email}"
end
def not_enough_users_assigned(task,user)
@task = task
@user = user
@task_url = url_for(:controller => "tasks", :action => "workgroup", :id => task.workgroup_id)
mail :to => user.email,
:subject => "[#{Foodsoft.config[:name]}] #{task.name} braucht noch Leute!"
end
end

View File

@ -21,7 +21,7 @@ class Invite < ActiveRecord::Base
# Sends an email to the invited user.
def after_create
Mailer.deliver_invite(self)
Mailer.invite(self).deliver
end
private

View File

@ -1,84 +0,0 @@
# ActionMailer class that handles all emails for the FoodSoft.
class Mailer < ActionMailer::Base
layout 'email' # Use views/layouts/email.html.erb
# Sends an email copy of the given internal foodsoft message.
def message(message, recipient)
headers 'Sender' => Foodsoft.config[:email_sender], 'Errors-To' => Foodsoft.config[:email_sender]
subject "[#{Foodsoft.config[:name]}] " + message.subject
recipients recipient.email
from "#{message.sender.nick} <#{message.sender.email}>"
body :body => message.body,
:sender => message.sender.nick,
:recipients => recipient.nick,
:reply => url_for(:controller => "messages", :action => "reply", :id => message.id),
:link => url_for(:controller => "messages", :action => "show", :id => message.id),
:profile => url_for(:controller => "home", :action => "profile")
end
# Sends an email with instructions on how to reset the password.
# Assumes user.setResetPasswordToken has been successfully called already.
def reset_password(user)
prepare_system_message(user)
subject "[#{Foodsoft.config[:name]}] Neues Passwort für/ New password for #{user.nick}"
body :user => user,
:link => url_for(:controller => "login", :action => "password", :id => user.id, :token => user.reset_password_token)
end
# Sends an invite email.
def invite(invite)
prepare_system_message(invite)
subject "Einladung in die Foodcoop #{Foodsoft.config[:name]} - Invitation to the Foodcoop"
body :invite => invite,
:link => url_for(:controller => "login", :action => "invite", :id => invite.token)
end
# Notify user of upcoming task.
def upcoming_tasks(user, task)
prepare_system_message(user)
subject "[#{Foodsoft.config[:name]}] Aufgaben werden fällig!"
body :user => user,
:task => task
end
# Sends order result for specific Ordergroup
def order_result(user, group_order)
prepare_system_message(user)
subject "[#{Foodsoft.config[:name]}] Bestellung beendet: #{group_order.order.name}"
body :order => group_order.order,
:group_order => group_order
end
# Notify user if account balance is less than zero
def negative_balance(user,transaction)
prepare_system_message(user)
subject "[#{Foodsoft.config[:name]}] Gruppenkonto im Minus"
body :group => user.ordergroup,
:transaction => transaction
end
def feedback(user, message)
subject "[Foodsoft] Feeback von #{user.email}"
recipients Foodsoft.config[:notification]["error_recipients"]
from "#{user.nick} <#{user.email}>"
headers 'Sender' => Foodsoft.config[:notification]["sender_address"],
'Errors-To' => Foodsoft.config[:notification]["sender_address"]
body :user => user, :message => message
end
def not_enough_users_assigned(task,user)
prepare_system_message(user)
subject "[#{Foodsoft.config[:name]}] #{task.name} braucht noch Leute!"
body :task => task, :user => user,
:task_url => url_for(:controller => "tasks", :action => "workgroup", :id => task.workgroup_id)
end
protected
def prepare_system_message(recipient)
recipients recipient.email
from "FoodSoft <#{Foodsoft.config[:email_sender]}>"
end
end

View File

@ -65,7 +65,7 @@ class Message < ActiveRecord::Base
for recipient in message.recipients
if recipient.settings['messages.sendAsEmail'] == "1" && !recipient.email.blank?
begin
Mailer.deliver_message(message, recipient)
Mailer.message(message, recipient).deliver
rescue
logger.warn "Deliver failed for #{recipient.nick}: #{recipient.email}"
end

View File

@ -76,7 +76,7 @@ class Ordergroup < Group
# Notify only when order group had a positive balance before the last transaction:
if (transaction.amount < 0 && self.account_balance < 0 && self.account_balance - transaction.amount >= 0)
for user in users
Mailer.deliver_negative_balance(user,transaction) if user.settings["notify.negativeBalance"] == '1'
Mailer.negative_balance(user,transaction).deliver if user.settings["notify.negativeBalance"] == '1'
end
end
end

View File

@ -11,7 +11,7 @@ namespace :foodsoft do
if user.settings['notify.upcoming_tasks'] == 1
begin
puts "#{user.email}.."
Mailer.deliver_upcoming_tasks(user, task)
Mailer.upcoming_tasks(user, task).deliver
rescue
puts "deliver aborted for #{user.email}.."
end
@ -42,7 +42,7 @@ namespace :foodsoft do
for user in workgroup.users
if user.settings['messages.sendAsEmail'] == "1" && !user.email.blank?
begin
Mailer.deliver_not_enough_users_assigned(task, user)
Mailer.not_enough_users_assigned(task, user).deliver
rescue
puts "deliver aborted for #{user.email}"
end
@ -72,7 +72,7 @@ namespace :foodsoft do
for group_order in order.group_orders
for user in group_order.ordergroup.users
begin
Mailer.deliver_order_result(user, group_order) if user.settings["notify.orderFinished"] == '1'
Mailer.order_result(user, group_order).deliver if user.settings["notify.orderFinished"] == '1'
rescue
puts "deliver aborted for #{user.email}.."
end