From f1c67729ded975296e1c91fa87b48a3490c4ef28 Mon Sep 17 00:00:00 2001 From: Osama Sayegh Date: Tue, 2 Jul 2019 04:53:16 +0300 Subject: [PATCH] Different fix (#7815) --- ...dmin-customize-email-templates-edit.js.es6 | 7 ++++++ .../controllers/admin-site-text-edit.js.es6 | 6 +++++ .../customize-email-templates-edit.hbs | 2 +- .../admin/templates/site-text-edit.hbs | 2 +- .../admin/email_templates_controller.rb | 6 ++++- .../admin/site_texts_controller.rb | 6 ++++- .../admin_email_template_serializer.rb | 12 +++++---- app/serializers/site_text_serializer.rb | 4 +-- .../admin/email_templates_controller_spec.rb | 25 ++++++++++++++++++- .../admin/site_texts_controller_spec.rb | 20 +++++++++++++++ 10 files changed, 78 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/admin/controllers/admin-customize-email-templates-edit.js.es6 b/app/assets/javascripts/admin/controllers/admin-customize-email-templates-edit.js.es6 index 926fd565e14..68a78a7d7f8 100644 --- a/app/assets/javascripts/admin/controllers/admin-customize-email-templates-edit.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-customize-email-templates-edit.js.es6 @@ -5,6 +5,13 @@ import computed from "ember-addons/ember-computed-decorators"; export default Ember.Controller.extend(bufferedProperty("emailTemplate"), { saved: false, + @computed("buffered.body", "buffered.subject") + saveDisabled(body, subject) { + return ( + this.emailTemplate.body === body && this.emailTemplate.subject === subject + ); + }, + @computed("buffered") hasMultipleSubjects(buffered) { if (buffered.getProperties("subject")["subject"]) { diff --git a/app/assets/javascripts/admin/controllers/admin-site-text-edit.js.es6 b/app/assets/javascripts/admin/controllers/admin-site-text-edit.js.es6 index cb361ed8187..4aa1c3d4288 100644 --- a/app/assets/javascripts/admin/controllers/admin-site-text-edit.js.es6 +++ b/app/assets/javascripts/admin/controllers/admin-site-text-edit.js.es6 @@ -1,9 +1,15 @@ import { popupAjaxError } from "discourse/lib/ajax-error"; import { bufferedProperty } from "discourse/mixins/buffered-content"; +import computed from "ember-addons/ember-computed-decorators"; export default Ember.Controller.extend(bufferedProperty("siteText"), { saved: false, + @computed("buffered.value") + saveDisabled(value) { + return this.siteText.value === value; + }, + actions: { saveChanges() { const buffered = this.buffered; diff --git a/app/assets/javascripts/admin/templates/customize-email-templates-edit.hbs b/app/assets/javascripts/admin/templates/customize-email-templates-edit.hbs index a1e3910e11b..4b80de8718a 100644 --- a/app/assets/javascripts/admin/templates/customize-email-templates-edit.hbs +++ b/app/assets/javascripts/admin/templates/customize-email-templates-edit.hbs @@ -10,7 +10,7 @@ {{d-editor value=buffered.body}} - {{#save-controls model=emailTemplate action=(action "saveChanges") saved=saved}} + {{#save-controls model=emailTemplate action=(action "saveChanges") saved=saved saveDisabled=saveDisabled}} {{#if emailTemplate.can_revert}} {{d-button action=(action "revertChanges") label="admin.customize.email_templates.revert"}} {{/if}} diff --git a/app/assets/javascripts/admin/templates/site-text-edit.hbs b/app/assets/javascripts/admin/templates/site-text-edit.hbs index 6c271600aa5..dc42c3ba26d 100644 --- a/app/assets/javascripts/admin/templates/site-text-edit.hbs +++ b/app/assets/javascripts/admin/templates/site-text-edit.hbs @@ -6,7 +6,7 @@ {{expanding-text-area value=buffered.value rows="1" class="site-text-value"}} - {{#save-controls model=siteText action=(action "saveChanges") saved=saved}} + {{#save-controls model=siteText action=(action "saveChanges") saved=saved saveDisabled=saveDisabled}} {{#if siteText.can_revert}} {{d-button action=(action "revertChanges") label="admin.site_text.revert" class="revert-site-text"}} {{/if}} diff --git a/app/controllers/admin/email_templates_controller.rb b/app/controllers/admin/email_templates_controller.rb index d00fca83f22..ff522341cd0 100644 --- a/app/controllers/admin/email_templates_controller.rb +++ b/app/controllers/admin/email_templates_controller.rb @@ -108,7 +108,7 @@ class Admin::EmailTemplatesController < Admin::AdminController end def index - render_serialized(self.class.email_keys, AdminEmailTemplateSerializer, root: 'email_templates', rest_serializer: true) + render_serialized(self.class.email_keys, AdminEmailTemplateSerializer, root: 'email_templates', rest_serializer: true, overridden_keys: overridden_keys) end private @@ -151,4 +151,8 @@ class Admin::EmailTemplatesController < Admin::AdminController message = update_result[:error_messages].join("
") I18n.t("errors.format_with_full_message", attribute: attribute, message: message) end + + def overridden_keys + TranslationOverride.where(locale: I18n.locale).pluck(:translation_key) + end end diff --git a/app/controllers/admin/site_texts_controller.rb b/app/controllers/admin/site_texts_controller.rb index c1be047c3e3..a5f9d8e766e 100644 --- a/app/controllers/admin/site_texts_controller.rb +++ b/app/controllers/admin/site_texts_controller.rb @@ -45,7 +45,7 @@ class Admin::SiteTextsController < Admin::AdminController end extras[:has_more] = true if results.size > 50 - render_serialized(results[0..49], SiteTextSerializer, root: 'site_texts', rest_serializer: true, extras: extras) + render_serialized(results[0..49], SiteTextSerializer, root: 'site_texts', rest_serializer: true, extras: extras, overridden_keys: overridden_keys) end def show @@ -172,4 +172,8 @@ class Admin::SiteTextsController < Admin::AdminController [k, value[k] || fallback_value[k] || fallback_value[:other]] end.to_h end + + def overridden_keys + TranslationOverride.where(locale: I18n.locale).pluck(:translation_key) + end end diff --git a/app/serializers/admin_email_template_serializer.rb b/app/serializers/admin_email_template_serializer.rb index d0e36bd9e58..07bfa929915 100644 --- a/app/serializers/admin_email_template_serializer.rb +++ b/app/serializers/admin_email_template_serializer.rb @@ -28,11 +28,13 @@ class AdminEmailTemplateSerializer < ApplicationSerializer end def can_revert? - current_body, current_subject = body, subject - - I18n.overrides_disabled do - return I18n.t("#{object}.subject_template") != current_subject || - I18n.t("#{object}.text_body_template") != current_body + subject_key = "#{object}.subject_template" + body_key = "#{object}.text_body_template" + keys = [subject_key, body_key] + if options[:overridden_keys] + keys.any? { |k| options[:overridden_keys].include?(k) } + else + TranslationOverride.exists?(locale: I18n.locale, translation_key: keys) end end end diff --git a/app/serializers/site_text_serializer.rb b/app/serializers/site_text_serializer.rb index 06987330b05..8415bc5bc3c 100644 --- a/app/serializers/site_text_serializer.rb +++ b/app/serializers/site_text_serializer.rb @@ -12,8 +12,8 @@ class SiteTextSerializer < ApplicationSerializer end def overridden? - if I18n.exists?(object[:id]) - I18n.overrides_disabled { I18n.t(object[:id]) != object[:value] } + if options[:overridden_keys] + options[:overridden_keys].include?(object[:id]) else TranslationOverride.exists?(locale: I18n.locale, translation_key: object[:id]) end diff --git a/spec/requests/admin/email_templates_controller_spec.rb b/spec/requests/admin/email_templates_controller_spec.rb index 458665a918b..9b98d4880aa 100644 --- a/spec/requests/admin/email_templates_controller_spec.rb +++ b/spec/requests/admin/email_templates_controller_spec.rb @@ -40,6 +40,29 @@ RSpec.describe Admin::EmailTemplatesController do json = ::JSON.parse(response.body) expect(json['email_templates']).to be_present end + + it 'returns overridden = true if subject or body has translation_overrides record' do + sign_in(admin) + + put '/admin/customize/email_templates/user_notifications.admin_login', params: { + email_template: { subject: original_subject, body: original_body } + }, headers: headers + expect(response.status).to eq(200) + + get '/admin/customize/email_templates.json' + expect(response.status).to eq(200) + templates = JSON.parse(response.body)['email_templates'] + template = templates.find { |t| t['id'] == 'user_notifications.admin_login' } + expect(template['can_revert']).to eq(true) + + TranslationOverride.destroy_all + + get '/admin/customize/email_templates.json' + expect(response.status).to eq(200) + templates = JSON.parse(response.body)['email_templates'] + template = templates.find { |t| t['id'] == 'user_notifications.admin_login' } + expect(template['can_revert']).to eq(false) + end end context "#update" do @@ -142,7 +165,7 @@ RSpec.describe Admin::EmailTemplatesController do include_examples "invalid email template" end - context "when subject and body are invalid invalid" do + context "when subject and body are invalid" do let(:email_subject) { 'Subject with %{invalid} interpolation key' } let(:email_body) { 'Body with some invalid interpolation keys: %{invalid}' } diff --git a/spec/requests/admin/site_texts_controller_spec.rb b/spec/requests/admin/site_texts_controller_spec.rb index 006476b457a..89132b9a708 100644 --- a/spec/requests/admin/site_texts_controller_spec.rb +++ b/spec/requests/admin/site_texts_controller_spec.rb @@ -177,6 +177,26 @@ RSpec.describe Admin::SiteTextsController do expect(response.status).to eq(404) end + it 'returns overridden = true if there is a translation_overrides record for the key' do + key = 'js.topic.list' + put "/admin/customize/site_texts/#{key}.json", params: { + site_text: { value: I18n.t(key) } + } + expect(response.status).to eq(200) + + get "/admin/customize/site_texts/#{key}.json" + expect(response.status).to eq(200) + json = JSON.parse(response.body) + expect(json['site_text']['overridden']).to eq(true) + + TranslationOverride.destroy_all + + get "/admin/customize/site_texts/#{key}.json" + expect(response.status).to eq(200) + json = JSON.parse(response.body) + expect(json['site_text']['overridden']).to eq(false) + end + context 'plural keys' do before do I18n.backend.store_translations(:en, colour: { one: '%{count} colour', other: '%{count} colours' })