Fixed EmberObject.create error on Admin start (#1841)

refs https://github.com/TryGhost/Ghost/pull/12632

- The `unsplash` setting has been migrated to a new format - plain boolean value. This is why it's no longer needed to do any serialization dances
This commit is contained in:
naz 2021-02-18 13:25:21 +13:00 committed by GitHub
parent 2c95cae248
commit 07abb5443a
11 changed files with 18 additions and 77 deletions

View File

@ -18,7 +18,7 @@
<div class="gh-btn gh-btn-white" data-test-file-input-description><span>{{this.description}}</span></div> <div class="gh-btn gh-btn-white" data-test-file-input-description><span>{{this.description}}</span></div>
</GhFileInput> </GhFileInput>
{{#if (and this.allowUnsplash this.settings.unsplash.isActive)}} {{#if (and this.allowUnsplash this.settings.unsplash)}}
<div class="gh-image-uploader-unsplash" {{action (toggle "_showUnsplash" this)}}> <div class="gh-image-uploader-unsplash" {{action (toggle "_showUnsplash" this)}}>
{{svg-jar "unsplash"}} {{svg-jar "unsplash"}}
</div> </div>

View File

@ -161,7 +161,7 @@ export default Component.extend(ShortcutsMixin, {
toolbar.splice(index, 1); toolbar.splice(index, 1);
} }
if (this.get('settings.unsplash.isActive')) { if (this.get('settings.unsplash')) {
let image = toolbar.findBy('name', 'image'); let image = toolbar.findBy('name', 'image');
let index = toolbar.indexOf(image) + 1; let index = toolbar.indexOf(image) + 1;

View File

@ -21,9 +21,9 @@ export default Controller.extend({
update(value) { update(value) {
if (!this.dirtyAttributes) { if (!this.dirtyAttributes) {
this.set('rollbackValue', this.get('unsplashSettings.isActive')); this.set('rollbackValue', this.get('unsplashSettings'));
} }
this.set('unsplashSettings.isActive', value); this.set('unsplashSettings', value);
this.set('dirtyAttributes', true); this.set('dirtyAttributes', true);
}, },
@ -60,7 +60,7 @@ export default Controller.extend({
} }
// roll back changes on model props // roll back changes on model props
this.set('unsplashSettings.isActive', this.rollbackValue); this.set('unsplashSettings', this.rollbackValue);
this.set('dirtyAttributes', false); this.set('dirtyAttributes', false);
this.set('rollbackValue', null); this.set('rollbackValue', null);

View File

@ -28,11 +28,7 @@ export default Model.extend(ValidationEngine, {
ampGtagId: attr('string'), ampGtagId: attr('string'),
firstpromoter: attr('boolean'), firstpromoter: attr('boolean'),
firstpromoterId: attr('string'), firstpromoterId: attr('string'),
unsplash: attr('unsplash-settings', { unsplash: attr('boolean'),
defaultValue() {
return {isActive: true};
}
}),
metaTitle: attr('string'), metaTitle: attr('string'),
metaDescription: attr('string'), metaDescription: attr('string'),
twitterTitle: attr('string'), twitterTitle: attr('string'),

View File

@ -1,5 +0,0 @@
import EmberObject from '@ember/object';
export default EmberObject.extend({
isActive: false
});

View File

@ -1,6 +1,5 @@
import AuthenticatedRoute from 'ghost-admin/routes/authenticated'; import AuthenticatedRoute from 'ghost-admin/routes/authenticated';
import CurrentUserSettings from 'ghost-admin/mixins/current-user-settings'; import CurrentUserSettings from 'ghost-admin/mixins/current-user-settings';
import UnsplashObject from 'ghost-admin/models/unsplash-integration';
import {inject as service} from '@ember/service'; import {inject as service} from '@ember/service';
export default AuthenticatedRoute.extend(CurrentUserSettings, { export default AuthenticatedRoute.extend(CurrentUserSettings, {
@ -24,11 +23,8 @@ export default AuthenticatedRoute.extend(CurrentUserSettings, {
// server doesn't have any unsplash settings by default but it can provide // server doesn't have any unsplash settings by default but it can provide
// overrides via config: // overrides via config:
// - isActive: use as default but allow settings override // - unsplash: use as default but allow settings override
// - applicationId: total override, no field is shown if present let unsplash = true;
let unsplash = UnsplashObject.create({
isActive: true
});
settings.set('unsplash', unsplash); settings.set('unsplash', unsplash);
}); });

View File

@ -142,7 +142,7 @@
</div> </div>
<div class="gh-card-right"> <div class="gh-card-right">
<div class="apps-configured"> <div class="apps-configured">
{{#if this.settings.unsplash.isActive}} {{#if this.settings.unsplash}}
<span class="gh-badge" data-test-app-status>Active</span> <span class="gh-badge" data-test-app-status>Active</span>
{{else}} {{else}}
<span data-test-app-status>Configure</span> <span data-test-app-status>Configure</span>

View File

@ -45,7 +45,7 @@
<label for="isActive" class="checkbox"> <label for="isActive" class="checkbox">
<input <input
type="checkbox" type="checkbox"
checked={{this.unsplashSettings.isActive}} checked={{this.unsplashSettings}}
id="isActive" id="isActive"
name="isActive" name="isActive"
onclick={{action "update" value="target.checked"}} onclick={{action "update" value="target.checked"}}

View File

@ -1,28 +1,25 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
import Transform from '@ember-data/serializer/transform'; import Transform from '@ember-data/serializer/transform';
import UnsplashObject from 'ghost-admin/models/unsplash-integration';
const DEFAULT_SETTINGS = { const DEFAULT_VALUE = true;
isActive: true
};
export default Transform.extend({ export default Transform.extend({
deserialize(serialized) { deserialize(serialized) {
if (serialized) { if (serialized) {
let settingsObject; let settingsObject;
try { try {
settingsObject = JSON.parse(serialized) || DEFAULT_SETTINGS; settingsObject = JSON.parse(serialized) || DEFAULT_VALUE;
} catch (e) { } catch (e) {
settingsObject = DEFAULT_SETTINGS; settingsObject = DEFAULT_VALUE;
} }
return UnsplashObject.create(settingsObject); return settingsObject;
} }
return DEFAULT_SETTINGS; return DEFAULT_VALUE;
}, },
serialize(deserialized) { serialize(deserialized) {
return deserialized ? JSON.stringify(deserialized) : JSON.stringify(DEFAULT_SETTINGS); return deserialized ? JSON.stringify(deserialized) : JSON.stringify(DEFAULT_VALUE);
} }
}); });

View File

@ -84,7 +84,7 @@ describe('Acceptance: Settings - Integrations - Unsplash', function () {
// server should now have an unsplash setting // server should now have an unsplash setting
let [setting] = this.server.db.settings.where({key: 'unsplash'}); let [setting] = this.server.db.settings.where({key: 'unsplash'});
expect(setting, 'unsplash setting after save').to.exist; expect(setting, 'unsplash setting after save').to.exist;
expect(setting.value).to.equal('{"isActive":true}'); expect(setting.value).to.equal(true);
// disable // disable
await click('[data-test-checkbox="unsplash"]'); await click('[data-test-checkbox="unsplash"]');
@ -98,7 +98,7 @@ describe('Acceptance: Settings - Integrations - Unsplash', function () {
// server should have an updated setting // server should have an updated setting
[setting] = this.server.db.settings.where({key: 'unsplash'}); [setting] = this.server.db.settings.where({key: 'unsplash'});
expect(setting.value).to.equal('{"isActive":false}'); expect(setting.value).to.equal(false);
}); });
it('warns when leaving without saving', async function () { it('warns when leaving without saving', async function () {

View File

@ -1,43 +0,0 @@
import {describe, it} from 'mocha';
import {expect} from 'chai';
import {setupTest} from 'ember-mocha';
describe('Unit: Transform: unsplash-settings', function () {
setupTest();
it('deserializes to default value when null', function () {
let serialized = null;
let result = this.owner.lookup('transform:unsplash-settings').deserialize(serialized);
expect(result.isActive).to.be.true;
});
it('deserializes to default value when blank string', function () {
let serialized = '';
let result = this.owner.lookup('transform:unsplash-settings').deserialize(serialized);
expect(result.isActive).to.be.true;
});
it('deserializes to default value when invalid JSON', function () {
let serialized = 'not JSON';
let result = this.owner.lookup('transform:unsplash-settings').deserialize(serialized);
expect(result.isActive).to.be.true;
});
it('deserializes valid JSON object', function () {
let serialized = '{"isActive":false}';
let result = this.owner.lookup('transform:unsplash-settings').deserialize(serialized);
expect(result.isActive).to.be.false;
});
it('serializes to JSON string', function () {
let deserialized = {isActive: false};
let result = this.owner.lookup('transform:unsplash-settings').serialize(deserialized);
expect(result).to.equal('{"isActive":false}');
});
it('serializes to default value when blank', function () {
let deserialized = '';
let result = this.owner.lookup('transform:unsplash-settings').serialize(deserialized);
expect(result).to.equal('{"isActive":true}');
});
});