Class: Mailer

Inherits:
ActionMailer::Base
  • Object
show all
Includes:
ApplicationHelper
Defined in:
app/mailers/mailer.rb

Overview

ActionMailer class that handles all emails for Foodsoft.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationHelper

#base_errors, #bootstrap_flash_patched, #close_button, #expand, #foodcoop_css_path, #foodcoop_css_tag, #format_currency, #format_date, #format_datetime, #format_datetime_timespec, #format_roles, #format_time, #heading_helper, #icon, #items_per_page, #link_to_gmaps, #link_to_top, #pagination_links_remote, #remote_link_to, #show_title?, #show_user, #show_user_link, #sort_link_helper, #tab_is_active?, #title, #truncate, #weekday

Class Method Details

.deliver_nowObject


130
131
132
133
134
135
# File 'app/mailers/mailer.rb', line 130

def self.deliver_now
  message = yield
  message.deliver_now
rescue => error
  MailDeliveryStatus.create email: message.to[0], message: error.message
end

.deliver_now_with_user_locale(user, &block) ⇒ Object


124
125
126
127
128
# File 'app/mailers/mailer.rb', line 124

def self.deliver_now_with_user_locale(user, &block)
  I18n.with_locale(user.settings['profile']['language']) do
    self.deliver_now &block
  end
end

Instance Method Details

#add_order_result_attachments(order, options = {}) ⇒ Object

separate method to allow plugins to mess with the attachments


138
139
140
141
# File 'app/mailers/mailer.rb', line 138

def add_order_result_attachments(order, options = {})
  attachments['order.pdf'] = OrderFax.new(order, options).to_pdf
  attachments['order.csv'] = OrderCsv.new(order, options).to_csv
end

#feedback(user, feedback) ⇒ Object


78
79
80
81
82
83
84
85
# File 'app/mailers/mailer.rb', line 78

def feedback(user, feedback)
  @user = user
  @feedback = feedback

  mail to: FoodsoftConfig[:notification][:error_recipients],
       from: user,
       subject: I18n.t('mailer.feedback.subject')
end

#invite(invite) ⇒ Object

Sends an invite email.


26
27
28
29
30
31
32
# File 'app/mailers/mailer.rb', line 26

def invite(invite)
  @invite = invite
  @link = accept_invitation_url(token: @invite.token)

  mail to: invite.email,
       subject: I18n.t('mailer.invite.subject')
end

#mail(args) ⇒ Object


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/mailers/mailer.rb', line 95

def mail(args)
  args[:message_id] ||= "<#{Mail.random_tag}@#{default_url_options[:host]}>"
  args[:subject] = "[#{FoodsoftConfig[:name]}] #{args[:subject]}"

  if args[:from].is_a? User
    args[:reply_to] ||= args[:from]
    args[:from] = format_address(FoodsoftConfig[:email_sender], I18n.t('mailer.from_via_foodsoft', name: show_user(args[:from])))
  end

  [:bcc, :cc, :reply_to, :sender, :to].each do |k|
    user = args[k]
    args[k] = format_address(user.email, show_user(user)) if user.is_a? User
  end

  if contact_email = FoodsoftConfig[:contact][:email]
    args[:reply_to] ||= format_address(contact_email, FoodsoftConfig[:name])
  end

  reply_email_domain = FoodsoftConfig[:reply_email_domain]
  if reply_email_domain && !args[:return_path] && args[:to].is_a?(String)    #TODO: Remove workaround for https://github.com/mikel/mail/issues/39 after next mail release

    ascii_to = args[:to].encode('ASCII', invalid: :replace, undef: :replace, replace: '_')
    address = Mail::Parsers::AddressListsParser.new.parse(ascii_to).addresses.first
    args[:return_path] = "<#{FoodsoftConfig.scope}.bounce+#{address.local}=#{address.domain}@#{reply_email_domain}>"
  end

  super
end

#negative_balance(user, transaction) ⇒ Object

Notify user if account balance is less than zero


70
71
72
73
74
75
76
# File 'app/mailers/mailer.rb', line 70

def negative_balance(user,transaction)
  @group        = user.ordergroup
  @transaction  = transaction

  mail to: user,
       subject: I18n.t('mailer.negative_balance.subject')
end

#not_enough_users_assigned(task, user) ⇒ Object


87
88
89
90
91
92
93
# File 'app/mailers/mailer.rb', line 87

def not_enough_users_assigned(task, user)
  @task = task
  @user = user

  mail to: user,
       subject: I18n.t('mailer.not_enough_users_assigned.subject', task: task.name)
end

#order_result(user, group_order) ⇒ Object

Sends order result for specific Ordergroup


44
45
46
47
48
49
50
# File 'app/mailers/mailer.rb', line 44

def order_result(user, group_order)
  @order        = group_order.order
  @group_order  = group_order

  mail to: user,
       subject: I18n.t('mailer.order_result.subject', name: group_order.order.name)
end

#order_result_supplier(user, order, options = {}) ⇒ Object

Sends order result to the supplier


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/mailers/mailer.rb', line 53

def order_result_supplier(user, order, options = {})
  @user     = user
  @order    = order
  @supplier = order.supplier

  add_order_result_attachments order, options

  subject = I18n.t('mailer.order_result_supplier.subject', :name => order.supplier.name)
  subject += " (#{I18n.t('activerecord.attributes.order.pickup')}: #{format_date(order.pickup)})" if order.pickup

  mail to: order.supplier.email,
       cc: user,
       reply_to: user,
       subject: subject
end

#reset_password(user) ⇒ Object

Sends an email with instructions on how to reset the password. Assumes user.setResetPasswordToken has been successfully called already.


17
18
19
20
21
22
23
# File 'app/mailers/mailer.rb', line 17

def reset_password(user)
  @user = user
  @link = new_password_url(id: @user.id, token: @user.reset_password_token)

  mail to: user,
       subject: I18n.t('mailer.reset_password.subject', username: show_user(user))
end

#upcoming_tasks(user, task) ⇒ Object

Notify user of upcoming task.


35
36
37
38
39
40
41
# File 'app/mailers/mailer.rb', line 35

def upcoming_tasks(user, task)
  @user = user
  @task = task

  mail to: user,
       subject: I18n.t('mailer.upcoming_tasks.subject')
end