Skip to content

Commit

Permalink
use dasherized model names everywhere
Browse files Browse the repository at this point in the history
Previously, we allowed container keys to be loosy-goosy and normalized
them in several places. This commit consolidates those efforts into
one place `normalizeTypeKey`.
  • Loading branch information
Stanley Stuart committed May 14, 2015
1 parent e0b1a09 commit d9609b0
Show file tree
Hide file tree
Showing 42 changed files with 623 additions and 396 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ var ActiveModelAdapter = RESTAdapter.extend({
```
@method pathForType
@param {String} typeKey
@param {String} modelName
@return String
*/
pathForType: function(typeKey) {
var decamelized = decamelize(typeKey);
pathForType: function(modelName) {
var decamelized = decamelize(modelName);
var underscored = underscore(decamelized);
return pluralize(underscored);
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { singularize } from "ember-inflector";
import RESTSerializer from "ember-data/serializers/rest-serializer";
import normalizeTypeKey from "ember-data/system/normalize-type-key";
/**
@module ember-data
*/
Expand Down Expand Up @@ -146,7 +147,7 @@ var ActiveModelSerializer = RESTSerializer.extend({
@param {Object} options
*/
serializeIntoHash: function(data, typeClass, snapshot, options) {
var root = underscore(decamelize(typeClass.typeKey));
var root = underscore(decamelize(typeClass.modelName));
data[root] = this.serialize(snapshot, options);
},

Expand All @@ -166,7 +167,7 @@ var ActiveModelSerializer = RESTSerializer.extend({
if (Ember.isNone(belongsTo)) {
json[jsonKey] = null;
} else {
json[jsonKey] = classify(belongsTo.typeKey).replace(/(\/)([a-z])/g, function(match, separator, chr) {
json[jsonKey] = classify(belongsTo.modelName).replace(/(\/)([a-z])/g, function(match, separator, chr) {
return match.toUpperCase();
}).replace('/', '::');
}
Expand Down Expand Up @@ -290,9 +291,10 @@ var ActiveModelSerializer = RESTSerializer.extend({
}
},
typeForRoot: function(key) {
return camelize(singularize(key)).replace(/(^|\:)([A-Z])/g, function(match, separator, chr) {
var convertedFromRubyModule = camelize(singularize(key)).replace(/(^|\:)([A-Z])/g, function(match, separator, chr) {
return match.toLowerCase();
}).replace('::', '/');
return normalizeTypeKey(convertedFromRubyModule);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test("serialize polymorphic", function() {
});

test("serialize polymorphic when type key is not camelized", function() {
YellowMinion.typeKey = 'evil-minions/yellow-minion';
YellowMinion.modelName = 'evil-minions/yellow-minion';
var tom, ray;
run(function() {
tom = env.store.createRecord(YellowMinion, { name: "Alex", id: "124" });
Expand All @@ -90,7 +90,7 @@ test("extractPolymorphic hasMany", function() {
"id": 1,
"name": "Dr Horrible",
"evilMinions": [{
type: "evilMinions/yellowMinion",
type: "evil-minions/yellow-minion",
id: 12
}]
});
Expand All @@ -111,7 +111,7 @@ test("extractPolymorphic", function() {
"id": 1,
"name": "DeathRay",
"evilMinion": {
type: "evilMinions/yellowMinion",
type: "evil-minions/yellow-minion",
id: 12
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ test("serializeIntoHash", function() {
});

test("serializeIntoHash with decamelized types", function() {
HomePlanet.typeKey = 'home-planet';
HomePlanet.modelName = 'home-planet';
run(function() {
league = env.store.createRecord(HomePlanet, { name: "Umber", id: "123" });
});
Expand Down Expand Up @@ -205,7 +205,7 @@ test("serialize polymorphic", function() {
});

test("serialize polymorphic when type key is not camelized", function() {
YellowMinion.typeKey = 'yellow-minion';
YellowMinion.modelName = 'yellow-minion';
var tom, ray;
run(function() {
tom = env.store.createRecord(YellowMinion, { name: "Alex", id: "124" });
Expand Down Expand Up @@ -246,7 +246,7 @@ test("extractPolymorphic hasMany", function() {
"id": 1,
"name": "Dr Horrible",
"evilMinions": [{
type: "yellowMinion",
type: "yellow-minion",
id: 12
}]
});
Expand All @@ -271,7 +271,7 @@ test("extractPolymorphic", function() {
"id": 1,
"name": "DeathRay",
"evilMinion": {
type: "yellowMinion",
type: "yellow-minion",
id: 12
}
});
Expand Down
96 changes: 48 additions & 48 deletions packages/ember-data/lib/adapters/build-url-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var get = Ember.get;
```javascript
export default DS.Adapter.extend(BuildURLMixin, {
find: function(store, type, id, snapshot) {
var url = this.buildURL(type.typeKey, id, snapshot, 'find');
var url = this.buildURL(type.modelName, id, snapshot, 'find');
return this.ajax(url, 'GET');
}
});
Expand Down Expand Up @@ -42,52 +42,52 @@ export default Ember.Mixin.create({
will be arrays of ids and snapshots.
@method buildURL
@param {String} typeKey
@param {String} modelName
@param {String|Array|Object} id single id or array of ids or query
@param {DS.Snapshot|Array} snapshot single snapshot or array of snapshots
@param {String} requestType
@return {String} url
*/
buildURL: function(typeKey, id, snapshot, requestType) {
buildURL: function(modelName, id, snapshot, requestType) {
switch (requestType) {
case 'find':
return this.urlForFind(id, typeKey, snapshot);
return this.urlForFind(id, modelName, snapshot);
case 'findAll':
return this.urlForFindAll(typeKey);
return this.urlForFindAll(modelName);
case 'findQuery':
return this.urlForFindQuery(id, typeKey);
return this.urlForFindQuery(id, modelName);
case 'findMany':
return this.urlForFindMany(id, typeKey, snapshot);
return this.urlForFindMany(id, modelName, snapshot);
case 'findHasMany':
return this.urlForFindHasMany(id, typeKey);
return this.urlForFindHasMany(id, modelName);
case 'findBelongsTo':
return this.urlForFindBelongsTo(id, typeKey);
return this.urlForFindBelongsTo(id, modelName);
case 'createRecord':
return this.urlForCreateRecord(typeKey, snapshot);
return this.urlForCreateRecord(modelName, snapshot);
case 'updateRecord':
return this.urlForUpdateRecord(id, typeKey, snapshot);
return this.urlForUpdateRecord(id, modelName, snapshot);
case 'deleteRecord':
return this.urlForDeleteRecord(id, typeKey, snapshot);
return this.urlForDeleteRecord(id, modelName, snapshot);
default:
return this._buildURL(typeKey, id);
return this._buildURL(modelName, id);
}
},

/**
@method _buildURL
@private
@param {String} typeKey
@param {String} modelName
@param {String} id
@return {String} url
*/
_buildURL: function(typeKey, id) {
_buildURL: function(modelName, id) {
var url = [];
var host = get(this, 'host');
var prefix = this.urlPrefix();
var path;

if (typeKey) {
path = this.pathForType(typeKey);
if (modelName) {
path = this.pathForType(modelName);
if (path) { url.push(path); }
}

Expand All @@ -105,31 +105,31 @@ export default Ember.Mixin.create({
/**
* @method urlForFind
* @param {String} id
* @param {String} typeKey
* @param {String} modelName
* @param {DS.Snapshot} snapshot
* @return {String} url
*/
urlForFind: function(id, typeKey, snapshot) {
return this._buildURL(typeKey, id);
urlForFind: function(id, modelName, snapshot) {
return this._buildURL(modelName, id);
},

/**
* @method urlForFindAll
* @param {String} typeKey
* @param {String} modelName
* @return {String} url
*/
urlForFindAll: function(typeKey) {
return this._buildURL(typeKey);
urlForFindAll: function(modelName) {
return this._buildURL(modelName);
},

/**
* @method urlForFindQuery
* @param {Object} query
* @param {String} typeKey
* @param {String} modelName
* @return {String} url
*/
urlForFindQuery: function(query, typeKey) {
return this._buildURL(typeKey);
urlForFindQuery: function(query, modelName) {
return this._buildURL(modelName);
},

/**
Expand All @@ -139,60 +139,60 @@ export default Ember.Mixin.create({
* @param {Array} snapshots
* @return {String} url
*/
urlForFindMany: function(ids, typeKey, snapshots) {
return this._buildURL(typeKey);
urlForFindMany: function(ids, modelName, snapshots) {
return this._buildURL(modelName);
},

/**
* @method urlForFindHasMany
* @param {String} id
* @param {String} typeKey
* @param {String} modelName
* @return {String} url
*/
urlForFindHasMany: function(id, typeKey) {
return this._buildURL(typeKey, id);
urlForFindHasMany: function(id, modelName) {
return this._buildURL(modelName, id);
},

/**
* @method urlForFindBelongTo
* @param {String} id
* @param {String} typeKey
* @param {String} modelName
* @return {String} url
*/
urlForFindBelongsTo: function(id, typeKey) {
return this._buildURL(typeKey, id);
urlForFindBelongsTo: function(id, modelName) {
return this._buildURL(modelName, id);
},

/**
* @method urlForCreateRecord
* @param {String} typeKey
* @param {String} modelName
* @param {DS.Snapshot} snapshot
* @return {String} url
*/
urlForCreateRecord: function(typeKey, snapshot) {
return this._buildURL(typeKey);
urlForCreateRecord: function(modelName, snapshot) {
return this._buildURL(modelName);
},

/**
* @method urlForUpdateRecord
* @param {String} id
* @param {String} typeKey
* @param {String} modelName
* @param {DS.Snapshot} snapshot
* @return {String} url
*/
urlForUpdateRecord: function(id, typeKey, snapshot) {
return this._buildURL(typeKey, id);
urlForUpdateRecord: function(id, modelName, snapshot) {
return this._buildURL(modelName, id);
},

/**
* @method urlForDeleteRecord
* @param {String} id
* @param {String} typeKey
* @param {String} modelName
* @param {DS.Snapshot} snapshot
* @return {String} url
*/
urlForDeleteRecord: function(id, typeKey, snapshot) {
return this._buildURL(typeKey, id);
urlForDeleteRecord: function(id, modelName, snapshot) {
return this._buildURL(modelName, id);
},

/**
Expand Down Expand Up @@ -251,19 +251,19 @@ export default Ember.Mixin.create({
```js
App.ApplicationAdapter = DS.RESTAdapter.extend({
pathForType: function(typeKey) {
var decamelized = Ember.String.decamelize(typeKey);
pathForType: function(modelName) {
var decamelized = Ember.String.decamelize(modelName);
return Ember.String.pluralize(decamelized);
}
});
```
@method pathForType
@param {String} typeKey
@param {String} modelName
@return {String} path
**/
pathForType: function(typeKey) {
var camelized = Ember.String.camelize(typeKey);
pathForType: function(modelName) {
var camelized = Ember.String.camelize(modelName);
return Ember.String.pluralize(camelized);
}
});
2 changes: 1 addition & 1 deletion packages/ember-data/lib/adapters/fixture-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default Adapter.extend({
@param {DS.Snapshot} snapshot
*/
mockJSON: function(store, typeClass, snapshot) {
return store.serializerFor(snapshot.typeKey).serialize(snapshot, { includeId: true });
return store.serializerFor(snapshot.modelName).serialize(snapshot, { includeId: true });
},

/**
Expand Down
Loading

0 comments on commit d9609b0

Please sign in to comment.