blob: f96ca4abf255a63236a516b045b2dd2240d57840 (
plain)
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
|
class InviteCodeValidator < ActiveModel::Validator
def validate(user)
user_invite_code = InviteCode.find_by_invite_code user.invite_code
if not_existent?(user_invite_code)
add_error_to_user("This is not a valid code", user)
elsif count_greater_than_zero?(user_invite_code)
add_error_to_user("This code has already been used", user)
end
end
private
def not_existent?(code)
code == nil
end
def count_greater_than_zero?(code)
code.invite_count > 0
end
def add_error_to_user(error, user)
user.errors[:invite_code] << error
end
end
|