app/models/model_mixin.rb in quo_vadis-1.0.7 vs app/models/model_mixin.rb in quo_vadis-1.1.0
- old
+ new
@@ -14,17 +14,17 @@
send :include, InstanceMethodsOnActivation
attr_reader :password
attr_protected :password_digest
- validates :username, :presence => true, :uniqueness => true
- validates :password, :presence => true, :if => Proc.new { |u| u.changed.include?('password_digest') }
- validates :password_digest, :presence => true
+ validates :username, :presence => true, :uniqueness => true, :if => :should_authenticate?
+ validates :password, :presence => true, :if => Proc.new { |u| u.should_authenticate? && u.changed.include?('password_digest') }
+ validates :password_digest, :presence => true, :if => :should_authenticate?
scope :valid_token, lambda { |token| where("token = ? AND token_created_at > ?", token, 3.hours.ago) }
- instance_eval <<-END
+ instance_eval <<-END, __FILE__, __LINE__ + 1
# Returns the user with the given <tt>username</tt> if the given password is
# correct, and <tt>nil</tt> otherwise.
def authenticate(username, plain_text_password)
user = where(:username => username).first
if user && user.has_matching_password?(plain_text_password)
@@ -33,11 +33,11 @@
nil
end
end
def find_by_salt(id, salt) # :nodoc:
- user = User.find_by_id id
+ user = find_by_id id
if user && user.has_matching_salt?(salt)
user
else
nil
end
@@ -45,9 +45,14 @@
END
end
end
module InstanceMethodsOnActivation
+ # Override this in your model if you need to bypass the Quo Vadis validations.
+ def should_authenticate?
+ true
+ end
+
def password=(plain_text_password) # :nodoc:
@password = plain_text_password
self.password_digest = BCrypt::Password.create plain_text_password
end