Extract ActiveStorage::Record

Permit applications to hack in custom DB config for ASt models until ASt has first-class multi-DB support:

    ActiveSupport.on_load(:active_storage_record) do
      connects_to reading: :active_storage_replica, writing: :active_storage_primary
    end

rebase
This commit is contained in:
George Claghorn 2020-09-22 18:10:52 -04:00 committed by George Claghorn
parent 1e2cd7a75e
commit a50d2f1993
5 changed files with 15 additions and 4 deletions

View File

@ -7,7 +7,7 @@ require "active_support/core_ext/module/delegation"
# on the attachments table prevents blobs from being purged if theyre still attached to any records.
#
# Attachments also have access to all methods from {ActiveStorage::Blob}[rdoc-ref:ActiveStorage::Blob].
class ActiveStorage::Attachment < ActiveRecord::Base
class ActiveStorage::Attachment < ActiveStorage::Record
self.table_name = "active_storage_attachments"
belongs_to :record, polymorphic: true, touch: true

View File

@ -14,7 +14,7 @@
# Blobs are intended to be immutable in as-so-far as their reference to a specific file goes. You're allowed to
# update a blob's metadata on a subsequent pass, but you should not update the key or change the uploaded file.
# If you need to create a derivative or otherwise change the blob, simply create a new blob and purge the old one.
class ActiveStorage::Blob < ActiveRecord::Base
class ActiveStorage::Blob < ActiveStorage::Record
# We use constant paths in the following include calls to avoid a gotcha of
# classic mode: If the parent application defines a top-level Analyzable, for
# example, and ActiveStorage::Blob::Analyzable is not yet loaded, a bare

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class ActiveStorage::Record < ActiveRecord::Base #:nodoc:
self.abstract_class = true
end
ActiveSupport.run_load_hooks :active_storage_record, ActiveStorage::Record

View File

@ -1,6 +1,6 @@
# frozen_string_literal: true
class ActiveStorage::VariantRecord < ActiveRecord::Base
class ActiveStorage::VariantRecord < ActiveStorage::Record
self.table_name = "active_storage_variant_records"
belongs_to :blob

View File

@ -133,7 +133,11 @@ class LoadingTest < ActiveSupport::TestCase
require "#{rails_root}/config/environment"
setup_ar!
initial = [ActiveStorage::Blob, ActiveStorage::Attachment, ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata, ApplicationRecord].collect(&:to_s).sort
initial = [
ActiveStorage::Record, ActiveStorage::Blob, ActiveStorage::Attachment,
ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata, ApplicationRecord
].collect(&:to_s).sort
assert_equal initial, ActiveRecord::Base.descendants.collect(&:to_s).sort.uniq
get "/load"
assert_equal [Post].collect(&:to_s).sort, ActiveRecord::Base.descendants.collect(&:to_s).sort - initial