rails--rails/activestorage/test/models
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
..
attached ActiveStorage: support empty attachments submits 2021-12-14 18:40:35 -05:00
attachment_test.rb Enable `Style/ExplicitBlockArgument` cop 2021-09-05 17:06:19 +02:00
blob_test.rb compose is primarily meant to take blobs so make them a positional argument 2021-12-04 02:14:48 +01:00
filename_test.rb
presence_validation_test.rb
preview_test.rb Make vips the default variant processor for new apps 2021-07-14 14:26:43 -03:00
reflection_test.rb Named variants should be defined using block syntax 2020-05-04 13:04:14 +03:00
representation_test.rb Make vips the default variant processor for new apps 2021-07-14 14:26:43 -03:00
strict_loading_test.rb Add strict loading for active storage 2020-11-16 22:09:15 +08:00
variant_test.rb Active Storage: incorrect defaults 2021-09-22 16:38:52 -05:00
variant_with_record_test.rb Make vips the default variant processor for new apps 2021-07-14 14:26:43 -03:00