From 60e8fc3e1309d1c972a7695e3344e63f5d633a06 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 12 Dec 2012 15:41:14 +0100 Subject: find users by email and aliases --- users/app/models/email.rb | 4 +--- users/app/models/user.rb | 30 ++++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) (limited to 'users/app') diff --git a/users/app/models/email.rb b/users/app/models/email.rb index 7c88c51..45101c1 100644 --- a/users/app/models/email.rb +++ b/users/app/models/email.rb @@ -3,11 +3,9 @@ class Email property :email, String - validates :email_forward, + validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, :message => "needs to be a valid email address"} - timestamps! - def to_s email end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index a41554d..7d1691a 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -28,16 +28,42 @@ class User < CouchRest::Model::Base # TODO: write a proper email validator to be used in the different places validates :email, - :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, :message => "needs to be a valid email address"} + :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} validates :email_forward, - :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/, :message => "needs to be a valid email address"} + :format => { :with => /\A(([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,}))?\Z/, :message => "needs to be a valid email address"} timestamps! design do view :by_login view :by_created_at + view :by_email + view :by_email_alias, + :map => <<-EOJS + function(doc) { + if (doc.type != 'User') { + return; + } + doc.email_aliases.forEach(function(alias){ + emit(alias.email, doc); + }); + } + EOJS + view :by_email_or_alias, + :map => <<-EOJS + function(doc) { + if (doc.type != 'User') { + return; + } + if (doc.email) { + emit(doc.email, doc); + } + doc.email_aliases.forEach(function(alias){ + emit(alias.email, doc); + }); + } + EOJS end class << self -- cgit v1.2.3