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

Fix mounter cache duplication on dup #2690

Merged
merged 4 commits into from
Aug 28, 2023

Conversation

rajyan
Copy link
Contributor

@rajyan rajyan commented Aug 22, 2023

Added a failing test for #2689

duplication should not affect the duplicated objects path.

Updated

And now fixes #2689

duplication should not affect the duplicated objects path
expect(new_event).not_to be @event
expect(new_event.image.model).to be new_event
expect(@event.save).to be_truthy
expect(@event.image.current_path).to eq public_path("uploads/event/image/#{@event.id}/test.jpeg")
Copy link
Contributor Author

@rajyan rajyan Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, current_path returns "uploads/event/image/test.jpeg" after the second save.

Copy link
Contributor Author

@rajyan rajyan Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, expect(File.exist?(public_path("uploads/event/image/test.jpeg"))).to be_truthty passes, so the files is saved to this path.

@rajyan
Copy link
Contributor Author

rajyan commented Aug 23, 2023

@mshibuya
Hope that I've understood the code enough.
I've read this (japanese) article https://qiita.com/QUANON/items/287ff2c5c72c93c2ff07 and think this might be the cause.

@rajyan rajyan changed the title add failing test for https://github.com/carrierwaveuploader/carrierwave/issues/2689 Fix mounter cache duplication on dup Aug 23, 2023
@@ -43,6 +43,7 @@ def reload(*)
# Reset cached mounter on record dup
def initialize_dup(other)
old_uploaders = _mounter(:"#{column}").uploaders
@_mounters = @_mounters.dup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR! Your fix works, but let me propose a slight modification...

As was discussed in #1962 (comment), this will be executed for every mount_uploader / mount_uploaders, not just once for a ActiveRecord instance. If we move this to CarrierWave::Mount::Extension like

--- a/lib/carrierwave/mount.rb
+++ b/lib/carrierwave/mount.rb
@@ -428,6 +428,11 @@ module CarrierWave

     private

+      def initialize_dup(other)
+        @_mounters =  @_mounters.dup
+        super
+      end
+
       def _mounter(column)
         # We cannot memoize in frozen objects :(
         return Mounter.build(self, column) if frozen?

I think we can resolve that mismatch. What do you think?

Copy link
Contributor Author

@rajyan rajyan Aug 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestion! This definitely looks like a better solution.

@mshibuya mshibuya merged commit 8815592 into carrierwaveuploader:master Aug 28, 2023
13 checks passed
@mshibuya
Copy link
Member

Thank you so much 👍

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

Successfully merging this pull request may close these issues.

Can't get model.id in store_dir
2 participants