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.rb | 2 ++ app/models/invite_code_validator.rb | 13 ++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) (limited to 'app/models') diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb index 30a6498..0086804 100644 --- a/app/models/invite_code.rb +++ b/app/models/invite_code.rb @@ -4,12 +4,14 @@ 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 :invite_max_uses, Integer, :default => 1, :accessible => true timestamps! design do view :by_invite_code view :by_invite_count + view :by_invite_max_uses end def initialize(attributes = {}, options = {}) 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