Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

consolidate work package mailing

ulferts672a5c79b671
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index 23f9bf3c64e..a986b3c43b9 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -39,22 +39,6 @@ class UserMailer < ApplicationMailer
end
end
- def work_package_watcher_changed(work_package, user, watcher_changer, action)
- User.execute_as user do
- @issue = work_package
- @watcher_changer = watcher_changer
- @action = action
-
- set_work_package_headers(work_package)
- message_id work_package, user
- references work_package, user
-
- with_locale_for(user) do
- mail to: user.mail, subject: subject_for_work_package(work_package)
- end
- end
- end
-
def backup_ready(user)
User.execute_as user do
@download_url = admin_backups_url
@@ -272,24 +256,6 @@ class UserMailer < ApplicationMailer
mail to: admin.mail, subject: t("mail_user_activation_limit_reached.subject")
end
end
-
- private
-
- def subject_for_work_package(wp)
- "#{wp.project.name} - #{wp.status.name} #{wp.type.name} ##{wp.id}: #{wp.subject}"
- end
-
- # TODO: Delete since moved to WorkPackageMailer
- def set_work_package_headers(work_package)
- open_project_headers 'Project' => work_package.project.identifier,
- 'Issue-Id' => work_package.id,
- 'Issue-Author' => work_package.author.login,
- 'Type' => 'WorkPackage'
-
- if work_package.assigned_to
- open_project_headers 'Issue-Assignee' => work_package.assigned_to.login
- end
- end
end
##
diff --git a/app/mailers/work_package_mailer.rb b/app/mailers/work_package_mailer.rb
index 9cdbc2c1dce..71a6731eb26 100644
--- a/app/mailers/work_package_mailer.rb
+++ b/app/mailers/work_package_mailer.rb
@@ -50,8 +50,28 @@ class WorkPackageMailer < ApplicationMailer
end
end
+ def watcher_changed(work_package, user, watcher_changer, action)
+ User.execute_as user do
+ @work_package = work_package
+ @watcher_changer = watcher_changer
+ @action = action
+
+ set_work_package_headers(work_package)
+ message_id work_package, user
+ references work_package, user
+
+ with_locale_for(user) do
+ mail to: user.mail, subject: subject_for_work_package(work_package)
+ end
+ end
+ end
+
private
+ def subject_for_work_package(wp)
+ "#{wp.project.name} - #{wp.status.name} #{wp.type.name} ##{wp.id}: #{wp.subject}"
+ end
+
def set_work_package_headers(work_package)
open_project_headers 'Project' => work_package.project.identifier,
'WorkPackage-Id' => work_package.id,
diff --git a/app/views/user_mailer/_issue_details.html.erb b/app/views/work_package_mailer/_work_package_details.html.erb
similarity index 72%
rename from app/views/user_mailer/_issue_details.html.erb
rename to app/views/work_package_mailer/_work_package_details.html.erb
index bf6a6c5bf5a..095ff17acc8 100644
--- a/app/views/user_mailer/_issue_details.html.erb
+++ b/app/views/work_package_mailer/_work_package_details.html.erb
@@ -27,27 +27,27 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
-<h1><%= link_to issue.to_s, work_package_url(issue) %></h1>
+<h1><%= link_to work_package.to_s, work_package_url(work_package) %></h1>
<ul>
- <li><%= WorkPackage.human_attribute_name(:author) %>: <%= issue.author %></li>
- <li><%= WorkPackage.human_attribute_name(:status) %>: <%= issue.status %></li>
- <li><%= WorkPackage.human_attribute_name(:priority) %>: <%= issue.priority %></li>
- <li><%= WorkPackage.human_attribute_name(:assigned_to) %>: <%= issue.assigned_to %></li>
- <li><%= WorkPackage.human_attribute_name(:responsible) %>: <%= issue.responsible %></li>
- <li><%= WorkPackage.human_attribute_name(:category) %>: <%= issue.category %></li>
- <li><%= WorkPackage.human_attribute_name(:version) %>: <%= issue.version %></li>
- <li><%= WorkPackage.human_attribute_name(:start_date) %>: <%= issue.start_date %></li>
- <li><%= WorkPackage.human_attribute_name(:due_date) %>: <%= issue.due_date %></li>
- <li><%= WorkPackage.human_attribute_name(:estimated_hours)%>: <%= issue.estimated_hours %></li>
+ <li><%= WorkPackage.human_attribute_name(:author) %>: <%= work_package.author %></li>
+ <li><%= WorkPackage.human_attribute_name(:status) %>: <%= work_package.status %></li>
+ <li><%= WorkPackage.human_attribute_name(:priority) %>: <%= work_package.priority %></li>
+ <li><%= WorkPackage.human_attribute_name(:assigned_to) %>: <%= work_package.assigned_to %></li>
+ <li><%= WorkPackage.human_attribute_name(:responsible) %>: <%= work_package.responsible %></li>
+ <li><%= WorkPackage.human_attribute_name(:category) %>: <%= work_package.category %></li>
+ <li><%= WorkPackage.human_attribute_name(:version) %>: <%= work_package.version %></li>
+ <li><%= WorkPackage.human_attribute_name(:start_date) %>: <%= work_package.start_date %></li>
+ <li><%= WorkPackage.human_attribute_name(:due_date) %>: <%= work_package.due_date %></li>
+ <li><%= WorkPackage.human_attribute_name(:estimated_hours)%>: <%= work_package.estimated_hours %></li>
<% if Setting.work_package_done_ratio != 'disabled' %>
- <li><%= WorkPackage.human_attribute_name(:done_ratio) %>: <%= issue.done_ratio %></li>
+ <li><%= WorkPackage.human_attribute_name(:done_ratio) %>: <%= work_package.done_ratio %></li>
<% end %>
- <% issue.custom_field_values.each do |value| %>
+ <% work_package.custom_field_values.each do |value| %>
<li><%= value.custom_field.name %>: <%= show_value(value) %></li>
<% end %>
</ul>
-<%= format_text(issue.description, attribute: :description, only_path: false, object: issue, project: issue.project) %>
+<%= format_text(work_package.description, attribute: :description, only_path: false, object: work_package, project: work_package.project) %>
diff --git a/app/views/user_mailer/_issue_details.text.erb b/app/views/work_package_mailer/_work_package_details.text.erb
similarity index 53%
rename from app/views/user_mailer/_issue_details.text.erb
rename to app/views/work_package_mailer/_work_package_details.text.erb
index ffe8a77e7c4..96ab67a3e7d 100644
--- a/app/views/user_mailer/_issue_details.text.erb
+++ b/app/views/work_package_mailer/_work_package_details.text.erb
@@ -27,24 +27,24 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
-<%= issue.to_s %>
-<%= work_package_url(issue) %>
-
-<%= WorkPackage.human_attribute_name(:author) %>: <%= issue.author %>
-<%= WorkPackage.human_attribute_name(:status) %>: <%= issue.status %>
-<%= WorkPackage.human_attribute_name(:priority) %>: <%= issue.priority %>
-<%= WorkPackage.human_attribute_name(:assigned_to) %>: <%= issue.assigned_to %>
-<%= WorkPackage.human_attribute_name(:responsible) %>: <%= issue.responsible %>
-<%= WorkPackage.human_attribute_name(:category) %>: <%= issue.category %>
-<%= WorkPackage.human_attribute_name(:version) %>: <%= issue.version %>
-<%= WorkPackage.human_attribute_name(:start_date) %>: <%= issue.start_date %>
-<%= WorkPackage.human_attribute_name(:due_date) %>: <%= issue.due_date %>
-<%= WorkPackage.human_attribute_name(:estimated_hours)%>: <%= issue.estimated_hours %>
+<%= work_package.to_s %>
+<%= work_package_url(work_package) %>
+
+<%= WorkPackage.human_attribute_name(:author) %>: <%= work_package.author %>
+<%= WorkPackage.human_attribute_name(:status) %>: <%= work_package.status %>
+<%= WorkPackage.human_attribute_name(:priority) %>: <%= work_package.priority %>
+<%= WorkPackage.human_attribute_name(:assigned_to) %>: <%= work_package.assigned_to %>
+<%= WorkPackage.human_attribute_name(:responsible) %>: <%= work_package.responsible %>
+<%= WorkPackage.human_attribute_name(:category) %>: <%= work_package.category %>
+<%= WorkPackage.human_attribute_name(:version) %>: <%= work_package.version %>
+<%= WorkPackage.human_attribute_name(:start_date) %>: <%= work_package.start_date %>
+<%= WorkPackage.human_attribute_name(:due_date) %>: <%= work_package.due_date %>
+<%= WorkPackage.human_attribute_name(:estimated_hours)%>: <%= work_package.estimated_hours %>
<% if Setting.work_package_done_ratio != 'disabled' %>
-<%= WorkPackage.human_attribute_name(:done_ratio) %>: <%= issue.done_ratio %>
+<%= WorkPackage.human_attribute_name(:done_ratio) %>: <%= work_package.done_ratio %>
<% end %>
-<% issue.custom_field_values.each do |value| %>
+<% work_package.custom_field_values.each do |value| %>
<%= value.custom_field.name %>: <%= show_value(value) %>
<% end %>
-<%= issue.description %>
+<%= work_package.description %>
diff --git a/app/views/user_mailer/work_package_watcher_changed.html.erb b/app/views/work_package_mailer/watcher_changed.html.erb
similarity index 80%
rename from app/views/user_mailer/work_package_watcher_changed.html.erb
rename to app/views/work_package_mailer/watcher_changed.html.erb
index 1d486ee8b71..533be3c6ba4 100644
--- a/app/views/user_mailer/work_package_watcher_changed.html.erb
+++ b/app/views/work_package_mailer/watcher_changed.html.erb
@@ -26,12 +26,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
See COPYRIGHT and LICENSE files for more details.
++#%>
-<%= t("text_work_package_watcher_#{@action}", id: "##{@issue.id}", watcher_changer: @watcher_changer) %>
+<%= t("text_work_package_watcher_#{@action}", id: "##{@work_package.id}", watcher_changer: @watcher_changer) %>
<hr />
-<%= render partial: 'issue_details', locals: { issue: @issue } %>
+<%= render partial: 'work_package_details', locals: { work_package: @work_package } %>
<p>
- <%= format_text(t(:text_latest_note, note: last_work_package_note(@issue)),
+ <%= format_text(t(:text_latest_note, note: last_work_package_note(@work_package)),
only_path: false,
- object: @issue,
- project: @issue.project) %>
+ object: @work_package,
+ project: @work_package.project) %>
</p>
diff --git a/app/views/user_mailer/work_package_watcher_changed.text.erb b/app/views/work_package_mailer/watcher_changed.text.erb
similarity index 81%
rename from app/views/user_mailer/work_package_watcher_changed.text.erb
rename to app/views/work_package_mailer/watcher_changed.text.erb
index 8254cf7c18b..1ca3a840c09 100644
--- a/app/views/user_mailer/work_package_watcher_changed.text.erb
+++ b/app/views/work_package_mailer/watcher_changed.text.erb
@@ -27,8 +27,8 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
-<%= t("text_work_package_watcher_#{@action}", id: "##{@issue.id}", watcher_changer: @watcher_changer) %>
+<%= t("text_work_package_watcher_#{@action}", id: "##{@work_package.id}", watcher_changer: @watcher_changer) %>
----------------------------------------
-<%= render partial: 'issue_details', locals: { issue: @issue } %>
-<%= t(:text_latest_note, note: last_work_package_note(@issue)) %>
+<%= render partial: 'work_package_details', locals: { work_package: @work_package } %>
+<%= t(:text_latest_note, note: last_work_package_note(@work_package)) %>
diff --git a/app/workers/mails/watcher_job.rb b/app/workers/mails/watcher_job.rb
index 3d1927f4185..788eb0de800 100644
--- a/app/workers/mails/watcher_job.rb
+++ b/app/workers/mails/watcher_job.rb
@@ -38,11 +38,11 @@ class Mails::WatcherJob < Mails::DeliverJob
end
def render_mail
- UserMailer
- .work_package_watcher_changed(watcher.watchable,
- recipient,
- sender,
- action)
+ WorkPackageMailer
+ .watcher_changed(watcher.watchable,
+ recipient,
+ sender,
+ action)
end
private
diff --git a/spec/lib/open_project/hook_spec.rb b/spec/lib/open_project/hook_spec.rb
index e3005e89083..f894b2a56a6 100644
--- a/spec/lib/open_project/hook_spec.rb
+++ b/spec/lib/open_project/hook_spec.rb
@@ -255,7 +255,7 @@ describe OpenProject::Hook do
end
end
let!(:comparison_mail) do
- UserMailer.work_package_watcher_changed(work_package, user, author, :added).deliver_now
+ WorkPackageMailer.watcher_changed(work_package, user, author, :added).deliver_now
ActionMailer::Base.deliveries.last
end
@@ -263,7 +263,7 @@ describe OpenProject::Hook do
test_hook_controller_class.new.call_hook(:view_layouts_base_html_head)
ActionMailer::Base.deliveries.clear
- UserMailer.work_package_watcher_changed(work_package, user, author, :added).deliver_now
+ WorkPackageMailer.watcher_changed(work_package, user, author, :added).deliver_now
mail2 = ActionMailer::Base.deliveries.last
assert_equal comparison_mail.text_part.body.encoded, mail2.text_part.body.encoded
diff --git a/spec/mailers/shared_examples.rb b/spec/mailers/shared_examples.rb
new file mode 100644
index 00000000000..3dc3e217151
--- /dev/null
+++ b/spec/mailers/shared_examples.rb
@@ -0,0 +1,44 @@
+shared_examples_for 'mail is sent' do
+ let(:letters_sent_count) { 1 }
+ let(:mail) { deliveries.first }
+ let(:html_body) { mail.body.parts.detect { |p| p.content_type.include? 'text/html' }.body.encoded }
+
+ it 'actually sends a mail' do
+ expect(deliveries.size).to eql(letters_sent_count)
+ end
+
+ it 'is sent to the recipient' do
+ expect(deliveries.first.to).to include(recipient.mail)
+ end
+
+ it 'is sent from the configured address' do
+ expect(deliveries.first.from).to match_array([Setting.mail_from])
+ end
+end
+
+shared_examples_for 'multiple mails are sent' do |set_letters_sent_count|
+ it_behaves_like 'mail is sent' do
+ let(:letters_sent_count) { set_letters_sent_count }
+ end
+end
+
+shared_examples_for 'mail is not sent' do
+ it 'sends no mail' do
+ expect(deliveries).to be_empty
+ end
+end
+
+shared_examples_for 'does not send mails to author' do
+ let(:user) { FactoryBot.build_stubbed(:user) }
+
+ context 'when mail is for another user' do
+ it_behaves_like 'mail is sent'
+ end
+
+ context 'when mail is for author' do
+ let(:recipient) { user }
+
+ it_behaves_like 'mail is not sent'
+ end
+end
+
diff --git a/spec/mailers/user_mailer_spec.rb b/spec/mailers/user_mailer_spec.rb
index dc0436da96f..4e39b69e169 100644
--- a/spec/mailers/user_mailer_spec.rb
+++ b/spec/mailers/user_mailer_spec.rb
@@ -29,6 +29,7 @@
#++
require 'spec_helper'
+require_relative './shared_examples'
describe UserMailer, type: :mailer do
subject(:deliveries) { ActionMailer::Base.deliveries }
@@ -61,50 +62,6 @@ describe UserMailer, type: :mailer do
allow(Setting).to receive(:default_language).and_return('en')
end
- shared_examples_for 'mail is sent' do
- let(:letters_sent_count) { 1 }
- let(:mail) { deliveries.first }
- let(:html_body) { mail.body.parts.detect { |p| p.content_type.include? 'text/html' }.body.encoded }
-
- it 'actually sends a mail' do
- expect(deliveries.size).to eql(letters_sent_count)
- end
-
- it 'is sent to the recipient' do
- expect(deliveries.first.to).to include(recipient.mail)
- end
-
- it 'is sent from the configured address' do
- expect(deliveries.first.from).to match_array([Setting.mail_from])
- end
- end
-
- shared_examples_for 'multiple mails are sent' do |set_letters_sent_count|
- it_behaves_like 'mail is sent' do
- let(:letters_sent_count) { set_letters_sent_count }
- end
- end
-
- shared_examples_for 'mail is not sent' do
- it 'sends no mail' do
- expect(deliveries).to be_empty
- end
- end
-
- shared_examples_for 'does not send mails to author' do
- let(:user) { FactoryBot.build_stubbed(:user) }
-
- context 'when mail is for another user' do
- it_behaves_like 'mail is sent'
- end
-
- context 'when mail is for author' do
- let(:recipient) { user }
-
- it_behaves_like 'mail is not sent'
- end
- end
-
describe '#with_deliveries' do
context 'with false' do
before do
@@ -156,21 +113,6 @@ describe UserMailer, type: :mailer do
end
end
- describe '#work_package_watcher_changed' do
- let(:watcher_changer) { user }
-
- before do
- described_class.work_package_watcher_changed(work_package, recipient, watcher_changer, 'added').deliver_now
- described_class.work_package_watcher_changed(work_package, recipient, watcher_changer, 'removed').deliver_now
- end
-
- include_examples 'multiple mails are sent', 2
-
- it 'contains the WP subject in the mail subject' do
- expect(deliveries.first.subject).to include(work_package.subject)
- end
- end
-
describe '#wiki_content_added' do
let(:wiki_content) { FactoryBot.create(:wiki_content) }
diff --git a/spec/mailers/work_package_mailer_spec.rb b/spec/mailers/work_package_mailer_spec.rb
index 7ad4498e0df..4c3a5f40c55 100644
--- a/spec/mailers/work_package_mailer_spec.rb
+++ b/spec/mailers/work_package_mailer_spec.rb
@@ -29,6 +29,7 @@
#++
require 'spec_helper'
+require_relative './shared_examples'
describe WorkPackageMailer, type: :mailer do
include OpenProject::ObjectLinking
@@ -37,6 +38,7 @@ describe WorkPackageMailer, type: :mailer do
let(:work_package) do
FactoryBot.build_stubbed(:work_package,
+ type: FactoryBot.build_stubbed(:type_standard),
project: project,
assigned_to: assignee)
end
@@ -105,4 +107,22 @@ describe WorkPackageMailer, type: :mailer do
.to eql work_package.assigned_to.login
end
end
+
+ describe '#watcher_changed' do
+ subject(:deliveries) { ActionMailer::Base.deliveries }
+
+ let(:watcher_changer) { author }
+
+ before do
+ described_class.watcher_changed(work_package, recipient, author, 'added').deliver_now
+ described_class.watcher_changed(work_package, recipient, author, 'removed').deliver_now
+ end
+
+ include_examples 'multiple mails are sent', 2
+
+ it 'contains the WP subject in the mail subject' do
+ expect(deliveries.first.subject)
+ .to include(work_package.subject)
+ end
+ end
end
diff --git a/spec/workers/mails/shared/watcher_job.rb b/spec/workers/mails/shared/watcher_job.rb
index a71214e933c..982587dcee0 100644
--- a/spec/workers/mails/shared/watcher_job.rb
+++ b/spec/workers/mails/shared/watcher_job.rb
@@ -38,7 +38,7 @@ shared_examples "watcher job" do |action|
let(:watcher_changer) do
FactoryBot.build_stubbed(:user)
end
- let(:work_package) { FactoryBot.build_stubbed(:work_package, project: project) }
+ let(:work_package) { FactoryBot.build_stubbed(:work_package, type: FactoryBot.build_stubbed(:type), project: project) }
let(:watcher) do
FactoryBot.build_stubbed(:watcher, watchable: work_package, user: watching_user)
end
@@ -67,16 +67,16 @@ shared_examples "watcher job" do |action|
before do
# make sure no actual calls make it into the UserMailer
- allow(UserMailer)
- .to receive(:work_package_watcher_changed)
+ allow(WorkPackageMailer)
+ .to receive(:watcher_changed)
.and_return(double('mail', deliver_now: nil))
end
shared_examples_for 'sends a mail' do
it 'sends a mail' do
subject
- expect(UserMailer)
- .to have_received(:work_package_watcher_changed)
+ expect(WorkPackageMailer)
+ .to have_received(:watcher_changed)
.with(work_package,
watching_user,
watcher_changer,
@@ -87,8 +87,8 @@ shared_examples "watcher job" do |action|
shared_examples_for 'sends no mail' do
it 'sends no mail' do
subject
- expect(UserMailer)
- .not_to have_received(:work_package_watcher_changed)
+ expect(WorkPackageMailer)
+ .not_to have_received(:watcher_changed)
end
end
@@ -97,7 +97,7 @@ shared_examples "watcher job" do |action|
before do
mail = double('mail')
allow(mail).to receive(:deliver_now).and_raise(SocketError)
- expect(UserMailer).to receive(:work_package_watcher_changed).and_return(mail)
+ expect(WorkPackageMailer).to receive(:watcher_changed).and_return(mail)
end
it 'raises the error' do