diff options
author | azul <azul@leap.se> | 2015-10-13 15:20:26 +0000 |
---|---|---|
committer | azul <azul@leap.se> | 2015-10-13 15:20:26 +0000 |
commit | fd55d71cd1cbadcc5378601c0c9dfa6292bd4725 (patch) | |
tree | c36e96d7deb8191f7bc04c2d509e9f70c6de6754 | |
parent | 70073b1ac48f96d3e375630fb7f3904351e0f696 (diff) | |
parent | cb89699283aeb1ef4195a5c99d0319f3232f5db7 (diff) |
Merge pull request #201 from Alster-Hamburgers/multi-invite
Allow multi-use invite codes
-rw-r--r-- | app/models/invite_code.rb | 1 | ||||
-rw-r--r-- | app/models/invite_code_validator.rb | 7 | ||||
-rw-r--r-- | lib/tasks/invite_code.rake | 17 | ||||
-rw-r--r-- | test/unit/invite_code_test.rb | 42 | ||||
-rw-r--r-- | test/unit/invite_code_validator_test.rb | 56 |
5 files changed, 72 insertions, 51 deletions
diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb index 30a6498..13c3a39 100644 --- a/app/models/invite_code.rb +++ b/app/models/invite_code.rb @@ -4,6 +4,7 @@ class InviteCode < CouchRest::Model::Base use_database 'invite_codes' property :invite_code, String, :read_only => true property :invite_count, Integer, :default => 0, :accessible => true + property :max_uses, Integer, :default => 1 timestamps! diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb index f96ca4a..676e4fa 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 @@ -6,7 +7,7 @@ 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) end end @@ -16,8 +17,8 @@ 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.max_uses end def add_error_to_user(error, user) diff --git a/lib/tasks/invite_code.rake b/lib/tasks/invite_code.rake index f3bafac..d6e2b49 100644 --- a/lib/tasks/invite_code.rake +++ b/lib/tasks/invite_code.rake @@ -1,16 +1,21 @@ desc "Generate a batch of invite codes" -task :generate_invites, [:n] => :environment do |task, args| +task :generate_invites, [:n, :u] => :environment do |task, args| - codes = args.n - codes = codes.to_i + codes = args.n + codes = codes.to_i - codes.times do |x| + if args.u != nil + max_uses = args.u + end + + codes.times do |x| x = InviteCode.new + x.max_uses = max_uses x.save - puts "#{x.invite_code} Code generated." - + puts x.invite_code end + end diff --git a/test/unit/invite_code_test.rb b/test/unit/invite_code_test.rb index b17d1bc..fd93f2f 100644 --- a/test/unit/invite_code_test.rb +++ b/test/unit/invite_code_test.rb @@ -21,47 +21,5 @@ class InviteCodeTest < ActiveSupport::TestCase end - test "Invite count >0 is not accepted for new account signup" do - validator = InviteCodeValidator.new nil - - user_code = InviteCode.new - user_code.invite_count = 1 - user_code.save - - user = FactoryGirl.build :user - user.invite_code = user_code.invite_code - - validator.validate(user) - - assert_equal ["This code has already been used"], user.errors[:invite_code] - - end - - test "Invite count 0 is accepted for new account signup" do - validator = InviteCodeValidator.new nil - - user_code = InviteCode.create - - user = FactoryGirl.build :user - user.invite_code = user_code.invite_code - - validator.validate(user) - - assert_equal [], user.errors[:invite_code] - end - - test "There is an error message if the invite code does not exist" do - validator = InviteCodeValidator.new nil - - user = FactoryGirl.build :user - user.invite_code = "wrongcode" - - validator.validate(user) - - assert_equal ["This is not a valid code"], user.errors[:invite_code] - - end - - end diff --git a/test/unit/invite_code_validator_test.rb b/test/unit/invite_code_validator_test.rb index ee8f1b3..62eeae6 100644 --- a/test/unit/invite_code_validator_test.rb +++ b/test/unit/invite_code_validator_test.rb @@ -27,4 +27,60 @@ class InviteCodeValidatorTest < ActiveSupport::TestCase assert_equal errors, invalid_user.errors.messages end end + + + test "Invite count >= invite max uses is not accepted for new account signup" do + validator = InviteCodeValidator.new nil + + user_code = InviteCode.new + user_code.invite_count = 1 + user_code.save + + user = FactoryGirl.build :user + user.invite_code = user_code.invite_code + + validator.validate(user) + + assert_equal ["This code has already been used"], user.errors[:invite_code] + + end + + test "Invite count < invite max uses is accepted for new account signup" do + validator = InviteCodeValidator.new nil + + user_code = InviteCode.create + user_code.save + + user = FactoryGirl.build :user + user.invite_code = user_code.invite_code + + validator.validate(user) + + assert_equal [], user.errors[:invite_code] + end + + test "Invite count 0 is accepted for new account signup" do + validator = InviteCodeValidator.new nil + + user_code = InviteCode.create + + user = FactoryGirl.build :user + user.invite_code = user_code.invite_code + + validator.validate(user) + + assert_equal [], user.errors[:invite_code] + end + + test "There is an error message if the invite code does not exist" do + validator = InviteCodeValidator.new nil + + user = FactoryGirl.build :user + user.invite_code = "wrongcode" + + validator.validate(user) + + assert_equal ["This is not a valid code"], user.errors[:invite_code] + end + end
\ No newline at end of file |