Skip to content

Commit e2585a2

Browse files
committed
Merge pull request rails#42506
2 parents b307450 + c8c967b commit e2585a2

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

activestorage/CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Deprecate usage of `purge` and `purge_later` from the association extension.
2+
3+
*Jacopo Beschi*
4+
15
* Passing extra parameters in `ActiveStorage::Blob#url` to S3 Client
26

37
This allows calls of `ActiveStorage::Blob#url` to have more interaction with
@@ -14,13 +18,13 @@
1418
*josegomezr*
1519

1620
* Allow setting a `Cache-Control` on files uploaded to GCS.
17-
21+
1822
```yaml
1923
gcs:
2024
service: GCS
2125
...
2226
cache_control: "public, max-age=3600"
23-
```
27+
```
2428
*maleblond*
2529

2630
* The parameters sent to `ffmpeg` for generating a video preview image are now

activestorage/lib/active_storage/attached/model.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,26 @@ def #{name}=(attachables)
155155

156156
has_many :"#{name}_attachments", -> { where(name: name) }, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: :destroy, strict_loading: strict_loading do
157157
def purge
158+
deprecate(:purge)
158159
each(&:purge)
159160
reset
160161
end
161162

162163
def purge_later
164+
deprecate(:purge_later)
163165
each(&:purge_later)
164166
reset
165167
end
168+
169+
private
170+
def deprecate(action)
171+
reflection_name = proxy_association.reflection.name
172+
attached_name = reflection_name.to_s.partition("_").first
173+
ActiveSupport::Deprecation.warn(<<-MSG.squish)
174+
Calling `#{action}` from `#{reflection_name}` is deprecated and will be removed in Rails 7.1.
175+
To migrate to Rails 7.1's behavior call `#{action}` from `#{attached_name}` instead: `#{attached_name}.#{action}`.
176+
MSG
177+
end
166178
end
167179
has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob, strict_loading: strict_loading
168180

activestorage/test/models/attached/many_test.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,28 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
456456
end
457457
end
458458

459+
test "purging from the attachments relation" do
460+
[ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs|
461+
@user.highlights.attach blobs
462+
assert @user.highlights.attached?
463+
464+
message = <<-MSG.squish
465+
Calling `purge` from `highlights_attachments` is deprecated and will be removed in Rails 7.1.
466+
To migrate to Rails 7.1's behavior call `purge` from `highlights` instead: `highlights.purge`.
467+
MSG
468+
assert_deprecated(message) do
469+
assert_changes -> { @user.updated_at } do
470+
@user.highlights_attachments.purge
471+
end
472+
end
473+
assert_not @user.highlights.attached?
474+
assert_not ActiveStorage::Blob.exists?(blobs.first.id)
475+
assert_not ActiveStorage::Blob.exists?(blobs.second.id)
476+
assert_not ActiveStorage::Blob.service.exist?(blobs.first.key)
477+
assert_not ActiveStorage::Blob.service.exist?(blobs.second.key)
478+
end
479+
end
480+
459481
test "purging attachment with shared blobs" do
460482
[
461483
create_blob(filename: "funky.jpg"),
@@ -529,6 +551,31 @@ class ActiveStorage::ManyAttachedTest < ActiveSupport::TestCase
529551
end
530552
end
531553

554+
test "purging later from the attachments relation" do
555+
[ create_blob(filename: "funky.jpg"), create_blob(filename: "town.jpg") ].tap do |blobs|
556+
@user.highlights.attach blobs
557+
assert @user.highlights.attached?
558+
559+
message = <<-MSG.squish
560+
Calling `purge_later` from `highlights_attachments` is deprecated and will be removed in Rails 7.1.
561+
To migrate to Rails 7.1's behavior call `purge_later` from `highlights` instead: `highlights.purge_later`.
562+
MSG
563+
assert_deprecated(message) do
564+
perform_enqueued_jobs do
565+
assert_changes -> { @user.updated_at } do
566+
@user.highlights_attachments.purge_later
567+
end
568+
end
569+
end
570+
571+
assert_not @user.highlights.attached?
572+
assert_not ActiveStorage::Blob.exists?(blobs.first.id)
573+
assert_not ActiveStorage::Blob.exists?(blobs.second.id)
574+
assert_not ActiveStorage::Blob.service.exist?(blobs.first.key)
575+
assert_not ActiveStorage::Blob.service.exist?(blobs.second.key)
576+
end
577+
end
578+
532579
test "purging attachment later with shared blobs" do
533580
[
534581
create_blob(filename: "funky.jpg"),

0 commit comments

Comments
 (0)