Skip to content

Commit 0f95a6c

Browse files
authored
Merge pull request #215 from carrierwaveuploader/carrierwave-3
Support CarrierWave 3.x
2 parents 2ce2f4f + 0e78d71 commit 0f95a6c

8 files changed

Lines changed: 356 additions & 222 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ jobs:
88
mongodb: [4.4]
99
ruby: [2.7, "3.0", 3.1, 3.2, 3.3]
1010
gemfile:
11-
- carrierwave-2.1
1211
- carrierwave-2.2
12+
- carrierwave-3.1
1313
- mongoid-7
1414
- mongoid-8
1515
- mongoid-9
@@ -20,6 +20,8 @@ jobs:
2020
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-1.2" }
2121
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-1.3" }
2222
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-2.0" }
23+
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-2.1" }
24+
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-3.0" }
2325
- { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-3" }
2426
- { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-4" }
2527
- { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-5" }
@@ -32,6 +34,8 @@ jobs:
3234
uses: supercharge/mongodb-github-action@1.3.0
3335
with:
3436
mongodb-version: ${{ matrix.mongodb }}
37+
- name: Install ImageMagick
38+
run: sudo apt-get -y install imagemagick
3539
- uses: actions/checkout@v4
3640
- name: Set up Ruby
3741
uses: ruby/setup-ruby@v1

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pkg/*
33
*.sw*
44
.bundle
55
spec/public
6+
spec/tmp
67
.rvmrc
78
.ruby-version
89
Gemfile.lock

carrierwave-mongoid.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Gem::Specification.new do |s|
2020
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
2121
s.require_paths = ["lib"]
2222

23-
s.add_dependency "carrierwave", [">= 0.8", "< 3"]
23+
s.add_dependency "carrierwave", [">= 0.8", "< 4"]
2424
s.add_dependency "mongoid", [">= 3.0", "< 10.0"]
2525
s.add_dependency "mongoid-grid_fs", [">= 1.3", "< 3.0"]
2626
s.add_dependency "mime-types", "< 3" if RUBY_VERSION < "2.0" # mime-types 3+ doesn't support ruby 1.9

gemfiles/carrierwave-3.0.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source "https://rubygems.org"
2+
3+
gem "carrierwave", "~> 3.0.0"
4+
5+
gemspec path: "../"

gemfiles/carrierwave-3.1.gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
source "https://rubygems.org"
2+
3+
gem "carrierwave", "~> 3.1.0"
4+
5+
gemspec path: "../"

lib/carrierwave/mongoid.rb

Lines changed: 261 additions & 154 deletions
Large diffs are not rendered by default.

spec/carrierwave/mongoid/mount_uploaders_spec.rb

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def self.model_name
140140

141141
describe 'model#remove_uploaders=' do
142142
before do
143+
model.images = [stub_file('test.jpg')]
143144
model.save
144145
end
145146

@@ -208,7 +209,7 @@ def self.model_name
208209

209210
before do
210211
model.save!
211-
model.collection.update_one({ _id: model.id }, { images: identifiers })
212+
model.collection.update_one({ _id: model.id }, '$set' => { images: identifiers })
212213
end
213214

214215
it 'returns an array of uploaders' do
@@ -239,10 +240,6 @@ def self.model_name
239240
expect(model.images.map(&:identifier)).to match_array(identifiers)
240241
end
241242

242-
it 'does not write anything to the database, in order to prevent overridden filenames to fail because of unassigned attributes' do
243-
expect(model[:images]).to match_array([])
244-
end
245-
246243
it 'copies a file into into the cache directory' do
247244
expect(model.images.first.current_path).to match(/^#{Regexp.escape(public_path('uploads/tmp'))}/)
248245
end
@@ -260,10 +257,6 @@ def self.model_name
260257
expect(model.images.map(&:identifier)).to match_array(['portrait.jpg', 'test.jpeg'])
261258
end
262259

263-
it 'does not write anything to the database, in order to prevent overridden filenames to fail because of unassigned attributes' do
264-
expect(model[:images]).to match_array(['portrait.jpg'])
265-
end
266-
267260
it 'copies a file into into the cache directory' do
268261
expect(model.images.map(&:current_path)).to all(match(/^#{Regexp.escape(public_path('uploads/tmp'))}/))
269262
end
@@ -301,10 +294,6 @@ def self.model_name
301294
expect(model.images).to all(be_an_instance_of(uploader_class))
302295
end
303296

304-
it 'does not write to the database' do
305-
expect(model[:images]).to be_empty
306-
end
307-
308297
it 'copies a file into into the cache directory' do
309298
expect(model.images.map(&:current_path)).to all(start_with(public_path('uploads/tmp')))
310299
end
@@ -393,15 +382,20 @@ def munge
393382

394383
before do
395384
model_class.create!(images: files)
396-
end
397-
398-
it 'replaced it by a file with the same name' do
399385
record.update!(images: [stub_file('test.jpeg')])
400-
401386
record.reload
387+
end
402388

403-
expect(record[:images]).to match_array(['test.jpeg'])
404-
expect(record.images_identifiers).to match_array(['test.jpeg'])
389+
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("3.0.beta")
390+
it "performs deduplication" do
391+
expect(record[:images]).to match_array(['test(2).jpeg'])
392+
expect(record.images_identifiers).to match_array(['test(2).jpeg'])
393+
end
394+
else
395+
it 'replaced it by a file with the same name' do
396+
expect(record[:images]).to match_array(['test.jpeg'])
397+
expect(record.images_identifiers).to match_array(['test.jpeg'])
398+
end
405399
end
406400
end
407401

@@ -492,12 +486,14 @@ def munge
492486
expect(model.save).to be_truthy
493487
expect(File).to exist(public_path('uploads/new.jpeg'))
494488
expect(File).to exist(public_path('uploads/old.jpeg'))
489+
ensure
490+
uploader_class.remove_previously_stored_files_after_update = true
495491
end
496492

497-
it 'does not remove file if old file had the same path' do
493+
it 'does not remove new file if both of files had the same path' do
498494
model.images = [stub_file('old.jpeg')]
499495
expect(model.save).to be_truthy
500-
expect(File).to exist(public_path('uploads/old.jpeg'))
496+
expect(File).to exist(model.images[0].path)
501497
end
502498

503499
it 'does not remove file if validations fail on save' do
@@ -519,13 +515,13 @@ def filename
519515

520516
let!(:model) { model_class.create!(name: 'Mike', images: [stub_file('old.jpeg')]) }
521517

522-
it 'does not remove file if old file had the same dynamic path' do
518+
it 'does not remove new file if both of files had the same path' do
523519
expect(File).to exist(public_path('uploads/Mike.jpeg'))
524520
expect(model.images.first.read).to eq 'this is stuff'
525521

526522
model.update!(images: [stub_file('test.jpeg')])
527523

528-
expect(File).to exist(public_path('uploads/Mike.jpeg'))
524+
expect(File).to exist(model.images[0].path)
529525
end
530526

531527
it 'removes old file if old file had a different dynamic path' do
@@ -553,12 +549,14 @@ def filename
553549
expect(embedded_model.save).to be_truthy
554550
expect(File).to exist(public_path('uploads/new.jpeg'))
555551
expect(File).to exist(public_path('uploads/old.jpeg'))
552+
ensure
553+
uploader_class.remove_previously_stored_files_after_update = true
556554
end
557555

558-
it 'does not remove file if old file had the same path' do
556+
it 'should not remove new file if both of files had the same path' do
559557
embedded_model.images = [stub_file('old.jpeg')]
560558
expect(embedded_model.save).to be_truthy
561-
expect(File).to exist(public_path('uploads/old.jpeg'))
559+
expect(File).to exist(embedded_model.images[0].path)
562560
end
563561

564562
it 'does not remove file if validations fail on save' do
@@ -592,12 +590,14 @@ def filename
592590
expect(double_embedded_model.save).to be_truthy
593591
expect(File).to exist(public_path('uploads/new.jpeg'))
594592
expect(File).to exist(public_path('uploads/old.jpeg'))
593+
ensure
594+
uploader_class.remove_previously_stored_files_after_update = true
595595
end
596596

597-
it 'does not remove file if old file had the same path' do
597+
it 'should not remove new file if both of files had the same path' do
598598
double_embedded_model.images = [stub_file('old.jpeg')]
599599
expect(double_embedded_model.save).to be_truthy
600-
expect(File).to exist(public_path('uploads/old.jpeg'))
600+
expect(File).to exist(double_embedded_model.images[0].path)
601601
end
602602

603603
it 'does not remove file if validations fail on save' do
@@ -878,14 +878,14 @@ def filename
878878
expect(File).not_to exist(public_path('uploads/thumb_old.jpeg'))
879879
end
880880

881-
it 'does not remove file if old file had the same path' do
881+
it 'does not remove new file if both of files had the same path' do
882882
expect(File).to exist(public_path('uploads/old.jpeg'))
883883
expect(File).to exist(public_path('uploads/thumb_old.jpeg'))
884884

885885
model.update!(images: [stub_file('old.jpeg')])
886886

887-
expect(File).to exist(public_path('uploads/old.jpeg'))
888-
expect(File).to exist(public_path('uploads/thumb_old.jpeg'))
887+
expect(File).to exist(model.images[0].path)
888+
expect(File).to exist(model.images[0].thumb.path)
889889
end
890890
end
891891

@@ -926,25 +926,25 @@ def self.model_name
926926
expect(File).not_to exist(public_path('uploads/old.txt'))
927927
end
928928

929-
it 'removes old file1 but not file2 if old file1 had a different path but old file2 has the same path' do
929+
it 'removes old file1 but not new file2 if old file1 had a different path but old file2 has the same path' do
930930
expect(File).to exist(public_path('uploads/old.jpeg'))
931931
expect(File).to exist(public_path('uploads/old.txt'))
932932

933933
model.update!(images: [stub_file('new.jpeg')], textfiles: [stub_file('old.txt')])
934934

935935
expect(File).to exist(public_path('uploads/new.jpeg'))
936936
expect(File).not_to exist(public_path('uploads/old.jpeg'))
937-
expect(File).to exist(public_path('uploads/old.txt'))
937+
expect(File).to exist(model.textfiles[0].path)
938938
end
939939

940-
it 'does not remove file1 or file2 if file1 and file2 have the same paths' do
940+
it 'does not remove new files if each pair of files has the same paths' do
941941
expect(File).to exist(public_path('uploads/old.jpeg'))
942942
expect(File).to exist(public_path('uploads/old.txt'))
943943

944944
model.update!(images: [stub_file('old.jpeg')], textfiles: [stub_file('old.txt')])
945945

946-
expect(File).to exist(public_path('uploads/old.jpeg'))
947-
expect(File).to exist(public_path('uploads/old.txt'))
946+
expect(File).to exist(model.images[0].path)
947+
expect(File).to exist(model.textfiles[0].path)
948948
end
949949
end
950950

@@ -981,12 +981,12 @@ def self.model_name
981981
expect(File).not_to exist(public_path('uploads/old.jpeg'))
982982
end
983983

984-
it 'does not remove file if old file had the same path' do
984+
it 'does not remove new file if both of files had the same path' do
985985
expect(File).to exist(public_path('uploads/old.jpeg'))
986986

987987
model.update!(avatars: [stub_file('old.jpeg')])
988988

989-
expect(File).to exist(public_path('uploads/old.jpeg'))
989+
expect(File).to exist(model.avatars[0].path)
990990
end
991991
end
992992
end

0 commit comments

Comments
 (0)