Skip to content
This repository
Browse code

Add make-rest-form. Update some of the forms tests.

  • Loading branch information...
commit 7d3536d1075c57ab652cf39c54e07d2fa113bdea 1 parent b3ea35d
Brenton Ashworth authored
17 script/coverage
... ... @@ -1,8 +1,9 @@
1   -lein radagast sandbar.auth \
2   - sandbar.core \
3   - sandbar.form-authentication \
4   - sandbar.forms \
5   - sandbar.stateful-session \
6   - sandbar.tables \
7   - sandbar.util \
8   - sandbar.validation
  1 +lein radagast sandbar.test.auth \
  2 + sandbar.test.core \
  3 + sandbar.test.form-authentication \
  4 + sandbar.test.forms \
  5 + sandbar.test.stateful-session \
  6 + sandbar.test.tables \
  7 + sandbar.test.util \
  8 + sandbar.test.validation
  9 +
44 src/sandbar/forms.clj
@@ -95,9 +95,11 @@
95 95 label)
96 96 req (cond (keyword? req) req
97 97 (contains? (set req) key) :required
98   - :else :optional)]
99   - [:div {:class "field-label"} label
100   - (if (= req :required) [:span {:class "required"} "*"] "")]))
  98 + :else :optional)
  99 + div [:div {:class "field-label"} label]]
  100 + (if (= req :required)
  101 + (vec (conj div [:span {:class "required"} "*"]))
  102 + div)))
101 103
102 104 (defn hidden
103 105 "Create a hidden form field."
@@ -106,7 +108,9 @@
106 108 {:type :hidden
107 109 :label ""
108 110 :field-name field-name
109   - :html [:input {:type "hidden" :name (name field-name) :value value}]}))
  111 + :html [:input {:type "hidden"
  112 + :name (name field-name)
  113 + :value (or value "")}]}))
110 114
111 115 #_(defn htmlfield
112 116 ([fname content]
@@ -279,7 +283,8 @@
279 283 [field-name & {:keys [title source value visible required]
280 284 :as options}]
281 285 (let [options (merge {:multiple true :size 5} options)]
282   - (-> (apply select field-name (flatten (seq options)))
  286 + (-> (apply select field-name (interleave (keys options)
  287 + (vals options)))
283 288 (assoc :type :multi-select))))
284 289
285 290 (defn wrap-checkboxes-in-group [coll]
@@ -1031,3 +1036,32 @@
1031 1036 followed by a doc-string and metadata map. See make-form for details."
1032 1037 (let [[name options] (name-with-attributes name options)]
1033 1038 `(def ~name (make-form ~(keyword name) ~@options))))
  1039 +
  1040 +(defn make-rest-form
  1041 + [name & {:keys [resource page-layout] :as options}]
  1042 + (let [page-layout (or page-layout identity)
  1043 + create-action resource
  1044 + update-action (str resource "/:id")
  1045 + update-method :put
  1046 + new-action (str resource "/new")
  1047 + edit-action (str resource "/:id/edit")
  1048 + options (-> options
  1049 + (dissoc :resource :page-layout)
  1050 + (merge {:create-action create-action
  1051 + :update-action update-action
  1052 + :update-method update-method}))
  1053 + form-handler (apply make-form name (interleave (keys options)
  1054 + (vals options)))]
  1055 + (routes
  1056 + (GET new-action request (page-layout (form-handler request)))
  1057 + (POST resource request (form-handler request))
  1058 + (GET edit-action request (page-layout (form-handler request)))
  1059 + (PUT update-action request (form-handler request)))))
  1060 +
  1061 +(defmacro defrestform [name & options]
  1062 + "Define a form handler function wrapped in a RESTful routing function. The
  1063 + name may optionally be followed by a doc-string and metadata map. See
  1064 + make-rest-form for details."
  1065 + (let [[name options] (name-with-attributes name options)]
  1066 + `(def ~name (make-rest-form ~(keyword name) ~@options))))
  1067 +
54 test/sandbar/test/forms.clj
@@ -7,7 +7,7 @@
7 7 ;; You must not remove this notice, or any other, from this software.
8 8
9 9 (ns sandbar.test.forms
10   - (:use [clojure.test :only [deftest testing]]
  10 + (:use [clojure.test :only [deftest testing is]]
11 11 [sandbar.forms]
12 12 [sandbar.stateful-session :only []]))
13 13
@@ -15,7 +15,7 @@
15 15 ;; Fixtures
16 16 ;;
17 17
18   -(defn create-test-multi-checkbox [fname coll]
  18 +#_(defn create-test-multi-checkbox [fname coll]
19 19 {:type :multi-checkbox
20 20 :field-name fname
21 21 :label ""
@@ -31,33 +31,33 @@
31 31 (partition 2 coll)))
32 32 :value-fn :id})
33 33
34   -(defn button-row [buttons]
  34 +#_(defn button-row [buttons]
35 35 [:tr
36 36 [:td {:colspan 1}
37 37 [:div.buttons
38 38 (display-buttons "basic" buttons)]]])
39 39
40   -(defn test-form-opt-label [field-name]
  40 +#_(defn test-form-opt-label [field-name]
41 41 [:div {:class "field-label"} field-name ""])
42 42
43   -(defn test-form-req-label [field-name]
  43 +#_(defn test-form-req-label [field-name]
44 44 [:div {:class "field-label"} field-name
45 45 [:span {:class "required"} "*"]])
46 46
47   -(defn form-textfield-fixture [field-name value]
  47 +#_(defn form-textfield-fixture [field-name value]
48 48 [:input {:size 35 :type "Text" :name field-name :value value
49 49 :class "textfield"}])
50 50
51   -(defn form-hidden-fixture [field-name value]
  51 +#_(defn form-hidden-fixture [field-name value]
52 52 [:input {:type "hidden" :name field-name :value value}])
53 53
54   -(defn form-password-fixture [field-name value]
  54 +#_(defn form-password-fixture [field-name value]
55 55 [:input {:size 35 :type "Password" :name field-name :value value
56 56 :class "textfield"}])
57 57
58   -(defmulti field-fixture (fn [& args] (first args)))
  58 +#_(defmulti field-fixture (fn [& args] (first args)))
59 59
60   -(defmethod field-fixture :textfield [_ label name & {:keys [value flags]}]
  60 +#_(defmethod field-fixture :textfield [_ label name & {:keys [value flags]}]
61 61 (let [field-value (or value "")]
62 62 [:div.sandbar-field
63 63 (if (contains? flags :required)
@@ -66,27 +66,27 @@
66 66 [:div.error-message {:style "display:none;"}]
67 67 (form-textfield-fixture name field-value)]))
68 68
69   -(defn form-checkbox-fixture [field-name value]
  69 +#_(defn form-checkbox-fixture [field-name value]
70 70 [:input {:type "checkbox" :name field-name :value "checkbox-true"}])
71 71
72   -(defn form-checkbox-label-fixture [title]
  72 +#_(defn form-checkbox-label-fixture [title]
73 73 [:span {:class "field-label"} title])
74 74
75   -(defn test-form-multi-checkbox [field-name value]
  75 +#_(defn test-form-multi-checkbox [field-name value]
76 76 [:div [:input {:type "Text", :name field-name, :value value}]])
77 77
78   -(defn test-form-textarea [field-name value]
  78 +#_(defn test-form-textarea [field-name value]
79 79 (if (empty? value)
80 80 [:textarea {:name field-name}]
81 81 [:textarea {:name field-name} value]))
82 82
83   -(def td-std-opts {:valign "top"})
  83 +#_(def td-std-opts {:valign "top"})
84 84
85 85 ;;
86 86 ;; Utilities
87 87 ;;
88 88
89   -#_(deftest get-params-test
  89 +(deftest get-params-test
90 90 (is (= (get-params [:a :b] {"a" "a" "b" "b"})
91 91 {:a "a" :b "b"}))
92 92 (is (= (get-params [:a :b] {:a "a" :b "b"})
@@ -99,7 +99,7 @@
99 99 (is (= (get-params [:a :b] {:a ["1" "2" "3.5"] :b ["a" "2" "4.5t"]})
100 100 {:a [1 2 3.5] :b ["a" 2 "4.5t"]})))
101 101
102   -#_(deftest clean-form-input-test
  102 +(deftest clean-form-input-test
103 103 (is (= (clean-form-input {:id "" :name "" :age 10})
104 104 {:name nil :age 10}))
105 105 (is (= (meta (clean-form-input
@@ -110,6 +110,24 @@
110 110 ;; Form Elements
111 111 ;;
112 112
  113 +(deftest field-label-test
  114 + (let [req [:span {:class "required"} "*"]
  115 + expected #(concat [:div {:class "field-label"}] %)]
  116 + (is (= (field-label "a" :b :optional)
  117 + (expected ["a"])))
  118 + (is (= (field-label {:b "c"} :b :optional)
  119 + (expected ["c"])))
  120 + (is (= (field-label {:a "c"} :b :optional)
  121 + (expected ["b"])))
  122 + (is (= (field-label "a" :b :required)
  123 + (expected ["a" req])))))
  124 +
  125 +(deftest hidden-test
  126 + (is (= (:html (hidden :a))
  127 + [:input {:type "hidden" :name "a" :value ""}]))
  128 + (is (= (:html (hidden :a "x"))
  129 + [:input {:type "hidden" :name "a" :value "x"}])))
  130 +
113 131 #_(deftest textfield-test
114 132 (are [args _ e-label exp-name exp-value]
115 133 (= (apply textfield args)
@@ -117,7 +135,7 @@
117 135 :label e-label
118 136 :field-name :name
119 137 :html (form-textfield-fixture exp-name exp-value)})
120   -
  138 +
121 139 ["f1" :name] :=> (test-form-opt-label "f1") "name" ""
122 140
123 141 ["f1" :name :required] :=> (test-form-req-label "f1") "name" ""

0 comments on commit 7d3536d

Please sign in to comment.
Something went wrong with that request. Please try again.