فهرست منبع

moved functions from route to model

sou 7 سال پیش
والد
کامیت
6be9e669c4
2فایلهای تغییر یافته به همراه15 افزوده شده و 21 حذف شده
  1. 14 1
      lib/models/page.js
  2. 1 20
      lib/routes/page.js

+ 14 - 1
lib/models/page.js

@@ -528,8 +528,10 @@ module.exports = function(crowi) {
   };
 
   // find all templates applicable to the new page
-  pageSchema.statics.findTemplate = function(pathList, templatePath) {
+  pageSchema.statics.findTemplate = function(path) {
     const Page = this;
+    const templatePath = cutOffLastSlash(path);
+    const pathList = generatePathsOnTree(templatePath, []);
     const regexpList = pathList.map(path => new RegExp(`${path}/_{1,2}template`));
 
     return Page
@@ -545,6 +547,17 @@ module.exports = function(crowi) {
     return path.substr(0, lastSlash);
   };
 
+  const generatePathsOnTree = (path, pathList) => {
+    const newPath = cutOffLastSlash(path);
+
+    if (newPath === '') {
+      return pathList;
+    }
+
+    pathList.push(newPath);
+    return generatePathsOnTree(newPath, pathList);
+  };
+
   const assignTemplateByType = (templates, path, type) => {
     for (let i = 0; i < templates.length; i++) {
       if (templates[i].path === `${path}/${type}template`) {

+ 1 - 20
lib/routes/page.js

@@ -311,10 +311,7 @@ module.exports = function(crowi, app) {
     .catch(function(err) {
       pageTeamplate = 'customlayout-selector/not_found';
 
-      const templatePath = cutOffLastSlash(originalPath);
-      const pathList = generatePathsOnTree(originalPath, []);
-
-      return Page.findTemplate(pathList, templatePath)
+      return Page.findTemplate(originalPath)
         .then(template => {
           renderVars.template = template;
         });
@@ -367,22 +364,6 @@ module.exports = function(crowi, app) {
     });
   };
 
-  const cutOffLastSlash = path => {
-    const lastSlash = path.lastIndexOf('/');
-    return path.substr(0, lastSlash);
-  };
-
-  const generatePathsOnTree = (path, pathList) => {
-    const newPath = cutOffLastSlash(path);
-
-    if (newPath === '') {
-      return pathList;
-    }
-
-    pathList.push(newPath);
-    return generatePathsOnTree(newPath, pathList);
-  };
-
   actions.deletedPageListShow = function(req, res) {
     var path = '/trash' + getPathFromRequest(req);
     var limit = 50;