Move some display logic out of model, upgrade old code patterns

This commit is contained in:
Robin Ward 2018-05-25 11:33:01 -04:00
parent fdce116838
commit c658fb6e31
4 changed files with 42 additions and 42 deletions

View File

@ -50,6 +50,36 @@ export default Ember.Controller.extend(CanCheckEmails, {
return userPath(`${username}/preferences`); return userPath(`${username}/preferences`);
}, },
@computed('model.can_delete_all_posts', 'model.staff', 'model.post_count')
deleteAllPostsExplanation(canDeleteAllPosts, staff, postCount) {
if (canDeleteAllPosts) {
return null;
}
if (staff) {
return I18n.t('admin.user.delete_posts_forbidden_because_staff');
}
if (postCount > this.siteSettings.delete_all_posts_max) {
return I18n.t('admin.user.cant_delete_all_too_many_posts', {count: this.siteSettings.delete_all_posts_max});
} else {
return I18n.t('admin.user.cant_delete_all_posts', {count: this.siteSettings.delete_user_max_post_age});
}
},
@computed('model.canBeDeleted', 'model.staff')
deleteExplanation(canBeDeleted, staff) {
if (canBeDeleted) {
return null;
}
if (staff) {
return I18n.t('admin.user.delete_forbidden_because_staff');
} else {
return I18n.t('admin.user.delete_forbidden', {count: this.siteSettings.delete_user_max_post_age});
}
},
actions: { actions: {
impersonate() { return this.get("model").impersonate(); }, impersonate() { return this.get("model").impersonate(); },

View File

@ -87,21 +87,6 @@ const AdminUser = Discourse.User.extend({
}).then(() => this.set('api_key', null)); }).then(() => this.set('api_key', null));
}, },
deleteAllPostsExplanation: function() {
if (!this.get('can_delete_all_posts')) {
if (this.get('deleteForbidden') && this.get('staff')) {
return I18n.t('admin.user.delete_posts_forbidden_because_staff');
}
if (this.get('post_count') > Discourse.SiteSettings.delete_all_posts_max) {
return I18n.t('admin.user.cant_delete_all_too_many_posts', {count: Discourse.SiteSettings.delete_all_posts_max});
} else {
return I18n.t('admin.user.cant_delete_all_posts', {count: Discourse.SiteSettings.delete_user_max_post_age});
}
} else {
return null;
}
}.property('can_delete_all_posts', 'deleteForbidden'),
deleteAllPosts() { deleteAllPosts() {
const user = this, const user = this,
message = I18n.messageFormat('admin.user.delete_all_posts_confirm_MF', { "POSTS": user.get('post_count'), "TOPICS": user.get('topic_count') }), message = I18n.messageFormat('admin.user.delete_all_posts_confirm_MF', { "POSTS": user.get('post_count'), "TOPICS": user.get('topic_count') }),
@ -351,8 +336,6 @@ const AdminUser = Discourse.User.extend({
}).catch(popupAjaxError); }).catch(popupAjaxError);
}, },
anonymizeForbidden: Em.computed.not("can_be_anonymized"),
anonymize() { anonymize() {
const user = this, const user = this,
message = I18n.t("admin.user.anonymize_confirm"); message = I18n.t("admin.user.anonymize_confirm");
@ -391,20 +374,6 @@ const AdminUser = Discourse.User.extend({
bootbox.dialog(message, buttons, { "classes": "delete-user-modal" }); bootbox.dialog(message, buttons, { "classes": "delete-user-modal" });
}, },
deleteForbidden: Em.computed.not("canBeDeleted"),
deleteExplanation: function() {
if (this.get('deleteForbidden')) {
if (this.get('staff')) {
return I18n.t('admin.user.delete_forbidden_because_staff');
} else {
return I18n.t('admin.user.delete_forbidden', {count: Discourse.SiteSettings.delete_user_max_post_age});
}
} else {
return null;
}
}.property('deleteForbidden'),
destroy(opts) { destroy(opts) {
const user = this, const user = this,
message = I18n.t("admin.user.delete_confirm"), message = I18n.t("admin.user.delete_confirm"),

View File

@ -408,7 +408,6 @@
</div> </div>
{{/if}} {{/if}}
</section> </section>
{{#if currentUser.admin}} {{#if currentUser.admin}}
@ -469,7 +468,7 @@
{{d-button class="btn-danger" action="deleteAllPosts" icon="trash-o" label="admin.user.delete_all_posts"}} {{d-button class="btn-danger" action="deleteAllPosts" icon="trash-o" label="admin.user.delete_all_posts"}}
{{/if}} {{/if}}
{{else}} {{else}}
{{model.deleteAllPostsExplanation}} {{deleteAllPostsExplanation}}
{{/if}} {{/if}}
</div> </div>
</div> </div>
@ -550,28 +549,26 @@
{{/if}} {{/if}}
{{/if}} {{/if}}
{{#unless model.anonymizeForbidden}} {{#if model.can_be_anonymized}}
{{d-button label="admin.user.anonymize" {{d-button label="admin.user.anonymize"
icon="exclamation-triangle" icon="exclamation-triangle"
class="btn-danger" class="btn-danger"
disabled=model.anonymizeForbidden
action="anonymize"}} action="anonymize"}}
{{/unless}} {{/if}}
{{#unless model.deleteForbidden}} {{#if model.canBeDeleted}}
{{d-button label="admin.user.delete" {{d-button label="admin.user.delete"
icon="exclamation-triangle" icon="exclamation-triangle"
class="btn-danger" class="btn-danger"
disabled=model.deleteForbidden
action="destroy"}} action="destroy"}}
{{/unless}} {{/if}}
</div> </div>
{{#if model.deleteExplanation}} {{#if deleteExplanation}}
<div class="clearfix"></div> <div class="clearfix"></div>
<br> <br>
<div class="pull-right"> <div class="pull-right">
{{d-icon "exclamation-triangle"}} {{model.deleteExplanation}} {{d-icon "exclamation-triangle"}} {{deleteExplanation}}
</div> </div>
{{/if}} {{/if}}
</section> </section>

View File

@ -1,5 +1,9 @@
class PenaltyCountsSerializer < ApplicationSerializer class PenaltyCountsSerializer < ApplicationSerializer
attributes :silenced, :suspended attributes :silenced, :suspended, :total
def total
object.silenced + object.suspended
end
def silenced def silenced
object.silenced object.silenced