-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only extend from application serializer / adapter, if it exists
This change updates the blueprints for adapter and serializer, so they extend from the application entity if it exists: If there is an application adapter - located in `app/adapters/application.js` - and the command `ember g adapter foo` is invoked, the created adapter extends from the application adapter: ```js // app/adapters/foo.js import ApplicationAdapter from './application'; export default ApplicationAdapter.extend(); ``` If there is no application entity, the JSONAPI equivalent is used. Consider no application serializer in `app/serializers/appliation.js` and the command `ember generate serializer foo` is invoked, then the created serializer extends from JSONAPISerializer: ```js // app/serializers/foo.js import JSONAPISerializer from 'ember-data/serializers/json-api'; export default JSONAPISerializer.extend(); ```
- Loading branch information
Showing
6 changed files
with
156 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import JSONAPISerializer from 'ember-data/serializers/json-api'; | ||
<%= importStatement %> | ||
|
||
export default JSONAPISerializer.extend({ | ||
export default <%= baseClass %>.extend({ | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
/*jshint node:true*/ | ||
|
||
var extendFromApplicationEntity = require('../../lib/utilities/extend-from-application-entity'); | ||
|
||
module.exports = { | ||
description: 'Generates an ember-data serializer.' | ||
description: 'Generates an ember-data serializer.', | ||
|
||
availableOptions: [ | ||
{ name: 'base-class', type: String } | ||
], | ||
|
||
locals: function(options) { | ||
return extendFromApplicationEntity('serializer', 'JSONAPISerializer', 'ember-data/serializers/json-api', options); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var stringUtil = require('ember-cli-string-utils'); | ||
var SilentError = require('silent-error'); | ||
var pathUtil = require('ember-cli-path-utils'); | ||
var existsSync = require('exists-sync'); | ||
var path = require('path'); | ||
|
||
module.exports = function(type, baseClass, packagePath, options) { | ||
var entityName = options.entity.name; | ||
var isAddon = options.inRepoAddon || options.project.isEmberCLIAddon(); | ||
var relativePath = pathUtil.getRelativePath(options.entity.name); | ||
|
||
if (options.pod && options.podPath) { | ||
relativePath = pathUtil.getRelativePath(options.podPath + options.entity.name); | ||
} | ||
|
||
var entityDirectory = type + 's'; | ||
var applicationEntityPath = path.join(options.project.root, 'app', entityDirectory, 'application.js'); | ||
var hasApplicationEntity = existsSync(applicationEntityPath); | ||
if (!isAddon && !options.baseClass && entityName !== 'application' && hasApplicationEntity) { | ||
options.baseClass = 'application'; | ||
packagePath = './application'; | ||
} | ||
|
||
if (options.baseClass === entityName) { | ||
throw new SilentError(stringUtil.classify(type) + 's cannot extend from themself. To resolve this, remove the `--base-class` option or change to a different base-class.'); | ||
} | ||
|
||
var importStatement = 'import ' + baseClass + ' from \'' + packagePath + '\';'; | ||
|
||
if (options.baseClass) { | ||
baseClass = stringUtil.classify(options.baseClass.replace('\/', '-')); | ||
baseClass = baseClass + stringUtil.classify(type); | ||
importStatement = 'import ' + baseClass + ' from \'' + relativePath + options.baseClass + '\';'; | ||
} | ||
|
||
return { | ||
importStatement: importStatement, | ||
baseClass: baseClass | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters