From 49b4af8b3dc18f11af205d1066ae9d6962643625 Mon Sep 17 00:00:00 2001 From: Ghouse Mohamed Date: Fri, 18 Feb 2022 23:22:13 +0530 Subject: [PATCH 1/2] Return blob/blobs when #attach is able to save the record and return false if it is not able to --- activestorage/lib/active_storage/attached/many.rb | 3 ++- activestorage/lib/active_storage/attached/one.rb | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/activestorage/lib/active_storage/attached/many.rb b/activestorage/lib/active_storage/attached/many.rb index f0ee58bec6..86bbb87a27 100644 --- a/activestorage/lib/active_storage/attached/many.rb +++ b/activestorage/lib/active_storage/attached/many.rb @@ -49,7 +49,8 @@ module ActiveStorage def attach(*attachables) if record.persisted? && !record.changed? record.public_send("#{name}=", blobs + attachables.flatten) - record.save + return record.public_send("#{name}") if record.save + false else record.public_send("#{name}=", (change&.attachables || blobs) + attachables.flatten) end diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb index 6efbfa7a54..80eb72ce17 100644 --- a/activestorage/lib/active_storage/attached/one.rb +++ b/activestorage/lib/active_storage/attached/one.rb @@ -56,7 +56,8 @@ module ActiveStorage def attach(attachable) if record.persisted? && !record.changed? record.public_send("#{name}=", attachable) - record.save + return record.public_send("#{name}") if record.save + false else record.public_send("#{name}=", attachable) end From fbfb6e6885e65f212771430a235359ee86654512 Mon Sep 17 00:00:00 2001 From: Ghouse Mohamed Date: Sat, 26 Feb 2022 02:18:51 +0530 Subject: [PATCH 2/2] Return blob/blobs when #attach is able to save the record and return if it is not able to --- activestorage/CHANGELOG.md | 10 ++++++++++ activestorage/lib/active_storage/attached/many.rb | 7 +++++-- activestorage/lib/active_storage/attached/one.rb | 7 +++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md index 64ad28d5d4..45d85a3d3f 100644 --- a/activestorage/CHANGELOG.md +++ b/activestorage/CHANGELOG.md @@ -1,3 +1,13 @@ +* Saving attachment(s) to a record returns the blob/blobs object + + Previously, saving attachments did not return the blob/blobs that + were attached. Now, saving attachments to a record with `#attach` + method returns the blob or array of blobs that were attached to + the record. If it fails to save the attachment(s), then it returns + `false`. + + *Ghouse Mohamed* + * Don't stream responses in redirect mode Previously, both redirect mode and proxy mode streamed their diff --git a/activestorage/lib/active_storage/attached/many.rb b/activestorage/lib/active_storage/attached/many.rb index 86bbb87a27..90775de254 100644 --- a/activestorage/lib/active_storage/attached/many.rb +++ b/activestorage/lib/active_storage/attached/many.rb @@ -49,8 +49,11 @@ module ActiveStorage def attach(*attachables) if record.persisted? && !record.changed? record.public_send("#{name}=", blobs + attachables.flatten) - return record.public_send("#{name}") if record.save - false + if record.save + record.public_send("#{name}") + else + false + end else record.public_send("#{name}=", (change&.attachables || blobs) + attachables.flatten) end diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb index 80eb72ce17..0e3e5a9fc4 100644 --- a/activestorage/lib/active_storage/attached/one.rb +++ b/activestorage/lib/active_storage/attached/one.rb @@ -56,8 +56,11 @@ module ActiveStorage def attach(attachable) if record.persisted? && !record.changed? record.public_send("#{name}=", attachable) - return record.public_send("#{name}") if record.save - false + if record.save + record.public_send("#{name}") + else + false + end else record.public_send("#{name}=", attachable) end