Skip to content

Commit

Permalink
Merge pull request #2707 from rajyan/fix-issue-2702
Browse files Browse the repository at this point in the history
Fix dirty handling on update
  • Loading branch information
mshibuya committed Sep 24, 2023
2 parents 96398d9 + c7262b9 commit 39f4da0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/carrierwave/uploader/store.rb
Expand Up @@ -87,7 +87,7 @@ def store!(new_file=nil)
end
@file = new_file
@identifier = storage.identifier
@cache_id = @deduplication_index = nil
@original_filename = @cache_id = @deduplication_index = nil
@staged = false
end
end
Expand Down
69 changes: 58 additions & 11 deletions spec/orm/activerecord_spec.rb
Expand Up @@ -426,6 +426,17 @@ def monkey
expect(@event.image_identifier).to eq(nil)
end

it "should cancel removing image if remove_image is switched to false" do
@event.image = stub_file('test.jpeg')
@event.save!
@event.remove_image = true
@event.remove_image = false
@event.save!
@event.reload
expect(@event[:image]).to eq('test.jpeg')
expect(@event.image_identifier).to eq('test.jpeg')
end

it "should mark image as changed when saving a new image" do
expect(@event.image_changed?).to be_falsey
@event.image = stub_file("test.jpeg")
Expand Down Expand Up @@ -635,18 +646,17 @@ def filename
end
end

describe 'with overriddent filename' do

describe '#save' do

before do
@uploader.class_eval do
def filename
model.name + File.extname(super)
end
describe 'with overridden filename' do
before do
@uploader.class_eval do
def filename
model.name + File.extname(super)
end
allow(@event).to receive(:name).and_return('jonas')
end
allow(@event).to receive(:name).and_return('jonas')
end

describe '#save' do

it "should copy the file to the upload directory when a file has been assigned" do
@event.image = stub_file('test.jpeg')
Expand All @@ -664,6 +674,32 @@ def filename

end

describe '#changes' do
it "should be generated" do
@event.image = stub_file('test.jpeg')
expect(@event.changes).to eq({'image' => [nil, 'test.jpeg']})
end

it "shouldn't be generated after second save" do
@event.image = stub_file('old.jpeg')
@event.save!
@event.image = stub_file('new.jpeg')
@event.save!

expect(@event.changes).to be_blank
expect(@event).not_to be_changed
end

it "shouldn't be generated when remove_image is canceled" do
@event.image = stub_file('test.jpeg')
@event.save!
@event.remove_image = true
@event.remove_image = false

expect(@event.changes).to be_blank
expect(@event).not_to be_changed
end
end
end

describe 'with validates_presence_of' do
Expand Down Expand Up @@ -1379,6 +1415,17 @@ def monkey
expect(@event.images_identifiers[0]).to eq(nil)
end

it "should cancel removing images if remove_images is switched to false" do
@event.images = [stub_file('test.jpeg')]
@event.save!
@event.remove_images = true
@event.remove_images = false
@event.save!
@event.reload
expect(@event[:images]).to eq(['test.jpeg'])
expect(@event.images_identifiers[0]).to eq('test.jpeg')
end

it "should mark images as changed when saving a new images" do
expect(@event.images_changed?).to be_falsey
@event.images = [stub_file("test.jpeg")]
Expand Down Expand Up @@ -1552,7 +1599,7 @@ def filename
end
end

describe 'with overriddent filename' do
describe 'with overridden filename' do

describe '#save' do

Expand Down

0 comments on commit 39f4da0

Please sign in to comment.