Skip to content

Commit b75fc7b

Browse files
Merge pull request rails#46396 from jonathanhefner/active_storage-deprecator
Add `ActiveStorage.deprecator`
2 parents a617fa8 + 07cd032 commit b75fc7b

File tree

11 files changed

+34
-21
lines changed

11 files changed

+34
-21
lines changed

activestorage/app/models/active_storage/blob.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,15 @@ def service
345345
def content_type=(value)
346346
unless ActiveStorage.silence_invalid_content_types_warning
347347
if INVALID_VARIABLE_CONTENT_TYPES_DEPRECATED_IN_RAILS_7.include?(value)
348-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
348+
ActiveStorage.deprecator.warn(<<-MSG.squish)
349349
#{value} is not a valid content type, it should not be used when creating a blob, and support for it will be removed in Rails 7.1.
350350
If you want to keep supporting this content type past Rails 7.1, add it to `config.active_storage.variable_content_types`.
351351
Dismiss this warning by setting `config.active_storage.silence_invalid_content_types_warning = true`.
352352
MSG
353353
end
354354

355355
if INVALID_VARIABLE_CONTENT_TYPES_TO_SERVE_AS_BINARY_DEPRECATED_IN_RAILS_7.include?(value)
356-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
356+
ActiveStorage.deprecator.warn(<<-MSG.squish)
357357
#{value} is not a valid content type, it should not be used when creating a blob, and support for it will be removed in Rails 7.1.
358358
If you want to keep supporting this content type past Rails 7.1, add it to `config.active_storage.content_types_to_serve_as_binary`.
359359
Dismiss this warning by setting `config.active_storage.silence_invalid_content_types_warning = true`.

activestorage/app/models/active_storage/current.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ class ActiveStorage::Current < ActiveSupport::CurrentAttributes # :nodoc:
44
attribute :url_options
55

66
def host=(host)
7-
ActiveSupport::Deprecation.warn("ActiveStorage::Current.host= is deprecated, instead use ActiveStorage::Current.url_options=")
7+
ActiveStorage.deprecator.warn("ActiveStorage::Current.host= is deprecated, instead use ActiveStorage::Current.url_options=")
88
self.url_options = { host: host }
99
end
1010

1111
def host
12-
ActiveSupport::Deprecation.warn("ActiveStorage::Current.host is deprecated, instead use ActiveStorage::Current.url_options")
12+
ActiveStorage.deprecator.warn("ActiveStorage::Current.host is deprecated, instead use ActiveStorage::Current.url_options")
1313
self.url_options&.dig(:host)
1414
end
1515
end

activestorage/lib/active_storage.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
require "active_support/core_ext/numeric/time"
3030

3131
require "active_storage/version"
32+
require "active_storage/deprecator"
3233
require "active_storage/errors"
3334

3435
require "marcel"

activestorage/lib/active_storage/attached/model.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def #{name}=(attachables)
148148
ActiveStorage::Attached::Changes::CreateMany.new("#{name}", self, attachables, pending_uploads: pending_uploads)
149149
end
150150
else
151-
ActiveSupport::Deprecation.warn \
151+
ActiveStorage.deprecator.warn \
152152
"config.active_storage.replace_on_assign_to_many is deprecated and will be removed in Rails 7.1. " \
153153
"Make sure that your code works well with config.active_storage.replace_on_assign_to_many set to true before upgrading. " \
154154
"To append new attachables to the Active Storage association, prefer using `attach`. " \
@@ -179,7 +179,7 @@ def purge_later
179179
def deprecate(action)
180180
reflection_name = proxy_association.reflection.name
181181
attached_name = reflection_name.to_s.partition("_").first
182-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
182+
ActiveStorage.deprecator.warn(<<-MSG.squish)
183183
Calling `#{action}` from `#{reflection_name}` is deprecated and will be removed in Rails 7.1.
184184
To migrate to Rails 7.1's behavior call `#{action}` from `#{attached_name}` instead: `#{attached_name}.#{action}`.
185185
MSG
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveStorage
4+
def self.deprecator # :nodoc:
5+
@deprecator ||= ActiveSupport::Deprecation.new
6+
end
7+
end

activestorage/lib/active_storage/engine.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class Engine < Rails::Engine # :nodoc:
8282

8383
config.eager_load_namespaces << ActiveStorage
8484

85+
initializer "active_storage.deprecator" do |app|
86+
app.deprecators[:active_storage] = ActiveStorage.deprecator
87+
end
88+
8589
initializer "active_storage.configs" do
8690
config.after_initialize do |app|
8791
ActiveStorage.logger = app.config.active_storage.logger || Rails.logger

activestorage/test/models/attached/many_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
207207

208208
test "attaching new blobs within a transaction with append_on_assign config uploads all the files" do
209209
append_on_assign do
210-
assert_deprecated do
210+
assert_deprecated(ActiveStorage.deprecator) do
211211
ActiveRecord::Base.transaction do
212212
@user.highlights.attach fixture_file_upload("racecar.jpg")
213213
@user.highlights.attach fixture_file_upload("video.mp4")
@@ -223,7 +223,7 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
223223

