Methods ".exists?" and 'metadata filename' feel unnatural. Am I missing something?

I am still experimenting with shrine. I think you’ve done a great job, thank you.

There are two things that feel unnatural and probably I am missing something and I wanted to check if that is the case:

  1. I am constantly doing
content_picture.image && content_picture.image.exists?

Is this by design? Is there a way to check just with content_picture.image.exists?

  1. Image name
content_picture.image_data["metadata"]["filename"]

This is different from

content_picture.image_url

So the url is accessed in one way but the filename through metadata. I understand that it is a json, but it just seems strange. Is there an image_name that I am missing somewhere? Probably in a helper or something?

Hi

Thank you on behalf of Janko.

content_picture.image && content_picture.image.exists?
Is this by design? Is there a way to check just with content_picture.image.exists?

This is a Ruby design due to use of method chaining to avoid MethodError when image is nil.

content_picture.image_data["metadata"]["filename"]
Is there an image_name that I am missing somewhere? Probably in a helper or something?

There is a helper on the UploadedFile object: content_picture.image.original_filename
See Accessing Metadata

1 Like

Thank you.

Accessing Metadata is exactly what I needed.

You could simply do this instead::

if content_picture.image # => returns nil if the image does not exist