Skip to content

Commit

Permalink
[Fix #1571] - Do not write to ActiveModel::Dirty changes when assigni…
Browse files Browse the repository at this point in the history
…ng something blank to a mounter that was originally blank
  • Loading branch information
avgerin0s committed Apr 30, 2015
1 parent 36dd774 commit 79dc62e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 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
49 changes: 38 additions & 11 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,29 +185,57 @@ class Event < ActiveRecord::Base; end # setup a basic AR class for testing
end

describe '#image=' do

it "should cache a file" do
it "caches a file" do
@event.image = stub_file('test.jpeg')
expect(@event.image).to be_an_instance_of(@uploader)
end

it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
it "writes nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
expect(@event[:image]).to be_nil
end

it "should copy a file into into the cache directory" do
it "copies a file into into the cache directory" do
@event.image = stub_file('test.jpeg')
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
Expand Down Expand Up @@ -260,7 +288,6 @@ def extension_black_list
end
end


context 'when validating processing' do
before do
@uploader.class_eval do
Expand Down

0 comments on commit 79dc62e

Please sign in to comment.