aboutsummaryrefslogtreecommitdiffstats
path: root/activestorage
diff options
context:
space:
mode:
authorGeorge Claghorn <george@basecamp.com>2017-08-29 18:30:26 -0400
committerGeorge Claghorn <george@basecamp.com>2017-08-29 18:30:45 -0400
commit29db6b3cb26708fa174e1e90dc7088cff92b8083 (patch)
treefa588a7e1ed2575ded06fd521b8f268338499dcc /activestorage
parent5c2c8d8cfd7fc8ec58c906e192c1d4f305bf7095 (diff)
downloadrails-29db6b3cb26708fa174e1e90dc7088cff92b8083.tar.gz
rails-29db6b3cb26708fa174e1e90dc7088cff92b8083.tar.bz2
rails-29db6b3cb26708fa174e1e90dc7088cff92b8083.zip
Refactor
Diffstat (limited to 'activestorage')
-rw-r--r--activestorage/lib/active_storage/attached/one.rb22
1 files changed, 10 insertions, 12 deletions
diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb
index 87c650b3d0..ac90f32d95 100644
--- a/activestorage/lib/active_storage/attached/one.rb
+++ b/activestorage/lib/active_storage/attached/one.rb
@@ -21,7 +21,9 @@ module ActiveStorage
# person.avatar.attach(io: File.open("~/face.jpg"), filename: "face.jpg", content_type: "image/jpg")
# person.avatar.attach(avatar_blob) # ActiveStorage::Blob object
def attach(attachable)
- purge_dependent_attachment do
+ if attached? && dependent == :purge_later
+ replace attachable
+ else
write_attachment create_attachment_from(attachable)
end
end
@@ -54,17 +56,13 @@ module ActiveStorage
end
private
- def purge_dependent_attachment
- if attached? && dependent == :purge_later
- blob.tap do
- transaction do
- destroy
- yield
- end
- end.purge_later
- else
- yield
- end
+ def replace(attachable)
+ blob.tap do
+ transaction do
+ destroy
+ write_attachment create_attachment_from(attachable)
+ end
+ end.purge_later
end
def create_attachment_from(attachable)