Skip to content

Commit

Permalink
Fix remove_#{column}! doesn't remove the file immediately
Browse files Browse the repository at this point in the history
Also the call to write_#{column}_identifier doesn't need to be in CarrierWave::ActiveRecord, since what it does is not specific to ActiveRecord.
Fixes #2540
  • Loading branch information
mshibuya committed Jan 2, 2023
1 parent f1eff6e commit b719fb3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
2 changes: 2 additions & 0 deletions lib/carrierwave/mount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ def remove_#{column}
def remove_#{column}!
_mounter(:#{column}).remove!
self.remove_#{column} = true
write_#{column}_identifier
end
def remove_#{column}=(value)
Expand Down
3 changes: 2 additions & 1 deletion lib/carrierwave/mounter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ def remove?

def remove!
uploaders.reject(&:blank?).each(&:remove!)
@uploaders = []
clear!
end

def clear!
@remove = nil
@uploaders = []
end

Expand Down
7 changes: 0 additions & 7 deletions lib/carrierwave/orm/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,6 @@ def remove_#{column}=(value)
result
end
def remove_#{column}!
self.remove_#{column} = true
write_#{column}_identifier
self.remove_#{column} = false
super
end
# Reset cached mounter on record reload
def reload(*)
@_mounters = nil
Expand Down
42 changes: 38 additions & 4 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,13 @@ def monkey
@event.save!
end

it "should remove the file immediately" do
file = @event.image.file
@event.remove_image!

expect(file).not_to exist
end

it "should clear the serialization column" do
@event.remove_image!

Expand All @@ -499,6 +506,16 @@ def monkey
@event.remove_image = "1"
expect(@event.image_changed?).to be_truthy
end

it "resets remove_image? to false on save" do
@event.remove_image = true

expect {
@event.save!
}.to change {
@event.remove_image?
}.from(true).to(false)
end
end

describe "#remote_image_url=" do
Expand Down Expand Up @@ -1282,18 +1299,25 @@ def monkey
before do
@event.images = [stub_file('test.jpeg')]
@event.save!
@event.remove_images!
end

it "should clear the serialization column" do
@event.remove_images!
expect(@event.attributes['images']).to be_blank
end

it "should return to false after being saved" do
@event.save!
expect(@event.remove_images).to eq(false)
it "should return to false" do
@event.remove_images!
expect(@event.remove_images).to be_falsy
expect(@event.remove_images?).to eq(false)
end

it "should remove the file immediately" do
file = @event.images.first.file
@event.remove_images!

expect(file).not_to exist
end
end

describe "remove_images=" do
Expand All @@ -1310,6 +1334,16 @@ def monkey
@event.remove_images = "false"
expect(@event.images_changed?).to be_falsey
end

it "resets remove_images? to false on save" do
@event.remove_images = true

expect {
@event.save!
}.to change {
@event.remove_images?
}.from(true).to(false)
end
end

describe "#remote_images_urls=" do
Expand Down

0 comments on commit b719fb3

Please sign in to comment.