Refactoring of message system.

This commit is contained in:
Benjamin Meichsner 2009-02-18 01:06:35 +01:00
parent a0e10141dc
commit 3ea8d5a2ef
21 changed files with 94 additions and 113 deletions

View file

@ -1,50 +1,67 @@
# ActionMailer class that handles all emails for the FoodSoft.
class Mailer < ActionMailer::Base
# Sends an email with instructions on how to reset the password.
# Assumes user.setResetPasswordToken has been successfully called already.
def password(user)
request = ApplicationController.current.request
subject "[#{APP_CONFIG[:name]}] Neues Passwort für/ New password for " + user.nick
recipients user.email
from "FoodSoft <#{APP_CONFIG[:email_sender]}>"
body :user => user,
:link => url_for(:host => request.host, :controller => "login", :action => "password", :id => user.id, :token => user.reset_password_token),
:foodsoftUrl => url_for(:host => request.host, :controller => "index")
end
layout 'email' # Use views/layouts/email.html.erb
# Sends an email copy of the given internal foodsoft message.
def message(message)
request = ApplicationController.current.request
subject "[#{APP_CONFIG[:name]}] " + message.subject
recipients message.recipient.email
from (message.system_message? ? "FoodSoft <#{APP_CONFIG[:email_sender]}>" : "#{message.sender.nick} <#{message.sender.email}>")
body :body => message.body, :sender => (message.system_message? ? 'Foodsoft' : message.sender.nick),
:recipients => message.recipients,
:reply => url_for(:host => request.host, :controller => "messages", :action => "reply", :id => message),
:profile => url_for(:host => request.host, :controller => "home", :action => "profile"),
:link => url_for(:host => request.host, :controller => "messages", :action => "show", :id => message),
:foodsoftUrl => url_for(:host => request.host, :controller => "/")
from "#{message.sender.nick} <#{message.sender.email}>"
body :body => message.body,
:sender => message.sender.nick,
:recipients => message.recipients,
:reply => "#{APP_CONFIG[:base_url]}/messages/reply/#{message}",
:profile => "#{APP_CONFIG[:base_url]}/home/profile",
:link => "#{APP_CONFIG[:base_url]}/messages/show/#{message}"
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 "[#{APP_CONFIG[:name]}] Neues Passwort für/ New password for #{user.nick}"
body :user => user,
:link => "#{APP_CONFIG[:base_url]}/login/password/#{user.id}?token=#{user.reset_password_token}"
end
# Sends an invite email.
def invite(invite)
request = ApplicationController.current.request
prepare_system_message(invite)
subject "Einladung in die Foodcoop #{APP_CONFIG[:name]} - Invitation to the Foodcoop"
recipients invite.email
from "FoodSoft <#{APP_CONFIG[:email_sender]}>"
body :invite => invite,
:link => url_for(:host => request.host, :controller => "login", :action => "invite", :id => invite.token),
:foodsoftUrl => url_for(:host => request.host, :controller => "index")
:link => "#{APP_CONFIG[:base_url]}/login/invite/#{invite.token}"
end
# Notify user of upcoming task.
def notify_upcoming_tasks(user, task)
def upcoming_tasks(user, task)
prepare_system_message(user)
subject "[#{APP_CONFIG[:name]}] Aufgaben werden fällig!"
recipients user.email
from "FoodSoft <#{APP_CONFIG[:email_sender]}>"
body :user => user, :task => task
body :user => user,
:task => task
end
# Sends order result for specific Ordergroup
def order_result(user, group_order)
prepare_system_message(user)
subject "[#{APP_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 "[#{APP_CONFIG[:name]}] Gruppenkonto im Minus"
body :group => user.ordergroup,
:transaction => transaction
end
protected
def prepare_system_message(recipient)
recipients recipient.email
from "FoodSoft <#{APP_CONFIG[:email_sender]}>"
end
end

View file

@ -21,7 +21,6 @@ class Message < ActiveRecord::Base
named_scope :pending, :conditions => { :email_state => 0 }
named_scope :sent, :conditions => { :email_state => 1 }
named_scope :user, :conditions => "sender_id IS NOT NULL", :order => 'created_at DESC', :include => :sender
# Values for the email_state attribute: :none, :pending, :sent, :failed
EMAIL_STATE = {
@ -85,21 +84,4 @@ class Message < ActiveRecord::Base
message.update_attribute(:email_state, 1)
end
end
# Returns a new message object created from the attributes specified (recipient, recipients, subject)
# and the body from the given template that can make use of the variables specified.
# The templates are to be stored in app/views/messages, i.e. the template name
# "order_finished" would invoke template file "app/views/messages/order_finished.rhtml".
# Note: you need to set the sender afterwards if this should not be a system message.
#
# Example:
# Message.from_template(
# 'order_finished',
# {:user => user, :group => ordergroup, :order => self, :results => results, :total => group_order.price},
# {:recipient_id => user.id, :recipients => recipients, :subject => "Bestellung beendet: #{self.name}"}
# ).save!
def self.from_template(template, vars, attributes)
view = ActionView::Base.new(Rails::Configuration.new.view_path, {}, MessagesController.new)
new(attributes.merge(:body => view.render(:file => "messages/#{template}.rhtml", :locals => vars)))
end
end

View file

@ -232,17 +232,8 @@ class Order < ActiveRecord::Base
# Sends "order finished" messages to users who have participated in this order.
def notify_order_finished
for group_order in self.group_orders
ordergroup = group_order.ordergroup
logger.debug("Send 'order finished' message to #{ordergroup.name}")
# Determine users that want a notification message:
users = ordergroup.users.reject{|u| u.settings["notify.orderFinished"] != '1'}
unless users.empty?
# Create user notification messages:
Message.from_template(
'order_finished',
{:group => ordergroup, :order => self, :group_order => group_order},
{:recipients_ids => users.collect(&:id), :subject => "Bestellung beendet: #{supplier.name}"}
).save!
for user in group_order.ordergroup.users
Mailer.deliver_order_result(user, group_order) if user.settings["notify.orderFinished"] == '1'
end
end
end

View file

@ -82,14 +82,9 @@ class Ordergroup < Group
# a message is sent to all users who have enabled notification.
def notify_negative_balance(transaction)
# 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)
users = self.users.reject { |u| u.settings["notify.negativeBalance"] != '1' }
unless users.empty?
Message.from_template(
'negative_balance',
{:group => self, :transaction => transaction},
{:recipients_ids => users.collect(&:id), :subject => "Gruppenkonto im Minus"}
).save!
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'
end
end
end