mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-28 21:33:24 +03:00
b4c3e1ae40
refs https://github.com/TryGhost/Admin/pull/2227 - a continuation of #2227 that runs the native classes codemod against app/transforms
43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
import classic from 'ember-classic-decorator';
|
|
/* eslint-disable camelcase */
|
|
import SlackObject from 'ghost-admin/models/slack-integration';
|
|
import Transform from '@ember-data/serializer/transform';
|
|
import {isArray as isEmberArray} from '@ember/array';
|
|
import {isEmpty} from '@ember/utils';
|
|
|
|
@classic
|
|
export default class SlackSettings extends Transform {
|
|
deserialize(serialized) {
|
|
let settingsArray;
|
|
try {
|
|
settingsArray = JSON.parse(serialized) || [];
|
|
} catch (e) {
|
|
settingsArray = [];
|
|
}
|
|
|
|
if (isEmpty(settingsArray)) {
|
|
settingsArray.push({url: '', username: ''});
|
|
}
|
|
|
|
let slackObjs = settingsArray.map(itemDetails => SlackObject.create(itemDetails));
|
|
|
|
return slackObjs;
|
|
}
|
|
|
|
serialize(deserialized) {
|
|
let settingsArray;
|
|
if (isEmberArray(deserialized)) {
|
|
settingsArray = deserialized.map((item) => {
|
|
let url = (item.get('url') || '').trim();
|
|
let username = (item.get('username') || '').trim();
|
|
if (url) {
|
|
return {url, username};
|
|
}
|
|
}).compact();
|
|
} else {
|
|
settingsArray = [];
|
|
}
|
|
return JSON.stringify(settingsArray);
|
|
}
|
|
}
|