Skip to content

Commit

Permalink
render must always return an html string.
Browse files Browse the repository at this point in the history
  • Loading branch information
brentonashworth committed Dec 25, 2010
1 parent 900f0f3 commit dc3f126
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 57 deletions.
52 changes: 24 additions & 28 deletions src/sandbar/forms3.clj
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,22 @@
(defn button? [field]
(contains? #{:cancel-button :submit-button} (:type (field-map field))))

(defrecord GridLayout [title]
(defrecord GridLayout []
Html
(render [_ {:keys [request response fields] :as form-info}]
(render [_ {:keys [request response fields title] :as form-info}]
(let [buttons (filter button? fields)
fields (filter (complement button?) fields)
rendered-fields (map #(render % form-info) fields)
rendered-buttons (map #(render % form-info) buttons)
title (if (fn? title)
(title request)
title)
body (html/html [:table
title)]
(html/html [:table
[:tr
[:td
[:div rendered-fields]
[:div.buttons
[:span.basic-buttons rendered-buttons]]]]])]
(-> form-info
(assoc-in [:response :body] body)
(assoc :title title)))))
[:span.basic-buttons rendered-buttons]]]]]))))

;;
;; Form
Expand All @@ -165,22 +162,20 @@
Html
(render [_ {:keys [request] :as form-info}]
(let [[action method] (action-method request)
form-info (render layout form-info)
method-str (.toUpperCase (name method))
body (html/html
[:div.sandbar-form
(-> (if (contains? #{:get :post} method)
[:form (merge {:method method-str
:action action}
attributes)]
[:form (merge {:method "POST" :action action} attributes)
[:input {:type "hidden"
:name "_method"
:value method-str}]])
(conj (-> form-info :response :body))
(vec))])]
(-> form-info
(assoc-in [:response :body] body)))))
layout (render layout form-info)
method-str (.toUpperCase (name method))]
(html/html
[:div.sandbar-form
(-> (if (contains? #{:get :post} method)
[:form (merge {:method method-str
:action action}
attributes)]
[:form (merge {:method "POST" :action action} attributes)
[:input {:type "hidden"
:name "_method"
:value method-str}]])
(conj layout)
(vec))]))))

(defrecord EmbeddedFormHandler [f]
FormHandler
Expand Down Expand Up @@ -257,9 +252,8 @@

(defn grid-layout
"This will implement all of the features of the current grid layout."
[& {:keys [title] :as options}]
(let [title (or title "")]
(GridLayout. title)))
[& {:keys [] :as options}]
(GridLayout.))

(defn replace-params
"Replace all routes params by values contained in the given params map."
Expand Down Expand Up @@ -334,7 +328,9 @@
(add-previous-input form)
load
defaults
(partial render form)]]
(fn [form-info]
(assoc-in form-info [:response :body]
(render form form-info)))]]
(apply comp (reverse processors))))

