Skip to content

Commit

Permalink
Merge pull request carrierwaveuploader#2690 from rajyan/issue-2689-fix
Browse files Browse the repository at this point in the history
Fix mounter cache duplication on dup
  • Loading branch information
mshibuya authored Aug 28, 2023
2 parents 9216dfc + e53c9c3 commit 8815592
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/carrierwave/mount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ def write_uploader(column, identifier); end

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?
Expand Down
24 changes: 24 additions & 0 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1991,6 +1991,30 @@ def reload(*)
end
end

context 'with store_dir using model attributes' do
before do
@uploader.class_eval do
def store_dir
"uploads/#{model.class.name.downcase}/#{mounted_as}/#{model.id}"
end
end
end

it "wont affect the duplicated object's saved path" do
@event.image = stub_file('test.jpeg')
expect(@event.save).to be_truthy
expect(@event.image.current_path).to eq public_path("uploads/event/image/#{@event.id}/test.jpeg")

new_event = @event.dup
expect(new_event).not_to be @event
expect(new_event.image.model).to be new_event
expect(@event.image.current_path).to eq public_path("uploads/event/image/#{@event.id}/test.jpeg")

expect(@event.save).to be_truthy
expect(@event.image.current_path).to eq public_path("uploads/event/image/#{@event.id}/test.jpeg")
end
end

context 'when #initialize_dup is overridden in the model' do
before do
Event.class_eval do
Expand Down

0 comments on commit 8815592

Please sign in to comment.