Commit 3cab8c3fb779ee72a5ad852caf940c4a112b100f
Committed by
Antonio Terceiro
1 parent
63868041
Exists in
master
and in
29 other branches
Send e-mail when a scrap is sent (ActionItem1704)
Showing
5 changed files
with
112 additions
and
0 deletions
Show diff stats
app/models/person.rb
... | ... | @@ -345,6 +345,10 @@ class Person < Profile |
345 | 345 | end |
346 | 346 | end |
347 | 347 | |
348 | + def wall_url | |
349 | + generate_url(:profile => identifier, :controller => 'profile', :action => 'index', :anchor => 'profile-wall') | |
350 | + end | |
351 | + | |
348 | 352 | protected |
349 | 353 | |
350 | 354 | def followed_by?(profile) | ... | ... |
app/models/scrap.rb
... | ... | @@ -16,6 +16,7 @@ class Scrap < ActiveRecord::Base |
16 | 16 | |
17 | 17 | after_create do |scrap| |
18 | 18 | scrap.root.update_attribute('updated_at', DateTime.now) unless scrap.root.nil? |
19 | + Scrap::Notifier.deliver_mail(scrap) unless scrap.sender == scrap.receiver | |
19 | 20 | end |
20 | 21 | |
21 | 22 | before_validation :strip_all_html_tags |
... | ... | @@ -25,4 +26,21 @@ class Scrap < ActiveRecord::Base |
25 | 26 | self.content = sanitizer.sanitize(self.content, :tags => []) |
26 | 27 | end |
27 | 28 | |
29 | + class Notifier < ActionMailer::Base | |
30 | + def mail(scrap) | |
31 | + sender, receiver = scrap.sender, scrap.receiver | |
32 | + recipients receiver.email | |
33 | + | |
34 | + from "#{sender.environment.name} <#{sender.environment.contact_email}>" | |
35 | + subject _("[%s] You received a scrap!") % [sender.environment.name] | |
36 | + body :recipient => receiver.name, | |
37 | + :sender => sender.name, | |
38 | + :sender_link => sender.url, | |
39 | + :scrap_content => scrap.content, | |
40 | + :wall_url => receiver.wall_url, | |
41 | + :environment => sender.environment.name, | |
42 | + :url => sender.environment.top_url | |
43 | + end | |
44 | + end | |
45 | + | |
28 | 46 | end | ... | ... |
... | ... | @@ -0,0 +1,16 @@ |
1 | +<%= _('Hi, %{recipient}!') % { :recipient => @recipient } %> | |
2 | + | |
3 | +<%= word_wrap(_('%{sender} (%{sender_link}) has left the following scrap for you:') % { :sender => @sender, :sender_link => url_for(@sender_link) }) %> | |
4 | + | |
5 | +------------------------------------------------------------------------------- | |
6 | +<%= word_wrap(@scrap_content) %> | |
7 | +------------------------------------------------------------------------------- | |
8 | + | |
9 | +<%= _('View this scrap on your wall:') %> | |
10 | +<%= url_for @wall_url %> | |
11 | + | |
12 | +<%= _("Greetings,") %> | |
13 | + | |
14 | +-- | |
15 | +<%= _('%s team.') % @environment %> | |
16 | +<%= url_for @url %> | ... | ... |
test/unit/person_test.rb
... | ... | @@ -1059,4 +1059,10 @@ class PersonTest < Test::Unit::TestCase |
1059 | 1059 | assert has_remove_member_notification |
1060 | 1060 | end |
1061 | 1061 | |
1062 | + should 'return url to a person wall' do | |
1063 | + environment = create_environment('mycolivre.net') | |
1064 | + profile = build(Person, :identifier => 'testprofile', :environment_id => create_environment('mycolivre.net').id) | |
1065 | + assert_equal({ :host => "mycolivre.net", :profile => 'testprofile', :controller => 'profile', :action => 'index', :anchor => 'profile-wall' }, profile.wall_url) | |
1066 | + end | |
1067 | + | |
1062 | 1068 | end | ... | ... |
... | ... | @@ -0,0 +1,68 @@ |
1 | +require File.dirname(__FILE__) + '/../test_helper' | |
2 | + | |
3 | +class ScrapNotifierTest < Test::Unit::TestCase | |
4 | + FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures' | |
5 | + CHARSET = "utf-8" | |
6 | + | |
7 | + def setup | |
8 | + ActionMailer::Base.delivery_method = :test | |
9 | + ActionMailer::Base.perform_deliveries = true | |
10 | + ActionMailer::Base.deliveries = [] | |
11 | + @sender = create_user('user_scrap_sender_test').person | |
12 | + @receiver = create_user('user_scrap_receiver_test').person | |
13 | + end | |
14 | + | |
15 | + should 'deliver mail after leave scrap' do | |
16 | + assert_difference ActionMailer::Base.deliveries, :size do | |
17 | + Scrap.create!(:sender => @sender, :receiver => @receiver, :content => 'Hi man!') | |
18 | + end | |
19 | + end | |
20 | + | |
21 | + should 'deliver mail even if it is a reply' do | |
22 | + s = Scrap.create!(:sender => @sender, :receiver => @receiver, :content => 'Hi man!') | |
23 | + assert_difference ActionMailer::Base.deliveries, :size do | |
24 | + s.replies << Scrap.new(:sender => @sender, :receiver => @receiver, :content => 'Hi again man!') | |
25 | + end | |
26 | + end | |
27 | + | |
28 | + should 'deliver mail to receiver of the scrap' do | |
29 | + Scrap.create!(:sender => @sender, :receiver => @receiver, :content => 'Hi man!') | |
30 | + sent = ActionMailer::Base.deliveries.first | |
31 | + assert_equal [@receiver.email], sent.to | |
32 | + end | |
33 | + | |
34 | + should 'display sender name in delivered mail' do | |
35 | + Scrap.create!(:sender => @sender, :receiver => @receiver, :content => 'Hi man!') | |
36 | + sent = ActionMailer::Base.deliveries.first | |
37 | + assert_match /user_scrap_sender_test/, sent.body | |
38 | + end | |
39 | + | |
40 | + should 'display scrap content in delivered mail' do | |
41 | + Scrap.create!(:sender => @sender, :receiver => @receiver, :content => 'Hi man!') | |
42 | + sent = ActionMailer::Base.deliveries.first | |
43 | + assert_match /Hi man!/, sent.body | |
44 | + end | |
45 | + | |
46 | + should 'display receiver wall link in delivered mail' do | |
47 | + Scrap.create!(:sender => @sender, :receiver => @receiver, :content => 'Hi man!') | |
48 | + sent = ActionMailer::Base.deliveries.first | |
49 | + assert_match /\/profile\/user_scrap_receiver_test#profile-wall/, sent.body | |
50 | + end | |
51 | + | |
52 | + should 'not deliver mail if notify receiver and sender are the same person' do | |
53 | + assert_no_difference ActionMailer::Base.deliveries, :size do | |
54 | + Scrap.create!(:sender => @sender, :receiver => @sender, :content => 'Hi myself!') | |
55 | + end | |
56 | + end | |
57 | + | |
58 | + private | |
59 | + | |
60 | + def read_fixture(action) | |
61 | + IO.readlines("#{FIXTURES_PATH}/mail_sender/#{action}") | |
62 | + end | |
63 | + | |
64 | + def encode(subject) | |
65 | + quoted_printable(subject, CHARSET) | |
66 | + end | |
67 | + | |
68 | +end | ... | ... |