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 Instrument ActiveStorage analyzers 2021-08-17 16:55:05 +09:00
controllers ✂️ 2021-12-06 22:39:44 +00:00
database Fix ActiveStorage tests for load_defaults 7.0 2021-06-21 07:44:33 +09:00
dummy Use dynamic Rails version in framework dummy apps 2021-12-08 11:31:49 -06:00
fixtures Reduce file size of tiff image in Active Storage 2021-08-12 11:37:44 -03:00
jobs Correct test class name 2019-09-28 10:31:26 -04:00
models ActiveStorage: support empty attachments submits 2021-12-14 18:40:35 -05:00
previewer Active Storage: raise PreviewError when a preview cannot be generated 2021-02-26 14:06:52 -05:00
service Pass an array through our stack, don't bother converting back & forth 2021-12-04 02:25:42 +01:00
template Make vips the default variant processor for new apps 2021-07-14 14:26:43 -03:00
urls Make vips the default variant processor for new apps 2021-07-14 14:26:43 -03:00
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 Fix Flaky ActiveStorage test (#41225) 2021-01-24 18:29:11 +01:00
javascript_package_test.rb Enforce that activestorage compiled JS is in sync with source code 2019-10-18 18:02:02 -07:00
migrations_test.rb Respect Active Record's primary_key_type in Active Storage migrations 2021-06-11 11:51:07 +03:00
test_helper.rb Depend on ruby/debug, replacing Byebug 2021-09-08 17:35:41 +02:00