From 154d32bbc7cfe21d83141ff2c9a3d805165231b8 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 28 May 2014 10:45:14 +0200 Subject: use Identity for testing login availability We create an identity alongside each user. Make sure the identity is valid when creating the user. This also ensures that the login picked is available because otherwise the identities address would not be available anymore. --- app/models/user.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'app/models/user.rb') diff --git a/app/models/user.rb b/app/models/user.rb index 6678de6..6b4d1a9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -24,7 +24,7 @@ class User < CouchRest::Model::Base :uniqueness => true, :if => :serverside? - validate :login_is_unique_alias + validate :identity_is_valid validates :password_salt, :password_verifier, :format => { :with => /\A[\dA-Fa-f]+\z/, :message => "Only hex numbers allowed" } @@ -161,12 +161,11 @@ class User < CouchRest::Model::Base # Validation Functions ## - def login_is_unique_alias - alias_identity = Identity.find_by_address(self.email_address) - return if alias_identity.blank? - if alias_identity.user != self - errors.add(:login, "has already been taken") - end + def identity_is_valid + refresh_identity + return if identity.valid? + # hand on the first error only for now + self.errors.add(:login, identity.errors.messages.values.first) end def password -- cgit v1.2.3 From 682b4060cb86c52ffda638f4f9a837f107540610 Mon Sep 17 00:00:00 2001 From: Azul Date: Wed, 28 May 2014 11:44:12 +0200 Subject: ensure identity is cleared on user.reload - fixes test --- app/models/user.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'app/models/user.rb') diff --git a/app/models/user.rb b/app/models/user.rb index 6b4d1a9..33508b5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -42,6 +42,11 @@ class User < CouchRest::Model::Base view :by_created_at end # end of design + def reload + super + @identity = nil + end + def to_json(options={}) { :login => login, -- cgit v1.2.3 From e0d31118d6e4110d2c280afa9415cfe9def29deb Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 29 May 2014 10:04:07 +0200 Subject: hand on errors from Email to Identity to User errors.each iterates through all errors for all attrbibutes nicely. --- app/models/user.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/models/user.rb') diff --git a/app/models/user.rb b/app/models/user.rb index 33508b5..84a795e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -167,10 +167,10 @@ class User < CouchRest::Model::Base ## def identity_is_valid - refresh_identity return if identity.valid? - # hand on the first error only for now - self.errors.add(:login, identity.errors.messages.values.first) + identity.errors.each do |attribute, error| + self.errors.add(:login, error) + end end def password -- cgit v1.2.3 From 85e066920568c19b788b8789c4659092224bb517 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 29 May 2014 10:37:31 +0200 Subject: ensure User#reload returns self --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/user.rb') diff --git a/app/models/user.rb b/app/models/user.rb index 84a795e..f8b9ddc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -43,8 +43,8 @@ class User < CouchRest::Model::Base end # end of design def reload - super @identity = nil + super end def to_json(options={}) -- cgit v1.2.3