Skip to content

Commit 5f7c5ad

Browse files
authored
Merge pull request rails#42596 from santib/deprecate-replace-on-assign-to-many
Deprecate replace_on_assign_to_many
2 parents 21243aa + 61a8cd6 commit 5f7c5ad

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

activestorage/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Deprecate `config.active_storage.replace_on_assign_to_many`. Future versions of Rails
2+
will behave the same way as when the config is set to `true`.
3+
4+
*Santiago Bartesaghi*
5+
16
* Remove deprecated methods: `build_after_upload`, `create_after_upload!` in favor of `create_and_upload!`,
27
and `service_url` in favor of `url`.
38

activestorage/lib/active_storage/attached/model.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ def #{name}=(attachables)
145145
ActiveStorage::Attached::Changes::CreateMany.new("#{name}", self, attachables)
146146
end
147147
else
148+
ActiveSupport::Deprecation.warn \
149+
"config.active_storage.replace_on_assign_to_many is deprecated and will be removed in Rails 7.1. " \
150+
"Make sure that your code works well with config.active_storage.replace_on_assign_to_many set to true before upgrading. " \
151+
"To append new attachables to the Active Storage association, prefer using `attach`. " \
152+
"Using association setter would result in purging the existing attached attachments and replacing them with new ones."
153+
148154
if Array(attachables).any?
149155
attachment_changes["#{name}"] =
150156
ActiveStorage::Attached::Changes::CreateMany.new("#{name}", self, #{name}.blobs + attachables)

activestorage/test/models/attached/many_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,21 @@ def highlights
777777
end
778778
end
779779

780+
test "deprecation warning when replace_on_assign_to_many is false" do
781+
append_on_assign do
782+
message = <<-MSG.squish
783+
DEPRECATION WARNING: config.active_storage.replace_on_assign_to_many is deprecated and will be removed in Rails 7.1.
784+
Make sure that your code works well with config.active_storage.replace_on_assign_to_many set to true before upgrading.
785+
To append new attachables to the Active Storage association, prefer using `attach`.
786+
Using association setter would result in purging the existing attached attachments and replacing them with new ones.
787+
MSG
788+
789+
assert_deprecated(message) do
790+
@user.update! highlights: [create_blob(filename: "whenever.jpg")]
791+
end
792+
end
793+
end
794+
780795
private
781796
def append_on_assign
782797
ActiveStorage.replace_on_assign_to_many, previous = false, ActiveStorage.replace_on_assign_to_many

guides/source/upgrading_ruby_on_rails.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ user.highlights.first.filename # => "funky.jpg"
697697
user.highlights.second.filename # => "town.jpg"
698698
```
699699

700-
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.
700+
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.
701701

702702
Upgrading from Rails 5.1 to Rails 5.2
703703
-------------------------------------

0 commit comments

Comments
 (0)