blob: d03d51004c88bc9297577b92ee9e4a18c45a5acd (
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
30
31
32
33
|
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.invite_count)
add_error_to_user("This code has already been used", user)
else
user_invite_code.invite_count += 1
user_invite_code.save
end
end
private
def not_existent?(code)
InviteCode.find_by_invite_code(code) == nil
end
def count_greater_than_zero?(code)
code > 0
end
def add_error_to_user(error, user)
user.errors[:invite_code] << error
end
end
|