mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-12-11 00:02:05 -05:00
1833b43ae2
Upon saving a badge or requesting a badge result preview, BadgeGranter.contract_checks! will examine the provided badge SQL for some contractual obligations - namely, the returned columns and use of trigger parameters. Saving the badge is wrapped in a transaction to make this easier, by raising ActiveRecord::Rollback on a detected violation. On the client, a modal view is added for the badge query sample run results, named admin-badge-preview. The preview action is moved up to the route. The save action, on failure, triggers a 'saveError' action (also in the route). The preview action gains a new parameter, 'explain', which will give the output of an EXPLAIN query for the badge sql, which can be used by forum admins to estimate the cost of their badge queries. The preview link is replaced by two links, one which omits (false) and includes (true) the EXPLAIN query. The Badge.save() method is amended to propogate errors. Badge::Trigger gets some utility methods for use in the BadgeGranter.contract_checks! method. Additionally, extra checks outside of BadgeGranter.contract_checks! are added in the preview() method, to cover cases of null granted_at columns. An uninitialized variable path is removed in the backfill() method. TODO - it would be nice to be able to get the actual names of all columns the provided query returns, so we could give more errors
54 lines
1.7 KiB
JavaScript
54 lines
1.7 KiB
JavaScript
Discourse.AdminBadgesRoute = Discourse.Route.extend({
|
|
setupController: function(controller) {
|
|
Discourse.ajax('/admin/badges.json').then(function(json){
|
|
|
|
controller.set('badgeGroupings', Em.A(json.badge_groupings));
|
|
controller.set('badgeTypes', json.badge_types);
|
|
controller.set('protectedSystemFields', json.admin_badges.protected_system_fields);
|
|
var triggers = [];
|
|
_.each(json.admin_badges.triggers,function(v,k){
|
|
triggers.push({id: v, name: I18n.t('admin.badges.trigger_type.'+k)});
|
|
});
|
|
controller.set('badgeTriggers', triggers);
|
|
controller.set('model', Discourse.Badge.createFromJson(json));
|
|
});
|
|
},
|
|
|
|
actions: {
|
|
editGroupings: function(model) {
|
|
Discourse.Route.showModal(this, 'admin_edit_badge_groupings', model);
|
|
},
|
|
|
|
saveError: function(jqXhr) {
|
|
if (jqXhr.status === 422) {
|
|
Discourse.Route.showModal(this, 'admin_badge_preview', jqXhr.responseJSON);
|
|
} else {
|
|
Em.Logger.error(jqXhr);
|
|
bootbox.alert(I18n.t('errors.description.unknown'));
|
|
}
|
|
},
|
|
|
|
preview: function(badge, explain) {
|
|
var self = this;
|
|
|
|
badge.set('preview_loading', true);
|
|
Discourse.ajax('/admin/badges/preview.json', {
|
|
method: 'post',
|
|
data: {
|
|
sql: badge.query,
|
|
target_posts: !!badge.target_posts,
|
|
trigger: badge.trigger,
|
|
explain: explain
|
|
}
|
|
}).then(function(json) {
|
|
badge.set('preview_loading', false);
|
|
Discourse.Route.showModal(self, 'admin_badge_preview', json);
|
|
}).catch(function(error) {
|
|
badge.set('preview_loading', false);
|
|
Em.Logger.error(error);
|
|
bootbox.alert("Network error");
|
|
});
|
|
}
|
|
}
|
|
|
|
});
|