Skip to content

Commit

Permalink
Merge branch 'flash' of github.com:ibdknox/noir into flash
Browse files Browse the repository at this point in the history
  • Loading branch information
ibdknox committed Dec 23, 2011
2 parents 0abdee0 + 72442b2 commit 87d815c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
2 changes: 1 addition & 1 deletion project.clj
@@ -1,6 +1,6 @@
(defproject noir "1.2.2-SNAPSHOT"
:description "Noir - a clojure web framework"
:dependencies [[org.clojure/clojure "[1.2.1],[1.3.0]"]
:dependencies [[org.clojure/clojure "1.3.0"]
[compojure "1.0.0-RC2"]
[org.clojure/tools.namespace "0.1.0"]
[clj-json "0.4.3"]
Expand Down
5 changes: 3 additions & 2 deletions src/noir/server.clj
Expand Up @@ -13,8 +13,9 @@
routes have already been added to the route table."
[& [opts]]
(-> (handler/base-handler opts)
(compojure/site)
(handler/wrap-noir-middleware opts)))
(compojure/site)
(handler/wrap-noir-middleware opts)
(handler/wrap-spec-routes opts)))

(defn load-views
"Require all the namespaces in the given dir so that the pages are loaded
Expand Down
37 changes: 24 additions & 13 deletions src/noir/server/handler.clj
@@ -1,7 +1,8 @@
(ns noir.server.handler
"Handler generation functions used by noir.server and other ring handler libraries."
(:use [compojure.core :only [routes ANY]]
ring.middleware.reload-modified)
ring.middleware.reload-modified
ring.middleware.flash)
(:import java.net.URLDecoder)
(:require [compojure.route :as c-route]
[hiccup.core :as hiccup]
Expand Down Expand Up @@ -84,8 +85,7 @@
(apply routes (concat (add-route-middleware @noir/pre-routes)
(add-route-middleware @noir/noir-routes)
(add-route-middleware @noir/post-routes)
@noir/compojure-routes
(spec-routes))))
@noir/compojure-routes)))

(defn- init-routes [opts]
(binding [options/*options* (options/compile-options opts)]
Expand All @@ -108,16 +108,27 @@
"Wrap a base handler in all of noir's middleware"
[handler opts]
(binding [options/*options* (options/compile-options opts)]
(->
handler
(wrap-base-url)
(session/wrap-noir-session)
(cookie/wrap-noir-cookies)
(validation/wrap-noir-validation)
(statuses/wrap-status-pages)
(wrap-route-updating)
(exception/wrap-exceptions)
(options/wrap-options opts))))
(-> handler
(wrap-base-url)
(session/wrap-noir-session)
(session/wrap-noir-flash)
(cookie/wrap-noir-cookies)
(validation/wrap-noir-validation)
(statuses/wrap-status-pages)
(wrap-route-updating)
(exception/wrap-exceptions)
(options/wrap-options opts))))

;; We want to not wrap these particular routes in session and flash middleware.
(defn wrap-spec-routes
"Wrap a handler in noir's resource and catch-all routes."
[handler opts]
(routes handler
(-> (apply routes (spec-routes))
(wrap-base-url)
(statuses/wrap-status-pages)
(exception/wrap-exceptions)
(options/wrap-options opts))))

(defn base-handler
"Get the most basic Noir request handler, only adding wrap-custom-middleware and wrap-request-map."
Expand Down
49 changes: 34 additions & 15 deletions src/noir/session.clj
Expand Up @@ -7,6 +7,8 @@
ring.middleware.session.memory)
(:require [noir.options :as options]))

;; ## Session

(declare ^:dynamic *noir-session*)
(defonce mem (atom {}))

Expand Down Expand Up @@ -37,21 +39,6 @@
[k]
(clojure.core/swap! *noir-session* dissoc (name k)))

(defn flash-put!
"Store a value with a lifetime of one retrieval (on the first flash-get,
it is removed). This is often used for passing small messages to pages
after a redirect."
[v]
(put! :_flash v))

(defn flash-get
"Retrieve the flash stored value. This will remove the flash from the
session."
[]
(let [flash (get :_flash)]
(remove! :_flash)
flash))

(defn noir-session [handler]
(fn [request]
(binding [*noir-session* (atom (:session request))]
Expand All @@ -69,3 +56,35 @@
(wrap-session
(assoc-if {:store (options/get :session-store (memory-store mem))}
:cookie-attrs (options/get :session-cookie-attrs)))))

;; ## Flash

(declare ^:dynamic *noir-flash*)

(defn flash-put!
"Store a value that will persist for this request and the next."
[k v]
(clojure.core/swap!
*noir-flash*
(fn [old]
(-> old
(assoc-in [:incoming k] v)
(assoc-in [:outgoing k] v)))))

(defn flash-get
"Retrieve the flash stored value. This will remove the flash from the
session."
([k]
(flash-get k nil))
([k not-found]
(get-in @*noir-flash* [:incoming k] not-found)))

(defn wrap-noir-flash [handler]
(fn [request]
(binding [*noir-flash* (atom {:incoming (:flash request)})]
(let [resp (handler request)
outgoing-flash (merge (:outgoing *noir-flash*)
(:flash resp))]
(if (and resp outgoing-flash)
(assoc resp :flash outgoing-flash)
resp)))))

0 comments on commit 87d815c

Please sign in to comment.