Permalink
Browse files

Moved call back functions into callback, and reverted changes to the …

…validation function. Moved the validation callback calls to the validate-by-model method. Removed the dependency of core on callbacks.
  • Loading branch information...
1 parent 0f4beab commit 7b6326b56d0efb228f60b718ca0b02a623e62a87 @macourtney macourtney committed May 28, 2010
Showing with 83 additions and 81 deletions.
  1. +56 −1 src/clj_record/callbacks.clj
  2. +2 −66 src/clj_record/core.clj
  3. +25 −14 src/clj_record/validation.clj
@@ -14,4 +14,59 @@
(defn run-callbacks [record model-name & hooks]
(let [callback-map (or (model-metadata-for model-name :callbacks) {})]
- (reduce #(%2 %1) record (filter identity (seq-utils/flatten (map callback-map hooks))))))
+ (reduce #(%2 %1) record (filter identity (seq-utils/flatten (map callback-map hooks))))))
+
+(defn after-destroy
+ "Runs the after destroy call back on the given attributes."
+ [model-name attributes]
+ (run-callbacks attributes model-name :after-destroy))
+
+(defn after-insert
+ "Runs the after insert call back on the given attributes."
+ [model-name attributes]
+ (run-callbacks attributes model-name :after-insert))
+
+(defn after-load
+ "Runs the after load call back on all of the given rows."
+ [model-name rows]
+ (map #(run-callbacks (merge {} %) model-name :after-load) rows))
+
+(defn after-save
+ "Runs the after save call back on all of the given rows."
+ [model-name attributes]
+ (run-callbacks attributes model-name :after-save))
+
+(defn after-update
+ "Runs the after update call back on all of the given rows."
+ [model-name attributes]
+ (run-callbacks attributes model-name :after-update))
+
+(defn after-validation
+ "Runs the after validation call back on all of the given rows."
+ [model-name attributes]
+ (run-callbacks attributes model-name :after-validation))
+
+(defn before-destroy
+ "Runs the before destroy call back on the given attributes."
+ [model-name attributes]
+ (run-callbacks attributes model-name :before-destroy))
+
+(defn before-insert
+ "Runs the before insert call back on the given attributes."
+ [model-name attributes]
+ (run-callbacks attributes model-name :before-insert))
+
+(defn before-save
+ "Runs the before save call back on the given attributes."
+ [model-name attributes]
+ (run-callbacks attributes model-name :before-save))
+
+(defn before-update
+ "Runs the before update call back on the given attributes."
+ [model-name attributes]
+ (run-callbacks attributes model-name :before-update))
+
+(defn before-validation
+ "Runs the before validation call back on all of the given rows."
+ [model-name attributes]
+ (run-callbacks attributes model-name :before-validation))
View
@@ -1,7 +1,6 @@
(ns clj-record.core
(:require [clojure.contrib.sql :as sql]
- [clojure.contrib.str-utils :as str-utils]
- [clj-record.validation :as validation])
+ [clojure.contrib.str-utils :as str-utils])
(:use (clj-record meta util callbacks)))
@@ -62,61 +61,6 @@ instance."
[& body]
`(transaction (db-spec-for ~'model-name) ~@body))
-(defn after-destroy
- "Runs the after destroy call back on the given attributes."
- [model-name attributes]
- (run-callbacks attributes model-name :after-destroy))
-
-(defn after-insert
- "Runs the after insert call back on the given attributes."
- [model-name attributes]
- (run-callbacks attributes model-name :after-insert))
-
-(defn after-load
- "Runs the after load call back on all of the given rows."
- [model-name rows]
- (map #(run-callbacks (merge {} %) model-name :after-load) rows))
-
-(defn after-save
- "Runs the after save call back on all of the given rows."
- [model-name attributes]
- (run-callbacks attributes model-name :after-save))
-
-(defn after-update
- "Runs the after update call back on all of the given rows."
- [model-name attributes]
- (run-callbacks attributes model-name :after-update))
-
-(defn after-validation
- "Runs the after validation call back on all of the given rows."
- [model-name attributes]
- (run-callbacks attributes model-name :after-validation))
-
-(defn before-destroy
- "Runs the before destroy call back on the given attributes."
- [model-name attributes]
- (run-callbacks attributes model-name :before-destroy))
-
-(defn before-insert
- "Runs the before insert call back on the given attributes."
- [model-name attributes]
- (run-callbacks attributes model-name :before-insert))
-
-(defn before-save
- "Runs the before save call back on the given attributes."
- [model-name attributes]
- (run-callbacks attributes model-name :before-save))
-
-(defn before-update
- "Runs the before update call back on the given attributes."
- [model-name attributes]
- (run-callbacks attributes model-name :before-update))
-
-(defn before-validation
- "Runs the before validation call back on all of the given rows."
- [model-name attributes]
- (run-callbacks attributes model-name :before-validation))
-
(defn find-by-sql
"Returns a vector of matching records.
select-query-and-values should be something like
@@ -215,14 +159,6 @@ instance."
(doseq [record rows-to-delete]
(after-destroy model-name record))))))
-(defn validate-record
- "Validates the given attributes."
- [model-name record]
- (before-validation model-name record)
- (let [errors (validation/validate model-name record)]
- (after-validation model-name record)
- errors))
-
(defn- defs-from-option-groups [model-name option-groups]
(reduce
(fn [def-forms [option-group-name & options]]
@@ -285,7 +221,7 @@ instance."
(defn ~'destroy-record [record#]
(destroy-record ~model-name record#))
(defn ~'validate [record#]
- (validate-record ~model-name record#))
+ (~'clj-record.validation/validate-by-model ~model-name record#))
(defn ~'after-destroy [attributes#]
(after-destroy ~model-name attributes#))
(defn ~'after-insert [attributes#]
@@ -1,7 +1,6 @@
(ns clj-record.validation
- (:use clj-record.util)
- (:use clj-record.meta)
- (:use clj-record.core))
+ (:use (clj-record core meta util))
+ (:require [clj-record.callbacks :as callbacks]))
(defn- validations-for [model-name] ((@all-models-metadata model-name) :validations))
@@ -10,17 +9,29 @@
(def messages-for get)
-(defn validate [model-name record]
- (reduce
- (fn [errors [attr message validation-fn]]
- (if (validation-fn (record attr))
- errors
- (merge-with
- (fn [result addl-val] (apply conj result addl-val))
- errors
- {attr [message]})))
- {}
- (validations-for model-name)))
+(defn- collect-errors [record errors [attr message validation-fn]]
+ (if (validation-fn (record attr))
+ errors
+ (merge-with
+ #(apply conj %1 %2)
+ errors
+ {attr [message]})))
+
+(defn validate-by-model [model-name record]
+ (callbacks/before-validation model-name record)
+ (let [errors (reduce
+ #(collect-errors record %1 %2)
+ ;(fn [errors [attr message validation-fn]]
+ ; (if (validation-fn (record attr))
+ ; errors
+ ; (merge-with
+ ; (fn [result addl-val] (apply conj result addl-val))
+ ; errors
+ ; {attr [message]})))
+ {}
+ (validations-for model-name))]
+ (callbacks/after-validation model-name record)
+ errors))
(defn add-validation
"Adds a validation to the named model.

0 comments on commit 7b6326b

Please sign in to comment.