Skip to content

Commit

Permalink
support optionsPropertyName
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelcobain committed Nov 26, 2022
1 parent b4488b9 commit 0e84eb7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
19 changes: 16 additions & 3 deletions addon/mixins/copyable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
IS_COPYABLE,
} from 'ember-data-copyable/-private/symbols';
import { task, all } from 'ember-concurrency';
import { assign } from '@ember/polyfills';
import { guidFor } from '@ember/object/internals';
import { Copyable } from 'ember-copy';
import { isEmpty } from '@ember/utils';
Expand Down Expand Up @@ -77,6 +76,10 @@ export default Mixin.create({
const store = this.store;
let isSuccessful = false;

if (options?.optionsPropertyName) {
_meta.optionsPropertyName = options.optionsPropertyName;
}

try {
const model = yield this.get(COPY_TASK).perform(deep, options, _meta);
isSuccessful = true;
Expand Down Expand Up @@ -116,7 +119,13 @@ export default Mixin.create({
* @private
*/
[COPY_TASK]: task(function* (deep, options, _meta) {
options = assign({}, DEFAULT_OPTIONS, this.copyableOptions, options);
const optionsPropertyName = _meta.optionsPropertyName ?? 'copyableOptions';
options = Object.assign(
{},
DEFAULT_OPTIONS,
this[optionsPropertyName],
options
);

const { ignoreAttributes, otherAttributes, copyByReference, overwrite } =
options;
Expand Down Expand Up @@ -238,7 +247,11 @@ export default Mixin.create({

// Build the final attrs pojo by merging otherAttributes, the copied
// attributes, and ant overwrites specified.
attrs = assign(this.getProperties(otherAttributes), attrs, overwrite);
attrs = Object.assign(
this.getProperties(otherAttributes),
attrs,
overwrite
);

// Set the properties on the model
model.setProperties(attrs);
Expand Down
6 changes: 6 additions & 0 deletions tests/dummy/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ export default function (config) {

this.get('/foo-fragment-holders');
this.get('/foo-fragment-holders/:id');

this.get('/override-options-parents');
this.get('/override-options-parents/:id');

this.get('/override-options-children');
this.get('/override-options-children/:id');
},
};

Expand Down
6 changes: 6 additions & 0 deletions tests/dummy/mirage/fixtures/override-options-children.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default [
{
id: 1,
overrideOptionsParentId: 1,
},
];
6 changes: 6 additions & 0 deletions tests/dummy/mirage/fixtures/override-options-parents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default [
{
id: 1,
overrideOptionsChildrenIds: [1],
},
];
39 changes: 39 additions & 0 deletions tests/helpers/define-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import config from '../../config/environment';
import { fragment, fragmentArray } from 'ember-data-model-fragments/attributes';
import Fragment from 'ember-data-model-fragments/fragment';
import { computed } from '@ember/object';

const CopyableModel = Model.extend(Copyable);

Expand Down Expand Up @@ -65,6 +66,44 @@ export default function defineModels(options, owner) {
foos: fragmentArray('fooFragment'),
bar: fragment('fooFragment'),
}),

'override-options-parent': CopyableModel.extend({
property: attr('string'),
overrideOptionsChilden: hasMany('override-options-child', options),
copyableOptions: computed(function () {
return {
overwrite: {
property: 'overriden',
},
};
}),
otherOptions: computed(function () {
return {
overwrite: {
property: 'derp',
},
};
}),
}),

'override-options-child': CopyableModel.extend({
property: attr('string'),
overrideOptionsParent: belongsTo('overrideOptionsParent', options),
otherOptions: computed(function () {
return {
overwrite: {
property: 'herp',
},
};
}),
copyableOptions: computed(function () {
return {
overwrite: {
property: 'overriden-child',
},
};
}),
}),
};

Object.keys(Models).forEach((name) => {
Expand Down
30 changes: 30 additions & 0 deletions tests/integration/copyable-options-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,34 @@ module('Integration | Copyable | options', function (hooks) {

assert.equal(copy.get('bar.foo.id'), 1);
});

test('it can use the `copyableOptions` model property for copyable options', async function (assert) {
assert.expect(2);

let model = this.store.peekRecord('override-options-parent', 1);

let copy = await model.copy(true);

assert.equal(copy.get('property'), 'overriden');
assert.equal(
copy.get('overrideOptionsChilden.firstObject.property'),
'overriden-child'
);
});

test('it can override the options property used for copyable options', async function (assert) {
assert.expect(2);

let model = this.store.peekRecord('override-options-parent', 1);

let copy = await model.copy(true, {
optionsPropertyName: 'otherOptions',
});

assert.equal(copy.get('property'), 'derp');
assert.equal(
copy.get('overrideOptionsChilden.firstObject.property'),
'herp'
);
});
});

0 comments on commit 0e84eb7

Please sign in to comment.