aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_vault/attached/one.rb
blob: 5566c1b97166e629576f741644b9380196869054 (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
class ActiveVault::Attached::One < ActiveVault::Attached
  delegate_missing_to :attachment

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

  def attach(attachable)
    if @attachment
      # FIXME: Have options to declare dependent: :purge to clean up
    end

    @attachment = ActiveVault::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable))
  end

  def attached?
    attachment.present?
  end

  def purge
    attachment.purge
    @attachment = nil
  end
end