aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/active_storage/attached/many.rb9
-rw-r--r--lib/active_storage/attached/one.rb13
-rw-r--r--test/models/attachments_test.rb4
3 files changed, 15 insertions, 11 deletions
diff --git a/lib/active_storage/attached/many.rb b/lib/active_storage/attached/many.rb
index 129f166776..704369ba89 100644
--- a/lib/active_storage/attached/many.rb
+++ b/lib/active_storage/attached/many.rb
@@ -7,15 +7,15 @@ class ActiveStorage::Attached::Many < ActiveStorage::Attached
# You don't have to call this method to access the attachments' methods as
# they are all available at the model level.
def attachments
- @attachments ||= record.public_send("#{name}_attachments")
+ record.public_send("#{name}_attachments")
end
# Associates one or several attachments with the current record, saving
# them to the database.
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))
- end
+ end)
end
# Checks the presence of attachments.
@@ -34,7 +34,7 @@ class ActiveStorage::Attached::Many < ActiveStorage::Attached
def purge
if attached?
attachments.each(&:purge)
- @attachments = nil
+ attachments.reload
end
end
@@ -42,7 +42,6 @@ class ActiveStorage::Attached::Many < ActiveStorage::Attached
def purge_later
if attached?
attachments.each(&:purge_later)
- @attachments = nil
end
end
end
diff --git a/lib/active_storage/attached/one.rb b/lib/active_storage/attached/one.rb
index 02fc9c9abc..d255412842 100644
--- a/lib/active_storage/attached/one.rb
+++ b/lib/active_storage/attached/one.rb
@@ -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
# they are all available at the model level.
def attachment
- @attachment ||= record.public_send("#{name}_attachment")
+ record.public_send("#{name}_attachment")
end
# Associates a given attachment with the current record, saving it to the
# database.
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
# Checks the presence of the attachment.
@@ -32,7 +33,7 @@ class ActiveStorage::Attached::One < ActiveStorage::Attached
def purge
if attached?
attachment.purge
- @attachment = nil
+ write_attachment nil
end
end
@@ -40,7 +41,11 @@ class ActiveStorage::Attached::One < ActiveStorage::Attached
def purge_later
if attached?
attachment.purge_later
- @attachment = nil
end
end
+
+ private
+ def write_attachment(attachment)
+ record.public_send("#{name}_attachment=", attachment)
+ end
end
diff --git a/test/models/attachments_test.rb b/test/models/attachments_test.rb
index 1a9fc6f932..45f62b0bbf 100644
--- a/test/models/attachments_test.rb
+++ b/test/models/attachments_test.rb
@@ -70,9 +70,9 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
test "find attached blobs" do
@user.highlights.attach(
- { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" },
+ { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" },
{ io: StringIO.new("IT"), filename: "country.jpg", content_type: "image/jpg" })
-
+
User.where(id: @user.id).includes(highlights_attachments: :blob).first
end