-
Notifications
You must be signed in to change notification settings - Fork 22k
Preserve existing attachment assignment behavior for upgraded apps #36716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or 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
5321642
to
1cd1477
Compare
1cd1477
to
bb67f60
Compare
gmcgibbon
reviewed
Jul 19, 2019
8d239c3
to
ce31f2d
Compare
Assigning to a collection of attachments appends rather than replacing, as in 5.2. Existing 5.2 apps that rely on this behavior will no longer break when they're upgraded to 6.0. For apps generated on 6.0 or newer, assigning replaces the existing attachments in the collection. #attach should be used to add new attachments to the collection without removing existing ones. I expect that we'll deprecate the old behavior in 6.1. Closes #36374.
ce31f2d
to
5d1adb7
Compare
seanpdoyle
added a commit
to seanpdoyle/rails
that referenced
this pull request
Dec 14, 2021
The background --- Configuration for replacing a collection was introduced in [rails#36716][]. However, since [rails#42596][] has been merged, Rails 7.1 and beyond will default to _replacing_ an Active Storage `has_many_attached` relationship, as opposed to _appending to it_. The problem --- With replacement as the established precedent, it's currently a challenge to replace an existing collection with an empty one. The solution --- This commit makes two changes. The first is to Action View and its form building helpers. The change draws inspiration from how an `<input type="checkbox">` field (or collection of fields) is paired with an `<input type="hidden">` field to represent the unchecked value. The change pairs any `<input type="file" multiple="multiple">` elements with an `<input type="hidden">` element to represent an empty collection. Like the [check_box][] form builder method, the `file_field` method accepts an `include_hidden:` option to skip the creation of the hidden element. The second is to how Active Storage generates attribute assignment methods through `has_many_attached`. With the possibility of an `<input type="file">` field being paired with an `<input type="hidden" value="">` field, the backing models need to be able to coerce an "empty-ish" value into an empty list. For example: ```ruby @user.highlights = [""] @user.highlights # => [] ``` When combined, these changes enable consumer applications to submit "empty" collections to blank out existing attachments. Support is configured through the `config.active_storage.multiple_file_field_include_hidden` configuration value, which defaults to `false`. [check_box]: https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-check_box [rails#36716]: rails#36716 [rails#42596]: rails#42596
jyoun-godaddy
pushed a commit
to jyoun-godaddy/activestorage
that referenced
this pull request
Jul 5, 2022
The background --- Configuration for replacing a collection was introduced in [rails/rails#36716][]. However, since [rails/rails#42596][] has been merged, Rails 7.1 and beyond will default to _replacing_ an Active Storage `has_many_attached` relationship, as opposed to _appending to it_. The problem --- With replacement as the established precedent, it's currently a challenge to replace an existing collection with an empty one. The solution --- This commit makes two changes. The first is to Action View and its form building helpers. The change draws inspiration from how an `<input type="checkbox">` field (or collection of fields) is paired with an `<input type="hidden">` field to represent the unchecked value. The change pairs any `<input type="file" multiple="multiple">` elements with an `<input type="hidden">` element to represent an empty collection. Like the [check_box][] form builder method, the `file_field` method accepts an `include_hidden:` option to skip the creation of the hidden element. The second is to how Active Storage generates attribute assignment methods through `has_many_attached`. With the possibility of an `<input type="file">` field being paired with an `<input type="hidden" value="">` field, the backing models need to be able to coerce an "empty-ish" value into an empty list. For example: ```ruby @user.highlights = [""] @user.highlights # => [] ``` When combined, these changes enable consumer applications to submit "empty" collections to blank out existing attachments. Support is configured through the `config.active_storage.multiple_file_field_include_hidden` configuration value, which defaults to `false`. [check_box]: https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-check_box [rails/rails#36716]: rails/rails#36716 [rails/rails#42596]: rails/rails#42596
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Assigning a collection of attachments appends to the collection as it did in 5.2. Existing 5.2 apps that rely on this behavior will no longer break when they're upgraded to 6.0.
For apps generated on 6.0 or newer, assigning replaces the existing attachments in the collection. #attach should be used to add new attachments to the collection without removing existing ones.
I expect that we'll deprecate the old behavior in 6.1.
Closes #36374.
To-do: