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:
parent
702f7a5a67
commit
cf44cdb082
|
@ -49,7 +49,7 @@ class Category < ActiveRecord::Base
|
||||||
|
|
||||||
validates :user_id, presence: true
|
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,
|
presence: true,
|
||||||
uniqueness: { scope: :parent_category_id, case_sensitive: false },
|
uniqueness: { scope: :parent_category_id, case_sensitive: false },
|
||||||
length: { in: 1..50 }
|
length: { in: 1..50 }
|
||||||
|
|
|
@ -314,6 +314,17 @@ describe CategoriesController do
|
||||||
expect(response.status).to eq(422)
|
expect(response.status).to eq(422)
|
||||||
end
|
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
|
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")
|
_other_category = Fabricate(:category, name: "Other", email_in: "mail@examle.com")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue