From b90ce01890907d1c7f46f46bafcef416570a4c4b Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 17 Dec 2012 17:48:39 +0100 Subject: enabled destroying email aliases - no ajax yet. --- users/app/models/email.rb | 8 ++++++++ users/app/models/user.rb | 4 ++++ 2 files changed, 12 insertions(+) (limited to 'users/app/models') diff --git a/users/app/models/email.rb b/users/app/models/email.rb index 4b01838..0745fda 100644 --- a/users/app/models/email.rb +++ b/users/app/models/email.rb @@ -14,4 +14,12 @@ class Email def to_s email end + + def ==(other) + other.is_a?(String) ? self.email == other : super + end + + def to_param + email + end end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 10f358d..d66b0e9 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -45,6 +45,7 @@ class User < CouchRest::Model::Base view :by_login view :by_created_at view :by_email + view :by_email_alias, :map => <<-EOJS function(doc) { @@ -56,6 +57,7 @@ class User < CouchRest::Model::Base }); } EOJS + view :by_email_or_alias, :map => <<-EOJS function(doc) { @@ -70,6 +72,7 @@ class User < CouchRest::Model::Base }); } EOJS + end class << self @@ -127,6 +130,7 @@ class User < CouchRest::Model::Base end end + ## # Validation Functions ## -- cgit v1.2.3 From 285b23f39765d8346a658a81eca8b70d70b3e9bf Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 18 Dec 2012 13:41:41 +0100 Subject: 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. --- users/app/models/local_email.rb | 21 ++++++++++++++++++++- users/app/models/user.rb | 26 +++++--------------------- 2 files changed, 25 insertions(+), 22 deletions(-) (limited to 'users/app/models') 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 diff --git a/users/app/models/user.rb b/users/app/models/user.rb index d66b0e9..0773f28 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -35,9 +35,7 @@ class User < CouchRest::Model::Base validates :email_forward, :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} - validate :no_duplicate_email_aliases - - validate :email_aliases_differ_from_email + validate :email_differs_from_email_aliases timestamps! @@ -117,34 +115,20 @@ class User < CouchRest::Model::Base APP_CONFIG['admins'].include? self.login end - def add_email_alias(email) - email = LocalEmail.new(email) unless email.is_a? Email - email_aliases << email - end - # this currently only adds the first email address submitted. # All the ui needs for now. def email_aliases_attributes=(attrs) - if attrs && attrs.values.first - add_email_alias attrs.values.first - end + email_aliases.build(attrs.values.first) if attrs end - ## # Validation Functions ## - # TODO: How do we handle these errors? - def no_duplicate_email_aliases - if email_aliases.count != email_aliases.map(&:email).uniq.count - errors.add(:email_aliases, "include a duplicate") - end - end - - def email_aliases_differ_from_email + def email_differs_from_email_aliases + return if email_aliases.last.errors.any? if email_aliases.map(&:email).include?(email) - errors.add(:email_aliases, "include the original email address") + errors.add(:email, "may not be the same as an alias") end end -- cgit v1.2.3 From 76904d3f677520eb191a90f2abb15b9df5fd8ae9 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 18 Dec 2012 14:02:31 +0100 Subject: make sure we have email_aliases at all before testing for an error on the last --- users/app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'users/app/models') diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 0773f28..1999a28 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -126,7 +126,7 @@ class User < CouchRest::Model::Base ## def email_differs_from_email_aliases - return if email_aliases.last.errors.any? + return if email_aliases.any? and email_aliases.last.errors.any? if email_aliases.map(&:email).include?(email) errors.add(:email, "may not be the same as an alias") end -- cgit v1.2.3