Commit f02d36c9ab13d0be854a317e7367a93435477c1c
1 parent
973928e0
Exists in
master
and in
27 other branches
rails3: fix contact
Showing
5 changed files
with
93 additions
and
87 deletions
Show diff stats
@@ -0,0 +1,64 @@ | @@ -0,0 +1,64 @@ | ||
1 | +class Contact | ||
2 | + | ||
3 | + include ActiveModel::Validations | ||
4 | + | ||
5 | + def initialize(attributes = nil) | ||
6 | + if attributes | ||
7 | + attributes.each do |attr,value| | ||
8 | + self.send("#{attr}=", value) | ||
9 | + end | ||
10 | + end | ||
11 | + end | ||
12 | + | ||
13 | + attr_accessor :name | ||
14 | + attr_accessor :subject | ||
15 | + attr_accessor :message | ||
16 | + attr_accessor :email | ||
17 | + attr_accessor :state | ||
18 | + attr_accessor :city | ||
19 | + attr_accessor :receive_a_copy | ||
20 | + attr_accessor :dest | ||
21 | + attr_accessor :sender | ||
22 | + | ||
23 | + N_('Subject'); N_('Message'); N_('City and state'); N_('e-Mail'); N_('Name') | ||
24 | + | ||
25 | + validates_presence_of :subject, :email, :message, :name | ||
26 | + validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) | ||
27 | + | ||
28 | + def deliver | ||
29 | + return false unless self.valid? | ||
30 | + Contact::Sender.notification(self).deliver | ||
31 | + end | ||
32 | + | ||
33 | + class Sender < ActionMailer::Base | ||
34 | + def notification(contact) | ||
35 | + @name = contact.name | ||
36 | + @email = contact.email | ||
37 | + @city = contact.city | ||
38 | + @state = contact.state | ||
39 | + @message = contact.message | ||
40 | + @environment = contact.dest.environment.name | ||
41 | + @url = url_for(:host => contact.dest.environment.default_hostname, :controller => 'home') | ||
42 | + @target = contact.dest.name | ||
43 | + | ||
44 | + options = { | ||
45 | + content_type: 'text/html', | ||
46 | + to: contact.dest.notification_emails, | ||
47 | + reply_to: contact.email, | ||
48 | + subject: "[#{contact.dest.short_name(30)}] " + contact.subject, | ||
49 | + from: "#{contact.name} <#{contact.dest.environment.contact_email}>" | ||
50 | + } | ||
51 | + | ||
52 | + if contact.sender | ||
53 | + options.merge!(headers: {'X-Noosfero-Sender' => contact.sender.identifier}) | ||
54 | + end | ||
55 | + | ||
56 | + if contact.receive_a_copy | ||
57 | + options.merge!(cc: "#{contact.name} <#{contact.email}>") | ||
58 | + end | ||
59 | + | ||
60 | + mail(options) | ||
61 | + end | ||
62 | + end | ||
63 | + | ||
64 | +end |
app/models/contact.rb
@@ -1,58 +0,0 @@ | @@ -1,58 +0,0 @@ | ||
1 | -class Contact | ||
2 | - | ||
3 | - include ActiveModel::Validations | ||
4 | - | ||
5 | - def initialize(attributes = nil) | ||
6 | - if attributes | ||
7 | - attributes.each do |attr,value| | ||
8 | - self.send("#{attr}=", value) | ||
9 | - end | ||
10 | - end | ||
11 | - end | ||
12 | - | ||
13 | - attr_accessor :name | ||
14 | - attr_accessor :subject | ||
15 | - attr_accessor :message | ||
16 | - attr_accessor :email | ||
17 | - attr_accessor :state | ||
18 | - attr_accessor :city | ||
19 | - attr_accessor :receive_a_copy | ||
20 | - attr_accessor :dest | ||
21 | - attr_accessor :sender | ||
22 | - | ||
23 | - N_('Subject'); N_('Message'); N_('City and state'); N_('e-Mail'); N_('Name') | ||
24 | - | ||
25 | - validates_presence_of :subject, :email, :message, :name | ||
26 | - validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|o| !o.email.blank?}) | ||
27 | - | ||
28 | - def deliver | ||
29 | - return false unless self.valid? | ||
30 | - Contact::Sender.deliver_mail(self) | ||
31 | - end | ||
32 | - | ||
33 | - class Sender < ActionMailer::Base | ||
34 | - def mail(contact) | ||
35 | - content_type 'text/html' | ||
36 | - emails = contact.dest.notification_emails | ||
37 | - recipients emails | ||
38 | - from "#{contact.name} <#{contact.dest.environment.contact_email}>" | ||
39 | - reply_to contact.email | ||
40 | - if contact.sender | ||
41 | - headers 'X-Noosfero-Sender' => contact.sender.identifier | ||
42 | - end | ||
43 | - if contact.receive_a_copy | ||
44 | - cc "#{contact.name} <#{contact.email}>" | ||
45 | - end | ||
46 | - subject "[#{contact.dest.short_name(30)}] " + contact.subject | ||
47 | - body :name => contact.name, | ||
48 | - :email => contact.email, | ||
49 | - :city => contact.city, | ||
50 | - :state => contact.state, | ||
51 | - :message => contact.message, | ||
52 | - :environment => contact.dest.environment.name, | ||
53 | - :url => url_for(:host => contact.dest.environment.default_hostname, :controller => 'home'), | ||
54 | - :target => contact.dest.name | ||
55 | - end | ||
56 | - end | ||
57 | - | ||
58 | -end |
app/views/contact/sender/mail.html.erb
@@ -1,27 +0,0 @@ | @@ -1,27 +0,0 @@ | ||
1 | -<!DOCTYPE html> | ||
2 | -<html> | ||
3 | - <head> | ||
4 | - <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | ||
5 | - </head> | ||
6 | - <body> | ||
7 | - <p><%= _('This message was sent by %{sender} to %{target} on %{environment}.') % | ||
8 | - {:sender => @name, :target => @target, :environment => @environment} %></p> | ||
9 | - <%= _('Information about the user who sent this message:')%> | ||
10 | - <ul> | ||
11 | - <li><%= content_tag('b', _('Name')+': ') + @name.to_s %></li> | ||
12 | - <li><%= content_tag('b', _('Email')+': ') + @email.to_s %></li> | ||
13 | - <% if @city || @state %> | ||
14 | - <li><%= content_tag('b', _('City and state')+': ') + (@city || '?')+'/'+(@state || '?') %></li> | ||
15 | - <% end %> | ||
16 | - </ul> | ||
17 | - <hr/> | ||
18 | - <%= content_tag('b', _('Message:')) %> | ||
19 | - <p><%= word_wrap(@message) %></p> | ||
20 | - | ||
21 | - --<br/> | ||
22 | - <%= _('Greetings,') %><br/> | ||
23 | - <%= _('%s team.') % @environment %><br/> | ||
24 | - <%= @url %> | ||
25 | - </body> | ||
26 | -</html> | ||
27 | - |
@@ -0,0 +1,27 @@ | @@ -0,0 +1,27 @@ | ||
1 | +<!DOCTYPE html> | ||
2 | +<html> | ||
3 | + <head> | ||
4 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | ||
5 | + </head> | ||
6 | + <body> | ||
7 | + <p><%= _('This message was sent by %{sender} to %{target} on %{environment}.') % | ||
8 | + {:sender => @name, :target => @target, :environment => @environment} %></p> | ||
9 | + <%= _('Information about the user who sent this message:')%> | ||
10 | + <ul> | ||
11 | + <li><%= content_tag('b', _('Name')+': ') + @name.to_s %></li> | ||
12 | + <li><%= content_tag('b', _('Email')+': ') + @email.to_s %></li> | ||
13 | + <% if @city || @state %> | ||
14 | + <li><%= content_tag('b', _('City and state')+': ') + (@city || '?')+'/'+(@state || '?') %></li> | ||
15 | + <% end %> | ||
16 | + </ul> | ||
17 | + <hr/> | ||
18 | + <%= content_tag('b', _('Message:')) %> | ||
19 | + <p><%= word_wrap(@message) %></p> | ||
20 | + | ||
21 | + --<br/> | ||
22 | + <%= _('Greetings,') %><br/> | ||
23 | + <%= _('%s team.') % @environment %><br/> | ||
24 | + <%= @url %> | ||
25 | + </body> | ||
26 | +</html> | ||
27 | + |
test/unit/contact_test.rb
@@ -22,7 +22,7 @@ class ContactTest < ActiveSupport::TestCase | @@ -22,7 +22,7 @@ class ContactTest < ActiveSupport::TestCase | ||
22 | should 'validates format of email only if not empty' do | 22 | should 'validates format of email only if not empty' do |
23 | contact = Contact.new | 23 | contact = Contact.new |
24 | contact.valid? | 24 | contact.valid? |
25 | - assert_match(/can't be blank/, contact.errors[:email]) | 25 | + assert_match(/can't be blank/, contact.errors[:email].first) |
26 | end | 26 | end |
27 | 27 | ||
28 | should 'inicialize fields on instanciate' do | 28 | should 'inicialize fields on instanciate' do |
@@ -62,7 +62,7 @@ class ContactTest < ActiveSupport::TestCase | @@ -62,7 +62,7 @@ class ContactTest < ActiveSupport::TestCase | ||
62 | ent = fast_create(Enterprise, :name => 'my enterprise', :identifier => 'myent') | 62 | ent = fast_create(Enterprise, :name => 'my enterprise', :identifier => 'myent') |
63 | c = Contact.new(:name => 'john', :email => 'john@invalid.com', :subject => 'hi', :message => 'hi, all', :dest => ent) | 63 | c = Contact.new(:name => 'john', :email => 'john@invalid.com', :subject => 'hi', :message => 'hi, all', :dest => ent) |
64 | email = c.deliver | 64 | email = c.deliver |
65 | - assert_equal c.email, email.reply_to.to_s | 65 | + assert_equal c.email, email.reply_to.first.to_s |
66 | end | 66 | end |
67 | 67 | ||
68 | end | 68 | end |