Updating to reflect changes in DM 0.9.1
[merb_mart.git] / app / models / user.rb
blob9dee6722d3bb09279c90828c3601d4b74d501747
1 require 'digest/sha1'
2 require "date"
3 require "dm-aggregates"
4 require "dm-validations"
5 begin
6   require File.join(File.dirname(__FILE__), '..', '..', "lib", "authenticated_system", "authenticated_dependencies")
7 rescue
8   nil
9 end
10 class User
12   include DataMapper::Resource
13   include AuthenticatedSystem::Model
15   attr_accessor :password, :password_confirmation
17   property :id,                         Integer,   :serial => true
18   property :login,                      String,    :unique => true
19   property :email,                      String
20   property :crypted_password,           String
21   property :salt,                       String
22   property :activation_code,            String
23   property :activated_at,               DateTime
24   property :remember_token_expires_at,  DateTime
25   property :remember_token,             String
26   property :created_at,                 DateTime
27   property :updated_at,                 DateTime
29   validates_length        :login,                   :within => 3..40
30   validates_is_unique     :login
31   validates_present       :email
32   validates_format        :email,                   :as => :email_address
33   validates_length        :email,                   :within => 3..100
34   validates_is_unique     :email
35   validates_present       :password,                :if => lambda { |r| r.password_required? }
36   validates_present       :password_confirmation,   :if => lambda { |r| r.password_required? }
37   validates_length        :password,                :within => 4..40, :if => lambda { |r| r.password_required? }
38   validates_is_confirmed  :password#,                :groups => :create
40   before :save,   :encrypt_password
42   before :create do
43     #User.make_activation_code
44   end
45   
46   after :create do
47     #User.send_signup_notification
48   end
50   def login=(value)
51     @login = value.downcase unless value.nil?
52   end
54   EMAIL_FROM = "info@mysite.com"
55   SIGNUP_MAIL_SUBJECT = "Welcome to MYSITE.  Please activate your account."
56   ACTIVATE_MAIL_SUBJECT = "Welcome to MYSITE"
58   # Activates the user in the database
59   def activate
60     @activated = true
61     self.activated_at = Time.now.utc
62     self.activation_code = nil
63     save
65     # send mail for activation
66     UserMailer.dispatch_and_deliver(  :activation_notification,
67                                   {   :from => User::EMAIL_FROM,
68                                       :to   => self.email,
69                                       :subject => User::ACTIVATE_MAIL_SUBJECT },
71                                       :user => self )
73   end
75   #def send_signup_notification
76   #  UserMailer.dispatch_and_deliver(
77   #      :signup_notification,
78   #    { :from => User::EMAIL_FROM,
79   #      :to  => self.email,
80   #      :subject => User::SIGNUP_MAIL_SUBJECT },
81   #      :user => self
82   #  )
83   #end
85 end