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

Support CarrierWave 3.x #215

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
mongodb: [4.4]
ruby: [2.7, "3.0", 3.1, 3.2, 3.3]
gemfile:
- carrierwave-2.1
- carrierwave-2.2
- carrierwave-3.1
- mongoid-7
- mongoid-8
- mongoid-9
Expand All @@ -20,6 +20,8 @@ jobs:
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-1.2" }
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-1.3" }
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-2.0" }
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-2.1" }
- { mongodb: "4.4", ruby: "2.6", gemfile: "carrierwave-3.0" }
- { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-3" }
- { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-4" }
- { mongodb: "4.4", ruby: "2.6", gemfile: "mongoid-5" }
Expand All @@ -32,6 +34,8 @@ jobs:
uses: supercharge/[email protected]
with:
mongodb-version: ${{ matrix.mongodb }}
- name: Install ImageMagick
run: sudo apt-get -y install imagemagick
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pkg/*
*.sw*
.bundle
spec/public
spec/tmp
.rvmrc
.ruby-version
Gemfile.lock
Expand Down
2 changes: 1 addition & 1 deletion carrierwave-mongoid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency "carrierwave", [">= 0.8", "< 3"]
s.add_dependency "carrierwave", [">= 0.8", "< 4"]
s.add_dependency "mongoid", [">= 3.0", "< 10.0"]
s.add_dependency "mongoid-grid_fs", [">= 1.3", "< 3.0"]
s.add_dependency "mime-types", "< 3" if RUBY_VERSION < "2.0" # mime-types 3+ doesn't support ruby 1.9
Expand Down
5 changes: 5 additions & 0 deletions gemfiles/carrierwave-3.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "https://rubygems.org"

gem "carrierwave", "~> 3.0.0"

gemspec path: "../"
5 changes: 5 additions & 0 deletions gemfiles/carrierwave-3.1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "https://rubygems.org"

gem "carrierwave", "~> 3.1.0"

gemspec path: "../"
415 changes: 261 additions & 154 deletions lib/carrierwave/mongoid.rb

Large diffs are not rendered by default.

74 changes: 37 additions & 37 deletions spec/carrierwave/mongoid/mount_uploaders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def self.model_name

describe 'model#remove_uploaders=' do
before do
model.images = [stub_file('test.jpg')]
model.save
end

Expand Down Expand Up @@ -208,7 +209,7 @@ def self.model_name

before do
model.save!
model.collection.update_one({ _id: model.id }, { images: identifiers })
model.collection.update_one({ _id: model.id }, '$set' => { images: identifiers })
end

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

it 'does not write anything to the database, in order to prevent overridden filenames to fail because of unassigned attributes' do
expect(model[:images]).to match_array([])
end

it 'copies a file into into the cache directory' do
expect(model.images.first.current_path).to match(/^#{Regexp.escape(public_path('uploads/tmp'))}/)
end
Expand All @@ -260,10 +257,6 @@ def self.model_name
expect(model.images.map(&:identifier)).to match_array(['portrait.jpg', 'test.jpeg'])
end

it 'does not write anything to the database, in order to prevent overridden filenames to fail because of unassigned attributes' do
expect(model[:images]).to match_array(['portrait.jpg'])
end

it 'copies a file into into the cache directory' do
expect(model.images.map(&:current_path)).to all(match(/^#{Regexp.escape(public_path('uploads/tmp'))}/))
end
Expand Down Expand Up @@ -301,10 +294,6 @@ def self.model_name
expect(model.images).to all(be_an_instance_of(uploader_class))
end

it 'does not write to the database' do
expect(model[:images]).to be_empty
end

it 'copies a file into into the cache directory' do
expect(model.images.map(&:current_path)).to all(start_with(public_path('uploads/tmp')))
end
Expand Down Expand Up @@ -393,15 +382,20 @@ def munge

before do
model_class.create!(images: files)
end

it 'replaced it by a file with the same name' do
record.update!(images: [stub_file('test.jpeg')])

record.reload
end

expect(record[:images]).to match_array(['test.jpeg'])
expect(record.images_identifiers).to match_array(['test.jpeg'])
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("3.0.beta")
it "performs deduplication" do
expect(record[:images]).to match_array(['test(2).jpeg'])
expect(record.images_identifiers).to match_array(['test(2).jpeg'])
end
else
it 'replaced it by a file with the same name' do
expect(record[:images]).to match_array(['test.jpeg'])
expect(record.images_identifiers).to match_array(['test.jpeg'])
end
end
end

Expand Down Expand Up @@ -492,12 +486,14 @@ def munge
expect(model.save).to be_truthy
expect(File).to exist(public_path('uploads/new.jpeg'))
expect(File).to exist(public_path('uploads/old.jpeg'))
ensure
uploader_class.remove_previously_stored_files_after_update = true
end

it 'does not remove file if old file had the same path' do
it 'does not remove new file if both of files had the same path' do
model.images = [stub_file('old.jpeg')]
expect(model.save).to be_truthy
expect(File).to exist(public_path('uploads/old.jpeg'))
expect(File).to exist(model.images[0].path)
end

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

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

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

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

expect(File).to exist(public_path('uploads/Mike.jpeg'))
expect(File).to exist(model.images[0].path)
end

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

it 'does not remove file if old file had the same path' do
it 'should not remove new file if both of files had the same path' do
embedded_model.images = [stub_file('old.jpeg')]
expect(embedded_model.save).to be_truthy
expect(File).to exist(public_path('uploads/old.jpeg'))
expect(File).to exist(embedded_model.images[0].path)
end

it 'does not remove file if validations fail on save' do
Expand Down Expand Up @@ -592,12 +590,14 @@ def filename
expect(double_embedded_model.save).to be_truthy
expect(File).to exist(public_path('uploads/new.jpeg'))
expect(File).to exist(public_path('uploads/old.jpeg'))
ensure
uploader_class.remove_previously_stored_files_after_update = true
end

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

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

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

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

expect(File).to exist(public_path('uploads/old.jpeg'))
expect(File).to exist(public_path('uploads/thumb_old.jpeg'))
expect(File).to exist(model.images[0].path)
expect(File).to exist(model.images[0].thumb.path)
end
end

Expand Down Expand Up @@ -926,25 +926,25 @@ def self.model_name
expect(File).not_to exist(public_path('uploads/old.txt'))
end

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

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

expect(File).to exist(public_path('uploads/new.jpeg'))
expect(File).not_to exist(public_path('uploads/old.jpeg'))
expect(File).to exist(public_path('uploads/old.txt'))
expect(File).to exist(model.textfiles[0].path)
end

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

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

expect(File).to exist(public_path('uploads/old.jpeg'))
expect(File).to exist(public_path('uploads/old.txt'))
expect(File).to exist(model.images[0].path)
expect(File).to exist(model.textfiles[0].path)
end
end

Expand Down Expand Up @@ -981,12 +981,12 @@ def self.model_name
expect(File).not_to exist(public_path('uploads/old.jpeg'))
end

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

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

expect(File).to exist(public_path('uploads/old.jpeg'))
expect(File).to exist(model.avatars[0].path)
end
end
end
Expand Down
Loading
Loading