summaryrefslogtreecommitdiff
path: root/users/app/models/local_email.rb
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2012-12-18 13:41:41 +0100
committerAzul <azul@leap.se>2012-12-18 13:41:41 +0100
commit285b23f39765d8346a658a81eca8b70d70b3e9bf (patch)
treeddb8f4d433c6f0461f7832ad08475b9a5bae99b2 /users/app/models/local_email.rb
parent6e8a45145722c12dee4d15b33cc28d2b09881e1a (diff)
refactored email_alias creation and validation
using CouchRests user.email_aliases.build so the casted_by method is set in the alias Used this to move the validations into the alias itself. This is where they belong and allows us to render the errors inline along the email field they belong to.
Diffstat (limited to 'users/app/models/local_email.rb')
-rw-r--r--users/app/models/local_email.rb21
1 files changed, 20 insertions, 1 deletions
diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb
index 7cca4f4..c654fcb 100644
--- a/users/app/models/local_email.rb
+++ b/users/app/models/local_email.rb
@@ -1,6 +1,9 @@
class LocalEmail < Email
validate :unique_on_server
+ validate :unique_alias_for_user
+ validate :differs_from_main_email
+ validates :casted_by, :presence => true
def to_partial_path
"emails/email"
@@ -9,7 +12,23 @@ class LocalEmail < Email
def unique_on_server
has_email = User.find_by_email_or_alias(email)
if has_email && has_email != self.base_doc
- errors.add(:email, "has already been taken")
+ errors.add :email, "has already been taken"
end
end
+
+ def unique_alias_for_user
+ aliases = self.casted_by.email_aliases
+ if aliases.select{|a|a.email == self.email}.count > 1
+ errors.add :email, "is already your alias"
+ end
+ end
+
+ def differs_from_main_email
+ user = self.casted_by
+ if user.email == self.email
+ errors.add :email, "may not be the same as your email address"
+ end
+ end
+
+
end