Merge pull request #3383 from techAPJ/patch-4

FIX: handle error for duplicate email_in address
This commit is contained in:
Robin Ward 2015-04-16 11:13:36 -04:00
commit 94fea09416
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