{"action":"created","comment":{"url":"https://api.github.com/repos/metabase/metabase/pulls/comments/44193199","id":44193199,"diff_hunk":"@@ -0,0 +1,159 @@\n+(ns metabase.models.pulse\n+  (:require [clojure.tools.logging :as log]\n+            [korma.core :as k]\n+            [korma.db :as kdb]\n+            [metabase.db :as db]\n+            (metabase.models [card :refer [Card]]\n+                             [common :refer [perms-readwrite]]\n+                             [hydrate :refer :all]\n+                             [interface :refer :all]\n+                             [pulse-card :refer [PulseCard]]\n+                             [pulse-channel :refer [PulseChannel] :as pulse-channel]\n+                             [user :refer [User]])\n+            [metabase.util :as u]))\n+\n+\n+(defrecord PulseInstance []\n+  ;; preserve normal IFn behavior so things like ((sel :one Database) :id) work correctly\n+  clojure.lang.IFn\n+  (invoke [this k]\n+    (get this k)))\n+\n+(extend-ICanReadWrite PulseInstance :read :public-perms, :write :public-perms)\n+\n+\n+(defentity Pulse\n+  [(k/table :pulse)\n+   (hydration-keys pulse)\n+   timestamped]\n+\n+  (pre-insert [_ pulse]\n+    (let [defaults {:public_perms perms-readwrite}]\n+      (merge defaults pulse)))\n+\n+  (post-select [_ {:keys [id creator_id] :as pulse}]\n+    (map->PulseInstance\n+      (u/assoc* pulse\n+                :cards    (delay (k/select Card\n+                                   (k/join PulseCard (= :pulse_card.card_id :id))\n+                                   (k/fields :id :name :description :display)\n+                                   (k/where {:pulse_card.pulse_id id})\n+                                   (k/order :pulse_card.position :asc)))\n+                :channels (delay (db/sel :many PulseChannel (k/where {:pulse_id id})))\n+                :creator  (delay (when creator_id (db/sel :one User :id creator_id))))))\n+\n+  (pre-cascade-delete [_ {:keys [id]}]\n+    (db/cascade-delete PulseCard :pulse_id id)\n+    (db/cascade-delete PulseChannel :pulse_id id)))\n+\n+(extend-ICanReadWrite PulseEntity :read :public-perms, :write :public-perms)\n+\n+\n+;; ## Helper Functions\n+\n+(defn- update-pulse-cards\n+  \"Update the `PulseCards` for a given PULSE.\n+   CARD-IDS should be a definitive collection of *all* IDs of cards for the pulse in the desired order.\n+\n+   *  If an ID in CARD-IDS has no corresponding existing `PulseCard` object, one will be created.\n+   *  If an existing `PulseCard` has no corresponding ID in CARD-IDs, it will be deleted.\n+   *  All cards will be updated with a `position` according to their place in the collection of CARD-IDS\"\n+  {:arglists '([pulse card-ids])}\n+  [{:keys [id]} card-ids]\n+  {:pre [(integer? id)\n+         (coll? card-ids)\n+         (every? integer? card-ids)]}\n+  ;; first off, just delete any cards associated with this pulse (we add them again below)\n+  (k/delete PulseCard (k/where {:pulse_id id}))","path":"src/metabase/models/pulse.clj","position":67,"original_position":67,"commit_id":"3b8da0cc6041a7200f085d73981ef4cd1498e9f5","original_commit_id":"3b8da0cc6041a7200f085d73981ef4cd1498e9f5","user":{"login":"camsaul","id":1455846,"avatar_url":"https://avatars.githubusercontent.com/u/1455846?v=3","gravatar_id":"","url":"https://api.github.com/users/camsaul","html_url":"https://github.com/camsaul","followers_url":"https://api.github.com/users/camsaul/followers","following_url":"https://api.github.com/users/camsaul/following{/other_user}","gists_url":"https://api.github.com/users/camsaul/gists{/gist_id}","starred_url":"https://api.github.com/users/camsaul/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/camsaul/subscriptions","organizations_url":"https://api.github.com/users/camsaul/orgs","repos_url":"https://api.github.com/users/camsaul/repos","events_url":"https://api.github.com/users/camsaul/events{/privacy}","received_events_url":"https://api.github.com/users/camsaul/received_events","type":"User","site_admin":false},"body":"probably better to do\r\n```clojure\r\n(cascade-delete PulseCard, :pulse_id id)\r\n```\r\nhere. That triggers the multimethod `pre-cascade-delete` so we can delete dependent objects beforehand if needed.","created_at":"2015-11-06T22:08:15Z","updated_at":"2015-11-06T22:08:15Z","html_url":"https://github.com/metabase/metabase/pull/1441#discussion_r44193199","pull_request_url":"https://api.github.com/repos/metabase/metabase/pulls/1441","_links":{"self":{"href":"https://api.github.com/repos/metabase/metabase/pulls/comments/44193199"},"html":{"href":"https://github.com/metabase/metabase/pull/1441#discussion_r44193199"},"pull_request":{"href":"https://api.github.com/repos/metabase/metabase/pulls/1441"}}},"pull_request":{"url":"https://api.github.com/repos/metabase/metabase/pulls/1441","id":49633428,"html_url":"https://github.com/metabase/metabase/pull/1441","diff_url":"https://github.com/metabase/metabase/pull/1441.diff","patch_url":"https://github.com/metabase/metabase/pull/1441.patch","issue_url":"https://api.github.com/repos/metabase/metabase/issues/1441","number":1441,"state":"open","locked":false,"title":"Pulse UI [WIP]","user":{"login":"tlrobinson","id":18193,"avatar_url":"https://avatars.githubusercontent.com/u/18193?v=3","gravatar_id":"","url":"https://api.github.com/users/tlrobinson","html_url":"https://github.com/tlrobinson","followers_url":"https://api.github.com/users/tlrobinson/followers","following_url":"https://api.github.com/users/tlrobinson/following{/other_user}","gists_url":"https://api.github.com/users/tlrobinson/gists{/gist_id}","starred_url":"https://api.github.com/users/tlrobinson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tlrobinson/subscriptions","organizations_url":"https://api.github.com/users/tlrobinson/orgs","repos_url":"https://api.github.com/users/tlrobinson/repos","events_url":"https://api.github.com/users/tlrobinson/events{/privacy}","received_events_url":"https://api.github.com/users/tlrobinson/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-11-04T00:34:13Z","updated_at":"2015-11-06T22:08:15Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1335ca5a5bca17ec12b79cc81d4194275f9e2f41","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/metabase/metabase/pulls/1441/commits","review_comments_url":"https://api.github.com/repos/metabase/metabase/pulls/1441/comments","review_comment_url":"https://api.github.com/repos/metabase/metabase/pulls/comments{/number}","comments_url":"https://api.github.com/repos/metabase/metabase/issues/1441/comments","statuses_url":"https://api.github.com/repos/metabase/metabase/statuses/3b8da0cc6041a7200f085d73981ef4cd1498e9f5","head":{"label":"metabase:pulses","ref":"pulses","sha":"3b8da0cc6041a7200f085d73981ef4cd1498e9f5","user":{"login":"metabase","id":10520629,"avatar_url":"https://avatars.githubusercontent.com/u/10520629?v=3","gravatar_id":"","url":"https://api.github.com/users/metabase","html_url":"https://github.com/metabase","followers_url":"https://api.github.com/users/metabase/followers","following_url":"https://api.github.com/users/metabase/following{/other_user}","gists_url":"https://api.github.com/users/metabase/gists{/gist_id}","starred_url":"https://api.github.com/users/metabase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metabase/subscriptions","organizations_url":"https://api.github.com/users/metabase/orgs","repos_url":"https://api.github.com/users/metabase/repos","events_url":"https://api.github.com/users/metabase/events{/privacy}","received_events_url":"https://api.github.com/users/metabase/received_events","type":"Organization","site_admin":false},"repo":{"id":30203935,"name":"metabase","full_name":"metabase/metabase","owner":{"login":"metabase","id":10520629,"avatar_url":"https://avatars.githubusercontent.com/u/10520629?v=3","gravatar_id":"","url":"https://api.github.com/users/metabase","html_url":"https://github.com/metabase","followers_url":"https://api.github.com/users/metabase/followers","following_url":"https://api.github.com/users/metabase/following{/other_user}","gists_url":"https://api.github.com/users/metabase/gists{/gist_id}","starred_url":"https://api.github.com/users/metabase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metabase/subscriptions","organizations_url":"https://api.github.com/users/metabase/orgs","repos_url":"https://api.github.com/users/metabase/repos","events_url":"https://api.github.com/users/metabase/events{/privacy}","received_events_url":"https://api.github.com/users/metabase/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/metabase/metabase","description":"The simplest, fastest way to get business intelligence and analytics  to everyone in your company","fork":false,"url":"https://api.github.com/repos/metabase/metabase","forks_url":"https://api.github.com/repos/metabase/metabase/forks","keys_url":"https://api.github.com/repos/metabase/metabase/keys{/key_id}","collaborators_url":"https://api.github.com/repos/metabase/metabase/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/metabase/metabase/teams","hooks_url":"https://api.github.com/repos/metabase/metabase/hooks","issue_events_url":"https://api.github.com/repos/metabase/metabase/issues/events{/number}","events_url":"https://api.github.com/repos/metabase/metabase/events","assignees_url":"https://api.github.com/repos/metabase/metabase/assignees{/user}","branches_url":"https://api.github.com/repos/metabase/metabase/branches{/branch}","tags_url":"https://api.github.com/repos/metabase/metabase/tags","blobs_url":"https://api.github.com/repos/metabase/metabase/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/metabase/metabase/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/metabase/metabase/git/refs{/sha}","trees_url":"https://api.github.com/repos/metabase/metabase/git/trees{/sha}","statuses_url":"https://api.github.com/repos/metabase/metabase/statuses/{sha}","languages_url":"https://api.github.com/repos/metabase/metabase/languages","stargazers_url":"https://api.github.com/repos/metabase/metabase/stargazers","contributors_url":"https://api.github.com/repos/metabase/metabase/contributors","subscribers_url":"https://api.github.com/repos/metabase/metabase/subscribers","subscription_url":"https://api.github.com/repos/metabase/metabase/subscription","commits_url":"https://api.github.com/repos/metabase/metabase/commits{/sha}","git_commits_url":"https://api.github.com/repos/metabase/metabase/git/commits{/sha}","comments_url":"https://api.github.com/repos/metabase/metabase/comments{/number}","issue_comment_url":"https://api.github.com/repos/metabase/metabase/issues/comments{/number}","contents_url":"https://api.github.com/repos/metabase/metabase/contents/{+path}","compare_url":"https://api.github.com/repos/metabase/metabase/compare/{base}...{head}","merges_url":"https://api.github.com/repos/metabase/metabase/merges","archive_url":"https://api.github.com/repos/metabase/metabase/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/metabase/metabase/downloads","issues_url":"https://api.github.com/repos/metabase/metabase/issues{/number}","pulls_url":"https://api.github.com/repos/metabase/metabase/pulls{/number}","milestones_url":"https://api.github.com/repos/metabase/metabase/milestones{/number}","notifications_url":"https://api.github.com/repos/metabase/metabase/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/metabase/metabase/labels{/name}","releases_url":"https://api.github.com/repos/metabase/metabase/releases{/id}","created_at":"2015-02-02T19:25:47Z","updated_at":"2015-11-06T22:05:18Z","pushed_at":"2015-11-06T21:33:26Z","git_url":"git://github.com/metabase/metabase.git","ssh_url":"git@github.com:metabase/metabase.git","clone_url":"https://github.com/metabase/metabase.git","svn_url":"https://github.com/metabase/metabase","homepage":"http://metabase.com","size":33808,"stargazers_count":1733,"watchers_count":1733,"language":"Clojure","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":69,"mirror_url":null,"open_issues_count":257,"forks":69,"open_issues":257,"watchers":1733,"default_branch":"master"}},"base":{"label":"metabase:master","ref":"master","sha":"c66f8a671994f122459276c7c935d932325cb961","user":{"login":"metabase","id":10520629,"avatar_url":"https://avatars.githubusercontent.com/u/10520629?v=3","gravatar_id":"","url":"https://api.github.com/users/metabase","html_url":"https://github.com/metabase","followers_url":"https://api.github.com/users/metabase/followers","following_url":"https://api.github.com/users/metabase/following{/other_user}","gists_url":"https://api.github.com/users/metabase/gists{/gist_id}","starred_url":"https://api.github.com/users/metabase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metabase/subscriptions","organizations_url":"https://api.github.com/users/metabase/orgs","repos_url":"https://api.github.com/users/metabase/repos","events_url":"https://api.github.com/users/metabase/events{/privacy}","received_events_url":"https://api.github.com/users/metabase/received_events","type":"Organization","site_admin":false},"repo":{"id":30203935,"name":"metabase","full_name":"metabase/metabase","owner":{"login":"metabase","id":10520629,"avatar_url":"https://avatars.githubusercontent.com/u/10520629?v=3","gravatar_id":"","url":"https://api.github.com/users/metabase","html_url":"https://github.com/metabase","followers_url":"https://api.github.com/users/metabase/followers","following_url":"https://api.github.com/users/metabase/following{/other_user}","gists_url":"https://api.github.com/users/metabase/gists{/gist_id}","starred_url":"https://api.github.com/users/metabase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/metabase/subscriptions","organizations_url":"https://api.github.com/users/metabase/orgs","repos_url":"https://api.github.com/users/metabase/repos","events_url":"https://api.github.com/users/metabase/events{/privacy}","received_events_url":"https://api.github.com/users/metabase/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/metabase/metabase","description":"The simplest, fastest way to get business intelligence and analytics  to everyone in your company","fork":false,"url":"https://api.github.com/repos/metabase/metabase","forks_url":"https://api.github.com/repos/metabase/metabase/forks","keys_url":"https://api.github.com/repos/metabase/metabase/keys{/key_id}","collaborators_url":"https://api.github.com/repos/metabase/metabase/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/metabase/metabase/teams","hooks_url":"https://api.github.com/repos/metabase/metabase/hooks","issue_events_url":"https://api.github.com/repos/metabase/metabase/issues/events{/number}","events_url":"https://api.github.com/repos/metabase/metabase/events","assignees_url":"https://api.github.com/repos/metabase/metabase/assignees{/user}","branches_url":"https://api.github.com/repos/metabase/metabase/branches{/branch}","tags_url":"https://api.github.com/repos/metabase/metabase/tags","blobs_url":"https://api.github.com/repos/metabase/metabase/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/metabase/metabase/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/metabase/metabase/git/refs{/sha}","trees_url":"https://api.github.com/repos/metabase/metabase/git/trees{/sha}","statuses_url":"https://api.github.com/repos/metabase/metabase/statuses/{sha}","languages_url":"https://api.github.com/repos/metabase/metabase/languages","stargazers_url":"https://api.github.com/repos/metabase/metabase/stargazers","contributors_url":"https://api.github.com/repos/metabase/metabase/contributors","subscribers_url":"https://api.github.com/repos/metabase/metabase/subscribers","subscription_url":"https://api.github.com/repos/metabase/metabase/subscription","commits_url":"https://api.github.com/repos/metabase/metabase/commits{/sha}","git_commits_url":"https://api.github.com/repos/metabase/metabase/git/commits{/sha}","comments_url":"https://api.github.com/repos/metabase/metabase/comments{/number}","issue_comment_url":"https://api.github.com/repos/metabase/metabase/issues/comments{/number}","contents_url":"https://api.github.com/repos/metabase/metabase/contents/{+path}","compare_url":"https://api.github.com/repos/metabase/metabase/compare/{base}...{head}","merges_url":"https://api.github.com/repos/metabase/metabase/merges","archive_url":"https://api.github.com/repos/metabase/metabase/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/metabase/metabase/downloads","issues_url":"https://api.github.com/repos/metabase/metabase/issues{/number}","pulls_url":"https://api.github.com/repos/metabase/metabase/pulls{/number}","milestones_url":"https://api.github.com/repos/metabase/metabase/milestones{/number}","notifications_url":"https://api.github.com/repos/metabase/metabase/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/metabase/metabase/labels{/name}","releases_url":"https://api.github.com/repos/metabase/metabase/releases{/id}","created_at":"2015-02-02T19:25:47Z","updated_at":"2015-11-06T22:05:18Z","pushed_at":"2015-11-06T21:33:26Z","git_url":"git://github.com/metabase/metabase.git","ssh_url":"git@github.com:metabase/metabase.git","clone_url":"https://github.com/metabase/metabase.git","svn_url":"https://github.com/metabase/metabase","homepage":"http://metabase.com","size":33808,"stargazers_count":1733,"watchers_count":1733,"language":"Clojure","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":69,"mirror_url":null,"open_issues_count":257,"forks":69,"open_issues":257,"watchers":1733,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/metabase/metabase/pulls/1441"},"html":{"href":"https://github.com/metabase/metabase/pull/1441"},"issue":{"href":"https://api.github.com/repos/metabase/metabase/issues/1441"},"comments":{"href":"https://api.github.com/repos/metabase/metabase/issues/1441/comments"},"review_comments":{"href":"https://api.github.com/repos/metabase/metabase/pulls/1441/comments"},"review_comment":{"href":"https://api.github.com/repos/metabase/metabase/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/metabase/metabase/pulls/1441/commits"},"statuses":{"href":"https://api.github.com/repos/metabase/metabase/statuses/3b8da0cc6041a7200f085d73981ef4cd1498e9f5"}}}}