Commit f12f742f authored by Vijay Hawoldar's avatar Vijay Hawoldar

Fix bug with snippet updating error losing changes

parent 4907a8d6
......@@ -48,10 +48,12 @@ module Snippets
true
rescue => e
# Restore old attributes
# Restore old attributes but re-assign changes so they're not lost
unless snippet.previous_changes.empty?
snippet.previous_changes.each { |attr, value| snippet[attr] = value[0] }
snippet.save
snippet.assign_attributes(params)
end
snippet.errors.add(:repository, 'Error updating the snippet')
......
---
title: Fix Snippet update error bug losing changes
merge_request: 31873
author:
type: fixed
......@@ -196,14 +196,24 @@ describe Snippets::UpdateService do
end
end
context 'with snippet modifications' do
let(:option_keys) { options.stringify_keys.keys }
it 'rolls back any snippet modifications' do
option_keys = options.stringify_keys.keys
orig_attrs = snippet.attributes.select { |k, v| k.in?(option_keys) }
subject
current_attrs = snippet.attributes.select { |k, v| k.in?(option_keys) }
expect(orig_attrs).to eq current_attrs
persisted_attrs = snippet.reload.attributes.select { |k, v| k.in?(option_keys) }
expect(orig_attrs).to eq persisted_attrs
end
it 'keeps any snippet modifications' do
subject
instance_attrs = snippet.attributes.select { |k, v| k.in?(option_keys) }
expect(options.stringify_keys).to eq instance_attrs
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment