only allow alphanumeric characters for a username
parent
c4fc51b070
commit
6504bddf97
|
@ -9,6 +9,7 @@ class User < ActiveRecord::Base
|
||||||
validates_uniqueness_of :user_name, :if => lambda { |x| x.facebook_id.nil? }
|
validates_uniqueness_of :user_name, :if => lambda { |x| x.facebook_id.nil? }
|
||||||
validates_format_of :user_name, :with => /[\w_-]+/, :if => lambda { |x| x.facebook_id.nil? }
|
validates_format_of :user_name, :with => /[\w_-]+/, :if => lambda { |x| x.facebook_id.nil? }
|
||||||
validates_presence_of :facebook_id, :if => lambda { |x| x.user_name.to_s.empty? }
|
validates_presence_of :facebook_id, :if => lambda { |x| x.user_name.to_s.empty? }
|
||||||
|
validate :user_name_is_alphanumeric
|
||||||
|
|
||||||
has_many :photos, :dependent => :destroy
|
has_many :photos, :dependent => :destroy
|
||||||
has_many :votes, :dependent => :destroy, :order => 'votes.photo_id ASC'
|
has_many :votes, :dependent => :destroy, :order => 'votes.photo_id ASC'
|
||||||
|
@ -39,6 +40,12 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
def user_name_is_alphanumeric
|
||||||
|
if self.facebook_id.to_i == 0 and self.user_name.to_s =~ /[^A-Za-z0-9]/
|
||||||
|
self.errors.add(:user_name, 'has illegal characters')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def saltify_password
|
def saltify_password
|
||||||
return true unless self.facebook_id.nil?
|
return true unless self.facebook_id.nil?
|
||||||
if !self.password.to_s.empty?
|
if !self.password.to_s.empty?
|
||||||
|
|
Reference in New Issue