blob: 676e4fab2ea863d54e384d0f21857c2eafc30858 (
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.max_uses
end
def add_error_to_user(error, user)
user.errors[:invite_code] << error
end
end
|