소스 검색

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.
Alexander Sulfrian 9 년 전
부모
커밋
a2888250f4
6개의 변경된 파일129개의 추가작업 그리고 75개의 파일을 삭제
  1. 1 3
      client/components/cards/labels.js
  2. 72 37
      models/boards.js
  3. 12 5
      models/cardComments.js
  4. 20 12
      models/cards.js
  5. 19 14
      models/lists.js
  6. 5 4
      models/unsavedEdits.js

+ 1 - 3
client/components/cards/labels.js

@@ -9,9 +9,7 @@ BlazeComponent.extendComponent({
   },
   },
 
 
   labels() {
   labels() {
-    return labelColors.map((color) => {
-      return { color, name: '' };
-    });
+    return labelColors.map((color) => ({ color, name: '' }));
   },
   },
 
 
   isSelected(color) {
   isSelected(color) {

+ 72 - 37
models/boards.js

@@ -6,25 +6,77 @@ Boards.attachSchema(new SimpleSchema({
   },
   },
   slug: {
   slug: {
     type: String,
     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: {
   archived: {
     type: Boolean,
     type: Boolean,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert && !this.isSet) {
+        return false;
+      }
+    },
   },
   },
   createdAt: {
   createdAt: {
     type: Date,
     type: Date,
-    denyUpdate: true,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert) {
+        return new Date();
+      } else {
+        this.unset();
+      }
+    },
   },
   },
   // XXX Inconsistent field naming
   // XXX Inconsistent field naming
   modifiedAt: {
   modifiedAt: {
     type: Date,
     type: Date,
-    denyInsert: true,
     optional: 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
   // De-normalized number of users that have starred this board
   stars: {
   stars: {
     type: Number,
     type: Number,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert) {
+        return 0;
+      }
+    },
   },
   },
   // De-normalized label system
   // 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': {
   'labels.$._id': {
     // We don't specify that this field must be unique in the board because that
     // 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
     // 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-
   // XXX We might want to maintain more informations under the member sub-
   // documents like de-normalized meta-data (the date the member joined the
   // documents like de-normalized meta-data (the date the member joined the
   // board, the number of contributions, etc.).
   // 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': {
   'members.$.userId': {
     type: String,
     type: String,
   },
   },
@@ -70,6 +135,11 @@ Boards.attachSchema(new SimpleSchema({
       'wisteria',
       'wisteria',
       'midnight',
       'midnight',
     ],
     ],
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert && !this.isSet) {
+        return Boards.simpleSchema()._schema.color.allowedValues[0];
+      }
+    },
   },
   },
   description: {
   description: {
     type: String,
     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) {
 if (Meteor.isServer) {
   // Let MongoDB ensure that a member is not included twice in the same board
   // Let MongoDB ensure that a member is not included twice in the same board
   Meteor.startup(() => {
   Meteor.startup(() => {

+ 12 - 5
models/cardComments.js

@@ -16,10 +16,22 @@ CardComments.attachSchema(new SimpleSchema({
   createdAt: {
   createdAt: {
     type: Date,
     type: Date,
     denyUpdate: false,
     denyUpdate: false,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert) {
+        return new Date();
+      } else {
+        this.unset();
+      }
+    },
   },
   },
   // XXX Should probably be called `authorId`
   // XXX Should probably be called `authorId`
   userId: {
   userId: {
     type: String,
     type: String,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert && !this.isSet) {
+        return this.userId;
+      }
+    },
   },
   },
 }));
 }));
 
 
