Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Associations should respect namingConvention #151

Closed
wants to merge 1 commit into from

3 participants

@ghempton
Collaborator

No description provided.

@rlivsey

I just got bitten by this one too with the latest version, however this patch doesn't apply cleanly against master anymore.

I've rebased it against the current master here: https://github.com/rlivsey/data/tree/has-one-naming-conventions

Cheers.

@wagenet
Owner

@rlivsey, @ghempton Can one of you provide an updated PR that merges cleanly? Thanks!

@rlivsey

I've made a pull request from my rebased branch - #199

Cheers

@wagenet
Owner

Closing in favor of #199.

@wagenet wagenet closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 9, 2012
  1. @ghempton
This page is out of date. Refresh to see the latest.
View
4 packages/ember-data/lib/system/model/associations.js
@@ -99,7 +99,7 @@ var hasAssociation = function(type, options, one) {
// Embedded belongsTo associations should not look for
// a foreign key.
if (embedded) {
- key = options.key || key;
+ key = options.key || get(this, 'namingConvention').keyToJSONKey(key);
// Non-embedded associations should look for a foreign key.
// For example, instead of person, we might look for person_id
@@ -110,7 +110,7 @@ var hasAssociation = function(type, options, one) {
association = id ? store.find(type, id) : null;
}
} else {
- key = options.key || key;
+ key = options.key || get(this, 'namingConvention').keyToJSONKey(key);
ids = findRecord(store, type, data, key);
association = store.findMany(type, ids);
set(association, 'parentRecord', this);
View
1  packages/ember-data/lib/system/model/model.js
@@ -214,6 +214,7 @@ DS.Model = Ember.Object.extend({
}
}
+ key = options.key || get(this, 'namingConvention').keyToJSONKey(key);
json[key] = records;
},
View
19 packages/ember-data/tests/unit/associations_test.js
@@ -446,3 +446,22 @@ test("hasOne embedded associations work the same as referenced ones, and have th
strictEqual(get(person, 'tag'), get(person, 'tag'), "the returned object is always the same");
strictEqual(get(person, 'tag'), store.find(Tag, 5), "association object are the same as object retrieved directly");
});
+
+test("embedded associations should respect namingConvention", function() {
+ var MyCustomTag = DS.Model.extend({
+ name: DS.attr('string')
+ });
+
+ var Person = DS.Model.extend({
+ name: DS.attr('string'),
+ myCustomTag: DS.hasOne(MyCustomTag, { embedded: true }),
+ myCustomTags: DS.hasMany(MyCustomTag, { embedded: true })
+ });
+
+ var store = DS.Store.create();
+ store.load(Person, 1, { id: 1, name: "Tom Dale", my_custom_tag: { id: 5, name: "UN-friendly" }, my_custom_tags: [ { id: 5, name: "UN-friendly" } ] });
+
+ var person = store.find(Person, 1);
+ equal(getPath(person, 'myCustomTag.name'), "UN-friendly", "hasOne tag should be set properly");
+ equal(getPath(person, 'myCustomTags.firstObject.name'), "UN-friendly", "hasMany tag should be set properly");
+});
View
12 packages/ember-data/tests/unit/to_json_test.js
@@ -126,13 +126,13 @@ test("toJSON includes associations when the association option is set", function
store.load(PhoneNumber, { id: 7, number: '123' });
store.load(PhoneNumber, { id: 8, number: '345' });
- store.load(Contact, { id: 1, name: "Chad", phoneNumbers: [7, 8] });
+ store.load(Contact, { id: 1, name: "Chad", phone_numbers: [7, 8] });
var record = store.find(Contact, 1);
deepEqual(record.toJSON(), { id: 1, name: "Chad" }, "precond - associations not included by default");
deepEqual(record.toJSON({ associations: true }),
- { id: 1, name: "Chad", phoneNumbers: [7,8] },
+ { id: 1, name: "Chad", phone_numbers: [7,8] },
"associations are included when association flag is set");
store.load(PhoneNumber, { id: 9, number: '789' });
@@ -141,7 +141,7 @@ test("toJSON includes associations when the association option is set", function
record.get('phoneNumbers').pushObject(phoneNumber);
deepEqual(record.toJSON({ associations: true }),
- { id: 1, name: "Chad", phoneNumbers: [7,8,9] },
+ { id: 1, name: "Chad", phone_numbers: [7,8,9] },
"association is updated after editing associations array");
});
@@ -157,7 +157,7 @@ test("toJSON includes embedded associations when an association is embedded", fu
})
});
- store.load(Contact, { id: 1, name: "Chad", phoneNumbers: [{
+ store.load(Contact, { id: 1, name: "Chad", phone_numbers: [{
id: 7,
number: '123'
},
@@ -171,7 +171,7 @@ test("toJSON includes embedded associations when an association is embedded", fu
deepEqual(record.toJSON(), { id: 1, name: "Chad" }, "precond - associations not included by default");
deepEqual(record.toJSON({ associations: true }),
- { id: 1, name: "Chad", phoneNumbers: [{
+ { id: 1, name: "Chad", phone_numbers: [{
id: 7,
number: '123'
},
@@ -188,7 +188,7 @@ test("toJSON includes embedded associations when an association is embedded", fu
record.get('phoneNumbers').pushObject(phoneNumber);
deepEqual(record.toJSON({ associations: true }),
- { id: 1, name: "Chad", phoneNumbers: [{
+ { id: 1, name: "Chad", phone_numbers: [{
id: 7,
number: '123'
},
Something went wrong with that request. Please try again.