summaryrefslogtreecommitdiff
path: root/app/models/invite_code_validator.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/invite_code_validator.rb')
-rw-r--r--app/models/invite_code_validator.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb
new file mode 100644
index 0000000..f96ca4a
--- /dev/null
+++ b/app/models/invite_code_validator.rb
@@ -0,0 +1,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
+
+