Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add delete-records with the cheap and fast delete behavior destroy-re…

…cords previously had.
  • Loading branch information...
commit c56bb7f4597fe4c043b62f9b1afd8e16098d5e48 1 parent 2b8e319
@duelinmarkers authored
View
11 src/clj_record/core.clj
@@ -146,7 +146,8 @@ instance."
(after-destroy model-name record)))
(defn destroy-records
- "Deletes all records matching (-> attributes to-conditions)."
+ "Deletes all records matching (-> attributes to-conditions),
+ running before- and after-destroy callbacks on each matching record."
[model-name attributes]
(let [conditions (to-conditions attributes)
model-table-name (table-name model-name)
@@ -160,6 +161,12 @@ instance."
(doall
(map #(after-destroy model-name %) rows-to-delete))))))
+(defn delete-records
+ "Deletes all records matching (-> attributes to-conditions) without running callbacks."
+ [model-name attributes]
+ (connected (db-spec-for model-name)
+ (sql/delete-rows (table-name model-name) (to-conditions attributes))))
+
(defn- defs-from-option-groups [model-name option-groups]
(reduce
(fn [def-forms [option-group-name & options]]
@@ -223,6 +230,8 @@ instance."
(destroy-record ~model-name record#))
(defn ~'destroy-records [attributes#]
(destroy-records ~model-name attributes#))
+ (defn ~'delete-records [attributes#]
+ (delete-records ~model-name attributes#))
(defn ~'validate [record#]
(~'clj-record.validation/validate ~model-name record#))
(defn ~'after-destroy [attributes#]
View
8 test/clj_record/callbacks_test.clj
@@ -116,3 +116,11 @@
(defdbtest validate-calls-before-validation?
(assure-called "manufacturer" :before-validation
(manufacturer/validate valid-manufacturer)))
+
+(defdbtest delete-records-does-not-call-before-destroy
+ (restoring-ref (manufacturer/model-metadata)
+ (restoring-ref test-ref
+ (callbacks/add-callback "manufacturer" :before-destroy (fn [r] (callback-called :before-destroy) r))
+ (manufacturer/create valid-manufacturer)
+ (manufacturer/delete-records valid-manufacturer)
+ (is (not (callback-called? :before-destroy))))))
View
9 test/clj_record/core_test.clj
@@ -62,13 +62,20 @@
(manufacturer/destroy-record {:id (humedai :id)})
(is (empty? (manufacturer/find-records {:id (humedai :id)})))))
-(defdbtest destroy-records-destroys-by-prototype
+(defdbtest destroy-records-deletes-by-attribute-conditions
(let [name-map { :name "Humedai Motors" }
humedai1 (manufacturer/create (valid-manufacturer-with name-map))
humedai2 (manufacturer/create (valid-manufacturer-with name-map))]
(manufacturer/destroy-records name-map)
(is (empty? (manufacturer/find-records name-map)))))
+(defdbtest delete-records-deletes-by-attribute-conditions
+ (let [name-map { :name "Humedai Motors" }
+ humedai1 (manufacturer/create (valid-manufacturer-with name-map))
+ humedai2 (manufacturer/create (valid-manufacturer-with name-map))]
+ (manufacturer/delete-records name-map)
+ (is (empty? (manufacturer/find-records name-map)))))
+
(defdbtest update-uses-id-to-update-other-given-attributes-leaving-unspecified-attributes-untouched
(let [humedai (manufacturer/create (valid-manufacturer-with {:name "Humedai Motors" :grade 90}))
id (humedai :id)]
Please sign in to comment.
Something went wrong with that request. Please try again.