diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-24 14:35:36 -0500 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-24 14:35:36 -0500 |
commit | 3eb8c89c0410fac34aefc8b89b8c6b5901c39415 (patch) | |
tree | feab05c7ed4a9ee67ada5803da2674c27f9172ea | |
parent | 20effee5671d76875c050e3d9e91ff60fa181a91 (diff) | |
download | rails-3eb8c89c0410fac34aefc8b89b8c6b5901c39415.tar.gz rails-3eb8c89c0410fac34aefc8b89b8c6b5901c39415.tar.bz2 rails-3eb8c89c0410fac34aefc8b89b8c6b5901c39415.zip |
Fix blob associations
cc @javan
-rw-r--r-- | lib/active_storage/attached/macros.rb | 4 | ||||
-rw-r--r-- | test/models/attachments_test.rb | 18 | ||||
-rw-r--r-- | test/test_helper.rb | 1 |
3 files changed, 21 insertions, 2 deletions
diff --git a/lib/active_storage/attached/macros.rb b/lib/active_storage/attached/macros.rb index 54c2731c08..11f88f16e5 100644 --- a/lib/active_storage/attached/macros.rb +++ b/lib/active_storage/attached/macros.rb @@ -18,7 +18,7 @@ module ActiveStorage::Attached::Macros end has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record - has_one :"#{name}_blob", through: :"#{name}_attachment" + has_one :"#{name}_blob", through: :"#{name}_attachment", class_name: "ActiveStorage::Blob", source: :blob if dependent == :purge_later before_destroy { public_send(name).purge_later } @@ -47,7 +47,7 @@ module ActiveStorage::Attached::Macros end has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment" - has_many :"#{name}_blobs", through: :"#{name}_attachments" + has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob scope :"with_attached_#{name}", -> { includes("#{name}_attachments": :blob) } diff --git a/test/models/attachments_test.rb b/test/models/attachments_test.rb index eac3cbe680..82256e1f44 100644 --- a/test/models/attachments_test.rb +++ b/test/models/attachments_test.rb @@ -30,6 +30,13 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase assert_equal "town.jpg", @user.avatar.filename.to_s end + test "access underlying associations of new blob" do + @user.avatar.attach create_blob(filename: "funky.jpg") + assert_equal @user, @user.avatar_attachment.record + assert_equal @user.avatar_attachment.blob, @user.avatar_blob + assert_equal "funky.jpg", @user.avatar_attachment.blob.filename.to_s + end + test "purge attached blob" do @user.avatar.attach create_blob(filename: "funky.jpg") avatar_key = @user.avatar.key @@ -79,6 +86,17 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase assert_equal "country.jpg", highlights.second.filename.to_s end + test "access underlying associations of new blobs" do + @user.highlights.attach( + { io: StringIO.new("STUFF"), filename: "town.jpg", content_type: "image/jpg" }, + { io: StringIO.new("IT"), filename: "country.jpg", content_type: "image/jpg" }) + + assert_equal @user, @user.highlights_attachments.first.record + assert_equal @user.highlights_attachments.collect(&:blob).sort, @user.highlights_blobs.sort + assert_equal "town.jpg", @user.highlights_attachments.first.blob.filename.to_s + end + + test "purge attached blobs" do @user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "wonky.jpg") highlight_keys = @user.highlights.collect(&:key) diff --git a/test/test_helper.rb b/test/test_helper.rb index 650e997205..d3a55e2cf0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -23,6 +23,7 @@ rescue Errno::ENOENT {} end +require "active_storage/blob" require "active_storage/service/disk_service" require "tmpdir" ActiveStorage::Blob.service = ActiveStorage::Service::DiskService.new(root: Dir.mktmpdir("active_storage_tests")) |