Handle Route#name being nil after an update
It was possible for the `routes.name` field to be `NULL`, causing `name_was` to be `nil` after a rename, resulting a bad first argument to `sub` when attempting to rename descendants. This change adds a condition to make sure `name_was` is present before attempting the descendant update.
This commit is contained in:
parent
b075d38c9b
commit
9bf9e6eb38
|
@ -21,7 +21,7 @@ class Route < ActiveRecord::Base
|
||||||
attributes[:path] = route.path.sub(path_was, path)
|
attributes[:path] = route.path.sub(path_was, path)
|
||||||
end
|
end
|
||||||
|
|
||||||
if name_changed? && route.name.present?
|
if name_changed? && name_was.present? && route.name.present?
|
||||||
attributes[:name] = route.name.sub(name_was, name)
|
attributes[:name] = route.name.sub(name_was, name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -43,14 +43,22 @@ describe Route, models: true do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'name update' do
|
context 'name update' do
|
||||||
before { route.update_attributes(name: 'bar') }
|
|
||||||
|
|
||||||
it "updates children routes with new path" do
|
it "updates children routes with new path" do
|
||||||
|
route.update_attributes(name: 'bar')
|
||||||
|
|
||||||
expect(described_class.exists?(name: 'bar')).to be_truthy
|
expect(described_class.exists?(name: 'bar')).to be_truthy
|
||||||
expect(described_class.exists?(name: 'bar / test')).to be_truthy
|
expect(described_class.exists?(name: 'bar / test')).to be_truthy
|
||||||
expect(described_class.exists?(name: 'bar / test / foo')).to be_truthy
|
expect(described_class.exists?(name: 'bar / test / foo')).to be_truthy
|
||||||
expect(described_class.exists?(name: 'gitlab-org')).to be_truthy
|
expect(described_class.exists?(name: 'gitlab-org')).to be_truthy
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'handles a rename from nil' do
|
||||||
|
# Note: using `update_columns` to skip all validation and callbacks
|
||||||
|
route.update_columns(name: nil)
|
||||||
|
|
||||||
|
expect { route.update_attributes(name: 'bar') }
|
||||||
|
.to change { route.name }.from(nil).to('bar')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue