From 1f8666e2d2ef4452ee7aa428e5280f575cc0c6d6 Mon Sep 17 00:00:00 2001 From: Martin Linkhorst Date: Mon, 13 Aug 2012 12:53:07 +0200 Subject: [PATCH] updated news added mailings --- app/mailers/user_mailer.rb | 19 +++++++++++++++ app/models/news_observer.rb | 2 +- app/views/mailer/news_added.html.erb | 4 ---- app/views/user_mailer/news_added.html.erb | 4 ++++ .../news_added.text.erb | 2 +- test/functional/user_mailer_test.rb | 24 +++++++++++++++++++ 6 files changed, 49 insertions(+), 6 deletions(-) delete mode 100644 app/views/mailer/news_added.html.erb create mode 100644 app/views/user_mailer/news_added.html.erb rename app/views/{mailer => user_mailer}/news_added.text.erb (75%) diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index e91fa0c731..2a94e1a386 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -89,6 +89,25 @@ class UserMailer < ActionMailer::Base mail :to => to, :subject => subject end end + + def news_added(user, news) + @news = news + + headers["X-OpenProject-Project"] = news.project.identifier + headers["X-OpenProject-Type"] = "News" + + #message_id news + + to = user.mail + + locale = user.language.presence || I18n.default_locale + + I18n.with_locale(locale) do + subject = "[#{news.project.name}] #{t(:label_news)}: #{news.title}" + + mail :to => to, :subject => subject + end + end private diff --git a/app/models/news_observer.rb b/app/models/news_observer.rb index b0bb43f337..c0c44dfa24 100644 --- a/app/models/news_observer.rb +++ b/app/models/news_observer.rb @@ -17,7 +17,7 @@ class NewsObserver < ActiveRecord::Observer if Setting.notified_events.include?('news_added') users = User.find_all_by_mails(news.recipients) users.each do |user| - Mailer.deliver_news_added(news, user) + UserMailer.news_added(user, news).deliver end end end diff --git a/app/views/mailer/news_added.html.erb b/app/views/mailer/news_added.html.erb deleted file mode 100644 index 758ebccb8d..0000000000 --- a/app/views/mailer/news_added.html.erb +++ /dev/null @@ -1,4 +0,0 @@ -

<%= link_to(h(@news.title), @news_url) %>

-<%=h @news.author.name %> - -<%= textilizable(@news, :description, :only_path => false) %> diff --git a/app/views/user_mailer/news_added.html.erb b/app/views/user_mailer/news_added.html.erb new file mode 100644 index 0000000000..582c703017 --- /dev/null +++ b/app/views/user_mailer/news_added.html.erb @@ -0,0 +1,4 @@ +

<%= link_to @news.title, news_url(@news) %>

+<%= @news.author.name %> + +<%= textilizable @news.description %> diff --git a/app/views/mailer/news_added.text.erb b/app/views/user_mailer/news_added.text.erb similarity index 75% rename from app/views/mailer/news_added.text.erb rename to app/views/user_mailer/news_added.text.erb index c8ae3035f3..bbe0da202b 100644 --- a/app/views/mailer/news_added.text.erb +++ b/app/views/user_mailer/news_added.text.erb @@ -1,5 +1,5 @@ <%= @news.title %> -<%= @news_url %> +<%= news_url(@news) %> <%= @news.author.name %> <%= @news.description %> diff --git a/test/functional/user_mailer_test.rb b/test/functional/user_mailer_test.rb index 20cb843ebd..bc651a48e5 100644 --- a/test/functional/user_mailer_test.rb +++ b/test/functional/user_mailer_test.rb @@ -147,6 +147,30 @@ class UserMailerTest < ActionMailer::TestCase mail end + def test_news_added + news = News.find(:first) + assert UserMailer.news_added(@user, news).deliver + end + + def test_should_not_send_email_without_recipient + news = News.find(:first) + user = news.author + # Remove members except news author + news.project.memberships.each {|m| m.destroy unless m.user == user} + + user.pref[:no_self_notified] = false + user.pref.save + User.current = user + UserMailer.news_added(user, news.reload).deliver + assert_equal 1, last_email.to.size + # nobody to notify + user.pref[:no_self_notified] = true + user.pref.save + User.current = user + ActionMailer::Base.deliveries.clear + UserMailer.news_added(user, news.reload).deliver + assert ActionMailer::Base.deliveries.empty? + end end