From 0ce24b48bd8b86da7d294c4b9210e8e24e320a64 Mon Sep 17 00:00:00 2001 From: Aileen Nowak Date: Tue, 19 Sep 2017 17:54:01 +0700 Subject: [PATCH] Moved `published_at` creation to fixtures/utils (#8595) no issue - follow-up from #8573 - bove the hack that creates published_at values from the migration fn to our fixture util --- .../data/migrations/init/2-create-fixtures.js | 11 +------- core/server/data/schema/fixtures/utils.js | 28 +++++++++++++++---- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/core/server/data/migrations/init/2-create-fixtures.js b/core/server/data/migrations/init/2-create-fixtures.js index 447621680fca..e8e6e36e8299 100644 --- a/core/server/data/migrations/init/2-create-fixtures.js +++ b/core/server/data/migrations/init/2-create-fixtures.js @@ -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({ @@ -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) { diff --git a/core/server/data/schema/fixtures/utils.js b/core/server/data/schema/fixtures/utils.js index 9fb8e8e9e46c..3609b65137e3 100644 --- a/core/server/data/schema/fixtures/utils.js +++ b/core/server/data/schema/fixtures/utils.js @@ -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, @@ -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) {