aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_storage/attached/one.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/active_storage/attached/one.rb')
-rw-r--r--lib/active_storage/attached/one.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/active_storage/attached/one.rb b/lib/active_storage/attached/one.rb
new file mode 100644
index 0000000000..d08d265992
--- /dev/null
+++ b/lib/active_storage/attached/one.rb
@@ -0,0 +1,29 @@
+class ActiveStorage::Attached::One < ActiveStorage::Attached
+ delegate_missing_to :attachment
+
+ def attachment
+ @attachment ||= ActiveStorage::Attachment.find_by(record_gid: record.to_gid.to_s, name: name)
+ end
+
+ def attach(attachable)
+ @attachment = ActiveStorage::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable))
+ end
+
+ def attached?
+ attachment.present?
+ end
+
+ def purge
+ if attached?
+ attachment.purge
+ @attachment = nil
+ end
+ end
+
+ def purge_later
+ if attached?
+ attachment.purge_later
+ @attachment = nil
+ end
+ end
+end