aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2017-07-09 18:48:26 +0200
committerKasper Timm Hansen <kaspth@gmail.com>2017-07-23 21:17:17 +0200
commit5963766d840ddcdb577a1bd10eb1491a4ef9132f (patch)
treee6cba96cac8865435486ba08a1a5fc0f04d6e9f7
parent4efbeaeaab72be070e203f39726b37703c1db1fa (diff)
downloadrails-5963766d840ddcdb577a1bd10eb1491a4ef9132f.tar.gz
rails-5963766d840ddcdb577a1bd10eb1491a4ef9132f.tar.bz2
rails-5963766d840ddcdb577a1bd10eb1491a4ef9132f.zip
Explore regular polymorphic associations rather than record_gid
-rw-r--r--app/models/active_storage/attachment.rb10
-rw-r--r--lib/active_storage/attached/macros.rb6
-rw-r--r--lib/active_storage/attached/many.rb4
-rw-r--r--lib/active_storage/attached/one.rb4
-rw-r--r--lib/active_storage/migration.rb9
-rw-r--r--test/models/attachments_test.rb8
6 files changed, 24 insertions, 17 deletions
diff --git a/app/models/active_storage/attachment.rb b/app/models/active_storage/attachment.rb
index 20c619aa5a..1dd202ca45 100644
--- a/app/models/active_storage/attachment.rb
+++ b/app/models/active_storage/attachment.rb
@@ -6,19 +6,11 @@ require "active_support/core_ext/module/delegation"
class ActiveStorage::Attachment < ActiveRecord::Base
self.table_name = "active_storage_attachments"
+ belongs_to :record, polymorphic: true
belongs_to :blob, class_name: "ActiveStorage::Blob"
delegate_missing_to :blob
- def record
- @record ||= GlobalID::Locator.locate(record_gid)
- end
-
- def record=(record)
- @record = record
- self.record_gid = record&.to_gid
- end
-
def purge
blob.purge
destroy
diff --git a/lib/active_storage/attached/macros.rb b/lib/active_storage/attached/macros.rb
index 1e0f9a6b7e..0452089a5f 100644
--- a/lib/active_storage/attached/macros.rb
+++ b/lib/active_storage/attached/macros.rb
@@ -16,6 +16,9 @@ module ActiveStorage::Attached::Macros
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::One.new(name, self))
end
+ has_one :"#{name}_attachment", -> { where(name: name) }, class_name: "ActiveStorage::Attachment", as: :record
+ has_one :"#{name}_blob", through: :"#{name}_attachment"
+
if dependent == :purge_later
before_destroy { public_send(name).purge_later }
end
@@ -38,6 +41,9 @@ module ActiveStorage::Attached::Macros
instance_variable_set("@active_storage_attached_#{name}", ActiveStorage::Attached::Many.new(name, self))
end
+ has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment"
+ has_many :"#{name}_blobs", through: :"#{name}_attachments"
+
if dependent == :purge_later
before_destroy { public_send(name).purge_later }
end
diff --git a/lib/active_storage/attached/many.rb b/lib/active_storage/attached/many.rb
index 99d980196a..129f166776 100644
--- a/lib/active_storage/attached/many.rb
+++ b/lib/active_storage/attached/many.rb
@@ -7,14 +7,14 @@ 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 ||= ActiveStorage::Attachment.where(record_gid: record.to_gid.to_s, name: 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|
- ActiveStorage::Attachment.create!(record_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable))
+ ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable))
end
end
diff --git a/lib/active_storage/attached/one.rb b/lib/active_storage/attached/one.rb
index 80e4cb6234..02fc9c9abc 100644
--- a/lib/active_storage/attached/one.rb
+++ b/lib/active_storage/attached/one.rb
@@ -7,13 +7,13 @@ 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 ||= ActiveStorage::Attachment.find_by(record_gid: record.to_gid.to_s, name: 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_gid: record.to_gid.to_s, name: name, blob: create_blob_from(attachable))
+ @attachment = ActiveStorage::Attachment.create!(record: record, name: name, blob: create_blob_from(attachable))
end
# Checks the presence of the attachment.
diff --git a/lib/active_storage/migration.rb b/lib/active_storage/migration.rb
index c56e7a1786..99d8b8554b 100644
--- a/lib/active_storage/migration.rb
+++ b/lib/active_storage/migration.rb
@@ -14,15 +14,16 @@ class ActiveStorageCreateTables < ActiveRecord::Migration[5.1] # :nodoc:
create_table :active_storage_attachments do |t|
t.string :name
- t.string :record_gid
+ t.string :record_type
+ t.integer :record_id
t.integer :blob_id
t.datetime :created_at
- t.index :record_gid
t.index :blob_id
- t.index [ :record_gid, :name ]
- t.index [ :record_gid, :blob_id ], unique: true
+ t.index [ :record_type, :record_id ]
+ t.index [ :record_type, :record_id, :name ], name: "index_active_storage_attachments_record_and_name"
+ t.index [ :record_type, :record_id, :blob_id ], name: "index_active_storage_attachments_uniqueness", unique: true
end
end
end
diff --git a/test/models/attachments_test.rb b/test/models/attachments_test.rb
index c0f5db819d..1a9fc6f932 100644
--- a/test/models/attachments_test.rb
+++ b/test/models/attachments_test.rb
@@ -68,6 +68,14 @@ class ActiveStorage::AttachmentsTest < ActiveSupport::TestCase
assert_equal "country.jpg", @user.highlights.second.filename.to_s
end
+ test "find attached 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" })
+
+ User.where(id: @user.id).includes(highlights_attachments: :blob).first
+ 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)