summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorazul <azul@leap.se>2015-09-30 10:51:21 +0200
committerazul <azul@leap.se>2015-09-30 10:51:21 +0200
commitd45f6c61f6a13be06f1977b857e0cb31e79c5317 (patch)
treed3089b334c2663ea2fd76cf62dea853bdd57b047 /app/models
parenta894966e425f27c31e7da196658c6ddee3fc3714 (diff)
parentd4f10a8d47572bcab4c44878b952146732d64d2e (diff)
Merge pull request #194 from Alster-Hamburgers/feature/invite_code
Request for feedback on invite code feature
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account.rb11
-rw-r--r--app/models/invite_code.rb22
-rw-r--r--app/models/invite_code_validator.rb28
-rw-r--r--app/models/user.rb6
4 files changed, 64 insertions, 3 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index af470ed..a5cd833 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.
@@ -20,6 +20,7 @@ class Account
user = nil
user = User.new(attrs)
user.save
+
if !user.tmp? && user.persisted?
identity = user.identity
identity.user_id = user.id
@@ -27,6 +28,12 @@ class Account
identity.errors.each do |attr, msg|
user.errors.add(attr, msg)
end
+
+ if APP_CONFIG[:invite_required]
+ user_invite_code = InviteCode.find_by_invite_code user.invite_code
+ user_invite_code.invite_count += 1
+ user_invite_code.save
+ end
end
rescue StandardError => ex
user.errors.add(:base, ex.to_s) if user
diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb
new file mode 100644
index 0000000..30a6498
--- /dev/null
+++ b/app/models/invite_code.rb
@@ -0,0 +1,22 @@
+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 = {})
+ super(attributes, options)
+ write_attribute('invite_code', CouponCode.generate) if new?
+ end
+
+end
+
+
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
+
+
diff --git a/app/models/user.rb b/app/models/user.rb
index d44df40..3daee0f 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -8,7 +8,7 @@ class User < CouchRest::Model::Base
property :password_salt, String, :accessible => true
property :contact_email, String, :accessible => true
property :contact_email_key, String, :accessible => true
-
+ property :invite_code, String, :accessible => true
property :enabled, TrueClass, :default => true
# these will be null by default but we shouldn't ever pull them directly, but only via the methods that will return the full ServiceLevel
@@ -39,6 +39,10 @@ class User < CouchRest::Model::Base
:email => true,
:mx_with_fallback => true
+
+ validates_with InviteCodeValidator, on: :create, if: -> {APP_CONFIG[:invite_required]}
+
+
timestamps!
design do