aboutsummaryrefslogtreecommitdiff
blob: 4acaff23dd20c8165b1d52c95f2cf674e2bd6017 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#    Gentoo Recruiters Web App - to help Gentoo recruiters do their job better
#   Copyright (C) 2010 Joachim Filip Bartosik
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as
#   published by the Free Software Foundation, version 3 of the License
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
class UserMailer < ActionMailer::Base
  def common(user, subject)
    @recipients = user.email_address
    @from       = "no-reply@#{ ActionMailer::Base.default_url_options[:host] }"
    @sent_on    = Time.now
    @headers    = {}
    @subject    = subject
  end

  def question_title(answer)
    answer.question._?.title || "none"
  end

  def forgot_password(user, key)
    common(user, "#{@app_name} -- forgotten password")
    @body = { :user => user, :key => key, :app_name => app_name }
  end

  def new_question(user, question)
    common(user, "New question")
    @body = { :title=> question.title, :category => question.category,
      :id => question.id}
  end

  def new_answer(user, answer)
    common(user, "New answer")
    @body = { :question_title=> question_title(answer), :recruit_name =>
      answer.owner.name, :id => answer.id}
  end

  def changed_answer(user, answer)
    common(user, "Changed answer")
    @body = { :question_title=> question_title(answer), :recruit_name =>
      answer.owner.name, :id => answer.id}
  end

  def new_comment(user, comment)
    common(user, "New comment")
    @body = { :question_title=> question_title(comment.answer), :id => comment.answer.id }
  end

  def unrecognized_email(user, email)
    common(user, "Your email sent to #{@app_name} wasn't recognized")

    fields          = [:subject, :from, :to].inject(String.new) do |res, cur|
      field_name    = cur.to_s.camelize
      field_content = email.send(cur)

      if field_content.class == Array # string with comma-separated values
        field_content = field_content.inject(String.new){ |r,c| r += "#{c.to_s}, " }
        field_content = field_content[0..-3]
      end

      res += "#{field_name}: #{field_content}\n"
    end

    @body = { :email => email, :app_name => @app_name, :fields => fields }
  end
end