Class: Collavre::Tools::CreativeRemoveAttachmentService

Inherits:
Object
  • Object
show all
Extended by:
T::Sig, ToolMeta
Defined in:
app/services/collavre/tools/creative_remove_attachment_service.rb

Instance Method Summary collapse

Instance Method Details

#call(creative_id:, signed_id:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/collavre/tools/creative_remove_attachment_service.rb', line 16

def call(creative_id:, signed_id:)
  raise "Current.user is required" unless Current.user

  creative = Creative.find_by(id: creative_id)
  return { error: "Creative not found", id: creative_id } unless creative

  unless creative.has_permission?(Current.user, :write)
    return { error: "No write permission on Creative", id: creative_id }
  end

  # HTML is the source of truth: strip the node from the description and let
  # after_save reconcile detach + safe-purge the blob. Removing only the
  # ActiveStorage attachment would leave a dangling node in the description
  # (broken asset, or reconciled back into creative.files on the next save).
  removed = creative.remove_attachment!(signed_id)
  return { error: "Attachment not found on this Creative" } unless removed

  { success: true, creative_id: creative.id, removed_signed_id: signed_id }
end