From 789e3775df1d5d63bf20abac98d52920250a3d38 Mon Sep 17 00:00:00 2001 From: Kane York Date: Mon, 2 Nov 2020 20:00:11 -0800 Subject: [PATCH] FIX: Make all email subject vars available in notification subjects (#11064) A site owner attempting to use both the email_subject site setting and translation overrides for normal post notification email subjects would find themselves frusturated at the lack of template argument parity. Make all the variables available for translation overrides by adding the subject variables to the custom interpolation keys list and applying them. Reported at https://meta.discourse.org/t/customize-subject-format-for-standard-emails/20801/47?u=riking --- app/models/translation_override.rb | 7 ++++++ lib/email/message_builder.rb | 10 +++++++- spec/components/email/message_builder_spec.rb | 23 ++++++++++++++----- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/app/models/translation_override.rb b/app/models/translation_override.rb index 1f217e9f847..1071d5dd2f0 100644 --- a/app/models/translation_override.rb +++ b/app/models/translation_override.rb @@ -9,6 +9,13 @@ class TranslationOverride < ActiveRecord::Base topic_title_url_encoded site_title_url_encoded context + + site_name + optional_re + optional_pm + optional_cat + optional_tags + topic_title } } include ActiveSupport::Deprecation::DeprecatedConstantAccessor diff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rb index 463e9c08684..fa42814bb24 100644 --- a/lib/email/message_builder.rb +++ b/lib/email/message_builder.rb @@ -53,7 +53,15 @@ module Email def subject if @opts[:template] && TranslationOverride.exists?(locale: I18n.locale, translation_key: "#{@opts[:template]}.subject_template") - subject = I18n.t("#{@opts[:template]}.subject_template", @template_args) + augmented_template_args = @template_args.merge({ + site_name: @template_args[:email_prefix], + optional_re: @opts[:add_re_to_subject] ? I18n.t('subject_re') : '', + optional_pm: @opts[:private_reply] ? @template_args[:subject_pm] : '', + optional_cat: @template_args[:show_category_in_subject] ? "[#{@template_args[:show_category_in_subject]}] " : '', + optional_tags: @template_args[:show_tags_in_subject] ? "#{@template_args[:show_tags_in_subject]} " : '', + topic_title: @template_args[:topic_title] ? @template_args[:topic_title] : '', + }) + subject = I18n.t("#{@opts[:template]}.subject_template", augmented_template_args) elsif @opts[:use_site_subject] subject = String.new(SiteSetting.email_subject) subject.gsub!("%{site_name}", @template_args[:email_prefix]) diff --git a/spec/components/email/message_builder_spec.rb b/spec/components/email/message_builder_spec.rb index 939257d201b..9043652b7c1 100644 --- a/spec/components/email/message_builder_spec.rb +++ b/spec/components/email/message_builder_spec.rb @@ -258,17 +258,28 @@ describe Email::MessageBuilder do end context "when use_site_subject is true" do - let(:templated_builder) { Email::MessageBuilder.new(to_address, template: 'mystery', use_site_subject: true) } + let(:templated_builder) { Email::MessageBuilder.new(to_address, template: 'user_notifications.user_replied', use_site_subject: true, topic_title: "Topic Title") } it "can use subject override" do - override = TranslationOverride.create( - locale: I18n.locale, - translation_key: "mystery.subject_template", - value: "my customized subject" + override = TranslationOverride.upsert!( + I18n.locale, + "user_notifications.user_replied.subject_template", + "my customized subject" ) - I18n.expects(:t).with("mystery.subject_template", templated_builder.template_args).returns(override.value) + override.save! expect(templated_builder.subject).to eq(override.value) end + + it "can use interpolation arguments in the override" do + SiteSetting.email_prefix = 'some email prefix' + override = TranslationOverride.upsert!( + I18n.locale, + "user_notifications.user_replied.subject_template", + "[%{site_name}] %{topic_title} my customized subject" + ).save! + expect(templated_builder.subject).to match("some email prefix") + expect(templated_builder.subject).to match("customized subject") + end end end