From 3ea8d5a2ef3806ede019c3941b469267fd2f40cb Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Wed, 18 Feb 2009 01:06:35 +0100 Subject: [PATCH] Refactoring of message system. --- app/controllers/home_controller.rb | 2 +- app/controllers/login_controller.rb | 2 +- app/controllers/messages_controller.rb | 3 +- app/models/mailer.rb | 77 +++++++++++-------- app/models/message.rb | 18 ----- app/models/order.rb | 13 +--- app/models/ordergroup.rb | 11 +-- app/views/layouts/email.html.erb | 6 ++ app/views/layouts/login.haml | 2 +- .../mailer/{invite.rhtml => invite.html.erb} | 2 - app/views/mailer/message.rhtml | 4 +- app/views/mailer/negative_balance.html.erb | 9 +++ app/views/mailer/order_result.html.erb | 14 ++++ ...password.rhtml => reset_password.html.erb} | 2 - ...tasks.html.erb => upcoming_tasks.html.erb} | 5 +- app/views/messages/negative_balance.rhtml | 9 --- app/views/messages/order_finished.rhtml | 14 ---- app/views/shared/_loginInfo.haml | 4 +- app/views/shared/_mail_footer.rhtml | 4 - config/app_config.yml.SAMPLE | 4 +- lib/tasks/foodsoft.rake | 2 +- 21 files changed, 94 insertions(+), 113 deletions(-) create mode 100644 app/views/layouts/email.html.erb rename app/views/mailer/{invite.rhtml => invite.html.erb} (94%) create mode 100644 app/views/mailer/negative_balance.html.erb create mode 100644 app/views/mailer/order_result.html.erb rename app/views/mailer/{password.rhtml => reset_password.html.erb} (94%) rename app/views/mailer/{notify_upcoming_tasks.html.erb => upcoming_tasks.html.erb} (80%) delete mode 100644 app/views/messages/negative_balance.rhtml delete mode 100644 app/views/messages/order_finished.rhtml delete mode 100644 app/views/shared/_mail_footer.rhtml diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 580d40de..9ed5fca1 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -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]) diff --git a/app/controllers/login_controller.rb b/app/controllers/login_controller.rb index 6d47d4fe..2db550d4 100644 --- a/app/controllers/login_controller.rb +++ b/app/controllers/login_controller.rb @@ -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 diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index ca3f1039..ddb32bd2 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -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]) diff --git a/app/models/mailer.rb b/app/models/mailer.rb index 0b4d4d56..d2cccd99 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -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 diff --git a/app/models/message.rb b/app/models/message.rb index 5e987028..4f2c0779 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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 diff --git a/app/models/order.rb b/app/models/order.rb index a41d2a5e..b3a8dee4 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -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 diff --git a/app/models/ordergroup.rb b/app/models/ordergroup.rb index f224a76a..5c6b1050 100644 --- a/app/models/ordergroup.rb +++ b/app/models/ordergroup.rb @@ -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 diff --git a/app/views/layouts/email.html.erb b/app/views/layouts/email.html.erb new file mode 100644 index 00000000..d789db3f --- /dev/null +++ b/app/views/layouts/email.html.erb @@ -0,0 +1,6 @@ +<%= yield %> + +-- +FoodSoft: <%= @foodsoftUrl %> +Foodcoop-Homepage: <%= APP_CONFIG[:base_url] %> +Hilfe/Help: <%= APP_CONFIG[:help_url] %> \ No newline at end of file diff --git a/app/views/layouts/login.haml b/app/views/layouts/login.haml index 87d7e15e..8bc89297 100644 --- a/app/views/layouts/login.haml +++ b/app/views/layouts/login.haml @@ -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] diff --git a/app/views/mailer/invite.rhtml b/app/views/mailer/invite.html.erb similarity index 94% rename from app/views/mailer/invite.rhtml rename to app/views/mailer/invite.html.erb index fa4158fd..25143e9d 100644 --- a/app/views/mailer/invite.rhtml +++ b/app/views/mailer/invite.html.erb @@ -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' %> diff --git a/app/views/mailer/message.rhtml b/app/views/mailer/message.rhtml index 352e5afc..4824c7f9 100644 --- a/app/views/mailer/message.rhtml +++ b/app/views/mailer/message.rhtml @@ -9,6 +9,4 @@ An: <%= @recipients %> Antworten: <%= @reply %> Nachricht online einsehen: <%= @link %> -Nachrichten-Einstellungen: <%= @profile %> - -<%= render :partial => 'shared/mail_footer' %> +Nachrichten-Einstellungen: <%= @profile %> diff --git a/app/views/mailer/negative_balance.html.erb b/app/views/mailer/negative_balance.html.erb new file mode 100644 index 00000000..4d6a405e --- /dev/null +++ b/app/views/mailer/negative_balance.html.erb @@ -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] %> \ No newline at end of file diff --git a/app/views/mailer/order_result.html.erb b/app/views/mailer/order_result.html.erb new file mode 100644 index 00000000..8ed2e57b --- /dev/null +++ b/app/views/mailer/order_result.html.erb @@ -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] %> \ No newline at end of file diff --git a/app/views/mailer/password.rhtml b/app/views/mailer/reset_password.html.erb similarity index 94% rename from app/views/mailer/password.rhtml rename to app/views/mailer/reset_password.html.erb index 0f6c2d4e..6f83c130 100644 --- a/app/views/mailer/password.rhtml +++ b/app/views/mailer/reset_password.html.erb @@ -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' %> diff --git a/app/views/mailer/notify_upcoming_tasks.html.erb b/app/views/mailer/upcoming_tasks.html.erb similarity index 80% rename from app/views/mailer/notify_upcoming_tasks.html.erb rename to app/views/mailer/upcoming_tasks.html.erb index 3fe53b37..793a1ee9 100644 --- a/app/views/mailer/notify_upcoming_tasks.html.erb +++ b/app/views/mailer/upcoming_tasks.html.erb @@ -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 --- -Meine Aufgaben: <%= APP_CONFIG[:base_url] %>/home/tasks \ No newline at end of file +Viele Grüße von <%= APP_CONFIG[:name] %> \ No newline at end of file diff --git a/app/views/messages/negative_balance.rhtml b/app/views/messages/negative_balance.rhtml deleted file mode 100644 index d5ff0c66..00000000 --- a/app/views/messages/negative_balance.rhtml +++ /dev/null @@ -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] %> \ No newline at end of file diff --git a/app/views/messages/order_finished.rhtml b/app/views/messages/order_finished.rhtml deleted file mode 100644 index 43d1fddd..00000000 --- a/app/views/messages/order_finished.rhtml +++ /dev/null @@ -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] %> \ No newline at end of file diff --git a/app/views/shared/_loginInfo.haml b/app/views/shared/_loginInfo.haml index c08f928a..f599785a 100644 --- a/app/views/shared/_loginInfo.haml +++ b/app/views/shared/_loginInfo.haml @@ -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' \ No newline at end of file diff --git a/app/views/shared/_mail_footer.rhtml b/app/views/shared/_mail_footer.rhtml deleted file mode 100644 index 31eb70b2..00000000 --- a/app/views/shared/_mail_footer.rhtml +++ /dev/null @@ -1,4 +0,0 @@ --- -FoodSoft: <%= @foodsoftUrl %> -Foodcoop-Homepage: <%= APP_CONFIG[:base_url] %> -Hilfe/Help: <%= APP_CONFIG[:help_url] %> \ No newline at end of file diff --git a/config/app_config.yml.SAMPLE b/config/app_config.yml.SAMPLE index 5402215d..ca5663c5 100644 --- a/config/app_config.yml.SAMPLE +++ b/config/app_config.yml.SAMPLE @@ -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 diff --git a/lib/tasks/foodsoft.rake b/lib/tasks/foodsoft.rake index 1306fc60..5b67c918 100644 --- a/lib/tasks/foodsoft.rake +++ b/lib/tasks/foodsoft.rake @@ -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