Skip to content

Commit

Permalink
Merge pull request #2674 from y-yagi/fix-NoMethodError-from-remove_added
Browse files Browse the repository at this point in the history
Fix a `NoMethodError` when a record is rollbacked
  • Loading branch information
mshibuya committed Jul 15, 2023
2 parents d68a111 + db95563 commit 73bacd2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/carrierwave/mounter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def remove_previous
end

def remove_added
current_paths = (@removed_uploaders + @uploaders.select(&:staged)).map(&:path)
current_paths = (@removed_uploaders + uploaders.select(&:staged)).map(&:path)
@added_uploaders
.reject {|uploader| current_paths.include?(uploader.path) }
.each { |uploader| uploader.remove! }
Expand Down
12 changes: 12 additions & 0 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,18 @@ def filename
expect(File.exist?(public_path('uploads/old.jpeg'))).to be_truthy
end

it 'should not remove a file if transaction is rollback' do
@event.image = stub_file('new.jpeg')
@event.save

Event.transaction do
@event.destroy
raise ActiveRecord::Rollback
end

expect(File.exist?(public_path('uploads/new.jpeg'))).to be_truthy
end

it "should clear @added_uploaders on commit" do
# Simulate the commit behavior, since we're using the transactional fixture
@event.run_callbacks(:commit) do
Expand Down

0 comments on commit 73bacd2

Please sign in to comment.