Skip to content

Commit

Permalink
Merge pull request #184 from whatthewhat/model-fragments-compatibility
Browse files Browse the repository at this point in the history
Fix compatibility with ember-data-model-fragments
  • Loading branch information
danielspaniel committed Mar 26, 2016
2 parents 5fe131b + 84bbca8 commit c084a35
Show file tree
Hide file tree
Showing 5 changed files with 42 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
12 changes: 12 additions & 0 deletions tests/dummy/app/serializers/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import DS from 'ember-data';

export default DS.JSONAPISerializer.extend({
transformFor: function (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 c084a35

Please sign in to comment.