couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robertkowal...@apache.org
Subject fauxton commit: updated refs/heads/master to c7f09aa
Date Wed, 07 Oct 2015 18:07:53 GMT
Repository: couchdb-fauxton
Updated Branches:
  refs/heads/master b539e8ba6 -> c7f09aad3


make code for getting safe doc ids reusable

this puts the tools for getting a safe id for a doc into a shared
location.

part of https://github.com/apache/couchdb-fauxton/pull/543

PR: #552
PR-URL: https://github.com/apache/couchdb-fauxton/pull/552
Reviewed-By: Michelle Phung <michellep@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/commit/c7f09aad
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/tree/c7f09aad
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/diff/c7f09aad

Branch: refs/heads/master
Commit: c7f09aad32754f2f025ecb4201f02f7db8f01ab4
Parents: b539e8b
Author: Robert Kowalski <robertkowalski@apache.org>
Authored: Wed Oct 7 15:21:02 2015 +0200
Committer: Robert Kowalski <robertkowalski@apache.org>
Committed: Wed Oct 7 20:07:45 2015 +0200

----------------------------------------------------------------------
 app/addons/documents/shared-resources.js | 13 ++---------
 app/core/tests/utilsSpec.js              | 31 +++++++++++++++++++++++++++
 app/core/utils.js                        | 21 ++++++++++++++++++
 3 files changed, 54 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/c7f09aad/app/addons/documents/shared-resources.js
----------------------------------------------------------------------
diff --git a/app/addons/documents/shared-resources.js b/app/addons/documents/shared-resources.js
index 00e0a59..0c16154 100644
--- a/app/addons/documents/shared-resources.js
+++ b/app/addons/documents/shared-resources.js
@@ -54,7 +54,7 @@ define([
     },
 
     docType: function () {
-      return this.id && this.id.match(/^_design\//) ? "design doc" : "doc";
+      return app.utils.getDocTypeFromId(this.id);
     },
 
     isDeletable: function () {
@@ -155,17 +155,8 @@ define([
       return this;
     },
 
-    // Need this to work around backbone router thinking _design/foo
-    // is a separate route. Alternatively, maybe these should be
-    // treated separately. For instance, we could default into the
-    // json editor for docs, or into a ddoc specific page.
     safeID: function () {
-      if (this.isDdoc()) {
-        var ddoc = this.id.replace(/^_design\//, "");
-        return "_design/" + app.utils.safeURLName(ddoc);
-      }else {
-        return app.utils.safeURLName(this.id);
-      }
+      return app.utils.getSafeIdForDoc(this.id);
     },
 
     destroy: function () {

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/c7f09aad/app/core/tests/utilsSpec.js
----------------------------------------------------------------------
diff --git a/app/core/tests/utilsSpec.js b/app/core/tests/utilsSpec.js
index 8b8634f..17d09ef 100644
--- a/app/core/tests/utilsSpec.js
+++ b/app/core/tests/utilsSpec.js
@@ -18,6 +18,37 @@ define([
 
   describe('Utils', function () {
 
+    describe('getDocTypeFromId', function () {
+
+      it('returns doc if id not given', function () {
+        var res = utils.getDocTypeFromId();
+        assert.equal(res, 'doc');
+      });
+
+      it('returns design doc for design docs', function () {
+        var res = utils.getDocTypeFromId('_design/foobar');
+        assert.equal(res, 'design doc');
+      });
+
+      it('returns doc for all others', function () {
+        var res = utils.getDocTypeFromId('blerg');
+        assert.equal(res, 'doc');
+      });
+    });
+
+    describe('getSafeIdForDoc', function () {
+
+      it('keeps _design/ intact', function () {
+        var res = utils.getSafeIdForDoc('_design/foo/do');
+        assert.equal(res, '_design/foo%2Fdo');
+      });
+
+      it('encodes all other', function () {
+        var res = utils.getSafeIdForDoc('_redesign/foobar');
+        assert.equal(res, '_redesign%2Ffoobar');
+      });
+    });
+
     describe('localStorage', function () {
 
       it('Should get undefined when getting a non-existent key', function () {

http://git-wip-us.apache.org/repos/asf/couchdb-fauxton/blob/c7f09aad/app/core/utils.js
----------------------------------------------------------------------
diff --git a/app/core/utils.js b/app/core/utils.js
index dbc1155..43fabbc 100644
--- a/app/core/utils.js
+++ b/app/core/utils.js
@@ -98,6 +98,27 @@ function ($, _) {
       return (checkforBad !== null) ? encodeURIComponent(name) : name;
     },
 
+    getDocTypeFromId: function (id) {
+      if (id && /^_design\//.test(id)) {
+        return 'design doc';
+      }
+
+      return 'doc';
+    },
+
+    // Need this to work around backbone router thinking _design/foo
+    // is a separate route. Alternatively, maybe these should be
+    // treated separately. For instance, we could default into the
+    // json editor for docs, or into a ddoc specific page.
+    getSafeIdForDoc: function (id) {
+      if (utils.getDocTypeFromId(id) === 'design doc') {
+        var ddoc = id.replace(/^_design\//, '');
+        return '_design/' + utils.safeURLName(ddoc);
+      }
+
+      return utils.safeURLName(id);
+    },
+
     // a pair of simple local storage wrapper functions. These ward against problems getting
or
     // setting (e.g. local storage full) and allow you to get/set complex data structures
     localStorageSet: function (key, value) {


Mime
View raw message