From 4b831d30306632b75c9a242a753383061b0e6d13 Mon Sep 17 00:00:00 2001 From: gered Date: Tue, 30 Jul 2013 21:40:56 -0400 Subject: [PATCH] minor and mostly unnecessary code cleanups --- src/blarg/datetime.clj | 28 +++++++++----------- src/blarg/models/files.clj | 4 +-- src/blarg/models/posts.clj | 12 +++------ src/blarg/routes/files.clj | 15 ++++++----- src/blarg/routes/posts.clj | 54 ++++++++++++++++++++------------------ src/blarg/routes/rss.clj | 17 ++++++------ src/blarg/views/layout.clj | 9 ++++--- 7 files changed, 68 insertions(+), 71 deletions(-) diff --git a/src/blarg/datetime.clj b/src/blarg/datetime.clj index 02729a9..2206b9b 100644 --- a/src/blarg/datetime.clj +++ b/src/blarg/datetime.clj @@ -32,32 +32,28 @@ [coll fields] (cond (map? coll) - (merge - coll - (apply - (fn [field] - (if-let [timestamp (get coll field)] - {field (parse-timestamp timestamp)})) - fields)) + (->> fields + (apply + #(if-let [timestamp (get coll %)] + {% (parse-timestamp timestamp)})) + (merge coll)) (seq? coll) - (map (fn [m] (string->date m fields)) coll))) + (map #(string->date % fields) coll))) (defn date->string "Does the reverse of string->date (converts LocalDate's back to strings)" [coll fields] (cond (map? coll) - (merge - coll - (apply - (fn [field] - (if-let [date (get coll field)] - {field (get-timestamp date)})) - fields)) + (->> fields + (apply + #(if-let [date (get coll %)] + {% (get-timestamp date)})) + (merge coll)) (seq? coll) - (map (fn [m] (date->string m fields)) coll))) + (map #(date->string % fields) coll))) (defn ->relative-timestamp "Returns a readable string representing the time between the current date diff --git a/src/blarg/models/files.clj b/src/blarg/models/files.clj index 51f4387..ddc3d99 100644 --- a/src/blarg/models/files.clj +++ b/src/blarg/models/files.clj @@ -20,7 +20,7 @@ (couch/get-view "files" view-name {:key f}))] (let [id (:_id file-info) attachment (:filename file-info) - attachment-info (second (first (:_attachments file-info))) + attachment-info (->> file-info :_attachments first second) gz (couch/get-attachment id attachment)] (if gz (merge @@ -34,7 +34,7 @@ (couch/get-view "files" "listAllByPath" {:key p})))] (map (fn [f] - (let [attachment (second (first (:_attachments f)))] + (let [attachment (->> f :_attachments first second)] {:id (:_id f) :filename (:filename f) :last_modified (parse-timestamp (:last_modified_at f)) diff --git a/src/blarg/models/posts.clj b/src/blarg/models/posts.clj index 5813538..f32f450 100644 --- a/src/blarg/models/posts.clj +++ b/src/blarg/models/posts.clj @@ -96,14 +96,10 @@ (if-let [posts (->post-list (couch/with-db posts (couch/get-view "posts" view-name {:descending true})))] - (vals - (group-by - (fn [p] - (:mmyyyy p)) - (map - (fn [p] - (assoc p :mmyyyy (->nicer-month-year-str (:mmyyyy p)))) - posts)))))) + (->> posts + (map #(assoc % :mmyyyy (->nicer-month-year-str (:mmyyyy %)))) + (group-by #(:mmyyyy %)) + (vals))))) (defn count-posts [unpublished?] diff --git a/src/blarg/routes/files.clj b/src/blarg/routes/files.clj index 8a68b5b..3d6960f 100644 --- a/src/blarg/routes/files.clj +++ b/src/blarg/routes/files.clj @@ -16,13 +16,14 @@ (defn list-files [path] (let [p (ensure-prefix-suffix path "/")] (layout/render - "files/list.html" {:html-title (->html-title [(str "Files in " p)]) - :path p - :files (files/list-files p) - :tree (files/get-tree) - :error (session/flash-get :file-error) - :success (session/flash-get :file-success) - :notice (session/flash-get :file-notice)}))) + "files/list.html" + {:html-title (->html-title [(str "Files in " p)]) + :path p + :files (files/list-files p) + :tree (files/get-tree) + :error (session/flash-get :file-error) + :success (session/flash-get :file-success) + :notice (session/flash-get :file-notice)}))) (defn handle-new-file [path file returnpath] (if (valid-upload? file) diff --git a/src/blarg/routes/posts.clj b/src/blarg/routes/posts.clj index ffbeeb8..450934c 100644 --- a/src/blarg/routes/posts.clj +++ b/src/blarg/routes/posts.clj @@ -13,11 +13,9 @@ [blarg.routes.auth :as auth])) (defn string->tags [s] - (set - (map - (fn [tag] - (->slug tag)) - (clojure.string/split s #",")))) + (->> (clojure.string/split s #",") + (map #(->slug %)) + (set))) (defn tags->string [tags] (clojure.string/join "," tags)) @@ -44,37 +42,42 @@ currentpage (make-in-range page 0 lastpage) offset (* currentpage posts/per-page)] (layout/render - "posts/list.html" {:posts (posts/list-posts (auth/logged-in?) posts/per-page offset) - :prevpage (- currentpage 1) - :nextpage (+ currentpage 1) - :atlastpage (= currentpage lastpage) - :atfirstpage (= currentpage 0) - :inlist true}))) + "posts/list.html" + {:posts (posts/list-posts (auth/logged-in?) posts/per-page offset) + :prevpage (- currentpage 1) + :nextpage (+ currentpage 1) + :atlastpage (= currentpage lastpage) + :atfirstpage (= currentpage 0) + :inlist true}))) (defn list-by-tag [tag] (layout/render - "posts/listbytag.html" {:posts (posts/list-posts-by-tag (auth/logged-in?) tag) - :tag tag})) + "posts/listbytag.html" + {:posts (posts/list-posts-by-tag (auth/logged-in?) tag) + :tag tag})) (defn list-archive [] (layout/render - "posts/listarchive.html" {:months (posts/list-posts-archive (auth/logged-in?))})) + "posts/listarchive.html" + {:months (posts/list-posts-archive (auth/logged-in?))})) (defn show-post-page [year month day slug] (let [date (str (string->int year) "-" (string->int month) "-" (string->int day)) post (posts/get-post-by-date-slug date slug)] (if (not-empty post) (layout/render - "posts/showpost.html" {:post post - :html-title (->html-title [(:title post)])}) + "posts/showpost.html" + {:post post + :html-title (->html-title [(:title post)])}) (resp/redirect "/notfound")))) (defn new-post-page [& post] (layout/render - "posts/newpost.html" (merge (first post) - {:all-tags (posts/list-tags) - :html-title (->html-title ["New Post"]) - :validation-errors @vali/*errors*}))) + "posts/newpost.html" + (merge (first post) + {:all-tags (posts/list-tags) + :html-title (->html-title ["New Post"]) + :validation-errors @vali/*errors*}))) (defn handle-new-post [title tags body] (if (valid-post? title tags body) @@ -90,11 +93,12 @@ (first posted-post) (posts/get-post id))] (layout/render - "posts/editpost.html" (merge post - {:tags (tags->string (:tags post)) - :all-tags (posts/list-tags) - :html-title (->html-title ["Edit Post"]) - :validation-errors @vali/*errors*})))) + "posts/editpost.html" + (merge post + {:tags (tags->string (:tags post)) + :all-tags (posts/list-tags) + :html-title (->html-title ["Edit Post"]) + :validation-errors @vali/*errors*})))) (defn handle-edit-post [id title tags body] (if (valid-post? title tags body) diff --git a/src/blarg/routes/rss.clj b/src/blarg/routes/rss.clj index 02936f4..9f03369 100644 --- a/src/blarg/routes/rss.clj +++ b/src/blarg/routes/rss.clj @@ -15,18 +15,17 @@ (defn handle-rss [] (let [channel {:title rss-title :link rss-site-url - :description rss-description} - items (doall - (map - (fn [post] + :description rss-description}] + (resp/content-type "text/xml" + (apply + (partial rss/channel-xml channel) + (->> (posts/list-posts false 10) + (map (fn [post] {:title (:title post) :pubDate (clj-time.coerce/to-date (:created_at post)) :link (str rss-site-url (subs (get-post-url post) 1)) - :description (md/md-to-html-string (:body post))}) - (posts/list-posts false 10)))] - (resp/content-type "text/xml" - (apply (partial rss/channel-xml channel) - items)))) + :description (md/md-to-html-string (:body post))})) + (doall)))))) (defroutes rss-routes (GET "/rss" [] (handle-rss))) diff --git a/src/blarg/views/layout.clj b/src/blarg/views/layout.clj index f015e69..1315861 100644 --- a/src/blarg/views/layout.clj +++ b/src/blarg/views/layout.clj @@ -7,7 +7,8 @@ (def template-path "blarg/views/templates/") (defn render [template & [params]] - (parser/render-file (str template-path template) - (assoc params - :context (:context *request*) - :user-id (session/get :user)))) + (parser/render-file + (str template-path template) + (assoc params + :context (:context *request*) + :user-id (session/get :user))))