Skip to content

Instantly share code, notes, and snippets.

@cball
Last active April 21, 2023 17:13
Show Gist options
  • Save cball/4ce48701a242a5f50ac5a1951fe4d2f9 to your computer and use it in GitHub Desktop.
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
import DS from 'ember-data';
export default DS.Model.extend({
<%= attrs.length ? ' ' + attrs : '' %>
});
/*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