add model fn to list posts in an archive format (by month/year, title only)
This commit is contained in:
parent
e308c35620
commit
2f44eabd70
|
@ -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}"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)))
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in a new issue