Skip to content

Commit 94f4662

Browse files
committed
chore: extract string from usage object to match what db needs
1 parent 431ed62 commit 94f4662

1 file changed

Lines changed: 39 additions & 35 deletions

File tree

api/models/Topic.js

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,39 @@ module.exports = {
2222
},
2323

2424
description: {
25-
type: Sequelize.TEXT,
25+
type: Sequelize.TEXT
2626
},
2727

2828
usage: {
29-
type: Sequelize.TEXT,
29+
type: Sequelize.TEXT
3030
},
3131

3232
details: {
33-
type: Sequelize.TEXT,
33+
type: Sequelize.TEXT
3434
},
3535

3636
value: {
37-
type: Sequelize.TEXT,
37+
type: Sequelize.TEXT
3838
},
3939

4040
references: {
41-
type: Sequelize.TEXT,
41+
type: Sequelize.TEXT
4242
},
4343

4444
note: {
45-
type: Sequelize.TEXT,
45+
type: Sequelize.TEXT
4646
},
4747

4848
author: {
49-
type: Sequelize.TEXT,
49+
type: Sequelize.TEXT
5050
},
5151

5252
seealso: {
53-
type: Sequelize.TEXT,
53+
type: Sequelize.TEXT
5454
},
5555

5656
examples: {
57-
type: Sequelize.TEXT,
57+
type: Sequelize.TEXT
5858
},
5959

6060
sourceJSON: {
@@ -65,7 +65,6 @@ module.exports = {
6565

6666
},
6767
associations: function() {
68-
6968
Topic.belongsTo(PackageVersion,
7069
{
7170
as: 'package_version',
@@ -115,7 +114,7 @@ module.exports = {
115114
.replace(':version', encodeURIComponent(this.package_version.version))
116115
.replace(':topic', encodeURIComponent(this.name))
117116
.replace('/api/', '/');
118-
} else return sails.getUrlFor({ target: 'Topic.findById' })
117+
} return sails.getUrlFor({ target: 'Topic.findById' })
119118
.replace(':id', encodeURIComponent(this.id))
120119
.replace('/api/', '/');
121120
},
@@ -125,9 +124,9 @@ module.exports = {
125124
.replace(':name', encodeURIComponent(this.package_version.package_name))
126125
.replace(':version', encodeURIComponent(this.package_version.version))
127126
.replace(':topic', encodeURIComponent(this.name));
128-
} else return sails.getUrlFor({ target: 'Topic.findById' })
127+
} return sails.getUrlFor({ target: 'Topic.findById' })
129128
.replace(':id', encodeURIComponent(this.id));
130-
},
129+
}
131130
},
132131

