aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage/lib/active_storage/attached/one.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activestorage/lib/active_storage/attached/one.rb')
-rw-r--r--activestorage/lib/active_storage/attached/one.rb19
1 files changed, 7 insertions, 12 deletions
diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb
index f992cb5f84..960ff99e63 100644
--- a/activestorage/lib/active_storage/attached/one.rb
+++ b/activestorage/lib/active_storage/attached/one.rb
@@ -10,11 +10,11 @@ module ActiveStorage
# You don't have to call this method to access the attachment's methods as
# they are all available at the model level.
def attachment
- record.public_send("#{name}_attachment")
+ change.present? ? change.attachment : record.public_send("#{name}_attachment")
end
def blank?
- attachment.blank?
+ !attached?
end
# Associates a given attachment with the current record, saving it to the database.
@@ -24,16 +24,10 @@ module ActiveStorage
# person.avatar.attach(io: File.open("/path/to/face.jpg"), filename: "face.jpg", content_type: "image/jpg")
# person.avatar.attach(avatar_blob) # ActiveStorage::Blob object
def attach(attachable)
- blob_was = blob if attached?
- blob = create_blob_from(attachable)
+ new_blob = create_blob_from(attachable)
- unless blob == blob_was
- transaction do
- detach
- write_attachment build_attachment(blob: blob)
- end
-
- blob_was.purge_later if blob_was && dependent == :purge_later
+ if !attached? || new_blob != blob
+ write_attachment build_attachment(blob: new_blob)
end
end
@@ -69,6 +63,7 @@ module ActiveStorage
def purge_later
if attached?
attachment.purge_later
+ write_attachment nil
end
end
@@ -76,7 +71,7 @@ module ActiveStorage
delegate :transaction, to: :record
def build_attachment(blob:)
- ActiveStorage::Attachment.new(record: record, name: name, blob: blob)
+ Attachment.new(record: record, name: name, blob: blob)
end
def write_attachment(attachment)