aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_vault/attachments.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/active_vault/attachments.rb')
-rw-r--r--lib/active_vault/attachments.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/active_vault/attachments.rb b/lib/active_vault/attachments.rb
new file mode 100644
index 0000000000..c66c142650
--- /dev/null
+++ b/lib/active_vault/attachments.rb
@@ -0,0 +1,30 @@
+require "active_vault/attachment"
+require "action_dispatch/http/upload"
+
+module ActiveVault::Attachments
+ def has_file(name)
+ define_method(name) do
+ (@active_vault_attachments ||= {})[name] ||=
+ ActiveVault::Attachment.find_by(record_gid: to_gid.to_s, name: name)&.tap { |a| a.record = self }
+ end
+
+ define_method(:"#{name}=") do |attachable|
+ case attachable
+ when ActiveVault::Blob
+ blob = attachable
+ when ActionDispatch::Http::UploadedFile
+ blob = ActiveVault::Blob.create_after_upload! \
+ io: attachable.open,
+ filename: attachable.original_filename,
+ content_type: attachable.content_type
+ when Hash
+ blob = ActiveVault::Blob.create_after_upload!(attachable)
+ when NilClass
+ blob = nil
+ end
+
+ (@active_vault_attachments ||= {})[name] = blob ?
+ ActiveVault::Attachment.create!(record_gid: to_gid.to_s, name: name, blob: blob)&.tap { |a| a.record = self } : nil
+ end
+ end
+end