Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mount_uploadcare_file makes network request every time model is saved #127

Closed
jackc opened this issue Mar 31, 2023 · 2 comments · Fixed by #128
Closed

mount_uploadcare_file makes network request every time model is saved #127

jackc opened this issue Mar 31, 2023 · 2 comments · Fixed by #128
Assignees
Labels

Comments

@jackc
Copy link
Contributor

jackc commented Mar 31, 2023

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:

after_save "uploadcare_store_#{attribute}!".to_sym unless Uploadcare::Rails.configuration.do_not_store

and

define_method "uploadcare_store_#{attribute}!" do |store_job = StoreFileJob|
file_uuid = public_send(attribute)&.uuid
return unless file_uuid
return store_job.perform_later(file_uuid) if Uploadcare::Rails.configuration.store_files_async
Uploadcare::FileApi.store_file(file_uuid)
end

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
Copy link
Member

rsedykh commented Apr 1, 2023

Hi @jackc! Would you mind reviewing this one #128 ?

@jackc
Copy link
Contributor Author

jackc commented Apr 3, 2023

It works for me locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants