Skip to content

Instantly share code, notes, and snippets.

@resure
Created August 27, 2011 20:44
Show Gist options
  • Save resure/1175846 to your computer and use it in GitHub Desktop.
Save resure/1175846 to your computer and use it in GitHub Desktop.
Key model
class Key < ActiveRecord::Base
has_secure_password
belongs_to :user
attr_accessible :title, :password, :password_confirmation, :old_password
before_validation :strip_values #, :set_password
validates :title, :length => { :within => 3..254 }
validates :user_id, :presence => true
validates :password, :length => { :within => 6..254 },
:confirmation => true,
:if => :password_digest_changed?
validates :old_password, :encrypt_password => true
def self.password=(unencrypted_password)
self.old_password_digest = self.password_digest
@password = unencrypted_password
unless unencrypted_password.blank?
self.password_digest = BCrypt::Password.create(unencrypted_password)
end
self.password_digest = 'ha-ha'
end
...
private
def strip_values
self.title.strip! unless self.title.nil?
if self.password && self.password_confirmation
self.password.strip!
self.password_confirmation.strip!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment