Skip to content
Snippets Groups Projects
Commit aa631ef3 authored by Allen Gilliland's avatar Allen Gilliland
Browse files

quick tweak to revision code to avoid protocol implementations on classes as...

quick tweak to revision code to avoid protocol implementations on classes as basic as java.lang.Object so that functions like `satisfies?` can be useful.  this simply removes the base implementation of IRevisioned from java.lang.Object and instead defines a couple default implementations of the protocol functions which entities can reuse.  also, we remove the base implementation of `serialize-instance` because that just feels dangerous and we want to encourage each entity to provide its own version of that function.
parent 05d49090
Branches
Tags
No related merge requests found
(ns metabase.models.card
(:require [korma.core :refer :all, :exclude [defentity update]]
[medley.core :as m]
[metabase.db :refer :all]
(metabase.models [interface :refer :all]
[revision :as revision]
[user :refer [User]])))
(def ^:const display-types
......@@ -60,3 +62,18 @@
(cascade-delete 'CardFavorite :card_id id)))
(extend-ICanReadWrite CardEntity :read :public-perms, :write :public-perms)
;;; ## ---------------------------------------- REVISIONS ----------------------------------------
(defn- serialize-instance [_ _ instance]
(->> (dissoc instance :created_at :updated_at)
(into {}) ; if it's a record type like CardInstance we need to convert it to a regular map or filter-vals won't work
(m/filter-vals (complement delay?))))
(extend CardEntity
revision/IRevisioned
{:serialize-instance serialize-instance
:revert-to-revision revision/default-revert-to-revision
:describe-diff revision/default-describe-diff})
......@@ -36,6 +36,10 @@
(extend-ICanReadWrite DashboardEntity :read :public-perms, :write :public-perms)
;;; ## ---------------------------------------- REVISIONS ----------------------------------------
(defn- serialize-instance [_ id {:keys [ordered_cards], :as dashboard}]
(-> dashboard
(select-keys [:description :name :public_perms])
......
......@@ -24,18 +24,20 @@
(describe-diff [this object1 object2]
"Return a string describing the difference between OBJECT1 and OBJECT2."))
;;; ## Default Impl
(extend-protocol IRevisioned
Object
(serialize-instance [_ _ instance]
(->> (dissoc instance :created_at :updated_at)
(into {}) ; if it's a record type like CardInstance we need to convert it to a regular map or filter-vals won't work
(m/filter-vals (complement delay?))))
(revert-to-revision [entity id serialized-instance]
(m/mapply upd entity id serialized-instance))
(describe-diff [entity o1 o2]
(diff-str (:name entity) o1 o2)))
;;; # Reusable Base Implementations for IRevisioned functions
;; NOTE that we do not provide a base implementation for `serialize-instance`, that should be done per entity.
(defn default-revert-to-revision
"Default implementation of `revert-to-revision` which simply does an update using the values from `serialized-instance`."
[entity id serialized-instance]
(m/mapply upd entity id serialized-instance))
(defn default-describe-diff
"Default implementation of `describe-diff` which calls `diff-str` on the 2 objects."
[entity o1 o2]
(diff-str (:name entity) o1 o2))
;;; # Revision Entity
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment