summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2014-05-29 11:19:21 +0200
committerAzul <azul@leap.se>2014-05-29 11:19:21 +0200
commitbbe7b3b7deb2b44d34f7c39dda2c3db284e2bf10 (patch)
tree1b921c5243e51d6aaa413ed71455d51f86f94353 /app
parent85e066920568c19b788b8789c4659092224bb517 (diff)
clearify identity validations
Identity.new.valid? should not crash. So validate presence where needed and skip the other validations if the value is absent.
Diffstat (limited to 'app')
-rw-r--r--app/models/identity.rb23
1 files changed, 16 insertions, 7 deletions
diff --git a/app/models/identity.rb b/app/models/identity.rb
index f2727c8..2f6241c 100644
--- a/app/models/identity.rb
+++ b/app/models/identity.rb
@@ -10,7 +10,9 @@ class Identity < CouchRest::Model::Base
property :keys, HashWithIndifferentAccess
property :cert_fingerprints, Hash
- validate :alias_available
+ validates :address, presence: true
+ validate :address_available
+ validates :destination, presence: true, if: :enabled?
validates :destination, uniqueness: {scope: :address}
validate :address_local_email
validate :destination_email
@@ -94,7 +96,11 @@ class Identity < CouchRest::Model::Base
end
def enabled?
- self.destination && self.user_id
+ self.user_id
+ end
+
+ def disabled?
+ !enabled?
end
def disable
@@ -123,12 +129,12 @@ class Identity < CouchRest::Model::Base
# for LoginFormatValidation
def login
- self.address.handle
+ address.handle if address.present?
end
protected
- def alias_available
+ def address_available
blocking_identities = Identity.by_address.key(address).all
blocking_identities.delete self
if self.user
@@ -140,15 +146,18 @@ class Identity < CouchRest::Model::Base
end
def address_local_email
- return if address.valid? #this ensures it is a valid local email address
+ # caught by presence validation
+ return if address.blank?
+ return if address.valid?
address.errors.each do |attribute, error|
self.errors.add(:address, error)
end
end
def destination_email
- return if destination.nil? # this identity is disabled
- return if destination.valid? # this ensures it is Email
+ # caught by presence validation or this identity is disabled
+ return if destination.blank?
+ return if destination.valid?
destination.errors.each do |attribute, error|
self.errors.add(:destination, error)
end