Skip to content

Commit

Permalink
Moved published_at creation to fixtures/utils (#8595)
Browse files Browse the repository at this point in the history
no issue

- follow-up from #8573
- bove the hack that creates published_at values from the migration fn to our fixture util
  • Loading branch information
aileen authored and kirrg001 committed Sep 19, 2017
1 parent 4ac34a7 commit 0ce24b4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
11 changes: 1 addition & 10 deletions core/server/data/migrations/init/2-create-fixtures.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var Promise = require('bluebird'),
_ = require('lodash'),
fixtures = require('../../schema/fixtures'),
logging = require('../../../logging'),
moment = require('moment');
logging = require('../../../logging');

module.exports = function insertFixtures(options) {
var localOptions = _.merge({
Expand All @@ -12,14 +11,6 @@ module.exports = function insertFixtures(options) {
return Promise.mapSeries(fixtures.models, function (model) {
logging.info('Model: ' + model.name);

// The Post model fixtures need a `published_at` date, where at least the seconds
// are different, otherwise `prev_post` and `next_post` helpers won't workd with
// them.
if (model.name === 'Post') {
_.forEach(model.entries, function (post, index) {
post.published_at = moment().add(index, 'seconds');
});
}
return fixtures.utils.addFixturesForModel(model, localOptions);
}).then(function () {
return Promise.mapSeries(fixtures.relations, function (relation) {
Expand Down
28 changes: 22 additions & 6 deletions core/server/data/schema/fixtures/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// # Fixture Utils
// Standalone file which can be required to help with advanced operations on the fixtures.json file
var _ = require('lodash'),
Promise = require('bluebird'),
models = require('../../../models'),
baseUtils = require('../../../models/base/utils'),
sequence = require('../../../utils/sequence'),
var _ = require('lodash'),
Promise = require('bluebird'),
models = require('../../../models'),
baseUtils = require('../../../models/base/utils'),
sequence = require('../../../utils/sequence'),
moment = require('moment'),

fixtures = require('./fixtures'),
fixtures = require('./fixtures'),

// Private
matchFunc,
Expand Down Expand Up @@ -95,6 +96,21 @@ fetchRelationData = function fetchRelationData(relation, options) {
* @returns {Promise.<*>}
*/
addFixturesForModel = function addFixturesForModel(modelFixture, options) {
// Clone the fixtures as they get changed in this function.
// The initial blog posts will be added a `published_at` property, which
// would change the fixturesHash.
modelFixture = _.cloneDeep(modelFixture);
// The Post model fixtures need a `published_at` date, where at least the seconds
// are different, otherwise `prev_post` and `next_post` helpers won't workd with
// them.
if (modelFixture.name === 'Post') {
_.forEach(modelFixture.entries, function (post, index) {
if (!post.published_at) {
post.published_at = moment().add(index, 'seconds');
}
});
}

return Promise.mapSeries(modelFixture.entries, function (entry) {
// CASE: if id is specified, only query by id
return models[modelFixture.name].findOne(entry.id ? {id: entry.id} : entry, options).then(function (found) {
Expand Down

0 comments on commit 0ce24b4

Please sign in to comment.