diff options
Diffstat (limited to 'app/models/asset.rb')
| -rw-r--r-- | app/models/asset.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/app/models/asset.rb b/app/models/asset.rb index 73970e21..8cec4371 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb | |||
| @@ -40,4 +40,34 @@ class Asset < ApplicationRecord | |||
| 40 | Node.where("head_id IN (:ids) OR draft_id IN (:ids) OR autosave_id IN (:ids)", | 40 | Node.where("head_id IN (:ids) OR draft_id IN (:ids) OR autosave_id IN (:ids)", |
| 41 | :ids => page_ids).distinct | 41 | :ids => page_ids).distinct |
| 42 | end | 42 | end |
| 43 | |||
| 44 | # Witnessed destruction. Destroying an asset is a public-facing act | ||
| 45 | # even when unattached. The original and its variants are publicly | ||
| 46 | # reachable under /system/uploads, so an entry is always written, | ||
| 47 | # before the row and its files die. Every currently-attached node | ||
| 48 | # participates so its zoomed history shows the loss; the asset itself | ||
| 49 | # participates as the first non-Node subject (its participant row | ||
| 50 | # dangles after destroy, by design, the name lives on in metadata). | ||
| 51 | def destroy_witnessed! user: | ||
| 52 | ActiveRecord::Base.transaction do | ||
| 53 | affected = attached_nodes.to_a | ||
| 54 | headline_losses = affected.select do |node| | ||
| 55 | [node.head, node.draft, node.autosave].compact.any? do |row| | ||
| 56 | row.related_assets.exists?(:asset_id => id, :headline => true) | ||
| 57 | end | ||
| 58 | end | ||
| 59 | |||
| 60 | metadata = { | ||
| 61 | :asset_name => name, | ||
| 62 | :content_type => upload_content_type, | ||
| 63 | :path => upload.url.sub(/\?\d+$/, ""), | ||
| 64 | } | ||
| 65 | metadata[:detached_from] = affected.map(&:unique_name) if affected.any? | ||
| 66 | metadata[:headline_removed_from] = headline_losses.map(&:unique_name) if headline_losses.any? | ||
| 67 | |||
| 68 | NodeAction.record!(:participants => [self] + affected, :user => user, | ||
| 69 | :action => "asset_destroy", **metadata) | ||
| 70 | destroy! | ||
| 71 | end | ||
| 72 | end | ||
| 43 | end | 73 | end |