@@ -44,11 +56,6 @@ CardComments.helpers({
 
 
 CardComments.hookOptions.after.update = { fetchPrevious: false };
 CardComments.hookOptions.after.update = { fetchPrevious: false };
 
 
-CardComments.before.insert((userId, doc) => {
-  doc.createdAt = new Date();
-  doc.userId = userId;
-});
-
 if (Meteor.isServer) {
 if (Meteor.isServer) {
   CardComments.after.insert((userId, doc) => {
   CardComments.after.insert((userId, doc) => {
     Activities.insert({
     Activities.insert({

+ 20 - 12
models/cards.js

@@ -9,6 +9,11 @@ Cards.attachSchema(new SimpleSchema({
   },
   },
   archived: {
   archived: {
     type: Boolean,
     type: Boolean,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert && !this.isSet) {
+        return false;
+      }
+    },
   },
   },
   listId: {
   listId: {
     type: String,
     type: String,
@@ -25,10 +30,19 @@ Cards.attachSchema(new SimpleSchema({
   },
   },
   createdAt: {
   createdAt: {
     type: Date,
     type: Date,
-    denyUpdate: true,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert) {
+        return new Date();
+      } else {
+        this.unset();
+      }
+    },
   },
   },
   dateLastActivity: {
   dateLastActivity: {
     type: Date,
     type: Date,
+    autoValue() {
+      return new Date();
+    },
   },
   },
   description: {
   description: {
     type: String,
     type: String,
@@ -46,6 +60,11 @@ Cards.attachSchema(new SimpleSchema({
   // the `members` field?
   // the `members` field?
   userId: {
   userId: {
     type: String,
     type: String,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert && !this.isSet) {
+        return this.userId;
+      }
+    },
   },
   },
   sort: {
   sort: {
     type: Number,
     type: Number,
@@ -190,17 +209,6 @@ Cards.mutations({
   },
   },
 });
 });
 
 
-Cards.before.insert((userId, doc) => {
-  doc.createdAt = new Date();
-  doc.dateLastActivity = new Date();
-  if(!doc.hasOwnProperty('archived')){
-    doc.archived = false;
-  }
-  if (!doc.userId) {
-    doc.userId = userId;
-  }
-});
-
 if (Meteor.isServer) {
 if (Meteor.isServer) {
   Cards.after.insert((userId, doc) => {
   Cards.after.insert((userId, doc) => {
     Activities.insert({
     Activities.insert({

+ 19 - 14
models/lists.js

@@ -6,13 +6,24 @@ Lists.attachSchema(new SimpleSchema({
   },
   },
   archived: {
   archived: {
     type: Boolean,
     type: Boolean,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert && !this.isSet) {
+        return false;
+      }
+    },
   },
   },
   boardId: {
   boardId: {
     type: String,
     type: String,
   },
   },
   createdAt: {
   createdAt: {
     type: Date,
     type: Date,
-    denyUpdate: true,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert) {
+        return new Date();
+      } else {
+        this.unset();
+      }
+    },
   },
   },
   sort: {
   sort: {
     type: Number,
     type: Number,
@@ -22,8 +33,14 @@ Lists.attachSchema(new SimpleSchema({
   },
   },
   updatedAt: {
   updatedAt: {
     type: Date,
     type: Date,
-    denyInsert: true,
     optional: true,
     optional: true,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isUpdate) {
+        return new Date();
+      } else {
+        this.unset();
+      }
+    },
   },
   },
 }));
 }));
 
 
@@ -73,18 +90,6 @@ Lists.mutations({
 
 
 Lists.hookOptions.after.update = { fetchPrevious: false };
 Lists.hookOptions.after.update = { fetchPrevious: false };
 
 
-Lists.before.insert((userId, doc) => {
-  doc.createdAt = new Date();
-  doc.archived = false;
-  if (!doc.userId)
-    doc.userId = userId;
-});
-
-Lists.before.update((userId, doc, fieldNames, modifier) => {
-  modifier.$set = modifier.$set || {};
-  modifier.$set.modifiedAt = new Date();
-});
-
 if (Meteor.isServer) {
 if (Meteor.isServer) {
   Lists.after.insert((userId, doc) => {
   Lists.after.insert((userId, doc) => {
     Activities.insert({
     Activities.insert({

+ 5 - 4
models/unsavedEdits.js

@@ -14,6 +14,11 @@ UnsavedEditCollection.attachSchema(new SimpleSchema({
   },
   },
   userId: {
   userId: {
     type: String,
     type: String,
+    autoValue() { // eslint-disable-line consistent-return
+      if (this.isInsert && !this.isSet) {
+        return this.userId;
+      }
+    },
   },
   },
 }));
 }));
 
 
@@ -28,7 +33,3 @@ if (Meteor.isServer) {
     fetch: ['userId'],
     fetch: ['userId'],
   });
   });
 }
 }
-
-UnsavedEditCollection.before.insert((userId, doc) => {
-  doc.userId = userId;
-});