user.rb
2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require_dependency 'user'
class User
GOV_SUFFIX = /^.*@[gov.br|jus.br|leg.br|mp.br]+$/
has_and_belongs_to_many :institutions
validate :email_different_secondary?, :email_has_already_been_used?,
:secondary_email_format, :email_suffix_is_gov?
scope :primary_or_secondary_email_already_used?, lambda { |email|
where("email=? OR secondary_email=?", email, email)
}
def email_different_secondary?
self.errors.add(
:base,
_("Email must be different from secondary email.")
) if self.email == self.secondary_email
end
def email_has_already_been_used?
user_already_saved = User.find(:first,
:conditions => ["email = ?", self.email])
if user_already_saved.nil?
primary_email_hasnt_been_used =
User.primary_or_secondary_email_already_used?(self.email).empty?
if !self.secondary_email.nil? and self.secondary_email.empty?
self.secondary_email = nil
end
secondary_email_hasnt_been_used =
User.primary_or_secondary_email_already_used?(self.secondary_email).
empty?
if !primary_email_hasnt_been_used or !secondary_email_hasnt_been_used
self.errors.add(:base, _("E-mail or secondary e-mail already taken."))
end
end
end
def secondary_email_format
if !self.secondary_email.nil? and self.secondary_email.length > 0
test = /\A[^@]+@([^@\.]+\.)+[^@\.]+\z/
unless test.match(self.secondary_email)
self.errors.add(:base, _("Invalid secondary email format."))
end
end
end
def email_suffix_is_gov?
check_gov_suffix_in_secondary_email
check_gov_email_have_institution
end
private
def valid_format?(value, string_format)
!value.nil? && value.length > 0 && !string_format.match(value).nil?
end
def check_gov_suffix_in_secondary_email
unless primary_email_has_gov_suffix?
self.errors.add(
:base,
_("The governamental email must be the primary one.")
) if secondary_email_has_gov_suffix?
end
end
def check_gov_email_have_institution
self.errors.add(
:base,
_("Institution is obligatory if user has a government email.")
) if primary_email_has_gov_suffix? && self.institutions.blank?
end
def primary_email_has_gov_suffix?
valid_format?(self.email, GOV_SUFFIX)
end
def secondary_email_has_gov_suffix?
valid_format?(self.secondary_email, GOV_SUFFIX)
end
end