mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix attaching with standard Rails associations.
Removes needless ivar caching (a Rails association handles that). Inserts a reload and a nil assign, since the association proxy doesn't seem to that it's been destroyed through `purge`.
This commit is contained in:
parent
212f925654
commit
a4f36f957e
3 changed files with 15 additions and 11 deletions
|
@ -7,15 +7,15 @@ class ActiveStorage::Attached::Many < ActiveStorage::Attached
|
||||||
# You don't have to call this method to access the attachments' methods as
|
# You don't have to call this method to access the attachments' methods as
|
||||||
# they are all available at the model level.
|
# they are all available at the model level.
|
||||||
def attachments
|
def attachments
|
||||||
@attachments ||= record.public_send("#{name}_attachments")
|
record.public_send("#{name}_attachments")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Associates one or several attachments with the current record, saving
|
# Associates one or several attachments with the current record, saving
|
||||||
# them to the database.
|
# them to the database.
|
||||||
def attach(*attachables)
|
def attach(*attachables)
|
||||||
@attachments = attachments | Array(attachables).flatten.collect do |attachable|
|
record.public_send("#{name}_attachments=", attachments | Array(attachables).flat_map do |attachable|
|
||||||
ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable))
|
ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable))
|
||||||
end
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks the presence of attachments.
|
# Checks the presence of attachments.
|
||||||
|
@ -34,7 +34,7 @@ class ActiveStorage::Attached::Many < ActiveStorage::Attached
|
||||||
def purge
|
def purge
|
||||||
if attached?
|
if attached?
|
||||||
attachments.each(&:purge)
|
attachments.each(&:purge)
|
||||||
@attachments = nil
|
attachments.reload
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -42,7 +42,6 @@ class ActiveStorage::Attached::Many < ActiveStorage::Attached
|
||||||
def purge_later
|
def purge_later
|
||||||
if attached?
|
if attached?
|
||||||
attachments.each(&:purge_later)
|
attachments.each(&:purge_later)
|
||||||
@attachments = nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,13 +7,14 @@ class ActiveStorage::Attached::One < ActiveStorage::Attached
|
||||||
# You don't have to call this method to access the attachment's methods as
|
# You don't have to call this method to access the attachment's methods as
|
||||||
# they are all available at the model level.
|
# they are all available at the model level.
|
||||||
def attachment
|
def attachment
|
||||||
@attachment ||= record.public_send("#{name}_attachment")
|
record.public_send("#{name}_attachment")
|
||||||
end
|
end
|
||||||
|
|
||||||
# Associates a given attachment with the current record, saving it to the
|
# Associates a given attachment with the current record, saving it to the
|
||||||
# database.
|
# database.
|
||||||
def attach(attachable)
|
def attach(attachable)
|
||||||
@attachment = ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable))
|
write_attachment \
|
||||||
|
ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable))
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks the presence of the attachment.
|
# Checks the presence of the attachment.
|
||||||
|
@ -32,7 +33,7 @@ class ActiveStorage::Attached::One < ActiveStorage::Attached
|
||||||
def purge
|
def purge
|
||||||
if attached?
|
if attached?
|
||||||
attachment.purge
|
attachment.purge
|
||||||
@attachment = nil
|
write_attachment nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -40,7 +41,11 @@ class ActiveStorage::Attached::One < ActiveStorage::Attached
|
||||||
def purge_later
|
def purge_later
|
||||||
if attached?
|
if attached?
|
||||||
attachment.purge_later
|
attachment.purge_later
|
||||||
@attachment = nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def write_attachment(attachment)
|
||||||
|
record.public_send("#{name}_attachment=", attachment)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue