From 2f44eabd70a829fcf3df2d7dc2ee3f0c4e700386 Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 26 May 2013 17:54:06 -0400 Subject: [PATCH] add model fn to list posts in an archive format (by month/year, title only) --- resources/couchdb/design_docs/posts.js | 6 ++++++ src/blarg/datetime.clj | 10 +++++++++- src/blarg/models/posts.clj | 10 ++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/resources/couchdb/design_docs/posts.js b/resources/couchdb/design_docs/posts.js index 23335e6..7e223cf 100644 --- a/resources/couchdb/design_docs/posts.js +++ b/resources/couchdb/design_docs/posts.js @@ -28,6 +28,12 @@ }, "listPublishedPostsByTag": { "map": "function(doc) {\n if (doc.type === \"post\" && doc.published === true) {\n for (var i = 0; i < doc.tags.length; ++i) {\n var post = {created_at: doc.created_at,\n slug: doc.slug,\n title: doc.title};\n emit(doc.tags[i], post)\n }\n }\n}" + }, + "listPublishedPostsArchive": { + "map": "function(doc) {\n if (doc.type === \"post\" && doc.published === true) {\n var date = new Date(doc.created_at)\n var monthYearStr = (date.getMonth() + 1) + \"-\" + date.getFullYear();\n var post = {mmyyyy: monthYearStr,\n created_at: doc.created_at,\n slug: doc.slug,\n title: doc.title};\n emit(doc.created_at, post)\n }\n}" + }, + "listPostsArchive": { + "map": "function(doc) {\n if (doc.type === \"post\") {\n var date = new Date(doc.created_at)\n var monthYearStr = (date.getMonth() + 1) + \"-\" + date.getFullYear();\n var post = {mmyyyy: monthYearStr,\n created_at: doc.created_at,\n slug: doc.slug,\n title: doc.title};\n emit(doc.created_at, post)\n }\n}" } } } \ No newline at end of file diff --git a/src/blarg/datetime.clj b/src/blarg/datetime.clj index 3f6ec6e..6d99363 100644 --- a/src/blarg/datetime.clj +++ b/src/blarg/datetime.clj @@ -59,7 +59,6 @@ (seq? coll) (map (fn [m] (date->string m fields)) coll))) - (defn ->relative-timestamp "Returns a readable string representing the time between the current date and the provided one" @@ -82,3 +81,12 @@ (> minutes 1) (str minutes " minutes ago") (= minutes 1) "1 minute ago" :else "just now")))) + +(defn ->nicer-month-year-str + "Given a date in 'MM-yyyy' format (numeric month), returns the same date in + 'MMM yyyy' format (name of month)" + [s] + (if-let [date (clj-time.format/parse (clj-time.format/formatter "MM-yyyy") s)] + (clj-time.format/unparse (clj-time.format/formatter "MMM yyyy") date))) + + diff --git a/src/blarg/models/posts.clj b/src/blarg/models/posts.clj index c946088..22be866 100644 --- a/src/blarg/models/posts.clj +++ b/src/blarg/models/posts.clj @@ -91,6 +91,16 @@ ;TODO: look into couchdb partial key matching to do this sort at the DB (sort-by :created_at clj-time.core/after? posts)))) +(defn list-posts-archive [unpublished?] + (let [view-name (if unpublished? "listPostsArchive" "listPublishedPostsArchive")] + (if-let [posts (->post-list + (couch/with-db posts + (couch/get-view "posts" view-name {:descending true})))] + (group-by + (fn [p] + (->nicer-month-year-str (:mmyyyy p))) + posts)))) + (defn count-posts [unpublished?] (->first-view-value