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

@ -8,7 +8,7 @@ class HomeController < ApplicationController
@unaccepted_tasks = @current_user.unaccepted_tasks
# task in next week
@next_tasks = @current_user.next_tasks
@messages = Message.user.find :all, :limit => 5
@messages = Message.all :order => 'created_at DESC', :limit => 5
# count tasks with no responsible person
# tasks for groups the current user is not a member are ignored
tasks = Task.find(:all, :conditions => ["assigned = ? and done = ?", false, false])

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_password(user)
email = Mailer.deliver_reset_password(user)
logger.debug("Sent password reset email to #{user.email}.")
end
end

View file

@ -2,7 +2,7 @@ class MessagesController < ApplicationController
# Renders the "inbox" action.
def index
@messages = Message.user
@messages = Message.all :order => 'created_at DESC', :limit => 100
end
# Creates a new message object.
@ -21,7 +21,6 @@ class MessagesController < ApplicationController
end
end
# Shows a single message.
def show
@message = Message.find(params[:id])

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),
from "#{message.sender.nick} <#{message.sender.email}>"
body :body => message.body,
:sender => 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 => "/")
: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

@ -83,13 +83,8 @@ class Ordergroup < Group
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!
for user in users
Mailer.deliver_negative_balance(user,transaction) if user.settings["notify.negativeBalance"] == '1'
end
end
end

View file

@ -0,0 +1,6 @@
<%= yield %>
--
FoodSoft: <%= @foodsoftUrl %>
Foodcoop-Homepage: <%= APP_CONFIG[:base_url] %>
Hilfe/Help: <%= APP_CONFIG[:help_url] %>

View file

@ -12,4 +12,4 @@
= yield
#meta
Foodcoop
= link_to_if APP_CONFIG[:base_url], APP_CONFIG[:name], APP_CONFIG[:base_url]
= link_to_if APP_CONFIG[:homepage], APP_CONFIG[:name], APP_CONFIG[:homepage]

View file

@ -15,5 +15,3 @@ To accept the invitation and to join the foodcoop please follow this link: <%= @
This link works only once and expires on <%= @invite.expires_at.strftime('%A den %d.%m.%Y um %H:%M') %>.
Greetings, your FoodSoft Team!
<%= render :partial => 'shared/mail_footer' %>

View file

@ -10,5 +10,3 @@ An: <%= @recipients %>
Antworten: <%= @reply %>
Nachricht online einsehen: <%= @link %>
Nachrichten-Einstellungen: <%= @profile %>
<%= render :partial => 'shared/mail_footer' %>

View file

@ -0,0 +1,9 @@
Liebe <%= @group.name %>,
euer Kontostand ist durch eine Buchung am <%= @transaction.created_on.strftime('%d.%m.%Y um %H:%M') %> ins Minus gerutscht: <%= @group.account_balance %>
Es wurden <%= @transaction.amount %> für "<%= @transaction.note %>" abgebucht, die Buchung wurde von <%= @transaction.user.nick %> erstellt.
Bitte zahlt so bald wie möglich wieder Geld ein, um das Gruppenkonto auszugleichen.
Viele Grüße von <%= APP_CONFIG[:name] %>

View file

@ -0,0 +1,14 @@
Liebe <%= @group_order.group.name %>,
die Bestellung für "<%= @order.name %>" wurde am <%= @order.ends.strftime('%d.%m.%Y um %H:%M') %> von <%= @order.updated_by.nick %> beendet.
Für Euch wurden die folgenden Artikel bestellt:
<% for group_order_article in @group_order.group_order_articles.all(:include => :order_article)
article = group_order_article.order_article.article -%>
<%= article.name %>: <%= group_order_article.quantity %> x <%= article.unit %> = <%= group_order_article.quantity * article.fc_price %>
<% end -%>
Gesamtpreis: <%= @group_order.price %>
Bestellung online einsehen: <%= "#{APP_CONFIG[:base_url]}/ordering/my_order_result/#{@order.id}" %>
Viele Grüße von <%= APP_CONFIG[:name] %>

