Skip to content

Commit

Permalink
Deprecate replace_on_assign_to_many
Browse files Browse the repository at this point in the history
  • Loading branch information
santib committed Jun 28, 2021
1 parent 0ffdf6d commit 61a8cd6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions activestorage/CHANGELOG.md
@@ -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`.

Expand Down
6 changes: 6 additions & 0 deletions activestorage/lib/active_storage/attached/model.rb
Expand Up @@ -145,6 +145,12 @@ def #{name}=(attachables)
ActiveStorage::Attached::Changes::CreateMany.new("#{name}", self, attachables)
end
else
ActiveSupport::Deprecation.warn \

This comment has been minimized.

Copy link
@phoet

phoet Nov 18, 2022

Contributor

What's "the rails way" to handle attaching transparently? I dont understand the reasoning behind getting rid of this feature. It's what makes the direct-uploads usable. See this stack-overflow discussion https://stackoverflow.com/questions/71990425/rails-active-storage-keep-existing-files-uploads

"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)
Expand Down
15 changes: 15 additions & 0 deletions activestorage/test/models/attached/many_test.rb
Expand Up @@ -777,6 +777,21 @@ def highlights
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
Expand Down
2 changes: 1 addition & 1 deletion guides/source/upgrading_ruby_on_rails.md
Expand Up @@ -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
-------------------------------------
Expand Down

0 comments on commit 61a8cd6

Please sign in to comment.