Why data_uri should be assigned before it can be read in record

When I take existing model with assigned file, I expect to get file_data_uri with base64, while using plugin :data_uri. Instead it returns nothing, as first of all attacher must assign a data so the data could be returned from the reader.

Can you please explain this behaviour?

I expect file_data_uri returns existing file data, so I can return it via to_json(methods: [:file_data_uri]).

System configuration

Ruby version: 2.6.5
Shrine version: 3.0.1

PS. Attacher data_uri writer changes chached version, so it could be updated once the record is saved, but why there are no possibility to read data directly from the file in case if there were no new changes?

So expected:

        def data_uri
          @data_uri || file.data_uri

The Attacher#data_uri attribute is meant only for attaching the file as a data URI. The attr reader returns the assigned data URI in case there were validations errors and the data URI needs to be re-submitted.

As you’ve already found, there is a way to retrieve the data URI of an uploaded file, which is via UploadedFile#data_uri. This is very different than the Attacher#data_uri attribute, because this one actually downloads the file from the storage and generates a data URI from it. So, it makes sense that this functionality is in a different method.

So, just replace