Skip to content

Commit

Permalink
Merge pull request #42596 from santib/deprecate-replace-on-assign-to-…
Browse files Browse the repository at this point in the history
…many

Deprecate replace_on_assign_to_many
  • Loading branch information
pixeltrix committed Jun 28, 2021
2 parents 21243aa + 61a8cd6 commit 5f7c5ad
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ def #{name}=(attachables)
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)
Expand Down
15 changes: 15 additions & 0 deletions activestorage/test/models/attached/many_test.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 5f7c5ad

Please sign in to comment.