Deprecate replace_on_assign_to_many

This commit is contained in:
Santiago Bartesaghi 2021-06-24 20:47:21 -03:00
parent 0ffdf6dfe7
commit 61a8cd6c8f
4 changed files with 27 additions and 1 deletions

View File

@ -1,3 +1,8 @@
* Deprecate `config.active_storage.replace_on_assign_to_many`. Future versions of Rails
will behave the same way as when the config is set to `true`.
*Santiago Bartesaghi*
* Remove deprecated methods: `build_after_upload`, `create_after_upload!` in favor of `create_and_upload!`,
and `service_url` in favor of `url`.

View File

@ -145,6 +145,12 @@ module ActiveStorage
ActiveStorage::Attached::Changes::CreateMany.new("#{name}", self, attachables)
end
else
ActiveSupport::Deprecation.warn \
"config.active_storage.replace_on_assign_to_many is deprecated and will be removed in Rails 7.1. " \
"Make sure that your code works well with config.active_storage.replace_on_assign_to_many set to true before upgrading. " \
"To append new attachables to the Active Storage association, prefer using `attach`. " \
"Using association setter would result in purging the existing attached attachments and replacing them with new ones."
if Array(attachables).any?
attachment_changes["#{name}"] =
ActiveStorage::Attached::Changes::CreateMany.new("#{name}", self, #{name}.blobs + attachables)

View File

@ -777,6 +777,21 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
end
end
test "deprecation warning when replace_on_assign_to_many is false" do
append_on_assign do
message = <<-MSG.squish
DEPRECATION WARNING: config.active_storage.replace_on_assign_to_many is deprecated and will be removed in Rails 7.1.
Make sure that your code works well with config.active_storage.replace_on_assign_to_many set to true before upgrading.
To append new attachables to the Active Storage association, prefer using `attach`.
Using association setter would result in purging the existing attached attachments and replacing them with new ones.
MSG
assert_deprecated(message) do
@user.update! highlights: [create_blob(filename: "whenever.jpg")]
end
end
end
private
def append_on_assign
ActiveStorage.replace_on_assign_to_many, previous = false, ActiveStorage.replace_on_assign_to_many

View File

@ -697,7 +697,7 @@ user.highlights.first.filename # => "funky.jpg"
user.highlights.second.filename # => "town.jpg"
```
Existing applications can opt in to this new behavior by setting `config.active_storage.replace_on_assign_to_many` to `true`. The old behavior will be deprecated in Rails 6.1 and removed in a subsequent release.
Existing applications can opt in to this new behavior by setting `config.active_storage.replace_on_assign_to_many` to `true`. The old behavior will be deprecated in Rails 7.0 and removed in Rails 7.1.
Upgrading from Rails 5.1 to Rails 5.2
-------------------------------------