summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/account.rb18
-rw-r--r--app/models/user.rb17
2 files changed, 30 insertions, 5 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index a85e56c..7310250 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -14,13 +14,26 @@ class Account
@user = user
end
+ #
+ # Creates a new user, with matching identity record.
+ #
# Returns the user record so it can be used in views.
- def self.create(attrs)
+ #
+ # options:
+ #
+ # :invite_required -- if 'false', will overrides app-wide
+ # configuration by same name.
+ #
+ def self.create(attrs, options={})
identity = nil
user = nil
user = User.new(attrs)
+ if options[:invite_required] == false
+ user.ignore_invites!
+ end
user.save
+ # this is not very atomic, but we do the best we can:
if !user.is_tmp? && user.persisted?
identity = user.identity
identity.user_id = user.id
@@ -28,8 +41,7 @@ class Account
identity.errors.each do |attr, msg|
user.errors.add(attr, msg)
end
-
- if APP_CONFIG[:invite_required]
+ if user.invite_required?
user_invite_code = InviteCode.find_by_invite_code user.invite_code
user_invite_code.invite_count += 1
user_invite_code.save
diff --git a/app/models/user.rb b/app/models/user.rb
index 51e9279..cb093cf 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -43,8 +43,7 @@ class User < CouchRest::Model::Base
:mx_with_fallback => true
- validates_with InviteCodeValidator, on: :create, if: -> {APP_CONFIG[:invite_required]}
-
+ validates_with InviteCodeValidator, :on => :create, :if => :invite_required?
timestamps!
@@ -57,6 +56,11 @@ class User < CouchRest::Model::Base
include TemporaryUser # MUST come after designs are defined.
+ def initialize(*args)
+ super
+ @invite_required = APP_CONFIG[:invite_required]
+ end
+
def self.login_starts_with(query)
self.by_login.startkey(query).endkey(query + "\ufff0")
end
@@ -113,6 +117,15 @@ class User < CouchRest::Model::Base
false
end
+ def invite_required?
+ @invite_required
+ end
+
+ # should only be called for testing or monitoring purposes
+ def ignore_invites!
+ @invite_required = false
+ end
+
def most_recent_tickets(count=3)
Ticket.for_user(self).limit(count).all #defaults to having most recent updated first
end