forms refactoring, unique attr-names

integration-tests
Josha von Gizycki 6 years ago
parent 9ca73e14ec
commit cff6e9e965

@ -8,7 +8,7 @@
[compojure "1.6.1"] [compojure "1.6.1"]
[ring/ring-defaults "0.3.2"] [ring/ring-defaults "0.3.2"]
[hiccup "1.0.5"] [hiccup "1.0.5"]
[joshavg/formulare "0.2.1-SNAPSHOT"] [joshavg/formulare "0.3.3-SNAPSHOT"]
[gorillalabs/neo4j-clj "1.1.0" [gorillalabs/neo4j-clj "1.1.0"
:exclusions [org.bouncycastle/bcprov-jdk15on :exclusions [org.bouncycastle/bcprov-jdk15on

@ -4,27 +4,28 @@
[formulare.core :as form] [formulare.core :as form]
[wanijo.framework.routing :refer [register! path]] [wanijo.framework.routing :refer [register! path]]
[wanijo.attribute.domain :as domain] [wanijo.attribute.domain :as domain]
[wanijo.schema.view :as view-schema] [wanijo.schema.view :as schema-view]
[wanijo.schema.domain :as domain-schema] [wanijo.schema.domain :as schema-domain]
[wanijo.schema.routes :as routes-schema])) [wanijo.schema.routes :as schema-routes]
[wanijo.schema.forms :as schema-forms]))
(defn new! [req] (defn new! [req]
(let [schema-uuid (get-in req [:params :schema])] (let [schema-uuid (get-in req [:params :schema])]
(if (form/valid? view-schema/attr-form req) (if (form/valid? schema-forms/attr-form req)
(do (do
(domain/create-new! (form/form-data view-schema/attr-form (domain/create-new! (form/form-data schema-forms/attr-form
req) req)
schema-uuid schema-uuid
(get-in req [:session :uuid])) (get-in req [:session :uuid]))
(resp/redirect (path :schema-show {:uuid schema-uuid}))) (resp/redirect (path :schema-show {:uuid schema-uuid})))
(routes-schema/view! schema-uuid req)))) (schema-routes/view! schema-uuid req))))
(defn edit! [schema-uuid req] (defn edit! [schema-uuid req]
(if (form/valid? view-schema/attr-form req) (if (form/valid? schema-forms/attr-form req)
(do (do
(domain/edit! (form/form-data view-schema/attr-form req)) (domain/edit! (form/form-data schema-forms/attr-form req))
(resp/redirect (path :schema-show {:uuid schema-uuid}))) (resp/redirect (path :schema-show {:uuid schema-uuid})))
(routes-schema/view! schema-uuid req))) (schema-routes/view! schema-uuid req)))
(defn delete! [uuid req] (defn delete! [uuid req]
(domain/delete-by-uuid! uuid) (domain/delete-by-uuid! uuid)

@ -0,0 +1,62 @@
(ns wanijo.schema.forms
(:require [wanijo.attribute.domain :as attr-domain]
[clojure.string :refer [capitalize]]
[clojure.spec.alpha :as spec]))
(defn unique-attr-name-in-schema! [req]
(let [schema-uuid (get-in req [:params :schema])
attr-uuid (get-in req [:params :uuid])
name (get-in req [:params :name])
duplicates (->> (attr-domain/find-by-schema! schema-uuid)
(map #(if (or (and (empty? attr-uuid)
(= name (:name %)))
(and (not (empty? attr-uuid))
(= name (:name %))
(not= attr-uuid (:uuid %))))
1 0))
(apply +))]
(not= 0 duplicates)))
(spec/def ::unique-attr-name-per-schema
unique-attr-name-in-schema!)
(def schema
{:fields {:name {:label "Name"
:required true
:spec :wanijo.schema.domain/name}
:uuid {:widget :hidden}}})
(def attr-form
{:fields {:name {:label "Name"
:required true
:spec ::attr-domain/name
}
:type {:label "Type"
:required true
:spec ::attr-domain/type
:widget :select
:options (map #(vector (capitalize %) %)
attr-domain/types)}
:required {:label "Required"
:required false
:spec ::attr-domain/required
:widget :checkbox
:from-req #(if (some? %) 1 0)}
:uuid {:widget :hidden}}
:form-specs [::unique-attr-name-per-schema]})
(def assign-form
{:fields {:assigned {:label "Users"
:required false
:spec :wanijo.schema.domain/assigned-to
:widget :mselect
:from-req #(if (vector? %) % [%])}
:uuid {:widget :hidden}}})
(def schema-connections-form
{:fields {:connections {:label "Schemas"
:required false
:spec :wanijo.schema.domain/assigned-to
:widget :mselect
:from-req #(if (vector? %) % [%])}
:uuid {:widget :hidden}}})

@ -6,31 +6,32 @@
[wanijo.framework.routing :refer [register! path]] [wanijo.framework.routing :refer [register! path]]
[wanijo.schema.domain :as domain] [wanijo.schema.domain :as domain]
[wanijo.user.domain :as domain-user] [wanijo.user.domain :as domain-user]
[wanijo.schema.view :as view-schema] [wanijo.schema.view :as schema-view]
[wanijo.schema.forms :as schema-forms]
[wanijo.attribute.domain :as domain-attr])) [wanijo.attribute.domain :as domain-attr]))
(defn new! [req] (defn new! [req]
(if (form/valid? view-schema/form req) (if (form/valid? schema-forms/schema req)
(do (do
(domain/create-new! (domain/create-new!
(get-in req [:params :name]) (get-in req [:params :name])
(get-in req [:session :uuid])) (get-in req [:session :uuid]))
(resp/redirect (path :schema-overview))) (resp/redirect (path :schema-overview)))
(view-schema/overview! req))) (schema-view/overview! req)))
(defn delete-schema! [uuid session] (defn delete-schema! [uuid session]
(domain/delete! uuid) (domain/delete! uuid)
(resp/redirect (path :schema-overview))) (resp/redirect (path :schema-overview)))
(defn view! [uuid req] (defn view! [uuid req]
(view-schema/show-schema! (schema-view/show-schema!
(domain/find-with-assigned-entities! uuid) (domain/find-with-assigned-entities! uuid)
(domain-attr/find-by-schema! uuid) (domain-attr/find-by-schema! uuid)
(assoc-in view-schema/assign-form (assoc-in schema-forms/assign-form
[:fields :assigned :options] [:fields :assigned :options]
(map #(vector (:ident %) (:uuid %)) (map #(vector (:ident %) (:uuid %))
(domain-user/all!))) (domain-user/all!)))
(assoc-in view-schema/schema-connections-form (assoc-in schema-forms/schema-connections-form
[:fields :connections :options] [:fields :connections :options]
(map #(vector (:name %) (:uuid %)) (map #(vector (:name %) (:uuid %))
(domain/all!))) (domain/all!)))
@ -38,23 +39,23 @@
(defn edit! [req] (defn edit! [req]
(let [uuid (get-in req [:params :uuid])] (let [uuid (get-in req [:params :uuid])]
(if (form/valid? view-schema/form req) (if (form/valid? schema-forms/schema req)
(do (do
(domain/edit! (:params req)) (domain/edit! (:params req))
(resp/redirect (path :schema-show (:params req)))) (resp/redirect (path :schema-show (:params req))))
(view! uuid req)))) (view! uuid req))))
(defn assign-users! [req] (defn assign-users! [req]
(let [{:keys [uuid assigned]} (form/form-data view-schema/assign-form req) (let [{:keys [uuid assigned]} (form/form-data schema-forms/assign-form req)
permission (get-in req [:params :permission])] permission (get-in req [:params :permission])]
(if (form/valid? view-schema/assign-form req) (if (form/valid? schema-forms/assign-form req)
(do (do
(domain/assign-users! uuid assigned permission) (domain/assign-users! uuid assigned permission)
(resp/redirect (path :schema-show (:params req)))) (resp/redirect (path :schema-show (:params req))))
(view! uuid req)))) (view! uuid req))))
(defn assign-schemas! [req] (defn assign-schemas! [req]
(let [form view-schema/schema-connections-form (let [form schema-forms/schema-connections-form
{:keys [uuid connections]} (form/form-data form req)] {:keys [uuid connections]} (form/form-data form req)]
(if (form/valid? form req) (if (form/valid? form req)
(do (do
@ -85,7 +86,7 @@
(defroutes routes (defroutes routes
(GET (register! :schema-overview "/schema") [] (GET (register! :schema-overview "/schema") []
view-schema/overview!) schema-view/overview!)
(GET (register! :schema-show "/schema/:uuid") (GET (register! :schema-show "/schema/:uuid")
[uuid :as req] [uuid :as req]
(view! uuid req)) (view! uuid req))

@ -1,52 +1,12 @@
(ns wanijo.schema.view (ns wanijo.schema.view
(:require [clojure.string :refer [capitalize]] (:require [hiccup.form :as hform]
[hiccup.form :as hform]
[ring.util.anti-forgery :refer [anti-forgery-field]] [ring.util.anti-forgery :refer [anti-forgery-field]]
[formulare.core :as form] [formulare.core :as form]
[wanijo.framework.view :as view] [wanijo.framework.view :as view]
[wanijo.framework.routing :refer [path]] [wanijo.framework.routing :refer [path]]
[wanijo.framework.time :refer [prettify-dt]] [wanijo.framework.time :refer [prettify-dt]]
[wanijo.schema.domain :as domain] [wanijo.schema.domain :as domain]
[wanijo.attribute.domain :as attr-domain])) [wanijo.schema.forms :as forms]))
(def form
{:fields {:name {:label "Name"
:required true
:spec ::domain/name}
:uuid {:widget :hidden}}})
(def attr-form
{:fields {:name {:label "Name"
:required true
:spec ::attr-domain/name}
:type {:label "Type"
:required true
:spec ::attr-domain/type
:widget :select
:options (map #(vector (capitalize %) %)
attr-domain/types)}
:required {:label "Required"
:required false
:spec ::attr-domain/required
:widget :checkbox
:from-req #(if (some? %) 1 0)}
:uuid {:widget :hidden}}})
(def assign-form
{:fields {:assigned {:label "Users"
:required false
:spec ::domain/assigned-to
:widget :mselect
:from-req #(if (vector? %) % [%])}
:uuid {:widget :hidden}}})
(def schema-connections-form
{:fields {:connections {:label "Schemas"
:required false
:spec ::domain/assigned-to
:widget :mselect
:from-req #(if (vector? %) % [%])}
:uuid {:widget :hidden}}})
(defn overview! [req] (defn overview! [req]
(let [session (:session req) (let [session (:session req)
@ -71,7 +31,7 @@
(prettify-dt (:created_at schema))]])]] (prettify-dt (:created_at schema))]])]]
[:h1 "New schema"] [:h1 "New schema"]
(hform/form-to [:post (path :schema-new)] (hform/form-to [:post (path :schema-new)]
(form/render-widgets form {} req) (form/render-widgets forms/schema {} req)
(hform/submit-button "Create"))]))) (hform/submit-button "Create"))])))
(defn show-schema! [schema attrs assign-form conn-form req] (defn show-schema! [schema attrs assign-form conn-form req]
@ -82,7 +42,7 @@
[:span.schema-title__name (:name schema)]] [:span.schema-title__name (:name schema)]]
[:h2 "Edit"] [:h2 "Edit"]
(hform/form-to [:post (path :schema-edit)] (hform/form-to [:post (path :schema-edit)]
(form/render-widgets form schema req) (form/render-widgets forms/schema schema req)
(hform/submit-button "Edit")) (hform/submit-button "Edit"))
[:h2 "Permissions"] [:h2 "Permissions"]
[:h3 "Read permissions"] [:h3 "Read permissions"]
@ -114,7 +74,8 @@
(for [attr attrs] (for [attr attrs]
[:li [:li
(hform/form-to [:post (path :attribute-edit {:schema (:uuid schema)})] (hform/form-to [:post (path :attribute-edit {:schema (:uuid schema)})]
(form/render-widgets attr-form attr req) (form/render-widgets forms/attr-form attr req)
(hform/hidden-field "schema" (:uuid schema))
(hform/submit-button "Save")) (hform/submit-button "Save"))
(hform/form-to [:delete (path :attribute-delete attr)] (hform/form-to [:delete (path :attribute-delete attr)]
(anti-forgery-field) (anti-forgery-field)
@ -124,7 +85,7 @@
[:em (prettify-dt (:created_at attr))]])])] [:em (prettify-dt (:created_at attr))]])])]
[:h3 "New attribute"] [:h3 "New attribute"]
(hform/form-to [:post (path :attribute-new)] (hform/form-to [:post (path :attribute-new)]
(form/render-widgets attr-form {} req) (form/render-widgets forms/attr-form {} req)
(hform/hidden-field "schema" (:uuid schema)) (hform/hidden-field "schema" (:uuid schema))
(hform/submit-button "Create")) (hform/submit-button "Create"))
[:h2 "Actions"] [:h2 "Actions"]

Loading…
Cancel
Save