FIX: Validate category name on parent change (#10815)

Previously, moving a category into another one, that already had a child category of that name (but with a non-conflicting slug) would cause a 500 error:

```
# PG::UniqueViolation:
#   ERROR:  duplicate key value violates unique constraint "unique_index_categories_on_name"
#   DETAIL:  Key (COALESCE(parent_category_id, '-1'::integer), name)=(5662, Amazing Category 0) already exists.
```

It now returns 422, and shows the same message as when you're renaming a category: "Category Name has already been taken".
This commit is contained in:
Jarek Radosz 2020-10-05 11:50:05 +02:00 committed by GitHub
parent 702f7a5a67
commit cf44cdb082
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -49,7 +49,7 @@ class Category < ActiveRecord::Base
validates :user_id, presence: true
validates :name, if: Proc.new { |c| c.new_record? || c.will_save_change_to_name? },
validates :name, if: Proc.new { |c| c.new_record? || c.will_save_change_to_name? || c.will_save_change_to_parent_category_id? },
presence: true,
uniqueness: { scope: :parent_category_id, case_sensitive: false },
length: { in: 1..50 }

View File

@ -314,6 +314,17 @@ describe CategoriesController do
expect(response.status).to eq(422)
end
it "returns errors when there is a name conflict while moving a category into another" do
parent_category = Fabricate(:category, name: "Parent", user: admin)
other_category = Fabricate(:category, name: category.name, user: admin, parent_category: parent_category, slug: "a-different-slug")
put "/categories/#{category.id}.json", params: {
parent_category_id: parent_category.id,
}
expect(response.status).to eq(422)
end
it "returns 422 if email_in address is already in use for other category" do
_other_category = Fabricate(:category, name: "Other", email_in: "mail@examle.com")