Can revert changes to email templates

This commit is contained in:
Robin Ward 2015-11-20 12:30:04 -05:00
parent bb1d0dea8a
commit 8eeb027c65
10 changed files with 75 additions and 11 deletions

View File

@ -6,11 +6,23 @@ export default Ember.Controller.extend(bufferedProperty('emailTemplate'), {
actions: {
saveChanges() {
const model = this.get('emailTemplate');
const buffered = this.get('buffered');
model.save(buffered.getProperties('subject', 'body')).then(() => {
this.get('emailTemplate').save(buffered.getProperties('subject', 'body')).then(() => {
this.set('saved', true);
}).catch(popupAjaxError);
},
revertChanges() {
this.set('saved', false);
bootbox.confirm(I18n.t('admin.customize.email_templates.revert_confirm'), result => {
if (result) {
this.get('emailTemplate').revert().then(props => {
const buffered = this.get('buffered');
buffered.setProperties(props);
this.commitBuffer();
}).catch(popupAjaxError);
}
});
}
}
});

View File

@ -0,0 +1,12 @@
import RestModel from 'discourse/models/rest';
const { getProperties } = Ember;
console.log(getProperties);
export default RestModel.extend({
revert() {
return Discourse.ajax(`/admin/customize/email_templates/${this.get('id')}`, {
method: 'DELETE'
}).then(result => getProperties(result.email_template, 'subject', 'body', 'can_revert'));
}
});

View File

@ -1,2 +1,5 @@
{{d-button action="saveChanges" disabled=buttonDisabled label=savingText}}
{{#if saved}}{{i18n 'saved'}}{{/if}}
{{d-button action="saveChanges" disabled=buttonDisabled label=savingText class="btn-primary"}}
{{yield}}
<div class='save-messages'>
{{#if saved}}{{i18n 'saved'}}{{/if}}
</div>

View File

@ -9,5 +9,9 @@
{{d-editor value=buffered.body}}
</label>
{{save-controls model=emailTemplate action="saveChanges" saved=saved}}
{{#save-controls model=emailTemplate action="saveChanges" saved=saved}}
{{#if emailTemplate.can_revert}}
{{d-button action="revertChanges" label="admin.customize.email_templates.revert"}}
{{/if}}
{{/save-controls}}
</div>

View File

@ -122,6 +122,10 @@ td.flaggers td {
.admin-container .controls {
@include clearfix;
.save-messages {
margin-top: 1em;
}
}
.admin-title {

View File

@ -35,7 +35,6 @@ class Admin::EmailTemplatesController < Admin::AdminController
def update
et = params[:email_template]
key = params[:id]
raise Discourse::NotFound unless self.class.email_keys.include?(params[:id])
TranslationOverride.upsert!(I18n.locale, "#{key}.subject_template", et[:subject])
@ -44,6 +43,13 @@ class Admin::EmailTemplatesController < Admin::AdminController
render_serialized(key, AdminEmailTemplateSerializer, root: 'email_template', rest_serializer: true)
end
def revert
key = params[:id]
raise Discourse::NotFound unless self.class.email_keys.include?(params[:id])
TranslationOverride.revert!(I18n.locale, "#{key}.subject_template", "#{key}.text_body_template")
render_serialized(key, AdminEmailTemplateSerializer, root: 'email_template', rest_serializer: true)
end
def index
render_serialized(self.class.email_keys, AdminEmailTemplateSerializer, root: 'email_templates', rest_serializer: true)
end

View File

@ -6,7 +6,18 @@ class TranslationOverride < ActiveRecord::Base
params = { locale: locale, translation_key: key }
row_count = where(params).update_all(value: value)
create!(params.merge(value: value)) if row_count == 0
i18n_changed
end
def self.revert!(locale, *keys)
TranslationOverride.where(locale: locale, translation_key: keys).delete_all
i18n_changed
end
protected
def self.i18n_changed
I18n.reload!
MessageBus.publish('/i18n-flush', { refresh: true })
end

View File

@ -1,5 +1,5 @@
class AdminEmailTemplateSerializer < ApplicationSerializer
attributes :id, :title, :subject, :body
attributes :id, :title, :subject, :body, :can_revert?
def id
object
@ -10,10 +10,19 @@ class AdminEmailTemplateSerializer < ApplicationSerializer
end
def subject
I18n.t("#{object}.subject_template")
@subject ||= I18n.t("#{object}.subject_template")
end
def body
I18n.t("#{object}.text_body_template")
@body ||= I18n.t("#{object}.text_body_template")
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
end
end
end

View File

@ -2093,6 +2093,8 @@ en:
subject: "Subject"
body: "Body"
none_selected: "Select an email template to begin editing."
revert: "Revert Changes"
revert_confirm: "Are you sure you want to revert your changes?"
css_html:
title: "CSS/HTML"

View File

@ -163,6 +163,7 @@ Discourse::Application.routes.draw do
get 'email_templates' => 'email_templates#index'
match 'email_templates/(:id)' => 'email_templates#show', :constraints => { :id => /[0-9a-z\_\.]+/ }, via: :get
match 'email_templates/(:id)' => 'email_templates#update', :constraints => { :id => /[0-9a-z\_\.]+/ }, via: :put
match 'email_templates/(:id)' => 'email_templates#revert', :constraints => { :id => /[0-9a-z\_\.]+/ }, via: :delete
end
resources :embeddable_hosts, constraints: AdminConstraint.new