2012-08-06 12:00:40 +02:00
|
|
|
# encoding: utf-8
|
2014-06-18 13:28:19 +02:00
|
|
|
# ActionMailer class that handles all emails for Foodsoft.
|
2011-05-11 10:53:18 +02:00
|
|
|
class Mailer < ActionMailer::Base
|
2013-09-22 14:38:56 +02:00
|
|
|
# XXX Quick fix to allow the use of show_user. Proper take would be one of
|
|
|
|
# (1) Use draper, decorate user
|
|
|
|
# (2) Create a helper with this method, include here and in ApplicationHelper
|
|
|
|
helper :application
|
|
|
|
include ApplicationHelper
|
2011-05-11 10:53:18 +02:00
|
|
|
|
2012-08-17 19:08:02 +02:00
|
|
|
layout 'email' # Use views/layouts/email.txt.erb
|
2011-05-11 10:53:18 +02:00
|
|
|
|
2014-06-18 13:28:19 +02:00
|
|
|
default from: "Foodsoft <#{FoodsoftConfig[:email_sender]}>",
|
2012-08-24 19:52:38 +02:00
|
|
|
sender: FoodsoftConfig[:email_sender],
|
|
|
|
errors_to: FoodsoftConfig[:email_sender]
|
2011-05-11 10:53:18 +02:00
|
|
|
|
|
|
|
# Sends an email with instructions on how to reset the password.
|
|
|
|
# Assumes user.setResetPasswordToken has been successfully called already.
|
2012-12-23 17:38:04 +01:00
|
|
|
def reset_password(user)
|
|
|
|
set_foodcoop_scope
|
|
|
|
@user = user
|
2012-10-08 11:51:56 +02:00
|
|
|
@link = new_password_url(id: @user.id, token: @user.reset_password_token)
|
2011-05-11 10:53:18 +02:00
|
|
|
|
2012-10-08 11:51:56 +02:00
|
|
|
mail :to => @user.email,
|
2013-09-22 14:38:56 +02:00
|
|
|
:subject => "[#{FoodsoftConfig[:name]}] " + I18n.t('mailer.reset_password.subject', :username => show_user(@user))
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Sends an invite email.
|
2012-12-23 17:38:04 +01:00
|
|
|
def invite(invite)
|
|
|
|
set_foodcoop_scope
|
|
|
|
@invite = invite
|
2012-10-08 11:51:56 +02:00
|
|
|
@link = accept_invitation_url(token: @invite.token)
|
2011-05-11 10:53:18 +02:00
|
|
|
|
2012-10-08 11:51:56 +02:00
|
|
|
mail :to => @invite.email,
|
2013-05-07 13:28:14 +02:00
|
|
|
:subject => I18n.t('mailer.invite.subject')
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Notify user of upcoming task.
|
|
|
|
def upcoming_tasks(user, task)
|
2012-12-23 17:38:04 +01:00
|
|
|
set_foodcoop_scope
|
2011-05-11 10:53:18 +02:00
|
|
|
@user = user
|
|
|
|
@task = task
|
|
|
|
|
|
|
|
mail :to => user.email,
|
2013-05-07 13:28:14 +02:00
|
|
|
:subject => "[#{FoodsoftConfig[:name]}] " + I18n.t('mailer.upcoming_tasks.subject')
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Sends order result for specific Ordergroup
|
|
|
|
def order_result(user, group_order)
|
2012-12-23 17:38:04 +01:00
|
|
|
set_foodcoop_scope
|
2011-05-11 10:53:18 +02:00
|
|
|
@order = group_order.order
|
|
|
|
@group_order = group_order
|
|
|
|
|
|
|
|
mail :to => user.email,
|
2013-05-07 13:28:14 +02:00
|
|
|
:subject => "[#{FoodsoftConfig[:name]}] " + I18n.t('mailer.order_result.subject', :name => group_order.order.name)
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Notify user if account balance is less than zero
|
|
|
|
def negative_balance(user,transaction)
|
2012-12-23 17:38:04 +01:00
|
|
|
set_foodcoop_scope
|
2011-05-11 10:53:18 +02:00
|
|
|
@group = user.ordergroup
|
|
|
|
@transaction = transaction
|
|
|
|
|
|
|
|
mail :to => user.email,
|
2014-05-30 10:13:21 +02:00
|
|
|
:subject => "[#{FoodsoftConfig[:name]}] " + I18n.t('mailer.negative_balance.subject')
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
|
|
|
|
2012-12-23 17:38:04 +01:00
|
|
|
def feedback(user, feedback)
|
|
|
|
set_foodcoop_scope
|
2011-05-11 10:53:18 +02:00
|
|
|
@user = user
|
2011-05-11 15:14:39 +02:00
|
|
|
@feedback = feedback
|
2011-05-11 10:53:18 +02:00
|
|
|
|
2012-08-24 19:52:38 +02:00
|
|
|
mail :to => FoodsoftConfig[:notification]["error_recipients"],
|
2013-09-22 14:38:56 +02:00
|
|
|
:from => "#{show_user user} <#{user.email}>",
|
2012-08-24 19:52:38 +02:00
|
|
|
:sender => FoodsoftConfig[:notification]["sender_address"],
|
|
|
|
:errors_to => FoodsoftConfig[:notification]["sender_address"],
|
2013-05-07 13:28:14 +02:00
|
|
|
:subject => "[#{FoodsoftConfig[:name]}] " + I18n.t('mailer.feedback.subject', :email => user.email)
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
|
|
|
|
2012-08-24 11:11:40 +02:00
|
|
|
def not_enough_users_assigned(task, user)
|
2012-12-23 17:38:04 +01:00
|
|
|
set_foodcoop_scope
|
2011-05-11 10:53:18 +02:00
|
|
|
@task = task
|
|
|
|
@user = user
|
2012-08-24 11:11:40 +02:00
|
|
|
|
2011-05-11 10:53:18 +02:00
|
|
|
mail :to => user.email,
|
2013-05-07 13:28:14 +02:00
|
|
|
:subject => "[#{FoodsoftConfig[:name]}] " + I18n.t('mailer.not_enough_users_assigned.subject', :task => task.name)
|
2011-05-11 10:53:18 +02:00
|
|
|
end
|
2012-10-08 11:51:56 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-09-02 12:18:50 +02:00
|
|
|
# @todo this global stuff gives threading problems when foodcoops have different values! - pass args to `url_for` instead
|
2012-12-23 17:38:04 +01:00
|
|
|
def set_foodcoop_scope(foodcoop = FoodsoftConfig.scope)
|
2014-09-02 12:18:50 +02:00
|
|
|
[:protocol, :host, :port].each do |k|
|
|
|
|
ActionMailer::Base.default_url_options[k] = FoodsoftConfig[k] if FoodsoftConfig[k]
|
|
|
|
end
|
2012-10-08 11:51:56 +02:00
|
|
|
ActionMailer::Base.default_url_options[:foodcoop] = foodcoop
|
|
|
|
end
|
2011-05-11 10:53:18 +02:00
|
|
|
|
|
|
|
end
|