FIX: handle error for duplicate email_in address

This commit is contained in:
Arpit Jalan 2015-04-16 16:09:48 +05:30
parent 3d30bd115d
commit 866d1cd8e3
3 changed files with 12 additions and 0 deletions

View file

@ -87,6 +87,9 @@ class CategoriesController < ApplicationController
if category_params.key? :email_in and category_params[:email_in].length == 0
# properly null the value so the database constrain doesn't catch us
category_params[:email_in] = nil
elsif category_params.key? :email_in and existing_category = Category.find_by(email_in: category_params[:email_in]) and existing_category.id != @category.id
# check if email_in address is already in use for other category
return render_json_error I18n.t('category.errors.email_in_already_exist', {email_in: category_params[:email_in], category_name: existing_category.name})
end
category_params.delete(:position)

View file

@ -341,6 +341,7 @@ en:
uncategorized_parent: "Uncategorized can't have a parent category"
self_parent: "A subcategory's parent cannot be itself"
depth: "You can't nest a subcategory under another"
email_in_already_exist: "Incoming email address '%{email_in}' is already in use for '%{category_name}' category."
cannot_delete:
uncategorized: "Can't delete Uncategorized"
has_subcategories: "Can't delete this category because it has sub-categories."

View file

@ -174,6 +174,14 @@ describe CategoriesController do
end
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" )
xhr :put, :update, id: @category.id, name: "Email", email_in: "mail@examle.com", color: "ff0", text_color: "fff"
expect(response).not_to be_success
expect(response.code.to_i).to eq(422)
end
describe "success" do
it "updates the group correctly" do