(defn embedded-form
Expand Down
49 changes: 20 additions & 29 deletions test/sandbar/test/forms3.clj
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,14 @@
:fields fields}]
(let [form (form :user-form
:id :user-form)]
(let [{{h :body} :response} (render form form-info)]
(let [h (render form form-info)]
(is (= (form-action h :user-form) "/a"))
(is (= (form-method h :user-form) "POST"))
(is (= (fields-and-vals h :user-form)
{:name ""
:submit "Submit"
:cancel "Cancel"})))
(let [{{h :body} :response}
(render form (assoc form-info :form-data {:name "b"}))]
(let [h (render form (assoc form-info :form-data {:name "b"}))]
(is (= (fields-and-vals h :user-form)
{:name "b"
:submit "Submit"
Expand All @@ -299,26 +298,25 @@
:update-action "/users/:id"
:update-method :put
:id :user-form)]
(let [{{h :body} :response} (render form form-info)]
(let [h (render form form-info)]
(is (= (form-action h :user-form) "/users"))
(is (= (form-method h :user-form) "POST"))
(is (= (fields-and-vals h :user-form)
{:name ""
:submit "Submit"
:cancel "Cancel"})))
(let [{{h :body} :response}
(render form
{:request (assoc request :route-params {"id" 7})
:form-data {:name "x"}
:fields fields})]
(let [h (render form
{:request (assoc request :route-params {"id" 7})
:form-data {:name "x"}
:fields fields})]
(is (= (form-action h :user-form) "/users/7"))
(is (= (form-method h :user-form) "POST"))
(is (= (fields-and-vals h :user-form)
{:name "x"
:_method "PUT"
:submit "Submit"
:cancel "Cancel"}))))
(let [form (form :user-form
#_(let [form (form :user-form
:layout (grid-layout :title "My Title"))]
(let [{title :title} (render form form-info)]
(is (= title "My Title"))))))
Expand All @@ -329,14 +327,13 @@
:update-action "/users/:id"
:update-method :put
:id :user-form
:layout (grid-layout :title "My Title"))]
:layout (grid-layout))]
(testing "embedded forms"
(let [request {:uri "/a"}
fields [(textfield :name :id :name- :label "My Name")]]
(let [ef (embedded-form form fields)
{:keys [title response]} (process-request ef request)
{:keys [response]} (process-request ef request)
body (:body response)]
(is (= title "My Title"))
(is (= (form-action body :user-form) "/users"))
(is (= (form-method body :user-form) "POST"))
(is (= (fields-and-vals body :user-form)
Expand All @@ -345,30 +342,27 @@
(let [ef (embedded-form form
fields
:defaults {:name "x"})
{:keys [title response errors]} (process-request ef request)
{:keys [response errors]} (process-request ef request)
body (:body response)]
(is (= (attr body :name- :value) "x"))
(is (nil? errors))
(is (= title "My Title"))))
(is (nil? errors))))
(testing "get defaults with function"
(let [ef (embedded-form form
fields
:defaults (fn [req] {:name "x"}))
{:keys [title response errors]} (process-request ef request)
{:keys [response errors]} (process-request ef request)
body (:body response)]
(is (= (attr body :name- :value) "x"))
(is (nil? errors))
(is (= title "My Title"))))
(is (nil? errors))))
(testing "loads data"
(let [ef (embedded-form form
fields
:load (fn [params] {:name "y"})
:defaults {:name "x"})
{:keys [title response errors]} (process-request ef request)
{:keys [response errors]} (process-request ef request)
body (:body response)]
(is (= (attr body :name- :value) "y"))
(is (nil? errors))
(is (= title "My Title"))))
(is (nil? errors))))
(testing "get errors and input"
(let [request {:flash {:user-form
{:errors {:name ["name err"]}
Expand All @@ -377,11 +371,10 @@
fields
:load (fn [params] {:name "y"})
:defaults {:name "x"})
{:keys [title response errors]} (process-request ef request)
{:keys [response errors]} (process-request ef request)
body (:body response)]
(is (= (attr body :name- :value) "z"))
(is (= errors {:name ["name err"]}))
(is (= title "My Title"))
(is (true? (error-visible? body :name-)))
(is (= (error-message body :name-) "name err")))))
#_(testing "fields as a function"
Expand All @@ -391,21 +384,19 @@
[(textfield :name :id :name- :label "My Name")
(textfield :age :id :age- :label "My Age")]))]
(let [ef (embedded-form form fields)
{:keys [title body]} (process-request ef {:uri "/a"})]
(is (= title "My Title"))
{:keys [body]} (process-request ef {:uri "/a"})]
(is (true? (exists? body :name-)))
(is (false? (exists? body :age-))))
(let [ef (embedded-form form fields)
{:keys [title body]} (process-request ef {:uri "/b"})]
(is (= title "My Title"))
{:keys [body]} (process-request ef {:uri "/b"})]
(is (true? (exists? body :name-)))
(is (true? (exists? body :age-))))))
#_(testing "cancel control"
(let [fields [(textfield :name :id :name- :label "My Name")
(button :submit)
(button :cancel)]]
(let [ef (embedded-form form fields)
{:keys [title body]} (process-request ef {:uri "/a"})]
{:keys [body]} (process-request ef {:uri "/a"})]
(is (= (fields-and-vals body :user-form)
{:name ""
:cancel "Cancel"
Expand Down

0 comments on commit dc3f126

Please sign in to comment.