Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ gemfile:
- Gemfile
- gemfiles/carrierwave-0.10.gemfile
- gemfiles/carrierwave-0.11.gemfile
- gemfiles/carrierwave-1.0.gemfile
- gemfiles/carrierwave-1.1.gemfile
- gemfiles/mongoid-3.1.gemfile
- gemfiles/mongoid-4.0.gemfile
- gemfiles/mongoid-5.0.gemfile
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", "< 1.1"]
s.add_dependency "carrierwave", [">= 0.8", "< 1.3"]
s.add_dependency "mongoid", [">= 3.0", "< 7.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-1.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "https://rubygems.org"

gem "carrierwave", "~> 1.0.0"

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

gem "carrierwave", "~> 1.1.0"

gemspec path: "../"
43 changes: 41 additions & 2 deletions lib/carrierwave/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,24 @@ def mount_uploader(column, uploader=nil, options={}, &block)
after_save :"store_#{column}!"
before_save :"write_#{column}_identifier"
after_destroy :"remove_#{column}!"
before_update :"store_previous_model_for_#{column}"
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("1.0.beta")
before_update :"store_previous_changes_for_#{column}"
else
before_update :"store_previous_model_for_#{column}"
end
after_save :"remove_previously_stored_#{column}"

class_eval <<-RUBY, __FILE__, __LINE__+1
def #{column}=(new_file)
column = _mounter(:#{column}).serialization_column

# We're using _new_ and _old_ placeholder values to force Mongoid to
# recognize changes in embedded documents. Before we assign these
# values, we need to store the original file name in case we need to
# delete it when document is saved.
previous_uploader_value = read_uploader(column)
@_previous_uploader_value_for_#{column} = previous_uploader_value

# mongoid won't upload a new file if there was no file previously.
write_uploader(column, '_old_') if self.persisted? && read_uploader(column).nil?

Expand Down Expand Up @@ -73,6 +84,29 @@ def #{column}_will_change!
changed_attributes["#{column}"] = '_new_'
end

# Since we had to use tricks with _old_ and _new_ values to properly
# track changes in embedded documents, we need to overwrite this method
# to remove the original file if it was replaced with a new one that
# had a different name.
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("1.0.beta")
def remove_previously_stored_#{column}
before, after = @_previous_changes_for_#{column}
# Don't delete if the files had the same name
return if before.nil? && after.nil?
# Proceed to remove the file, use the original name instead of '_new_'
before = @_previous_uploader_value_for_#{column} || before
_mounter(:#{column}).remove_previous([before], [after])
end
end

# CarrierWave 1.1 references ::ActiveRecord constant directly which
# will fail in projects without ActiveRecord. We need to overwrite this
# method to avoid it.
# See https://github.com/carrierwaveuploader/carrierwave/blob/07dc4d7bd7806ab4b963cf8acbad73d97cdfe74e/lib/carrierwave/mount.rb#L189
def store_previous_changes_for_#{column}
@_previous_changes_for_#{column} = changes[_mounter(:#{column}).serialization_column]
end

def find_previous_model_for_#{column}
if self.embedded?
if self.respond_to?(:__metadata) # Mongoid >= 4.0.0.beta1
Expand All @@ -97,7 +131,12 @@ def serializable_hash(options=nil)

self.class.uploaders.each do |column, uploader|
if (!only && !except) || (only && only.include?(column.to_s)) || (except && !except.include?(column.to_s))
hash[column.to_s] = _mounter(column.to_sym).uploader.serializable_hash
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("1.0.beta")
next if _mounter(column.to_sym).uploaders.blank?
hash[column.to_s] = _mounter(column.to_sym).uploaders[0].serializable_hash
else
hash[column.to_s] = _mounter(column.to_sym).uploader.serializable_hash
end
end
end
super(options).merge(hash)
Expand Down
14 changes: 9 additions & 5 deletions spec/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ def extension_white_list
@doc.save!
@doc.reload

expect(JSON.parse({:data => @doc.image}.to_json)).to eq("data"=>{"image"=>{"url"=>"/uploads/test.jpeg"}})
if Gem::Version.new(CarrierWave::VERSION) >= Gem::Version.new("1.0.beta")
expect(JSON.parse({:data => @doc.image}.to_json)).to eq({"data"=>{"url"=>"/uploads/test.jpeg"}})
else
expect(JSON.parse({:data => @doc.image}.to_json)).to eq("data"=>{"image" => {"url"=>"/uploads/test.jpeg"}})
end
end

it "should respect options[:only] when passed to to_json for the serializable hash" do
Expand Down Expand Up @@ -409,7 +413,7 @@ def extension_white_list
end

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

describe 'normally' do
Expand All @@ -422,7 +426,7 @@ def extension_white_list
end

it "should not remove old file if old file had a different path but config is false" do
allow(@uploader).to receive(:remove_previously_stored_files_after_update).and_return(false)
@doc.image.class.remove_previously_stored_files_after_update = false
@doc.image = stub_file('new.jpeg')
expect(@doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
Expand Down Expand Up @@ -482,7 +486,7 @@ def filename
end

it "should not remove old file if old file had a different path but config is false" do
allow(@embedded_doc.image).to receive(:remove_previously_stored_files_after_update).and_return(false)
@embedded_doc.image.class.remove_previously_stored_files_after_update = false
@embedded_doc.image = stub_file('new.jpeg')
expect(@embedded_doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
Expand Down Expand Up @@ -520,7 +524,7 @@ def filename
end

it "should not remove old file if old file had a different path but config is false" do
allow(@double_embedded_doc.image).to receive(:remove_previously_stored_files_after_update).and_return(false)
@double_embedded_doc.image.class.remove_previously_stored_files_after_update = false
@double_embedded_doc.image = stub_file('new.jpeg')
expect(@double_embedded_doc.save).to be_truthy
expect(File.exists?(public_path('uploads/new.jpeg'))).to be_truthy
Expand Down