Skip to content

Commit

Permalink
Restore previous behavior of clearing attachments via attribute assig…
Browse files Browse the repository at this point in the history
…nment

Fixes #2654, Refs. #2613
  • Loading branch information
mshibuya committed Feb 9, 2023
1 parent d90c399 commit f8ea354
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
16 changes: 4 additions & 12 deletions lib/carrierwave/mount.rb
Expand Up @@ -174,12 +174,8 @@ def write_#{column}_identifier
return if frozen?
mounter = _mounter(:#{column})
if mounter.remove?
mounter.clear!
write_uploader(mounter.serialization_column, nil)
elsif mounter.identifiers.first
write_uploader(mounter.serialization_column, mounter.identifiers.first)
end
mounter.clear! if mounter.remove?
write_uploader(mounter.serialization_column, mounter.identifiers.first)
end
def #{column}_identifier
Expand Down Expand Up @@ -353,12 +349,8 @@ def write_#{column}_identifier
return if frozen?
mounter = _mounter(:#{column})
if mounter.remove?
mounter.clear!
write_uploader(mounter.serialization_column, nil)
elsif mounter.identifiers.any?
write_uploader(mounter.serialization_column, mounter.identifiers)
end
mounter.clear! if mounter.remove?
write_uploader(mounter.serialization_column, mounter.identifiers.presence)
end
def #{column}_identifiers
Expand Down
5 changes: 5 additions & 0 deletions lib/carrierwave/orm/activerecord.rb
Expand Up @@ -91,6 +91,11 @@ def remove_#{column}=(value)
result
end
def write_#{column}_identifier
return unless has_attribute?(_mounter(:#{column}).serialization_column)
super
end
# Reset cached mounter on record reload
def reload(*)
@_mounters = nil
Expand Down
30 changes: 30 additions & 0 deletions spec/orm/activerecord_spec.rb
Expand Up @@ -681,6 +681,21 @@ def filename
end

end

describe 'calling #update! on the instance' do
before do
@event.image = stub_file('test.jpeg')
@event.save!
@event.reload
end

it "allows clearing the stored image" do
@event.update!(image: nil)
expect(@event.image.file).to be_nil
expect(File.exist?(public_path('uploads/test.jpeg'))).to be_falsy
expect(@event[:image]).to be_nil
end
end
end

describe '#mount_uploader with mount_on' do
Expand Down Expand Up @@ -1509,6 +1524,21 @@ def filename
end

end

describe 'calling #update! on the instance' do
before do
@event.images = [stub_file('test.jpeg')]
@event.save!
@event.reload
end

it "allows clearing the stored images" do
@event.update!(images: [])
expect(@event.images).to be_empty
expect(File.exist?(public_path('uploads/test.jpeg'))).to be_falsy
expect(@event[:images]).to be nil
end
end
end

describe '#mount_uploaders with mount_on' do
Expand Down

0 comments on commit f8ea354

Please sign in to comment.