forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rails#42383 from intrip/has-many-attached-attachme…
…nt-not-persisted Fix ActiveStorage has_many_attached when record is not persisted
- Loading branch information
Showing
9 changed files
with
149 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
activestorage/lib/active_storage/attached/changes/purge_many.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveStorage | ||
class Attached::Changes::PurgeMany #:nodoc: | ||
attr_reader :name, :record, :attachments | ||
|
||
def initialize(name, record, attachments) | ||
@name, @record, @attachments = name, record, attachments | ||
end | ||
|
||
def purge | ||
attachments.each(&:purge) | ||
reset | ||
end | ||
|
||
def purge_later | ||
attachments.each(&:purge_later) | ||
reset | ||
end | ||
|
||
private | ||
def reset | ||
record.attachment_changes.delete(name) | ||
record.public_send("#{name}_attachments").reset | ||
end | ||
end | ||
end |
27 changes: 27 additions & 0 deletions
27
activestorage/lib/active_storage/attached/changes/purge_one.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveStorage | ||
class Attached::Changes::PurgeOne #:nodoc: | ||
attr_reader :name, :record, :attachment | ||
|
||
def initialize(name, record, attachment) | ||
@name, @record, @attachment = name, record, attachment | ||
end | ||
|
||
def purge | ||
attachment&.purge | ||
reset | ||
end | ||
|
||
def purge_later | ||
attachment&.purge_later | ||
reset | ||
end | ||
|
||
private | ||
def reset | ||
record.attachment_changes.delete(name) | ||
record.public_send("#{name}_attachment=", nil) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters