summaryrefslogtreecommitdiff
path: root/app/models/user.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/user.rb')
-rw-r--r--app/models/user.rb94
1 files changed, 48 insertions, 46 deletions
diff --git a/app/models/user.rb b/app/models/user.rb
index b2c7862..a39037c 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,6 +1,7 @@
class User < ActiveRecord::Base
- before_save :default_values
-
+ ##################################################################
+ # Relationships #
+ ##################################################################
has_and_belongs_to_many :tournaments_played, class_name: "Tournament", foreign_key: "player_id", join_table: "players_tournaments"
has_and_belongs_to_many :tournaments_hosted, class_name: "Tournament", foreign_key: "host_id", join_table: "hosts_tournaments"
has_and_belongs_to_many :teams
@@ -8,20 +9,49 @@ class User < ActiveRecord::Base
has_many :statistics
has_many :remote_usernames
- apply_simple_captcha
+ ##################################################################
+ # Attributes #
+ ##################################################################
- acts_as_messageable
+ # name:string
+ validates(:name, presence: true, length: { maximum: 50 })
+ # email:string:uniq
+ before_save { self.email = email.downcase }
+ validates(:email,
+ presence: true,
+ format: {with: /\A\S+@\S+\.\S+\z/i},
+ uniqueness: { case_sensitive: false })
+
+ # user_name:string_uniq
+ validates(:user_name,
+ presence: true,
+ length:{maximum: 50},
+ format: {with: /\A[a-zA-Z0-9 _\-]+\z/},
+ uniqueness: {case_sensitive: false })
+
+ # password_digest:string
+ has_secure_password validations: false # maps :password and :password_confirmation to :password_digest
+ validates(:password,
+ length: { minimum: 6 },
+ confirmation: true,
+ unless: Proc.new { |u| u.password.try(:empty?) and not u.password_digest.try(:empty?) })
+
+ # permissions:integer
+ before_save { self.permissions ||= Server.first.default_user_permissions }
+
+ ##################################################################
+ # XXX: hard-coded-ish. It makes me feel dirty. #
+ ##################################################################
+ apply_simple_captcha
+ acts_as_messageable
def mailboxer_email(object)
return nil
end
- before_save { self.email = email.downcase }
- before_save { self.user_name = user_name }
-
- def default_values
- self.permissions ||= Server.first.default_user_permissions
- end
+ ##################################################################
+ # remote_usernames #
+ ##################################################################
def set_remote_username(game, data)
remote = self.remote_usernames.where(:game => game).first
@@ -46,6 +76,10 @@ class User < ActiveRecord::Base
end
end
+ ##################################################################
+ # Permissions #
+ ##################################################################
+
def self.permission_bits
return {
:create_tournament => (2**1),
@@ -104,7 +138,6 @@ class User < ActiveRecord::Base
end
end
-
# A representation of the permission bits as a mock-array.
def abilities
@abilities ||= Abilities.new(self)
@@ -122,7 +155,7 @@ class User < ActiveRecord::Base
# easy to modify them using a form.
class Abilities
def initialize(user)
- @user = user
+o @user = user
end
def [](ability)
return @user.can?(ability)
@@ -146,40 +179,9 @@ class User < ActiveRecord::Base
end
end
- # VAILD_EMAIL is the regex used to validate a user given email.
- VALID_EMAIL_REG = /\A\S+@\S+\.\S+\z/i
-
- # VALID_USER_NAME checks to make sure a user's user_name
- # is in the proper format.
- VALID_USER_NAME_REG = /\A[a-zA-Z0-9 _\-]+\z/
-
- # The following lines put a user account through a series of
- # validations in order to make sure all of their information
- # is in the proper format.
- #
- # validates :symbol_to_be_validated
- #
- # - presence: determines whether or not a symbol is filled or not
- # - length: ensures there is a length limit on the symbol
- # - format: checks the format of given information to ensure
- # validity
- validates(:name, presence: true, length: { maximum: 50 })
- validates(:email, presence: true, format: {with:
- VALID_EMAIL_REG},
- uniqueness: { case_sensitive: false })
- validates(:user_name, presence: true, length:{maximum: 50},
- format: {with: VALID_USER_NAME_REG },
- uniqueness: {case_sensitive: false })
-
- # Instead of adding password and password_confirmation
- # attributes, requiring the presence of a password,
- # requiring that pw and pw_com match, and add an authenticate
- # method to compare an encrypted password to the
- # password_digest to authenticate users, I can just add
- # has_secure_password which does all of this for me.
- has_secure_password
-
- validates :password, length: { minimum: 6 }
+ ##################################################################
+ # Null-object pattern #
+ ##################################################################
class NilUser
def nil?