summaryrefslogtreecommitdiff
path: root/app/models/invite_code_validator.rb
diff options
context:
space:
mode:
authorankonym <ankonym@gmail.com>2015-08-25 14:11:00 +0200
committerankonym <ankonym@gmail.com>2015-09-28 15:12:45 +0200
commit45c3fadd930a474951bd918a50e1ea2b00af75c7 (patch)
tree3439fc94cc8e6533a92d06a2fd751a3804e71050 /app/models/invite_code_validator.rb
parent5f9aec72f124a971b765c14052363f3ce1e16c65 (diff)
Make sure codes can only be used once, fix validations
We introduced a count on invite codes to make sure that (at the moment) codes can only be used once. (The code will also allow multi-use codes in the future.) Also, some of our validations weren't validating against the correct data, which is now fixed.
Diffstat (limited to 'app/models/invite_code_validator.rb')
-rw-r--r--app/models/invite_code_validator.rb20
1 files changed, 18 insertions, 2 deletions
diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb
index 978d3f5..73c333a 100644
--- a/app/models/invite_code_validator.rb
+++ b/app/models/invite_code_validator.rb
@@ -1,7 +1,17 @@
class InviteCodeValidator < ActiveModel::Validator
def validate(user)
- if not_existent?(user.invite_code)
+
+ user_invite_code = InviteCode.find_by_invite_code user.invite_code
+
+ if not_existent?(user_invite_code.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
@@ -10,7 +20,13 @@ class InviteCodeValidator < ActiveModel::Validator
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 \ No newline at end of file
+end
+
+