From cff6e9e965057c7e87fad7cedc5f7445f534c493 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Thu, 11 Oct 2018 14:58:17 +0200 Subject: [PATCH] forms refactoring, unique attr-names --- project.clj | 2 +- src/wanijo/attribute/routes.clj | 19 +++++----- src/wanijo/schema/forms.clj | 62 +++++++++++++++++++++++++++++++++ src/wanijo/schema/routes.clj | 23 ++++++------ src/wanijo/schema/view.clj | 53 ++++------------------------ 5 files changed, 92 insertions(+), 67 deletions(-) create mode 100644 src/wanijo/schema/forms.clj diff --git a/project.clj b/project.clj index 61a0c60..b73eeec 100644 --- a/project.clj +++ b/project.clj @@ -8,7 +8,7 @@ [compojure "1.6.1"] [ring/ring-defaults "0.3.2"] [hiccup "1.0.5"] - [joshavg/formulare "0.2.1-SNAPSHOT"] + [joshavg/formulare "0.3.3-SNAPSHOT"] [gorillalabs/neo4j-clj "1.1.0" :exclusions [org.bouncycastle/bcprov-jdk15on diff --git a/src/wanijo/attribute/routes.clj b/src/wanijo/attribute/routes.clj index 43b0b58..9e6b407 100644 --- a/src/wanijo/attribute/routes.clj +++ b/src/wanijo/attribute/routes.clj @@ -4,27 +4,28 @@ [formulare.core :as form] [wanijo.framework.routing :refer [register! path]] [wanijo.attribute.domain :as domain] - [wanijo.schema.view :as view-schema] - [wanijo.schema.domain :as domain-schema] - [wanijo.schema.routes :as routes-schema])) + [wanijo.schema.view :as schema-view] + [wanijo.schema.domain :as schema-domain] + [wanijo.schema.routes :as schema-routes] + [wanijo.schema.forms :as schema-forms])) (defn new! [req] (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 - (domain/create-new! (form/form-data view-schema/attr-form + (domain/create-new! (form/form-data schema-forms/attr-form req) schema-uuid (get-in req [:session :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] - (if (form/valid? view-schema/attr-form req) + (if (form/valid? schema-forms/attr-form req) (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}))) - (routes-schema/view! schema-uuid req))) + (schema-routes/view! schema-uuid req))) (defn delete! [uuid req] (domain/delete-by-uuid! uuid) diff --git a/src/wanijo/schema/forms.clj b/src/wanijo/schema/forms.clj new file mode 100644 index 0000000..891f6b7 --- /dev/null +++ b/src/wanijo/schema/forms.clj @@ -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}}}) diff --git a/src/wanijo/schema/routes.clj b/src/wanijo/schema/routes.clj index 93e386d..f5d9e56 100644 --- a/src/wanijo/schema/routes.clj +++ b/src/wanijo/schema/routes.clj @@ -6,31 +6,32 @@ [wanijo.framework.routing :refer [register! path]] [wanijo.schema.domain :as domain] [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])) (defn new! [req] - (if (form/valid? view-schema/form req) + (if (form/valid? schema-forms/schema req) (do (domain/create-new! (get-in req [:params :name]) (get-in req [:session :uuid])) (resp/redirect (path :schema-overview))) - (view-schema/overview! req))) + (schema-view/overview! req))) (defn delete-schema! [uuid session] (domain/delete! uuid) (resp/redirect (path :schema-overview))) (defn view! [uuid req] - (view-schema/show-schema! + (schema-view/show-schema! (domain/find-with-assigned-entities! uuid) (domain-attr/find-by-schema! uuid) - (assoc-in view-schema/assign-form + (assoc-in schema-forms/assign-form [:fields :assigned :options] (map #(vector (:ident %) (:uuid %)) (domain-user/all!))) - (assoc-in view-schema/schema-connections-form + (assoc-in schema-forms/schema-connections-form [:fields :connections :options] (map #(vector (:name %) (:uuid %)) (domain/all!))) @@ -38,23 +39,23 @@ (defn edit! [req] (let [uuid (get-in req [:params :uuid])] - (if (form/valid? view-schema/form req) + (if (form/valid? schema-forms/schema req) (do (domain/edit! (:params req)) (resp/redirect (path :schema-show (:params req)))) (view! uuid 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])] - (if (form/valid? view-schema/assign-form req) + (if (form/valid? schema-forms/assign-form req) (do (domain/assign-users! uuid assigned permission) (resp/redirect (path :schema-show (:params req)))) (view! uuid 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)] (if (form/valid? form req) (do @@ -85,7 +86,7 @@ (defroutes routes (GET (register! :schema-overview "/schema") [] - view-schema/overview!) + schema-view/overview!) (GET (register! :schema-show "/schema/:uuid") [uuid :as req] (view! uuid req)) diff --git a/src/wanijo/schema/view.clj b/src/wanijo/schema/view.clj index ad4a2c3..733aedc 100644 --- a/src/wanijo/schema/view.clj +++ b/src/wanijo/schema/view.clj @@ -1,52 +1,12 @@ (ns wanijo.schema.view - (:require [clojure.string :refer [capitalize]] - [hiccup.form :as hform] + (:require [hiccup.form :as hform] [ring.util.anti-forgery :refer [anti-forgery-field]] [formulare.core :as form] [wanijo.framework.view :as view] [wanijo.framework.routing :refer [path]] [wanijo.framework.time :refer [prettify-dt]] [wanijo.schema.domain :as domain] - [wanijo.attribute.domain :as attr-domain])) - -(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}}}) + [wanijo.schema.forms :as forms])) (defn overview! [req] (let [session (:session req) @@ -71,7 +31,7 @@ (prettify-dt (:created_at schema))]])]] [:h1 "New schema"] (hform/form-to [:post (path :schema-new)] - (form/render-widgets form {} req) + (form/render-widgets forms/schema {} req) (hform/submit-button "Create"))]))) (defn show-schema! [schema attrs assign-form conn-form req] @@ -82,7 +42,7 @@ [:span.schema-title__name (:name schema)]] [:h2 "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")) [:h2 "Permissions"] [:h3 "Read permissions"] @@ -114,7 +74,8 @@ (for [attr attrs] [:li (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/form-to [:delete (path :attribute-delete attr)] (anti-forgery-field) @@ -124,7 +85,7 @@ [:em (prettify-dt (:created_at attr))]])])] [:h3 "New attribute"] (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/submit-button "Create")) [:h2 "Actions"]