Skip to content

Commit 4ca61ff

Browse files
committed
Take AR affixes into account for AStorage models
All of ActiveStorage database modeltable nameshave been hard coded. Therefore, ActiveRecord::Base.(prefix|suffix) were not taken into consideration. To fix this we remove the hard coded lines. But then we need to also override an internal method for specifying the prefix because of a mystical ActiveRecord/ActiveStorage sync issue (Suffix does not appear to have the issue) Some tests were refactored to remove hard coded table name references, making ActiveStorage test suite compatible with ActiveRecord config.
1 parent c057eda commit 4ca61ff

File tree

5 files changed

+6
-7
lines changed

5 files changed

+6
-7
lines changed

activestorage/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Fix all Active Storage database related models to respect
2+
`ActiveRecord::Base.table_name_prefix` configuration.
3+
4+
*Chedli Bourguiba*
5+
16
* Fix `ActiveStorage::Representations::ProxyController` not returning the proper
27
preview image variant for previewable files.
38

activestorage/app/models/active_storage/attachment.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
# # preloads blobs and variant records (if using `ActiveStorage.track_variants`)
1919
# User.first.avatars.with_all_variant_records
2020
class ActiveStorage::Attachment < ActiveStorage::Record
21-
self.table_name = "active_storage_attachments"
22-
2321
##
2422
# :method:
2523
#

activestorage/app/models/active_storage/blob.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
# update a blob's metadata on a subsequent pass, but you should not update the key or change the uploaded file.
1818
# If you need to create a derivative or otherwise change the blob, simply create a new blob and purge the old one.
1919
class ActiveStorage::Blob < ActiveStorage::Record
20-
self.table_name = "active_storage_blobs"
21-
2220
MINIMUM_TOKEN_LENGTH = 28
2321

2422
has_secure_token :key, length: MINIMUM_TOKEN_LENGTH

activestorage/app/models/active_storage/variant_record.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
class ActiveStorage::VariantRecord < ActiveStorage::Record
4-
self.table_name = "active_storage_variant_records"
5-
64
belongs_to :blob
75
has_one_attached :image
86
end

activestorage/test/migrations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def rerun_migration
5353
end
5454

5555
def active_storage_tables
56-
[:active_storage_blobs, :active_storage_attachments, :active_storage_variant_records]
56+
@active_storage_tables ||= ActiveStorage::Record.descendants.map { |klass| klass.table_name.to_sym }
5757
end
5858

5959
def primary_key(table)

0 commit comments

Comments
 (0)