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

linting

ulferts0717357d3154
diff --git a/app/helpers/messages_helper.rb b/app/helpers/messages_helper.rb
index 832efabe722..fd66327bb6f 100644
--- a/app/helpers/messages_helper.rb
+++ b/app/helpers/messages_helper.rb
@@ -34,4 +34,8 @@ module MessagesHelper
current_user: current_user,
embed_links: true)
end
+
+ def message_url(message)
+ topic_url(message.root, r: message.id, anchor: "message-#{message.id}")
+ end
end
diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb
index 217994d21bc..e8af6bafd94 100644
--- a/app/mailers/application_mailer.rb
+++ b/app/mailers/application_mailer.rb
@@ -123,4 +123,43 @@ class ApplicationMailer < ActionMailer::Base
format.html unless Setting.plain_text_mail?
format.text
end
+
+ def send_mail(user, subject)
+ with_locale_for(user) do
+ mail to: user.mail, subject: subject
+ end
+ end
+end
+
+##
+# Interceptors
+#
+# These are registered in config/initializers/register_mail_interceptors.rb
+#
+# Unfortunately, this results in changes on the interceptor classes during development mode
+# not being reflected until a server restart.
+
+class DefaultHeadersInterceptor
+ def self.delivering_email(mail)
+ mail.headers(default_headers)
+ end
+
+ def self.default_headers
+ {
+ 'X-Mailer' => 'OpenProject',
+ 'X-OpenProject-Host' => Setting.host_name,
+ 'X-OpenProject-Site' => Setting.app_title,
+ 'Precedence' => 'bulk',
+ 'Auto-Submitted' => 'auto-generated'
+ }
+ end
+end
+
+class DoNotSendMailsWithoutReceiverInterceptor
+ def self.delivering_email(mail)
+ receivers = [mail.to, mail.cc, mail.bcc]
+ # the above fields might be empty arrays (if entries have been removed
+ # by another interceptor) or nil, therefore checking for blank?
+ mail.perform_deliveries = false if receivers.all?(&:blank?)
+ end
end
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index cfc5cfb711c..5599edc70d8 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -29,23 +29,25 @@
#++
class UserMailer < ApplicationMailer
+ include MessagesHelper
+
+ helper_method :message_url
+
def test_mail(user)
@welcome_url = url_for(controller: '/homescreen')
- headers['X-OpenProject-Type'] = 'Test'
+ open_project_headers 'Type' => 'Test'
- with_locale_for(user) do
- mail to: "\"#{user.name}\" <#{user.mail}>", subject: 'OpenProject Test'
- end
+ send_mail(user,
+ 'OpenProject Test')
end
def backup_ready(user)
User.execute_as user do
@download_url = admin_backups_url
- with_locale_for(user) do
- mail to: user.mail, subject: I18n.t("mail_subject_backup_ready")
- end
+ send_mail(recipient,
+ I18n.t("mail_subject_backup_ready"))
end
end
@@ -55,9 +57,8 @@ class UserMailer < ApplicationMailer
@waiting_period = waiting_period
User.execute_as recipient do
- with_locale_for(recipient) do
- mail to: recipient.mail, subject: I18n.t("mail_subject_backup_token_reset")
- end
+ send_mail(recipient,
+ I18n.t("mail_subject_backup_token_reset"))
end
end
@@ -71,11 +72,8 @@ class UserMailer < ApplicationMailer
open_project_headers 'Type' => 'Account'
- user = @token.user
- with_locale_for(user) do
- subject = t(:mail_subject_lost_password, value: Setting.app_title)
- mail to: user.mail, subject: subject
- end
+ send_mail(token.user,
+ t(:mail_subject_lost_password, value: Setting.app_title))
end
def news_added(user, news)
@@ -86,11 +84,10 @@ class UserMailer < ApplicationMailer
message_id @news, user
- with_locale_for(user) do
- subject = "#{News.model_name.human}: #{@news.title}"
- subject = "[#{@news.project.name}] #{subject}" if @news.project
- mail to: user.mail, subject: subject
- end
+ subject = "#{News.model_name.human}: #{@news.title}"
+ subject = "[#{@news.project.name}] #{subject}" if @news.project
+
+ send_mail(user, subject)
end
def user_signed_up(token)
@@ -104,11 +101,8 @@ class UserMailer < ApplicationMailer
open_project_headers 'Type' => 'Account'
- user = token.user
- with_locale_for(user) do
- subject = t(:mail_subject_register, value: Setting.app_title)
- mail to: user.mail, subject: subject
- end
+ send_mail(token.user,
+ t(:mail_subject_register, value: Setting.app_title))
end
def news_comment_added(user, comment)
@@ -120,26 +114,20 @@ class UserMailer < ApplicationMailer
message_id @comment, user
references @news, user
- with_locale_for(user) do
- subject = "#{News.model_name.human}: #{@news.title}"
- subject = "Re: [#{@news.project.name}] #{subject}" if @news.project
- mail to: user.mail, subject: subject
- end
+ subject = "#{News.model_name.human}: #{@news.title}"
+ subject = "Re: [#{@news.project.name}] #{subject}" if @news.project
+
+ send_mail(user, subject)
end
def wiki_content_added(user, wiki_content)
@wiki_content = wiki_content
- open_project_headers 'Project' => @wiki_content.project.identifier,
- 'Wiki-Page-Id' => @wiki_content.page.id,
- 'Type' => 'Wiki'
-
+ open_project_wiki_headers @wiki_content
message_id @wiki_content, user
- with_locale_for(user) do
- subject = "[#{@wiki_content.project.name}] #{t(:mail_subject_wiki_content_added, id: @wiki_content.page.title)}"
- mail to: user.mail, subject: subject
- end
+ send_mail(user,
+ "[#{@wiki_content.project.name}] #{t(:mail_subject_wiki_content_added, id: @wiki_content.page.title)}")
end
def wiki_content_updated(user, wiki_content)
@@ -150,33 +138,22 @@ class UserMailer < ApplicationMailer
id: wiki_content.page.slug,
version: wiki_content.version)
- open_project_headers 'Project' => @wiki_content.project.identifier,
- 'Wiki-Page-Id' => @wiki_content.page.id,
- 'Type' => 'Wiki'
-
+ open_project_wiki_headers @wiki_content
message_id @wiki_content, user
- with_locale_for(user) do
- subject = "[#{@wiki_content.project.name}] #{t(:mail_subject_wiki_content_updated, id: @wiki_content.page.title)}"
- mail to: user.mail, subject: subject
- end
+ send_mail(user,
+ "[#{@wiki_content.project.name}] #{t(:mail_subject_wiki_content_updated, id: @wiki_content.page.title)}")
end
def message_posted(user, message)
- @message = message
- @message_url = topic_url(@message.root, r: @message.id, anchor: "message-#{@message.id}")
-
- open_project_headers 'Project' => @message.project.identifier,
- 'Message-Id' => @message.parent_id || @message.id,
- 'Type' => 'Forum'
+ @message = message
+ open_project_message_headers(@message)
message_id @message, user
references @message.parent, user if @message.parent
- with_locale_for(user) do
- subject = "[#{@message.forum.project.name} - #{@message.forum.name} - msg#{@message.root.id}] #{@message.subject}"
- mail to: user.mail, subject: subject
- end
+ send_mail(user,
+ "[#{@message.forum.project.name} - #{@message.forum.name} - msg#{@message.root.id}] #{@message.subject}")
end
def account_activated(user)
@@ -184,10 +161,8 @@ class UserMailer < ApplicationMailer
open_project_headers 'Type' => 'Account'
- with_locale_for(user) do
- subject = t(:mail_subject_register, value: Setting.app_title)
- mail to: user.mail, subject: subject
- end
+ send_mail(user,
+ t(:mail_subject_register, value: Setting.app_title))
end
def account_information(user, password)
@@ -196,10 +171,8 @@ class UserMailer < ApplicationMailer
open_project_headers 'Type' => 'Account'
- with_locale_for(user) do
- subject = t(:mail_subject_register, value: Setting.app_title)
- mail to: user.mail, subject: subject
- end
+ send_mail(user,
+ t(:mail_subject_register, value: Setting.app_title))
end
def account_activation_requested(admin, user)
@@ -211,10 +184,8 @@ class UserMailer < ApplicationMailer
open_project_headers 'Type' => 'Account'
- with_locale_for(admin) do
- subject = t(:mail_subject_account_activation_request, value: Setting.app_title)
- mail to: admin.mail, subject: subject
- end
+ send_mail(admin,
+ t(:mail_subject_account_activation_request, value: Setting.app_title))
end
def reminder_mail(user, issues, days, group = nil)
@@ -234,14 +205,13 @@ class UserMailer < ApplicationMailer
open_project_headers 'Type' => 'Issue'
- with_locale_for(user) do
- subject = if @group
- t(:mail_subject_group_reminder, count: @issues.size, days: @days, group: @group.name)
- else
- t(:mail_subject_reminder, count: @issues.size, days: @days)
- end
- mail to: user.mail, subject: subject
- end
+ subject = if @group
+ t(:mail_subject_group_reminder, count: @issues.size, days: @days, group: @group.name)
+ else
+ t(:mail_subject_reminder, count: @issues.size, days: @days)
+ end
+
+ send_mail(user, subject)
end
##
@@ -252,41 +222,21 @@ class UserMailer < ApplicationMailer
def activation_limit_reached(user_email, admin)
@email = user_email
- with_locale_for(admin) do
- mail to: admin.mail, subject: t("mail_user_activation_limit_reached.subject")
- end
+ send_mail(admin, t("mail_user_activation_limit_reached.subject"))
end
-end
-##
-# Interceptors
-#
-# These are registered in config/initializers/register_mail_interceptors.rb
-#
-# Unfortunately, this results in changes on the interceptor classes during development mode
-# not being reflected until a server restart.
+ private
-class DefaultHeadersInterceptor
- def self.delivering_email(mail)
- mail.headers(default_headers)
+ def open_project_wiki_headers(wiki_content)
+ open_project_headers 'Project' => wiki_content.project.identifier,
+ 'Wiki-Page-Id' => wiki_content.page.id,
+ 'Type' => 'Wiki'
end
- def self.default_headers
- {
- 'X-Mailer' => 'OpenProject',
- 'X-OpenProject-Host' => Setting.host_name,
- 'X-OpenProject-Site' => Setting.app_title,
- 'Precedence' => 'bulk',
- 'Auto-Submitted' => 'auto-generated'
- }
+ def open_project_message_headers(message)
+ open_project_headers 'Project' => message.project.identifier,
+ 'Message-Id' => message.parent_id || message.id,
+ 'Type' => 'Forum'
end
end
-class DoNotSendMailsWithoutReceiverInterceptor
- def self.delivering_email(mail)
- receivers = [mail.to, mail.cc, mail.bcc]
- # the above fields might be empty arrays (if entries have been removed
- # by another interceptor) or nil, therefore checking for blank?
- mail.perform_deliveries = false if receivers.all?(&:blank?)
- end
-end
diff --git a/app/mailers/work_package_mailer.rb b/app/mailers/work_package_mailer.rb
index 247836be94e..255c74e68c6 100644
--- a/app/mailers/work_package_mailer.rb
+++ b/app/mailers/work_package_mailer.rb
@@ -65,8 +65,9 @@ class WorkPackageMailer < ApplicationMailer
private
- def subject_for_work_package(wp)
- "#{wp.project.name} - #{wp.status.name} #{wp.type.name} ##{wp.id}: #{wp.subject}"
+ def subject_for_work_package(work_package)
+ "#{work_package.project.name} - #{work_package.status.name} #{work_package.type.name} " +
+ "##{work_package.id}: #{work_package.subject}"
end
def set_work_package_headers(work_package)
diff --git a/app/views/user_mailer/message_posted.html.erb b/app/views/user_mailer/message_posted.html.erb
index 623d6d2c0fa..5902f0057dd 100644
--- a/app/views/user_mailer/message_posted.html.erb
+++ b/app/views/user_mailer/message_posted.html.erb
@@ -28,7 +28,7 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
<h1>
- <%= @message.forum.project.name %> - <%= @message.forum.name %>: <%= link_to(@message.subject, @message_url) %>
+ <%= @message.forum.project.name %> - <%= @message.forum.name %>: <%= link_to(@message.subject, message_url(@message)) %>
</h1>
<em><%= @message.author %></em>
diff --git a/app/views/user_mailer/message_posted.text.erb b/app/views/user_mailer/message_posted.text.erb
index 393c9c20119..90092a4eac8 100644
--- a/app/views/user_mailer/message_posted.text.erb
+++ b/app/views/user_mailer/message_posted.text.erb
@@ -27,7 +27,7 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
-<%= @message_url %>
+<%= message_url(@message) %>
<%= @message.author %>
<%= @message.content %>
diff --git a/config/initializers/register_mail_interceptors.rb b/config/initializers/register_mail_interceptors.rb
index fa0ab7d5364..609eb9302ea 100644
--- a/config/initializers/register_mail_interceptors.rb
+++ b/config/initializers/register_mail_interceptors.rb
@@ -31,6 +31,6 @@
# Register interceptors defined in app/mailers/user_mailer.rb
# Do this here, so they aren't registered multiple times due to reloading in development mode.
-UserMailer.register_interceptor(DefaultHeadersInterceptor)
+ApplicationMailer.register_interceptor(DefaultHeadersInterceptor)
# following needs to be the last interceptor
-UserMailer.register_interceptor(DoNotSendMailsWithoutReceiverInterceptor)
+ApplicationMailer.register_interceptor(DoNotSendMailsWithoutReceiverInterceptor)
diff --git a/spec/workers/mails/shared/watcher_job.rb b/spec/workers/mails/shared/watcher_job.rb
index 982587dcee0..3c5ae106d22 100644
--- a/spec/workers/mails/shared/watcher_job.rb
+++ b/spec/workers/mails/shared/watcher_job.rb
@@ -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(WorkPackageMailer).to receive(:watcher_changed).and_return(mail)
+ allow(WorkPackageMailer).to receive(:watcher_changed).and_return(mail)
end
it 'raises the error' do