diff options
author | Alexander Sulfrian <alexander.sulfrian@fu-berlin.de> | 2016-04-22 00:59:05 +0200 |
---|---|---|
committer | Alexander Sulfrian <alexander.sulfrian@fu-berlin.de> | 2016-06-03 03:56:35 +0200 |
commit | a2888250f4ab3615c1590ab3bbf90302963a7c57 (patch) | |
tree | f72abd0d33187b8240b913fdb2490d1e85fc7b6c /models/boards.js | |
parent | 14e2b3c15fdaaa7a45716041dd9e44740a747b26 (diff) | |
download | wekan-a2888250f4ab3615c1590ab3bbf90302963a7c57.tar.gz wekan-a2888250f4ab3615c1590ab3bbf90302963a7c57.tar.bz2 wekan-a2888250f4ab3615c1590ab3bbf90302963a7c57.zip |
Models: Replace before.insert with autoValues
The before.insert hooks have the problem, that they are executed in a
different order if called from the client or from the server. If called
from the client, the before.insert hook is called before validation of
the schema, but if called from the server, the validation is called
first and fails.
Diffstat (limited to 'models/boards.js')
-rw-r--r-- | models/boards.js | 109 |
1 files changed, 72 insertions, 37 deletions
diff --git a/models/boards.js b/models/boards.js index 52272cce..715bb838 100644 --- a/models/boards.js +++ b/models/boards.js @@ -6,25 +6,77 @@ Boards.attachSchema(new SimpleSchema({ }, slug: { type: String, + autoValue() { // eslint-disable-line consistent-return + // XXX We need to improve slug management. Only the id should be necessary + // to identify a board in the code. + // XXX If the board title is updated, the slug should also be updated. + // In some cases (Chinese and Japanese for instance) the `getSlug` function + // return an empty string. This is causes bugs in our application so we set + // a default slug in this case. + if (this.isInsert && !this.isSet) { + let slug = 'board'; + const title = this.field('title'); + if (title.isSet) { + slug = getSlug(title.value) || slug; + } + return slug; + } + }, }, archived: { type: Boolean, + autoValue() { // eslint-disable-line consistent-return + if (this.isInsert && !this.isSet) { + return false; + } + }, }, createdAt: { type: Date, - denyUpdate: true, + autoValue() { // eslint-disable-line consistent-return + if (this.isInsert) { + return new Date(); + } else { + this.unset(); + } + }, }, // XXX Inconsistent field naming modifiedAt: { type: Date, - denyInsert: true, optional: true, + autoValue() { // eslint-disable-line consistent-return + if (this.isUpdate) { + return new Date(); + } else { + this.unset(); + } + }, }, // De-normalized number of users that have starred this board stars: { type: Number, + autoValue() { // eslint-disable-line consistent-return + if (this.isInsert) { + return 0; + } + }, }, // De-normalized label system + 'labels': { + type: [Object], + autoValue() { // eslint-disable-line consistent-return + if (this.isInsert && !this.isSet) { + const colors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues; + const defaultLabelsColors = _.clone(colors).splice(0, 6); + return defaultLabelsColors.map((color) => ({ + color, + _id: Random.id(6), + name: '', + })); + } + }, + }, 'labels.$._id': { // We don't specify that this field must be unique in the board because that // will cause performance penalties and is not necessary since this field is @@ -47,6 +99,19 @@ Boards.attachSchema(new SimpleSchema({ // XXX We might want to maintain more informations under the member sub- // documents like de-normalized meta-data (the date the member joined the // board, the number of contributions, etc.). + 'members': { + type: [Object], + autoValue() { // eslint-disable-line consistent-return + if (this.isInsert && !this.isSet) { + return [{ + userId: this.userId, + isAdmin: true, + isActive: true, + isInvited: false, + }]; + } + }, + }, 'members.$.userId': { type: String, }, @@ -70,6 +135,11 @@ Boards.attachSchema(new SimpleSchema({ 'wisteria', 'midnight', ], + autoValue() { // eslint-disable-line consistent-return + if (this.isInsert && !this.isSet) { + return Boards.simpleSchema()._schema.color.allowedValues[0]; + } + }, }, description: { type: String, @@ -338,41 +408,6 @@ if (Meteor.isServer) { }); } -Boards.before.insert((userId, doc) => { - // XXX We need to improve slug management. Only the id should be necessary - // to identify a board in the code. - // XXX If the board title is updated, the slug should also be updated. - // In some cases (Chinese and Japanese for instance) the `getSlug` function - // return an empty string. This is causes bugs in our application so we set - // a default slug in this case. - doc.slug = doc.slug || getSlug(doc.title) || 'board'; - doc.createdAt = new Date(); - doc.archived = false; - doc.members = doc.members || [{ - userId, - isAdmin: true, - isActive: true, - }]; - doc.stars = 0; - doc.color = Boards.simpleSchema()._schema.color.allowedValues[0]; - - // Handle labels - const colors = Boards.simpleSchema()._schema['labels.$.color'].allowedValues; - const defaultLabelsColors = _.clone(colors).splice(0, 6); - doc.labels = defaultLabelsColors.map((color) => { - return { - color, - _id: Random.id(6), - name: '', - }; - }); -}); - -Boards.before.update((userId, doc, fieldNames, modifier) => { - modifier.$set = modifier.$set || {}; - modifier.$set.modifiedAt = new Date(); -}); - if (Meteor.isServer) { // Let MongoDB ensure that a member is not included twice in the same board Meteor.startup(() => { |