133132
classMethods: {
@@ -140,8 +139,8 @@ module.exports = {
140139
var options = _.mergeWith({
141140
where: criteria,
142141
include: [
143-
{model: Argument, as: 'arguments', attributes: ['name', 'description', 'topic_id'], separate:true },
144-
{model: Section, as: 'sections', attributes: ['name', 'description', 'topic_id'], separate:true },
142+
{model: Argument, as: 'arguments', attributes: ['name', 'description', 'topic_id'], separate: true },
143+
{model: Section, as: 'sections', attributes: ['name', 'description', 'topic_id'], separate: true },
145144
{model: Tag, as: 'keywords', attributes: ['name']},
146145
{model: Alias, as: 'aliases', attributes: ['name', 'topic_id'], separate: true },
147146
{model: Example, as: 'user_examples',
@@ -162,23 +161,24 @@ module.exports = {
162161
package_name: packageName
163162
};
164163

165-
if(version) { //if version is specified, find in this one
164+
if (version) { // if version is specified, find in this one
166165
packageCriteria.version = version;
167166
}
168167

169168
var params = {
170169
where: { name: name },
171170
include: [
172171
{ model: PackageVersion, as: 'package_version', attributes: ['package_name', 'version'], where: packageCriteria }
173-
],
172+
]
174173
};
175174

176175
return Promise.join(
177176
Topic.findAll(params),
178177
Topic.findByAliasInPackage(packageName, name, version),
179178
function(topics, aliasesTopics) {
180179
return topics.concat(aliasesTopics).sort(PackageService.compareVersions('desc', function(topic) {
181-
return topic.package_version.version })
180+
return topic.package_version.version;
181+
})
182182
)[0];
183183
});
184184
},
@@ -188,7 +188,7 @@ module.exports = {
188188
package_name: packageName
189189
};
190190

191-
if(version) { //if version is specified, find in this one
191+
if (version) { // if version is specified, find in this one
192192
packageCriteria.version = version;
193193
}
194194

@@ -200,16 +200,19 @@ module.exports = {
200200
};
201201

202202
return Topic.findAll(params).then(function(topicInstances) {
203-
if(topicInstances) return topicInstances.sort(PackageService.compareVersions('desc', function(topic) {
204-
return topic.package_version.version })
203+
if (topicInstances) {
204+
return topicInstances.sort(PackageService.compareVersions('desc', function(topic) {
205+
return topic.package_version.version;
206+
})
205207
)[0];
206-
else return topicInstances;
208+
}
209+
return topicInstances;
207210
});
208211
},
209212

210213
createWithRdFile: function(opts) {
211214
var rdJSON = opts.input;
212-
return sequelize.transaction(function (t) {
215+
return sequelize.transaction(function(t) {
213216
var attributes = [
214217
'name',
215218
'title',
@@ -225,11 +228,15 @@ module.exports = {
225228

226229
var topic = _.pick(rdJSON, attributes);
227230

231+
if (topic.usage !== null && typeof topic.usage === 'object') {
232+
topic.usage = topic.usage.contents;
233+
}
234+
228235
var arrayToString = function(val) {
229236
if (val instanceof Array) {
230-
if(_.isEmpty(val)) return "";
231-
else return val.join("\n\n");
232-
} else return val;
237+
if (_.isEmpty(val)) return '';
238+
return val.join('\n\n');
239+
} return val;
233240
};
234241

235242
topic = _.mapValues(topic, arrayToString);
@@ -245,14 +252,13 @@ module.exports = {
245252
var packageVersion = {
246253
package_name: opts.packageName,
247254
version: opts.packageVersion,
248-
description: "",
249-
license: ""
255+
description: '',
256+
license: ''
250257
};
251258

252259
return PackageVersion.upsertPackageVersion(packageVersion, {
253260
transaction: t
254261
}).spread(function(version, created) {
255-
256262
topic.package_version_id = version.id;
257263

258264

@@ -281,12 +287,12 @@ module.exports = {
281287
{model: Alias, as: 'aliases'}
282288
]
283289
}),
284-
Tag.bulkCreate(keywordsRecords, {transaction:t, ignoreDuplicates: true})
290+
Tag.bulkCreate(keywordsRecords, {transaction: t, ignoreDuplicates: true})
285291
.then(function(instances) {
286-
var names = _.map(instances, function (inst) {
292+
var names = _.map(instances, function(inst) {
287293
return inst.name;
288294
});
289-
return Tag.findAll({where: {name: {$in: names}}, transaction:t });
295+
return Tag.findAll({where: {name: {$in: names}}, transaction: t });
290296
}),
291297
function(instanceCreatedArray, keywordsInstances) {
292298
var topicInstance = instanceCreatedArray[0];
@@ -329,11 +335,9 @@ module.exports = {
329335
topicInstance.setKeywords(keywordsInstances, {transaction: t }),
330336
topicInstance.save({transaction: t})
331337
]).then(_.partial(Topic.findOnePopulated, {id: topicInstance.id}, {transaction: t}));
332-
});
338+
});
333339
});
334-
335340
});
336-
337341
}
338342
}
339343
}

0 commit comments

Comments
 (0)