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 dup mounter cache behavior #2706

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/carrierwave/orm/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def reload(*)
# Reset cached mounter on record dup
def initialize_dup(other)
old_uploaders = _mounter(:"#{column}").uploaders
@_mounters[:"#{column}"] = nil
super
@_mounters[:"#{column}"] = nil
# The attribute needs to be cleared to prevent it from picked up as identifier
write_attribute(_mounter(:#{column}).serialization_column, nil)
_mounter(:"#{column}").cache(old_uploaders)
Expand Down
27 changes: 27 additions & 0 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,10 @@ def reload(*)
Event.mount_uploader(:image, @uploader)
end

after do
FileUtils.rm_rf(public_path("uploads"))
end

it "caches the existing file into the new model" do
@event.image = stub_file('test.jpeg')
@event.save
Expand Down Expand Up @@ -1956,6 +1960,15 @@ def reload(*)
expect { @event.dup }.not_to change { @event[:image] }
end

it "can upload a file" do
rajyan marked this conversation as resolved.
Show resolved Hide resolved
@event.image = stub_file('test.jpeg')
@event.dup

expect(@event.save).to be_truthy
expect(@event.image.path).to eq public_path('uploads/test.jpeg')
expect(File.exist?(@event.image.path)).to be_truthy
end

context "with more than one mount" do
before do
@uploader1 = Class.new(CarrierWave::Uploader::Base)
Expand Down Expand Up @@ -2013,6 +2026,20 @@ def store_dir
expect(@event.save).to be_truthy
expect(@event.image.current_path).to eq public_path("uploads/event/image/#{@event.id}/test.jpeg")
end

it "upload files to appropriate paths" do
@event.image = stub_file('test.jpeg')
new_event = @event.dup
expect(new_event).not_to be @event

expect(@event.save).to be_truthy
expect(@event.image.path).to eq public_path("uploads/event/image/#{@event.id}/test.jpeg")
expect(File.exist?(@event.image.path)).to be_truthy

expect(new_event.save).to be_truthy
expect(new_event.image.path).to eq public_path("uploads/event/image/#{new_event.id}/test.jpeg")
expect(File.exist?(new_event.image.path)).to be_truthy
end
end

context 'when #initialize_dup is overridden in the model' do
Expand Down