Permalink
Browse files

Merge pull request #447 from shwoodard/master-with-serializer-associa…

…tion-name

Add test for #418
  • Loading branch information...
2 parents 77d3153 + c6fec5c commit 101d692117cc9dc9a89e97e5f1602b9e62b8278b @wagenet wagenet committed Oct 30, 2012
@@ -10,9 +10,8 @@ DS.RESTSerializer = DS.Serializer.create({
},
addBelongsTo: function(hash, record, key, relationship) {
- var hashKey = this._keyForBelongsTo(record.constructor, key),
- id = get(record, key+'.id');
+ var id = get(record, relationship.key+'.id');
- if (!Ember.none(id)) { hash[hashKey] = id; }
+ if (!Ember.none(id)) { hash[key] = id; }
}
});
@@ -251,11 +251,15 @@ DS.Serializer = Ember.Object.extend({
addRelationships: function(hash, record) {
record.eachAssociation(function(name, relationship) {
- var key = this._keyForAttributeName(record.constructor, name);
+ var key;
if (relationship.kind === 'belongsTo') {
+ key = this._keyForBelongsTo(record.constructor, name);
+
this.addBelongsTo(hash, record, key, relationship);
} else if (relationship.kind === 'hasMany') {
+ key = this._keyForHasMany(record.constructor, name);
+
this.addHasMany(hash, record, key, relationship);
}
}, this);
@@ -950,3 +950,29 @@ test("data loaded from the server is converted from underscores to camelcase", f
equal(person.get('name'), "Tom", "precond - data was materialized");
equal(person.get('lastName'), "Dale", "the attribute name was camelized");
});
+
+test("When a record with a belongsTo is saved the foreign key should be sent.", function () {
+ var PersonType = DS.Model.extend({
+ title: DS.attr("string"),
+ people: DS.hasMany(Person)
+ });
+
+ Person.reopen({
+ personType: DS.belongsTo(PersonType)
+ });
+
+ store.load(PersonType, {id: 1, title: "Developer"});
+ var personType = store.find(PersonType, 1);
+
+ // FIXME this mass-assignment of a belongs to is broken. It must be set separately.
+ // var person = store.createRecord(Person, {name: 'Sam Woodard', personType: personType});
+ var person = store.createRecord(Person, {name: 'Sam Woodard'});
+ person.set('personType', personType);
+
+ store.commit();
+
+ expectUrl('/people');
+ expectType("POST");
+ expectData({ person: { name: "Sam Woodard", person_type_id: "1" } });
+ ajaxHash.success({ person: { name: 'Sam Woodard', person_type_id: 1}});
+});

0 comments on commit 101d692

Please sign in to comment.