Просмотр исходного кода

remove cutOffLastSlash method and clean code

Yuki Takei 7 лет назад
Родитель
Сommit
ff35955b4e
2 измененных файлов с 11 добавлено и 23 удалено
  1. 3 10
      src/server/models/GlobalNotificationSetting/index.js
  2. 8 13
      src/server/models/page.js

+ 3 - 10
src/server/models/GlobalNotificationSetting/index.js

@@ -1,4 +1,5 @@
 const mongoose = require('mongoose');
 const mongoose = require('mongoose');
+const nodePath = require('path');
 
 
 /**
 /**
  * parent schema for GlobalNotificationSetting model
  * parent schema for GlobalNotificationSetting model
@@ -74,22 +75,14 @@ class GlobalNotificationSetting {
   }
   }
 }
 }
 
 
-
-// move this to util
-// remove this from models/page
-const cutOffLastSlash = path => {
-  const lastSlash = path.lastIndexOf('/');
-  return path.substr(0, lastSlash);
-};
-
 const generatePathsOnTree = (path, pathList) => {
 const generatePathsOnTree = (path, pathList) => {
   pathList.push(path);
   pathList.push(path);
 
 
-  if (path === '') {
+  if (path === '/') {
     return pathList;
     return pathList;
   }
   }
 
 
-  const newPath = cutOffLastSlash(path);
+  const newPath = nodePath.posix.dirname(path);
 
 
   return generatePathsOnTree(newPath, pathList);
   return generatePathsOnTree(newPath, pathList);
 };
 };

+ 8 - 13
src/server/models/page.js

@@ -1,4 +1,5 @@
 const debug = require('debug')('growi:models:page');
 const debug = require('debug')('growi:models:page');
+const nodePath = require('path');
 const mongoose = require('mongoose');
 const mongoose = require('mongoose');
 const uniqueValidator = require('mongoose-unique-validator');
 const uniqueValidator = require('mongoose-unique-validator');
 const ObjectId = mongoose.Schema.Types.ObjectId;
 const ObjectId = mongoose.Schema.Types.ObjectId;
@@ -505,12 +506,11 @@ module.exports = function(crowi) {
    * find all templates applicable to the new page
    * find all templates applicable to the new page
    */
    */
   pageSchema.statics.findTemplate = function(path) {
   pageSchema.statics.findTemplate = function(path) {
-    const Page = this;
-    const templatePath = cutOffLastSlash(path);
-    const pathList = generatePathsOnTree(templatePath, []);
+    const templatePath = nodePath.posix.dirname(path);
+    const pathList = generatePathsOnTree(path, []);
     const regexpList = pathList.map(path => new RegExp(`^${escapeStringRegexp(path)}/_{1,2}template$`));
     const regexpList = pathList.map(path => new RegExp(`^${escapeStringRegexp(path)}/_{1,2}template$`));
 
 
-    return Page
+    return this
       .find({path: {$in: regexpList}})
       .find({path: {$in: regexpList}})
       .populate({path: 'revision', model: 'Revision'})
       .populate({path: 'revision', model: 'Revision'})
       .then(templates => {
       .then(templates => {
@@ -518,19 +518,14 @@ module.exports = function(crowi) {
       });
       });
   };
   };
 
 
-  const cutOffLastSlash = path => {
-    const lastSlash = path.lastIndexOf('/');
-    return path.substr(0, lastSlash);
-  };
-
   const generatePathsOnTree = (path, pathList) => {
   const generatePathsOnTree = (path, pathList) => {
     pathList.push(path);
     pathList.push(path);
 
 
-    if (path === '') {
+    if (path === '/') {
       return pathList;
       return pathList;
     }
     }
 
 
-    const newPath = cutOffLastSlash(path);
+    const newPath = nodePath.posix.dirname(path);
 
 
     return generatePathsOnTree(newPath, pathList);
     return generatePathsOnTree(newPath, pathList);
   };
   };
@@ -549,11 +544,11 @@ module.exports = function(crowi) {
       return decendantsTemplate;
       return decendantsTemplate;
     }
     }
 
 
-    if (path === '') {
+    if (path === '/') {
       return;
       return;
     }
     }
 
 
-    const newPath = cutOffLastSlash(path);
+    const newPath = nodePath.posix.dirname(path);
     return assignDecendantsTemplate(decendantsTemplates, newPath);
     return assignDecendantsTemplate(decendantsTemplates, newPath);
   };
   };