contact.rb
1.67 KB
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
class Contact
  include ActiveModel::Validations
  def initialize(attributes = nil)
    if attributes
      attributes.each do |attr,value|
        self.send("#{attr}=", value)
      end
    end
  end
  attr_accessor :name
  attr_accessor :subject
  attr_accessor :message
  attr_accessor :email
  attr_accessor :state
  attr_accessor :city
  attr_accessor :receive_a_copy
  attr_accessor :dest
  attr_accessor :sender
  N_('Subject'); N_('Message'); N_('City and state'); N_('e-Mail'); N_('Name')
  validates_presence_of :subject, :email, :message, :name
  validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?})
  def deliver
    return false unless self.valid?
    Contact::Sender.notification(self).deliver
  end
  class Sender < ApplicationMailer
    def notification(contact)
      self.environment = contact.dest.environment
      @name = contact.name
      @email = contact.email
      @city = contact.city
      @state = contact.state
      @message = contact.message
      @url = url_for(:host => contact.dest.environment.default_hostname, :controller => 'home')
      @target = contact.dest.name
      options = {
        content_type: 'text/html',
        to: contact.dest.notification_emails,
        reply_to: contact.email,
        subject: "[#{contact.dest.short_name(30)}] " + contact.subject,
        from: "#{contact.name} <#{contact.dest.environment.noreply_email}>"
      }
      if contact.sender
        options.merge!('X-Noosfero-Sender' => contact.sender.identifier)
      end
      if contact.receive_a_copy
        options.merge!(cc: "#{contact.name} <#{contact.email}>")
      end
      mail(options)
    end
  end
end