From 0acace58c31c96fc1f8836167aeb4f204f72617f Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 18 Jul 2013 17:17:36 +0200 Subject: allow available and unique forwards only --- users/app/models/identity.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'users/app/models/identity.rb') diff --git a/users/app/models/identity.rb b/users/app/models/identity.rb index c9c8b73..4dff93a 100644 --- a/users/app/models/identity.rb +++ b/users/app/models/identity.rb @@ -7,7 +7,29 @@ class Identity < CouchRest::Model::Base property :address, LocalEmail property :destination, Email + validate :unique_forward + validate :alias_available + design do view :by_user_id + view :by_address_and_destination + view :by_address + end + + protected + + def unique_forward + same = Identity.find_by_address_and_destination([address, destination]) + if same && same != self + errors.add :base, "This alias already exists" + end end + + def alias_available + same = Identity.find_by_address(address) + if same && same.user != self.user + errors.add :base, "This email has already been taken" + end + end + end -- cgit v1.2.3