224224
test "attaching new blobs within a transaction with append_on_assign config create the exact amount of records" do
225225
append_on_assign do
226-
assert_deprecated do
226+
assert_deprecated(ActiveStorage.deprecator) do
227227
assert_difference -> { ActiveStorage::Blob.count }, +2 do
228228
ActiveRecord::Base.transaction do
229229
@user.highlights.attach fixture_file_upload("racecar.jpg")
@@ -414,7 +414,7 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
414414

415415
test "updating an existing record with attachments when appending on assign" do
416416
append_on_assign do
417-
assert_deprecated do
417+
assert_deprecated(ActiveStorage.deprecator) do
418418
@user.highlights.attach create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg")
419419

420420
assert_difference -> { @user.reload.highlights.count }, +2 do
@@ -605,7 +605,7 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
605605
Calling `purge` from `highlights_attachments` is deprecated and will be removed in Rails 7.1.
606606
To migrate to Rails 7.1's behavior call `purge` from `highlights` instead: `highlights.purge`.
607607
MSG
608-
assert_deprecated(message) do
608+
assert_deprecated(message, ActiveStorage.deprecator) do
609609
assert_changes -> { @user.updated_at } do
610610
@user.highlights_attachments.purge
611611
end
@@ -700,7 +700,7 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
700700
Calling `purge_later` from `highlights_attachments` is deprecated and will be removed in Rails 7.1.
701701
To migrate to Rails 7.1's behavior call `purge_later` from `highlights` instead: `highlights.purge_later`.
702702
MSG
703-
assert_deprecated(message) do
703+
assert_deprecated(message, ActiveStorage.deprecator) do
704704
perform_enqueued_jobs do
705705
assert_changes -> { @user.updated_at } do
706706
@user.highlights_attachments.purge_later
@@ -963,7 +963,7 @@ def highlights
963963

964964
test "successfully attaches new blobs and destroys attachments marked for destruction via nested attributes" do
965965
append_on_assign do
966-
assert_deprecated do
966+
assert_deprecated(ActiveStorage.deprecator) do
967967
town_blob = create_blob(filename: "town.jpg")
968968
@user.highlights.attach(town_blob)
969969
@user.reload
@@ -991,7 +991,7 @@ def highlights
991991
Using association setter would result in purging the existing attached attachments and replacing them with new ones.
992992
MSG
993993

994-
assert_deprecated(message) do
994+
assert_deprecated(message, ActiveStorage.deprecator) do
995995
@user.update! highlights: [create_blob(filename: "whenever.jpg")]
996996
end
997997
end

activestorage/test/models/blob_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,15 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
349349
end
350350

351351
test "warning if blob is created with invalid mime type" do
352-
assert_deprecated do
352+
assert_deprecated(ActiveStorage.deprecator) do
353353
create_blob(filename: "funky.jpg", content_type: "image/jpg")
354354
end
355355

356-
assert_not_deprecated do
356+
assert_not_deprecated(ActiveStorage.deprecator) do
357357
create_blob(filename: "funky.jpg", content_type: "image/jpeg")
358358
end
359359

360-
assert_not_deprecated do
360+
assert_not_deprecated(ActiveStorage.deprecator) do
361361
create_file_blob(filename: "colors.bmp", content_type: "image/bmp")
362362
end
363363
end
@@ -366,15 +366,15 @@ class ActiveStorage::BlobTest < ActiveSupport::TestCase
366366
warning_was = ActiveStorage.silence_invalid_content_types_warning
367367
ActiveStorage.silence_invalid_content_types_warning = true
368368

369-
assert_not_deprecated do
369+
assert_not_deprecated(ActiveStorage.deprecator) do
370370
create_blob(filename: "funky.jpg", content_type: "image/jpg")
371371
end
372372

373-
assert_not_deprecated do
373+
assert_not_deprecated(ActiveStorage.deprecator) do
374374
create_blob(filename: "funky.jpg", content_type: "image/jpeg")
375375
end
376376

377-
assert_not_deprecated do
377+
assert_not_deprecated(ActiveStorage.deprecator) do
378378
create_file_blob(filename: "colors.bmp", content_type: "image/bmp")
379379
end
380380

activestorage/test/models/variant_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class ActiveStorage::VariantTest < ActiveSupport::TestCase
201201
blob = create_file_blob(filename: "racecar.jpg")
202202

203203
# image/jpg is not recognised by mini_mime (image/jpeg is correct)
204-
assert_deprecated do
204+
assert_deprecated(ActiveStorage.deprecator) do
205205
blob.update(content_type: "image/jpg")
206206
end
207207

activestorage/test/service/disk_service_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ActiveStorage::Service::DiskServiceTest < ActiveSupport::TestCase
5151

5252
test "URL generation keeps working with ActiveStorage::Current.host set" do
5353
ActiveStorage::Current.url_options = nil
54-
assert_deprecated { ActiveStorage::Current.host = "https://example.com" }
54+
assert_deprecated(ActiveStorage.deprecator) { ActiveStorage::Current.host = "https://example.com" }
5555

5656
original_url_options = Rails.application.routes.default_url_options.dup
5757
Rails.application.routes.default_url_options.merge!(protocol: "http", host: "test.example.com", port: 3001)

0 commit comments

Comments
 (0)