summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/account.rb5
-rw-r--r--app/models/invite_code.rb3
-rw-r--r--app/models/invite_code_validator.rb20
-rw-r--r--app/models/user.rb2
-rw-r--r--test/unit/invite_code_test.rb33
5 files changed, 56 insertions, 7 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index af470ed..a57e3f7 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -1,7 +1,7 @@
#
-# The Account model takes care of the livecycle of a user.
+# The Account model takes care of the lifecycle of a user.
# It composes a User record and it's identity records.
-# It also allows for other engines to hook into the livecycle by
+# It also allows for other engines to hook into the lifecycle by
# monkeypatching the create, update and destroy methods.
# There's an ActiveSupport load_hook at the end of this file to
# make this more easy.
@@ -19,6 +19,7 @@ class Account
identity = nil
user = nil
user = User.new(attrs)
+
user.save
if !user.tmp? && user.persisted?
identity = user.identity
diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb
index d52436f..30a6498 100644
--- a/app/models/invite_code.rb
+++ b/app/models/invite_code.rb
@@ -3,10 +3,13 @@ require 'coupon_code'
class InviteCode < CouchRest::Model::Base
use_database 'invite_codes'
property :invite_code, String, :read_only => true
+ property :invite_count, Integer, :default => 0, :accessible => true
+
timestamps!
design do
view :by_invite_code
+ view :by_invite_count
end
def initialize(attributes = {}, options = {})
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
+
+
diff --git a/app/models/user.rb b/app/models/user.rb
index 06e1a0f..a4dd132 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -39,8 +39,10 @@ class User < CouchRest::Model::Base
:email => true,
:mx_with_fallback => true
+
validates_with InviteCodeValidator
+
timestamps!
design do
diff --git a/test/unit/invite_code_test.rb b/test/unit/invite_code_test.rb
index 2684f8e..b6044f4 100644
--- a/test/unit/invite_code_test.rb
+++ b/test/unit/invite_code_test.rb
@@ -10,12 +10,39 @@ class InviteCodeTest < ActiveSupport::TestCase
test "the invite code can be read from couch db correctly" do
code1 = InviteCode.new
code1.save
-
code2 = InviteCode.find_by__id code1.id
-
assert_equal code1.invite_code, code2.invite_code
+ end
+ test "the invite code count gets set to 0 upon creation" do
+ code1 = InviteCode.new
+ code1.save
+ assert_equal code1.invite_count, 0
end
+ # TODO: does the count go up when code gets entered?
+ test "Invite code count goes up by 1 when the invite code is entered" do
+
+ validator = InviteCodeValidator.new nil
+
+ user = FactoryGirl.build :user
+ user_code = InviteCode.new
+ user_code.save
+ user.invite_code = user_code.invite_code
+
+
+ validator.validate(user)
+
+ user_code.reload
+ assert_equal 1, user_code.invite_count
+
+ end
+#
+#
+# # TODO: count >0 is not accepted for signup
+ # test "Invite count >0 is not accepted for new account signup" do
+
+ # end
+
+end
-end \ No newline at end of file