diff options
author | George Claghorn <george@basecamp.com> | 2017-08-29 15:34:50 -0400 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2017-08-29 15:34:50 -0400 |
commit | 656ee8b2dd1b06541f03ea19302d3529085f5139 (patch) | |
tree | 969368cc54572a6dc30579a300beaa53f8d938c7 /activestorage/lib | |
parent | 96742b5e0f04a2ed795032df04736cb1f16cc3a4 (diff) | |
download | rails-656ee8b2dd1b06541f03ea19302d3529085f5139.tar.gz rails-656ee8b2dd1b06541f03ea19302d3529085f5139.tar.bz2 rails-656ee8b2dd1b06541f03ea19302d3529085f5139.zip |
Fix replacing a singular attachment
Diffstat (limited to 'activestorage/lib')
-rw-r--r-- | activestorage/lib/active_storage/attached.rb | 6 | ||||
-rw-r--r-- | activestorage/lib/active_storage/attached/macros.rb | 4 | ||||
-rw-r--r-- | activestorage/lib/active_storage/attached/one.rb | 22 |
3 files changed, 25 insertions, 7 deletions
diff --git a/activestorage/lib/active_storage/attached.rb b/activestorage/lib/active_storage/attached.rb index 9c4b6d5d1d..c08fd56652 100644 --- a/activestorage/lib/active_storage/attached.rb +++ b/activestorage/lib/active_storage/attached.rb @@ -8,10 +8,10 @@ module ActiveStorage # Abstract base class for the concrete ActiveStorage::Attached::One and ActiveStorage::Attached::Many # classes that both provide proxy access to the blob association for a record. class Attached - attr_reader :name, :record + attr_reader :name, :record, :dependent - def initialize(name, record) - @name, @record = name, record + def initialize(name, record, dependent:) + @name, @record, @dependent = name, record, dependent end private diff --git a/activestorage/lib/active_storage/attached/macros.rb b/activestorage/lib/active_storage/attached/macros.rb index f3879ee2e3..35a081adc4 100644 --- a/activestorage/lib/active_storage/attached/macros.rb +++ b/activestorage/lib/active_storage/attached/macros.rb @@ -26,7 +26,7 @@ module ActiveStorage def has_one_attached(name, dependent: :purge_later) class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name} - @active_storage_attached_#{name} ||= ActiveStorage::Attached::One.new("#{name}", self) + @active_storage_attached_#{name} ||= ActiveStorage::Attached::One.new("#{name}", self, dependent: #{dependent == :purge_later ? ":purge_later" : "false"}) end CODE @@ -65,7 +65,7 @@ module ActiveStorage def has_many_attached(name, dependent: :purge_later) class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name} - @active_storage_attached_#{name} ||= ActiveStorage::Attached::Many.new("#{name}", self) + @active_storage_attached_#{name} ||= ActiveStorage::Attached::Many.new("#{name}", self, dependent: #{dependent == :purge_later ? ":purge_later" : "false"}) end CODE diff --git a/activestorage/lib/active_storage/attached/one.rb b/activestorage/lib/active_storage/attached/one.rb index 92a00c20a7..87c650b3d0 100644 --- a/activestorage/lib/active_storage/attached/one.rb +++ b/activestorage/lib/active_storage/attached/one.rb @@ -21,8 +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) - write_attachment \ - ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable)) + purge_dependent_attachment do + write_attachment create_attachment_from(attachable) + end end # Returns +true+ if an attachment has been made. @@ -53,6 +54,23 @@ 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 + end + + def create_attachment_from(attachable) + ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable)) + end + def write_attachment(attachment) record.public_send("#{name}_attachment=", attachment) end |