Module: RailsCom::AttachedOne

Defined in:
lib/rails_com/active_storage/attached_macros.rb

Instance Method Summary collapse

Instance Method Details

#attachmentObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rails_com/active_storage/attached_macros.rb', line 5

def attachment
  if super
    return super
  elsif defined?(@attachment)
    return @attachment
  end

  id = Com::BlobDefault.defaults["#{record.class.name}_#{name}"]
  if id
    begin
      @attachment = ActiveStorage::Attachment.new(record: record, name: name, blob: ActiveStorage::Blob.find(id))
    rescue ActiveRecord::RecordNotFound => e
      Rails.cache.delete('blob_default/default')
      retry
    end
  end
end

#copy(from, from_name) ⇒ Object



23
24
25
26
# File 'lib/rails_com/active_storage/attached_macros.rb', line 23

def copy(from, from_name)
  from_attachment = from.send "#{from_name}_attachment"
  record.send "create_#{name}_attachment", blob_id: from_attachment.blob_id
end