Skip to content

Instantly share code, notes, and snippets.

@eladmeidar
Created March 11, 2010 20:29
Show Gist options
  • Save eladmeidar/329609 to your computer and use it in GitHub Desktop.
Save eladmeidar/329609 to your computer and use it in GitHub Desktop.
class BasalPlan
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::DeepVersioning
field :name, :type => String
belongs_to :user, :inverse_of => :basal_plan
has_many :basal_entries
accepts_nested_attributes_for :basal_entries, :reject_if => lambda { |attrs| attrs[:_delete].present? }
end
ruby-1.8.7-p249 > a = User.first
=> #<User _id: 4b88846df387730fc5000002, created_at: Sat Feb 27 02:33:17 UTC 2010, updated_at: Thu Mar 11 20:27:52 UTC 2010, username: "eladmeidar", password_salt: "fee3e2f80f8ecd9a50b284491dd9cf26eee79eed", password_hash: "234d2142004c42f2e945616eec0783b457a87adc", email: "[email protected]">
ruby-1.8.7-p249 > a.basal_plan
=> nil
ruby-1.8.7-p249 > a.create_basal_plan(:name => 'elad')
=> #<BasalPlan _id: 4b995236f387733b83000002, name: "elad", created_at: Thu Mar 11 20:27:34 UTC 2010, updated_at: Thu Mar 11 20:28:28 UTC 2010, version: 1>
ruby-1.8.7-p249 > a.basal_plan.new_record?
=> true
class User
include Mongoid::Document
include Mongoid::Timestamps
# new columns need to be added here to be writable through mass assignment
# attr_accessible :username, :email, :password, :password_confirmation
attr_accessor :password, :password_confirmation
field :username, :type => String
field :email, :type => String
field :password_hash, :type => String
field :password_salt, :type => String
has_many :blood_tests
has_many :foods
has_one :setting
has_one :basal_plan
accepts_nested_attributes_for :setting
before_save :prepare_password
before_create :setup_default_setting
validates_presence_of :username
validates_uniqueness_of :username, :email, :allow_blank => true
validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@"
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validate :check_password
def check_password
if self.new_record?
errors.add(:base, "Password can't be blank") if self.password.blank?
errors.add(:base, "Password and confirmation does not match") unless self.password == self.password_confirmation
errors.add(:base, "Password must be at least 4 chars long") if self.password.length < 4
end
end
# login can be either username or email address
def self.authenticate(login, pass)
user = first(:conditions => {:username => login}) || first(:conditions => {:email => login})
return user if user && user.matching_password?(pass)
end
def matching_password?(pass)
self.password_hash == encrypt_password(pass)
end
private
def setup_default_setting
build_setting(:insulin_type => Setting::INSULIN_TYPES.first)
end
def prepare_password
unless password.blank?
self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
self.password_hash = encrypt_password(password)
end
end
def encrypt_password(pass)
Digest::SHA1.hexdigest([pass, password_salt].join)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment