blob: 9dfe6fa5d2682b24285576f2b5483db5c3e70fd5 (
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
29
|
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 has_no_uses_left?(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 has_no_uses_left?(code)
code.invite_count >= code.invite_max_uses
end
def add_error_to_user(error, user)
user.errors[:invite_code] << error
end
end
|