uploadcare / uploadcare-rails

Rails API client that handles uploads and further operations with files by wrapping Uploadcare Upload and REST APIs.
https://uploadcare.com
MIT License
58 stars 70 forks source link

mount_uploadcare_file makes network request every time model is saved #127

Closed jackc closed 1 year ago

jackc commented 1 year ago

Describe the bug

Given an ActiveRecord model that has a mount_uploadcare_file attribute, a Uploadcare::FileApi.store_file is called on every save even when the mount_uploadcare_file attribute has not changed.

I would not expect a network request to be triggered when saving a model when the attribute hasn't changed.

The culprit seems to be:

https://github.com/uploadcare/uploadcare-rails/blob/428a7e64153449b85e587c2aedf9f6df130b8cb8/lib/uploadcare/rails/active_record/mount_uploadcare_file.rb#L50

and

https://github.com/uploadcare/uploadcare-rails/blob/428a7e64153449b85e587c2aedf9f6df130b8cb8/lib/uploadcare/rails/active_record/mount_uploadcare_file.rb#L34-L40

I presume this could be easily fixed by checking if the attribute has changed and only then making the API call, but perhaps the current behavior is handling some edge case I am not aware of.

rsedykh commented 1 year ago

Hi @jackc! Would you mind reviewing this one https://github.com/uploadcare/uploadcare-rails/pull/128 ?

jackc commented 1 year ago

It works for me locally.