REFACTOR: badge model (#7690)

This commit is contained in:
Joffrey JAFFEUX 2019-06-04 17:12:02 +02:00 committed by GitHub
parent 1bca6fdf51
commit 7b19e63ce7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 87 deletions

View File

@ -11,23 +11,14 @@ const Badge = RestModel.extend({
return Discourse.getURL(`/badges/${this.id}/${this.slug}`); return Discourse.getURL(`/badges/${this.id}/${this.slug}`);
}, },
/** updateFromJson(json) {
Update this badge with the response returned by the server on save.
@method updateFromJson
@param {Object} json The JSON response returned by the server
**/
updateFromJson: function(json) {
const self = this;
if (json.badge) { if (json.badge) {
Object.keys(json.badge).forEach(function(key) { Object.keys(json.badge).forEach(key => this.set(key, json.badge[key]));
self.set(key, json.badge[key]);
});
} }
if (json.badge_types) { if (json.badge_types) {
json.badge_types.forEach(function(badgeType) { json.badge_types.forEach(badgeType => {
if (badgeType.id === self.get("badge_type_id")) { if (badgeType.id === this.badge_type_id) {
self.set("badge_type", Object.create(badgeType)); this.set("badge_type", Object.create(badgeType));
} }
}); });
} }
@ -36,77 +27,57 @@ const Badge = RestModel.extend({
@computed("badge_type.name") @computed("badge_type.name")
badgeTypeClassName(type) { badgeTypeClassName(type) {
type = type || ""; type = type || "";
return "badge-type-" + type.toLowerCase(); return `badge-type-${type.toLowerCase()}`;
}, },
/** save(data) {
Save and update the badge from the server's response.
@method save
@returns {Promise} A promise that resolves to the updated `Badge`
**/
save: function(data) {
let url = "/admin/badges", let url = "/admin/badges",
requestType = "POST"; type = "POST";
const self = this;
if (this.id) { if (this.id) {
// We are updating an existing badge. // We are updating an existing badge.
url += "/" + this.id; url += `/${this.id}`;
requestType = "PUT"; type = "PUT";
} }
return ajax(url, { return ajax(url, { type, data })
type: requestType, .then(json => {
data: data this.updateFromJson(json);
}) return this;
.then(function(json) {
self.updateFromJson(json);
return self;
}) })
.catch(function(error) { .catch(error => {
throw new Error(error); throw new Error(error);
}); });
}, },
/** destroy() {
Destroy the badge.
@method destroy
@returns {Promise} A promise that resolves to the server response
**/
destroy: function() {
if (this.newBadge) return Ember.RSVP.resolve(); if (this.newBadge) return Ember.RSVP.resolve();
return ajax("/admin/badges/" + this.id, {
return ajax(`/admin/badges/${this.id}`, {
type: "DELETE" type: "DELETE"
}); });
} }
}); });
Badge.reopenClass({ Badge.reopenClass({
/** createFromJson(json) {
Create `Badge` instances from the server JSON response.
@method createFromJson
@param {Object} json The JSON returned by the server
@returns Array or instance of `Badge` depending on the input JSON
**/
createFromJson: function(json) {
// Create BadgeType objects. // Create BadgeType objects.
const badgeTypes = {}; const badgeTypes = {};
if ("badge_types" in json) { if ("badge_types" in json) {
json.badge_types.forEach(function(badgeTypeJson) { json.badge_types.forEach(
badgeTypes[badgeTypeJson.id] = Ember.Object.create(badgeTypeJson); badgeTypeJson =>
}); (badgeTypes[badgeTypeJson.id] = Ember.Object.create(badgeTypeJson))
);
} }
const badgeGroupings = {}; const badgeGroupings = {};
if ("badge_groupings" in json) { if ("badge_groupings" in json) {
json.badge_groupings.forEach(function(badgeGroupingJson) { json.badge_groupings.forEach(
badgeGroupings[badgeGroupingJson.id] = BadgeGrouping.create( badgeGroupingJson =>
badgeGroupingJson (badgeGroupings[badgeGroupingJson.id] = BadgeGrouping.create(
); badgeGroupingJson
}); ))
);
} }
// Create Badge objects. // Create Badge objects.
@ -116,13 +87,12 @@ Badge.reopenClass({
} else if (json.badges) { } else if (json.badges) {
badges = json.badges; badges = json.badges;
} }
badges = badges.map(function(badgeJson) { badges = badges.map(badgeJson => {
const badge = Badge.create(badgeJson); const badge = Badge.create(badgeJson);
badge.set("badge_type", badgeTypes[badge.get("badge_type_id")]); badge.setProperties({
badge.set( badge_type: badgeTypes[badge.badge_type_id],
"badge_grouping", badge_grouping: badgeGroupings[badge.badge_grouping_id]
badgeGroupings[badge.get("badge_grouping_id")] });
);
return badge; return badge;
}); });
@ -133,35 +103,21 @@ Badge.reopenClass({
} }
}, },
/** findAll(opts) {
Find all `Badge` instances that have been defined.
@method findAll
@returns {Promise} a promise that resolves to an array of `Badge`
**/
findAll: function(opts) {
let listable = ""; let listable = "";
if (opts && opts.onlyListable) { if (opts && opts.onlyListable) {
listable = "?only_listable=true"; listable = "?only_listable=true";
} }
return ajax("/badges.json" + listable, { data: opts }).then(function(
badgesJson return ajax(`/badges.json${listable}`, { data: opts }).then(badgesJson =>
) { Badge.createFromJson(badgesJson)
return Badge.createFromJson(badgesJson); );
});
}, },
/** findById(id) {
Returns a `Badge` that has the given ID. return ajax(`/badges/${id}`).then(badgeJson =>
Badge.createFromJson(badgeJson)
@method findById );
@param {Number} id ID of the badge
@returns {Promise} a promise that resolves to a `Badge`
**/
findById: function(id) {
return ajax("/badges/" + id).then(function(badgeJson) {
return Badge.createFromJson(badgeJson);
});
} }
}); });