View file

@ -16,5 +16,3 @@ This link works only once and expires on <%= @user.reset_password_expires.strfti
If you don't want to change your password, just ignore this message. Your password hasn't been changed yet.
Greetings, your FoodSoft Team!
<%= render :partial => 'shared/mail_footer' %>

View file

@ -11,7 +11,6 @@ Aufgaben für die nächste Woche:
<% end -%>
<% end -%>
Viele Grüße von <%= APP_CONFIG[:name] %>
--
Meine Aufgaben: <%= APP_CONFIG[:base_url] %>/home/tasks
Viele Grüße von <%= APP_CONFIG[:name] %>

View file

@ -1,9 +0,0 @@
Liebe <%= group.name %>,
euer Kontostand ist durch eine Buchung am <%= transaction.created_on.strftime('%d.%m.%Y um %H:%M') %> ins Minus gerutscht: <%= group.account_balance %>
Es wurden <%= transaction.amount %> für "<%= transaction.note %>" abgebucht, die Buchung wurde von <%= transaction.user.nick %> erstellt.
Bitte zahlt so bald wie möglich wieder Geld ein, um das Gruppenkonto auszugleichen.
Viele Grüße von <%= APP_CONFIG[:name] %>

View file

@ -1,14 +0,0 @@
Liebe <%= group.name %>,
die Bestellung für "<%= order.name %>" wurde am <%= order.ends.strftime('%d.%m.%Y um %H:%M') %> von <%= order.updated_by.nick %> beendet.
Für Euch wurden die folgenden Artikel bestellt:
<% for group_order_article in group_order.group_order_articles.all(:include => :order_article)
article = group_order_article.order_article.article -%>
<%= article.name %>: <%= group_order_article.quantity %> x <%= article.unit %> = <%= group_order_article.quantity * article.fc_price %>
<% end -%>
Gesamtpreis: <%= group_order.price %>
Bestellung online einsehen: <%= ApplicationController.current.url_for(:controller => 'ordering', :action => 'my_order_result', :id => order.id) %>
Viele Grüße von <%= APP_CONFIG[:name] %>

View file

@ -2,7 +2,7 @@
%li
= image_tag 'b_user.png' , :size => '7x10', :border => 0, :alt => _("User")
= link_to h(@current_user.nick), my_profile_path, { :title => _("User Settings") }
- if APP_CONFIG[:base_url]
%li= link_to APP_CONFIG[:name], APP_CONFIG[:base_url], { :title => _("Go to your FoodCoop-Hompage") }
- if APP_CONFIG[:homepage]
%li= link_to APP_CONFIG[:name], APP_CONFIG[:homepage], { :title => _("Go to your FoodCoop-Hompage") }
%li= link_to _("Help"), 'http://dev.foodcoops.net/wiki/FoodsoftDoku'
%li= link_to _("Logout"), :controller => '/login', :action => 'logout'

View file

@ -1,4 +0,0 @@
--
FoodSoft: <%= @foodsoftUrl %>
Foodcoop-Homepage: <%= APP_CONFIG[:base_url] %>
Hilfe/Help: <%= APP_CONFIG[:help_url] %>

View file

@ -12,7 +12,9 @@ development: &defaults
email: foodsoft@myfoodcoop.org
phone: "030 323 23249"
# base URL for this installation
base_url: http://www.fctest.de
base_url: http://foodsoft.fctest.de
# Homepage
homepage: http://www.fctest.de
# foodsoft documentation URL
help_url: http://foodsoft.fcschinke09.de/trac/wiki/FoodsoftDoku
# price markup in percent

View file

@ -33,7 +33,7 @@ namespace :foodsoft do
for user in task.users
if user.settings['notify.upcoming_tasks'] == 1
puts "#{user.email}.."
Mailer.deliver_notify_upcoming_tasks(user,task)
Mailer.deliver_upcoming_tasks(user, task)
end
end
end