Skip to content

Commit

Permalink
[Fix carrierwaveuploader#1571] - Do not write to ActiveModel::Dirty c…
Browse files Browse the repository at this point in the history
…hanges when assigning something blank to a mounter that was originally blank
  • Loading branch information
avgerin0s authored and Kevin Mehlbrech committed Aug 9, 2016
1 parent d1b053d commit 0d7991f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/carrierwave/orm/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def mount_base(column, uploader=nil, options={}, &block)
class_eval <<-RUBY, __FILE__, __LINE__+1
def #{column}=(new_file)
column = _mounter(:#{column}).serialization_column
send(:"\#{column}_will_change!")
if !(new_file.blank? && send(:#{column}).blank?)
send(:"\#{column}_will_change!")
end
super
end
Expand Down
42 changes: 36 additions & 6 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,46 @@ class Event < ActiveRecord::Base; end # setup a basic AR class for testing
expect(@event.image.current_path).to match(%r(^#{public_path('uploads/tmp')}))
end

it "should do nothing when nil is assigned" do
@event.image = nil
expect(@event.image).to be_blank
context "when empty string is assigned" do
it "does nothing when" do
@event.image = ''
expect(@event.image).to be_blank
end

context "and the previous value was an empty string" do
before do
@event.image = ""
@event.save
end

it "does not write to dirty changes" do
@event.image = ''
expect(@event.changes.keys).not_to include("image")
end
end

end

it "should do nothing when an empty string is assigned" do
@event.image = ''
expect(@event.image).to be_blank
context "when nil is assigned" do
it "does nothing" do
@event.image = nil
expect(@event.image).to be_blank
end

context "and the previous value was nil" do
before do
@event.image = nil
@event.save
end

it "does not write to dirty changes" do
@event.image = nil
expect(@event.changes.keys).not_to include("image")
end
end
end


context 'when validating white list integrity' do
before do
@uploader.class_eval do
Expand Down

0 comments on commit 0d7991f

Please sign in to comment.