From 82a33300818dd8f6e06856944fe7c658746efca1 Mon Sep 17 00:00:00 2001 From: ankonym Date: Fri, 2 Oct 2015 16:51:57 +0200 Subject: Allow multi-use invite codes Introduce a invite_max_uses property to invite codes to allow admins to set a maximum number of uses for invite codes. --- app/models/invite_code_validator.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'app/models/invite_code_validator.rb') diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb index f96ca4a..d52e698 100644 --- a/app/models/invite_code_validator.rb +++ b/app/models/invite_code_validator.rb @@ -6,8 +6,11 @@ class InviteCodeValidator < ActiveModel::Validator 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) + elsif has_no_uses_left?(user_invite_code) add_error_to_user("This code has already been used", user) + + # elsif count_greater_than_zero?(user_invite_code) + # add_error_to_user("This code has already been used", user) end end @@ -16,10 +19,14 @@ class InviteCodeValidator < ActiveModel::Validator code == nil end - def count_greater_than_zero?(code) - code.invite_count > 0 + def has_no_uses_left?(code) + code.invite_count >= code.invite_max_uses 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 -- cgit v1.2.3 From 6f09343e85ab23c4d81f827bbd47a720f3e2cf7b Mon Sep 17 00:00:00 2001 From: ankonym Date: Fri, 2 Oct 2015 20:58:29 +0200 Subject: Update rake task to allow generation of multi-use invites The rake task now takes a second (optional) argument that sets the number of uses per invite code. If this is omitted, the default number of uses is 1. (This commit also contains some minor code cleanup that removes some stuff that I'd commented out but not removed.) --- app/models/invite_code_validator.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'app/models/invite_code_validator.rb') diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb index d52e698..9dfe6fa 100644 --- a/app/models/invite_code_validator.rb +++ b/app/models/invite_code_validator.rb @@ -1,4 +1,5 @@ class InviteCodeValidator < ActiveModel::Validator + def validate(user) user_invite_code = InviteCode.find_by_invite_code user.invite_code @@ -8,9 +9,6 @@ class InviteCodeValidator < ActiveModel::Validator elsif has_no_uses_left?(user_invite_code) add_error_to_user("This code has already been used", user) - - # elsif count_greater_than_zero?(user_invite_code) - # add_error_to_user("This code has already been used", user) end end @@ -23,10 +21,6 @@ class InviteCodeValidator < ActiveModel::Validator code.invite_count >= code.invite_max_uses 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 -- cgit v1.2.3 From da9608a906d4369afb70e042069e219bc2e2d282 Mon Sep 17 00:00:00 2001 From: ankonym Date: Tue, 6 Oct 2015 13:27:32 +0200 Subject: Integrated feedback on multi-invite codes Removing some superfluous code, mostly, and structuring tests better. --- app/models/invite_code_validator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/invite_code_validator.rb') diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb index 9dfe6fa..676e4fa 100644 --- a/app/models/invite_code_validator.rb +++ b/app/models/invite_code_validator.rb @@ -18,7 +18,7 @@ class InviteCodeValidator < ActiveModel::Validator end def has_no_uses_left?(code) - code.invite_count >= code.invite_max_uses + code.invite_count >= code.max_uses end def add_error_to_user(error, user) -- cgit v1.2.3