summaryrefslogtreecommitdiff
path: root/users/app/models/local_email.rb
diff options
context:
space:
mode:
authorjessib <jessib@riseup.net>2013-08-08 11:54:19 -0700
committerjessib <jessib@riseup.net>2013-08-08 11:54:19 -0700
commitbca39b8f0d44dc31f77a3bfc8a94d452b4c77670 (patch)
treee3ee949bf6e02fc1bbc2ab8e3f247105b962b08e /users/app/models/local_email.rb
parent31441fc921c3a60bff7c606f1da343fdd62d80d5 (diff)
parentb1065710102193ba11e2a18b5f6506ba1d5b31f9 (diff)
Merge pull request #64 from azul/feature/identity-rewrite
Feature/identity rewrite
Diffstat (limited to 'users/app/models/local_email.rb')
-rw-r--r--users/app/models/local_email.rb68
1 files changed, 22 insertions, 46 deletions
diff --git a/users/app/models/local_email.rb b/users/app/models/local_email.rb
index 69cba01..c1f7c11 100644
--- a/users/app/models/local_email.rb
+++ b/users/app/models/local_email.rb
@@ -1,63 +1,39 @@
-class LocalEmail
- include CouchRest::Model::Embeddable
- include Email
+class LocalEmail < Email
- property :username, String
- before_validation :strip_domain_if_needed
-
- validates :username,
- :presence => true,
- :format => { :with => /\A([^@\s]+)(@#{APP_CONFIG[:domain]})?\Z/i, :message => "needs to be a valid login or email address @#{APP_CONFIG[:domain]}"}
-
- validate :unique_on_server
- validate :unique_alias_for_user
- validate :differs_from_login
-
- validates :casted_by, :presence => true
-
- def email
- return '' if username.nil?
- username + '@' + APP_CONFIG[:domain]
+ def self.domain
+ APP_CONFIG[:domain]
end
- def email=(value)
- return if value.blank?
- self.username = value
- strip_domain_if_needed
+ validates :email,
+ :format => {
+ :with => /@#{domain}\Z/i,
+ :message => "needs to end in @#{domain}"
+ }
+
+ def initialize(s)
+ super
+ append_domain_if_needed
end
def to_key
- [username]
+ [handle]
end
- protected
-
- def unique_on_server
- has_email = User.find_by_login_or_alias(username)
- if has_email && has_email != self.casted_by
- errors.add :username, "has already been taken"
- end
+ def handle
+ gsub(/@#{domain}/i, '')
end
- def unique_alias_for_user
- aliases = self.casted_by.email_aliases
- if aliases.select{|a|a.username == self.username}.count > 1
- errors.add :username, "is already your alias"
- end
+ def domain
+ LocalEmail.domain
end
- def differs_from_login
- # 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.login == self.username
- errors.add :username, "may not be the same as your email address"
- end
- end
+ protected
- def strip_domain_if_needed
- self.username.gsub! /@#{APP_CONFIG[:domain]}/i, ''
+ def append_domain_if_needed
+ unless self.index('@')
+ self << '@' + domain
+ end
end
end