rails--rails/activestorage/test
Sean Doyle 4f191b9754 ActiveStorage: support empty attachments submits
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]: https://github.com/rails/rails/pull/36716
[rails/rails#42596]: https://github.com/rails/rails/pull/42596
2021-12-14 18:40:35 -05:00
..
analyzer
controllers ✂️ 2021-12-06 22:39:44 +00:00
database
dummy Use dynamic Rails version in framework dummy apps 2021-12-08 11:31:49 -06:00
fixtures
jobs
models ActiveStorage: support empty attachments submits 2021-12-14 18:40:35 -05:00
previewer
service Pass an array through our stack, don't bother converting back & forth 2021-12-04 02:25:42 +01:00
template
urls
direct_upload_token_test.rb Support direct uploads to multiple services 2021-11-14 11:49:06 +03:00
engine_test.rb Active Storage: incorrect defaults 2021-09-22 16:38:52 -05:00
fixture_set_test.rb
javascript_package_test.rb
migrations_test.rb
test_helper.rb