aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_vault/attached/many.rb
blob: 6f79a1c555cebf3b36b01eff68d2a8254f2027d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class ActiveVault::Attached::Many < ActiveVault::Attached
  delegate_missing_to :attachments

  def attachments
    @attachments ||= ActiveVault::Attachment.where(record_gid: record.to_gid.to_s, name: name)
  end

  def attach(*attachables)
    @attachments = attachments | Array(attachables).flatten.collect do |attachable|
      ActiveVault::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable))
    end
  end

  def attached?
    attachments.any?
  end

  def purge
    if attached?
      attachments.each(&:purge)
      @attachments = nil
    end
  end

  def purge_later
    if attached?
      attachments.each(&:purge_later)
      @attachments = nil
    end
  end
end