From e43d604264eedd09461d1a62cc7f2d5087293cc5 Mon Sep 17 00:00:00 2001 From: Jacob Bertram Date: Mon, 14 May 2012 10:02:21 -0500 Subject: [PATCH] Fetching posts by tag now handled in view `couchpress/tag`, added `couchpress/tag` view to install script --- controllers/posts.coffee | 8 +++----- setup/install.js | 13 ++++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/controllers/posts.coffee b/controllers/posts.coffee index 69da7a4..409071b 100644 --- a/controllers/posts.coffee +++ b/controllers/posts.coffee @@ -31,16 +31,14 @@ class Post callback null, res findByTag: (tag, callback) -> - @db.view 'couchpress/all', {descending: true}, (err, res) -> + @db.view 'couchpress/tag', {key: tag, descending: true}, (err, res) -> if (err) callback(err) else docs = [] - # find posts with a certain tag res.forEach (row) -> - if (row.tags && row.tags.indexOf(tag) != -1) - row.created_at = moment(row.created_at).fromNow() - docs.push row + row.created_at = moment(row.created_at).fromNow() + docs.push row callback(null, docs) save: (article, callback) -> diff --git a/setup/install.js b/setup/install.js index eee04a4..224ed7f 100644 --- a/setup/install.js +++ b/setup/install.js @@ -215,8 +215,19 @@ function installViews(data,cb){ all: { map: 'function(doc) {\n if (doc.type !== \'upload\')\n emit(doc.created_at, doc);\n}\n' }, + tag: { + map: function(doc) { + for (tag in doc.tags) { + emit(doc.tags[tag], doc) + } + } + }, uploads_all: { - map: 'function(doc) {\n if (doc.type === \'upload\')\n emit(doc.created_at, doc);\n}' + map: function(doc) { + if (doc.type === 'upload') { + emit(doc.created_at, doc); + } + } } } }, function(error,res){