Last active
April 21, 2023 17:13
-
-
Save cball/4ce48701a242a5f50ac5a1951fe4d2f9 to your computer and use it in GitHub Desktop.
Ember blueprint to automatically generate related mirage files when you generate a model
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
import DS from 'ember-data'; | |
export default DS.Model.extend({ | |
<%= attrs.length ? ' ' + attrs : '' %> | |
}); |
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
/*jshint node:true*/ | |
let ModelBlueprint = require('ember-data/blueprints/model/index'); | |
let inflection = require('inflection'); | |
let RSVP = require('rsvp'); | |
let singularBlueprints = ['mirage-model', 'mirage-factory']; | |
let pluralBlueprints = ['mirage-fixture']; | |
ModelBlueprint.description = 'Generates an ember-data model and mirage model/factory/fixtures' | |
ModelBlueprint.afterInstall = function(options) { | |
let installs = []; | |
let pluralOptions = pluralizedOptions(options); | |
singularBlueprints.forEach((b) => { | |
let blueprint = this.lookupBlueprint(b); | |
installs.push(blueprint.install(options)); | |
}); | |
pluralBlueprints.forEach((b) => { | |
let blueprint = this.lookupBlueprint(b); | |
installs.push(blueprint.install(pluralOptions)); | |
}) | |
return RSVP.all(installs); | |
} | |
ModelBlueprint.afterUninstall = function(options) { | |
let installs = []; | |
let pluralOptions = pluralizedOptions(options); | |
singularBlueprints.forEach((b) => { | |
let blueprint = this.lookupBlueprint(b); | |
installs.push(blueprint.uninstall(options)); | |
}); | |
pluralBlueprints.forEach((b) => { | |
let blueprint = this.lookupBlueprint(b); | |
installs.push(blueprint.uninstall(pluralOptions)); | |
}) | |
return RSVP.all(installs); | |
} | |
module.exports = ModelBlueprint; | |
function pluralizedOptions(options) { | |
let pluralName = inflection.pluralize(options.entity.name); | |
return Object.assign({}, options, { entity: { name: pluralName }}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment