Skip to content

Commit

Permalink
Remove deprecated purge and purge_later methods from the attachme…
Browse files Browse the repository at this point in the history
…nts association
  • Loading branch information
rafaelfranca committed Mar 3, 2023
1 parent c720b7e commit 18e53fb
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 70 deletions.
4 changes: 4 additions & 0 deletions activestorage/CHANGELOG.md
@@ -1,3 +1,7 @@
* Remove deprecated `purge` and `purge_later` methods from the attachments association.

*Rafael Mendonça França*

* Remove deprecated behavior when assigning to a collection of attachments.

Instead of appending to the collection, the collection is now replaced.
Expand Down
24 changes: 1 addition & 23 deletions activestorage/lib/active_storage/attached/model.rb
Expand Up @@ -148,29 +148,7 @@ def #{name}=(attachables)
end
CODE

has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: :destroy, strict_loading: strict_loading do
def purge
deprecate(:purge)
each(&:purge)
reset
end

def purge_later
deprecate(:purge_later)
each(&:purge_later)
reset
end

private
def deprecate(action)
reflection_name = proxy_association.reflection.name
attached_name = reflection_name.to_s.partition("_").first
ActiveStorage.deprecator.warn(<<-MSG.squish)
Calling `#{action}` from `#{reflection_name}` is deprecated and will be removed in Rails 7.1.
To migrate to Rails 7.1's behavior call `#{action}` from `#{attached_name}` instead: `#{attached_name}.#{action}`.
MSG
end
end
has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: :destroy, strict_loading: strict_loading
has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob, strict_loading: strict_loading

scope :"with_attached_#{name}", -> {
Expand Down
47 changes: 0 additions & 47 deletions activestorage/test/models/attached/many_test.rb
Expand Up @@ -565,28 +565,6 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
end
end

test "purging from the attachments relation" do
[ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs|
@user.highlights.attach blobs
assert @user.highlights.attached?

message = <<-MSG.squish
Calling `purge` from `highlights_attachments` is deprecated and will be removed in Rails 7.1.
To migrate to Rails 7.1's behavior call `purge` from `highlights` instead: `highlights.purge`.
MSG
assert_deprecated(message, ActiveStorage.deprecator) do
assert_changes -> { @user.updated_at } do
@user.highlights_attachments.purge
end
end
assert_not @user.highlights.attached?
assert_not ActiveStorage::Blob.exists?(blobs.first.id)
assert_not ActiveStorage::Blob.exists?(blobs.second.id)
assert_not ActiveStorage::Blob.service.exist?(blobs.first.key)
assert_not ActiveStorage::Blob.service.exist?(blobs.second.key)
end
end

test "purging attachment with shared blobs" do
[
create_blob(filename: "funky.jpg"),
Expand Down Expand Up @@ -660,31 +638,6 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
end
end

test "purging later from the attachments relation" do
[ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs|
@user.highlights.attach blobs
assert @user.highlights.attached?

message = <<-MSG.squish
Calling `purge_later` from `highlights_attachments` is deprecated and will be removed in Rails 7.1.
To migrate to Rails 7.1's behavior call `purge_later` from `highlights` instead: `highlights.purge_later`.
MSG
assert_deprecated(message, ActiveStorage.deprecator) do
perform_enqueued_jobs do
assert_changes -> { @user.updated_at } do
@user.highlights_attachments.purge_later
end
end
end

assert_not @user.highlights.attached?
assert_not ActiveStorage::Blob.exists?(blobs.first.id)
assert_not ActiveStorage::Blob.exists?(blobs.second.id)
assert_not ActiveStorage::Blob.service.exist?(blobs.first.key)
assert_not ActiveStorage::Blob.service.exist?(blobs.second.key)
end
end

test "purging attachment later with shared blobs" do
[
create_blob(filename: "funky.jpg"),
Expand Down
2 changes: 2 additions & 0 deletions guides/source/7_1_release_notes.md
Expand Up @@ -127,6 +127,8 @@ Please refer to the [Changelog][active-storage] for detailed changes.
* Remove deprecated behavior when assigning to a collection of attachments. Instead of appending to the collection,
the collection is now replaced.

* Remove deprecated `purge` and `purge_later` methods from the attachments association.

### Deprecations

### Notable changes
Expand Down

0 comments on commit 18e53fb

Please sign in to comment.