Skip to content

Commit

Permalink
Use serializer#transformFor to lookup transforms for compatibility wi…
Browse files Browse the repository at this point in the history
…th model-fragments

Context: adopted-ember-addons#182
  • Loading branch information
whatthewhat committed Mar 25, 2016
1 parent 5fe131b commit ae50586
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
17 changes: 12 additions & 5 deletions addon/converter/fixture-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,22 @@ export default class {
return serializer['keyFor' + type] || this.defaultKeyTransformFn;
}

getTransformValueFunction(type) {
getTransformValueFunction(type, modelName) {
if (!this.transformKeys) {
return this.noTransformFn;
}
if (!type) {
return this.defaultValueTransformFn;
}
let container = Ember.getOwner ? Ember.getOwner(this.store) : this.store.container;
return container.lookup('transform:' + type).serialize;
let serializer = this.store.serializerFor(modelName);
if ('function' == typeof serializer.transformFor) {
// Use serializer.transformFor for compatibility with ember-data-model-fragments,
// for context see https://github.com/danielspaniel/ember-data-factory-guy/issues/182
return serializer.transformFor(type).serialize;
} else {
let container = Ember.getOwner ? Ember.getOwner(this.store) : this.store.container;
return container.lookup('transform:' + type).serialize;
}
}

extractAttributes(modelName, fixture) {
Expand All @@ -45,7 +52,7 @@ export default class {

this.store.modelFor(modelName).eachAttribute((attribute, meta)=> {
let attributeKey = transformKeyFunction(attribute);
let transformValueFunction = this.getTransformValueFunction(meta.type);
let transformValueFunction = this.getTransformValueFunction(meta.type, modelName);

if (fixture.hasOwnProperty(attribute)) {
attributes[attributeKey] = transformValueFunction(fixture[attribute]);
Expand Down Expand Up @@ -206,4 +213,4 @@ export default class {
relationships[relationshipKey] = this.assignRelationship(records);
}

}
}
1 change: 1 addition & 0 deletions tests/dummy/app/models/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default DS.Model.extend({
camelCaseDescription: DS.attr('string'),
snake_case_description: DS.attr('string'),
aBooleanField: DS.attr('boolean'),
foo: DS.attr('just-a-string'),
superHero: DS.belongsTo('super-hero', {async: false}),
company: DS.belongsTo('company', {async: false}),
group: DS.belongsTo('group', {async: false, polymorphic: true})
Expand Down
13 changes: 13 additions & 0 deletions tests/dummy/app/serializers/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
transformFor: function (attributeType) {
console.log(attributeType);
if (attributeType === 'just-a-string') {
return this.container.lookup('transform:string');
}
else {
return this._super.apply(this, arguments);
}
}
});
11 changes: 11 additions & 0 deletions tests/dummy/app/transforms/just-a-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import DS from 'ember-data';
import Ember from 'ember';

export default DS.Transform.extend({
serialize: function(value) {
return 'failed';
},
deserialize: function(value) {
return 'failed';
}
});
7 changes: 6 additions & 1 deletion tests/unit/models/profile-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ test('composing a profile with a company association by making both', function()
test('using this.subject for profile and make for company associaion', function() {
let profile = this.subject({company: make('company')});
ok(profile.get('company.profile') === profile);
});
});

test('uses customized transformFor', function () {
let profile = make('profile', {foo: 'bar'});
equal(profile.get('foo'), 'bar');
});

0 comments on commit ae50586

Please sign in to comment.