From aa84a06bf96eb1fd5073263ee5955a62c61b9dc8 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 20 Dec 2012 12:47:31 +0100 Subject: validating email domain and displaying it as the placeholder This even works client side. :) --- users/app/models/local_email.rb | 9 +++++++++ users/app/models/user.rb | 4 ++++ users/app/views/users/_email_aliases.html.haml | 2 +- users/app/views/users/_email_field.html.haml | 2 +- 4 files changed, 15 insertions(+), 2 deletions(-) (limited to 'users/app') diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb index c654fcb..c35fdf7 100644 --- a/users/app/models/local_email.rb +++ b/users/app/models/local_email.rb @@ -3,12 +3,18 @@ class LocalEmail < Email validate :unique_on_server validate :unique_alias_for_user validate :differs_from_main_email + before_validation :add_domain_if_needed + validates :email, + :format => { :with => /@#{APP_CONFIG[:domain]}\Z/, + :message => "needs to end in @#{APP_CONFIG[:domain]}"} validates :casted_by, :presence => true def to_partial_path "emails/email" end + protected + def unique_on_server has_email = User.find_by_email_or_alias(email) if has_email && has_email != self.base_doc @@ -30,5 +36,8 @@ class LocalEmail < Email end end + def add_domain_if_needed + self.email = self.email + "@" + APP_CONFIG[:domain] unless self.email.include?("@") + end end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 1999a28..8f776a3 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -32,6 +32,10 @@ class User < CouchRest::Model::Base validates :email, :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} + validates :email, + :format => { :with => /@#{APP_CONFIG[:domain]}\Z/, + :message => "needs to end in @#{APP_CONFIG[:domain]}"} + validates :email_forward, :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} diff --git a/users/app/views/users/_email_aliases.html.haml b/users/app/views/users/_email_aliases.html.haml index 121acc4..e012429 100644 --- a/users/app/views/users/_email_aliases.html.haml +++ b/users/app/views/users/_email_aliases.html.haml @@ -3,4 +3,4 @@ =render @user.email_aliases .clearfix = f.simple_fields_for :email_aliases, @email_alias do |e| - = e.input :email, :placeholder => "alias@#{request.domain}" + = e.input :email, :placeholder => "alias@#{APP_CONFIG[:domain]}" diff --git a/users/app/views/users/_email_field.html.haml b/users/app/views/users/_email_field.html.haml index 36bbeca..edf62c9 100644 --- a/users/app/views/users/_email_field.html.haml +++ b/users/app/views/users/_email_field.html.haml @@ -1 +1 @@ -= f.input :email += f.input :email, :placeholder => "me@#{APP_CONFIG[:domain]}" -- cgit v1.2.3 From bbfdf2df83c0469b0e4a84b76df072179fe1f0d6 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 20 Dec 2012 12:55:23 +0100 Subject: this is what += is there for --- users/app/models/local_email.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'users/app') diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb index c35fdf7..24624e7 100644 --- a/users/app/models/local_email.rb +++ b/users/app/models/local_email.rb @@ -37,7 +37,7 @@ class LocalEmail < Email end def add_domain_if_needed - self.email = self.email + "@" + APP_CONFIG[:domain] unless self.email.include?("@") + self.email += "@" + APP_CONFIG[:domain] unless self.email.include?("@") end end -- cgit v1.2.3 From c83ada50de6b9a7084a23037068ba7f12e09dfc9 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 20 Dec 2012 17:38:36 +0100 Subject: fixed tests, testing corner cases, fixed these --- users/app/models/local_email.rb | 10 ++++++++-- users/app/models/user.rb | 16 ++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'users/app') diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb index 24624e7..d23df71 100644 --- a/users/app/models/local_email.rb +++ b/users/app/models/local_email.rb @@ -5,6 +5,7 @@ class LocalEmail < Email validate :differs_from_main_email before_validation :add_domain_if_needed validates :email, + :presence => true, :format => { :with => /@#{APP_CONFIG[:domain]}\Z/, :message => "needs to end in @#{APP_CONFIG[:domain]}"} validates :casted_by, :presence => true @@ -16,8 +17,8 @@ class LocalEmail < Email protected def unique_on_server - has_email = User.find_by_email_or_alias(email) - if has_email && has_email != self.base_doc + has_email = User.find_by_email_or_alias(email) + if has_email && has_email != self.base_doc errors.add :email, "has already been taken" end end @@ -30,6 +31,8 @@ class LocalEmail < Email end def differs_from_main_email + # If this has not changed but the email let's mark the email invalid instead. + return if self.persisted? user = self.casted_by if user.email == self.email errors.add :email, "may not be the same as your email address" @@ -37,6 +40,9 @@ class LocalEmail < Email end def add_domain_if_needed + if email.blank? + errors.add :email, "may not be empty." + end self.email += "@" + APP_CONFIG[:domain] unless self.email.include?("@") end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 8f776a3..2a8a57b 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -33,9 +33,11 @@ class User < CouchRest::Model::Base :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} validates :email, - :format => { :with => /@#{APP_CONFIG[:domain]}\Z/, + :format => { :with => /\A(.+@#{APP_CONFIG[:domain]})?\Z/, :message => "needs to end in @#{APP_CONFIG[:domain]}"} + validate :email_unique_on_server + validates :email_forward, :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} @@ -125,18 +127,28 @@ class User < CouchRest::Model::Base email_aliases.build(attrs.values.first) if attrs end + protected + ## # Validation Functions ## def email_differs_from_email_aliases + # If this has not changed but the email aliases let's not mark this invalid. 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 end - protected + def email_unique_on_server + return unless email + has_email = User.find_by_email_or_alias(email) + if has_email && has_email != self.base_doc + errors.add :email, "has already been taken" + end + end + def password password_verifier end -- cgit v1.2.3